feat: add admin crud for resources and rooms

This commit is contained in:
fromme committed 2026-09-17 16:26:29 +02:00
1 parent ad5d5acb00
commit 3d67942c62
19 files changed
+1059 -4

No files matched your search

@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
namespace App\Controller\Admin\Resource;
use App\Entity\Resource;
use App\Form\Admin\ResourceFormType;
use App\Htmx\HxRedirectResponse;
use App\Repository\BookingRepository;
use App\Security\Role;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
/**
* Renaming, moving to another room, and retiring or reactivating.
*
* Retiring is a checkbox here rather than a confirmation modal of its own: it deletes nothing and
* is undone by ticking the box again. What it does leave behind -- bookings that still point at
* the resource -- is named in the flash, so the administrator can release them from
* /admin/bookings if they should not stand.
*/
#[IsGranted(Role::ADMIN)]
final class EditController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly BookingRepository $bookings,
private readonly LoggerInterface $logger,
) {
}
#[Route('/admin/resources/{id}/edit', name: 'app_admin_resource_edit', methods: ['GET', 'POST'])]
public function index(Request $request, Resource $resource): Response
{
$wasActive = $resource->isActive();
$form = $this->createForm(ResourceFormType::class, $resource, [
'action' => $this->generateUrl('app_admin_resource_edit', ['id' => $resource->getId()]),
'with_active' => true,
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->flush();
$this->logger->info('Admin edited a resource.', [
'admin' => $this->getUser()?->getUserIdentifier(),
'resource_id' => $resource->getId(),
'label' => $resource->getLabel(),
'group' => $resource->getGroup()?->getLabel(),
'active' => $resource->isActive(),
]);
$remaining = $wasActive && !$resource->isActive()
? $this->bookings->countFromDate($resource, new \DateTimeImmutable('today'))
: 0;
if ($remaining > 0) {
$this->addFlash('warning', \sprintf(
'%s ist nicht mehr buchbar. %d bestehende Buchungen ab heute bleiben erhalten.',
$resource->getLabel(),
$remaining,
));
} else {
$this->addFlash('success', \sprintf('%s gespeichert.', $resource->getLabel()));
}
return new HxRedirectResponse($this->generateUrl('app_admin_resource'));
}
return $this->render('admin/_modal_resource.html.twig', [
'form' => $form,
'resource' => $resource,
]);
}
}