HegreLand/src/Controller/RepresentationController.php
2024-12-20 09:02:51 +01:00

95 lines
3.4 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\Employee;
use App\Entity\Representation;
use App\Entity\Ride;
use App\Form\RepresentationType;
use App\Repository\RepresentationRepository;
use App\Repository\RideRepository;
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;
#[Route('/representation', name: 'representation')]
final class RepresentationController extends AbstractController
{
public function __toString(): string
{
// TODO: Implement __toString() method.
return "";
}
#[Route(name: '_index', methods: ['GET'])]
public function index(RepresentationRepository $representationRepository): Response
{
return $this->render('representation/index.html.twig', [
'representations' => $representationRepository->findAll(),
]);
}
#[Route('/new', name: '_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager, RideRepository $rideRepository): Response
{
$representation = new Representation();
$form = $this->createForm(RepresentationType::class, $representation);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$ride = $rideRepository->find($representation->getRide());
$newCount = $ride->getCount() + $representation->getCount();
$ride->setCount($newCount);
$entityManager->persist($representation);
$entityManager->flush();
//
return $this->redirectToRoute('representation_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('representation/new.html.twig', [
'representation' => $representation,
'form' => $form,
]);
}
#[Route('/{employee}', name: '_show', methods: ['GET'])]
public function show(Representation $representation): Response
{
return $this->render('representation/show.html.twig', [
'representation' => $representation,
]);
}
#[Route('/{employee}/edit', name: '_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Representation $representation, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(RepresentationType::class, $representation);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('representation_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('representation/edit.html.twig', [
'representation' => $representation,
'form' => $form,
]);
}
#[Route('/{employee}', name: '_delete', methods: ['POST'])]
public function delete(Request $request, Representation $representation, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$representation->getEmployee(), $request->getPayload()->getString('_token'))) {
$entityManager->remove($representation);
$entityManager->flush();
}
return $this->redirectToRoute('representation_index', [], Response::HTTP_SEE_OTHER);
}
}