src/Controller/OrganisationController.php line 17

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Organisation;
  4. use App\Form\OrganisationType;
  5. use App\Repository\OrganisationRepository;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. #[Route('/organisation')]
  11. class OrganisationController extends AbstractController
  12. {
  13.     #[Route('/'name'app_organisation_index'methods: ['GET'])]
  14.     public function index(OrganisationRepository $organisationRepository): Response
  15.     {
  16.         return $this->render('organisation/index.html.twig', [
  17.             'organisations' => $organisationRepository->findAll(),
  18.         ]);
  19.     }
  20.     #[Route('/new'name'app_organisation_new'methods: ['GET''POST'])]
  21.     public function new(Request $requestOrganisationRepository $organisationRepository): Response
  22.     {
  23.         $organisation = new Organisation();
  24.         $form $this->createForm(OrganisationType::class, $organisation);
  25.         $form->handleRequest($request);
  26.         if ($form->isSubmitted() && $form->isValid()) {
  27.             $r $form->get("parent_autocomplete")->getData();
  28.             $parent $organisationRepository->findOneBy(["id"=>$r["val"]??-1]);
  29.             $organisation->setParent($parent && $r["txt"]==$parent->getLibelle()?$parent:null);
  30.             $organisationRepository->save($organisationtrue);
  31.             return $this->redirectToRoute('app_organisation_edit', ["id"=>$organisation->getId()], Response::HTTP_SEE_OTHER);
  32.         }
  33.         return $this->render('organisation/new.html.twig', [
  34.             'organisation' => $organisation,
  35.             'form' => $form->createView(),
  36.         ]);
  37.     }
  38.     #[Route('/{id}'name'app_organisation_show'methods: ['GET'])]
  39.     public function show(Organisation $organisation): Response
  40.     {
  41.         return $this->render('organisation/show.html.twig', [
  42.             'organisation' => $organisation,
  43.         ]);
  44.     }
  45.     #[Route('/{id}/edit'name'app_organisation_edit'methods: ['GET''POST'])]
  46.     public function edit(Request $requestOrganisation $organisationOrganisationRepository $organisationRepository): Response
  47.     {
  48.         $form $this->createForm(OrganisationType::class, $organisation);
  49.         if($organisation->getParent()!=null){
  50.             $parent=[
  51.                 "val"=>$organisation->getParent()->getId(),
  52.                 "txt"=>$organisation->getParent()->getLibelle(),
  53.             ];
  54.             $form->get("parent_autocomplete")->setData($parent);
  55.         }
  56.         $form->handleRequest($request);
  57.         if ($form->isSubmitted() && $form->isValid()) {
  58.             $r $form->get("parent_autocomplete")->getData();
  59.             $parent $organisationRepository->findOneBy(["id"=>$r["val"]??-1]);
  60.             $organisation->setParent($parent && $r["txt"]==$parent->getLibelle()?$parent:null);
  61.             $organisationRepository->save($organisationtrue);
  62.             return $this->redirectToRoute('app_organisation_show', ["id"=>$organisation->getId()], Response::HTTP_SEE_OTHER);
  63.         }
  64.         return $this->render('organisation/edit.html.twig', [
  65.             'organisation' => $organisation,
  66.             'form' =>  $form->createView(),
  67.         ]);
  68.     }
  69.     #[Route('/{id}'name'app_organisation_delete'methods: ['POST'])]
  70.     public function delete(Request $requestOrganisation $organisationOrganisationRepository $organisationRepository): Response
  71.     {
  72.         if ($this->isCsrfTokenValid('delete'.$organisation->getId(), $request->request->get('_token'))) {
  73.             $organisationRepository->remove($organisationtrue);
  74.         }
  75.         return $this->redirectToRoute('app_organisation_index', [], Response::HTTP_SEE_OTHER);
  76.     }
  77. }