Files
ephub/src/Controller/Admin/Resource/EditController.php
T
2026-10-05 17:10:02 +02:00

77 lines
2.7 KiB
PHP

<?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 App\Service\AuditLogger;
use Doctrine\ORM\EntityManagerInterface;
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
* /manage/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 AuditLogger $audit,
) {
}
#[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->audit->resourceUpdated($resource, $wasActive);
$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,
]);
}
}