Files
myep/src/Controller/Admin/AccommodationBooking/CreateController.php
T

69 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Controller\Admin\AccommodationBooking;
use App\Entity\Groups\AccommodationBooking;
use App\Entity\User;
use App\Form\Admin\Groups\AccommodationBookingCreateType;
use App\Htmx\HxRedirectResponse;
use App\Service\AccommodationBookingService;
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;
#[IsGranted('ROLE_GROUPS_MANAGER')]
class CreateController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly LoggerInterface $logger,
private readonly AccommodationBookingService $bookingService,
) {
}
#[Route('/admin/accommodation-booking/create', name: 'app_admin_accommodationbooking_create')]
public function index(Request $request): Response
{
$booking = new AccommodationBooking();
// Whoever creates a booking in the backoffice looks after it until someone else is
// assigned in the edit form. The route is behind ROLE_GROUPS_MANAGER, so the creator
// is always eligible as Betreuer.
$user = $this->getUser();
if ($user instanceof User) {
$booking->setManagedBy($user);
}
$form = $this->createForm(AccommodationBookingCreateType::class, $booking, [
'hx_post' => $request->getRequestUri(),
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->bookingService->refreshPriceSnapshot($booking);
$this->entityManager->persist($booking);
$this->entityManager->flush();
$this->addFlash('success', 'Die Buchung wurde erstellt');
$this->logger->info('Created accommodation booking', [
'id' => $booking->getId(),
]);
return new HxRedirectResponse(
$this->generateUrl('app_admin_accommodationbooking_edit', ['id' => $booking->getId()])
);
}
return $this->render('admin/accommodation_booking/modal_create.html.twig', [
'form' => $form,
]);
}
}