src/Controller/QuaterController.php line 56

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Quater;
  4. use App\Form\QuaterType;
  5. use App\Repository\QuaterRepository;
  6. use App\Repository\SchoolYearRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  16. use App\Service\SchoolYearService;
  17. /**
  18.  * Quater controller.
  19.  *
  20.  * @Route("/admin/quaters")
  21.  */
  22. class QuaterController extends AbstractController
  23. {
  24.     private EntityManagerInterface $em;
  25.     private SchoolYearRepository $scRepo;
  26.     private QuaterRepository $repo;
  27.     private SessionInterface $session;
  28.     private SchoolYearService $schoolYearService;
  29.     public function __construct(SchoolYearService $schoolYearService,EntityManagerInterface $emSchoolYearRepository $scRepoQuaterRepository $repoSessionInterface $session)
  30.     {
  31.         $this->em $em;
  32.         $this->repo $repo;
  33.         $this->scRepo $scRepo;
  34.         $this->session $session;
  35.         $this->schoolYearService $schoolYearService;
  36.     }
  37.     /**
  38.      * Lists all Quaterme entities.
  39.      *
  40.      * @Route("/", name="admin_quaters")
  41.      * @Method("GET")
  42.      * @Template()
  43.      */
  44.     public function indexAction()
  45.     {
  46.         $year $this->schoolYearService->sessionYearById();
  47.         $quaters $this->repo->findQuaterThisYear($year);
  48.         return $this->render('quater/index.html.twig'compact("quaters"));
  49.     }
  50.     public function uniqueness(Quater $quater null)
  51.     {
  52.         $allQuaters = ($quater != null) ? $this->repo->findAllExcept($quater) : $this->repo->findAll();
  53.         if ($quater != null) {
  54.             if ($quater->getActivated()) {
  55.                 foreach ($allQuaters as $qt) {
  56.                     $qt->disable();
  57.                 }
  58.                 $quater->unable();
  59.             } else {
  60.                 if ($this->repo->countActivatedExcept($quater)[0]["count"] == 0) {
  61.                     $this->addFlash('warning''You cannot deactivate all the quaters, one must be activated at a time.');
  62.                     return $this->redirectToRoute('admin_quaters');
  63.                 }
  64.             }
  65.         } else {
  66.             foreach ($allQuaters as $qt) {
  67.                 $qt->disable();
  68.             }
  69.         }
  70.     }
  71.     /**
  72.      * Finds and displays a Quaterme entity.
  73.      *
  74.      * @Route("/{id}/show", name="admin_quaters_show", requirements={"id"="\d+"})
  75.      * @Method("GET")
  76.      * @Template()
  77.      */
  78.     public function showAction(Quater $quater)
  79.     {
  80.         return $this->render('quater/show.html.twig'compact("quater"));
  81.     }
  82.     /**
  83.      * @Route("/create",name= "admin_quaters_new", methods={"GET","POST"})
  84.      */
  85.     public function create(Request $requestQuaterRepository $quaterRepository): Response
  86.     {
  87.         $quater = new Quater();
  88.         $form $this->createForm(QuaterType::class, $quater);
  89.         $form->handleRequest($request);
  90.         if ($form->isSubmitted() && $form->isValid()) {
  91.             $em $this->getDoctrine()->getManager();
  92.             // Désactiver tous les trimestres existants pour cette année scolaire
  93.             if ($quater->getActivated()) {
  94.                 $allQuaters $quaterRepository->findAll(array("schoolYear" =>  $quater->getSchoolYear()));
  95.                 foreach ($allQuaters as $quat) {
  96.                     $quat->setActivated(false);
  97.                     $em->persist($quat);
  98.                 }
  99.                 $quater->unable();
  100.             }
  101.             // Activer le trimestre créé
  102.             $em->persist($quater);
  103.             $em->flush();
  104.             $this->addFlash('success''Quater successfully created');
  105.             return $this->redirectToRoute('admin_quaters');
  106.         }
  107.         return $this->render(
  108.             'quater/new.html.twig',
  109.             ['form' => $form->createView()]
  110.         );
  111.     }
  112.     /**
  113.      * Displays a form to edit an existing Quaterme entity.
  114.      *
  115.      * @Route("/{id}/edt", name="admin_quaters_edit", requirements={"id"="\d+"}, methods={"GET","PUT"})
  116.      * @Template()
  117.      */
  118.     public function edit(Request $requestQuater $quaterQuaterRepository $quaterRepository): Response
  119.     {
  120.         $form $this->createForm(QuaterType::class, $quater, [
  121.             'method' => 'GET',
  122.         ]);
  123.         $form->handleRequest($request);
  124.         if ($form->isSubmitted() && $form->isValid()) {
  125.             if ($quater->getActivated()) {
  126.                 $allQuaters $this->repo->findAllExcept($quater);
  127.                 foreach ($allQuaters as $year) {
  128.                     $year->disable();
  129.                 }
  130.                 $quater->unable();
  131.             } else {
  132.                 if ($this->repo->countActivatedExcept($quater)[0]["count"] == 0) {
  133.                     $this->addFlash('warning''You cannot deactivate all the quaters, one must be activated at a time.');
  134.                     return $this->redirectToRoute('admin_quaters');
  135.                 }
  136.             }
  137.             $em $this->getDoctrine()->getManager();
  138.             $this->em->persist($quater);
  139.             $em->flush();
  140.             $this->addFlash('success''Quater successfully updated');
  141.             return $this->redirectToRoute('admin_quaters');
  142.         }
  143.         return $this->render('quater/edit.html.twig', [
  144.             'quater' => $quater,
  145.             'form' => $form->createView()
  146.         ]);
  147.     }
  148.     /**
  149.      * Deletes a Quater entity.
  150.      *
  151.      * @Route("/{id}/delete", name="admin_quaters_delete", requirements={"id"="\d+"}, methods={"DELETE"})
  152.      
  153.      */
  154.     public function delete(Request $requestQuater $q): Response
  155.     {
  156.         if ($this->isCsrfTokenValid('quaters_deletion' $q->getId(), $request->request->get('csrf_token'))) {
  157.             if ($this->repo->countActivatedExcept($q)[0]["count"] == 0) {
  158.                 $this->addFlash('warning''You cannot delete all quaters, one must be activated at a time.');
  159.                 return $this->redirectToRoute('admin_quaters');
  160.             }
  161.             $this->em->remove($q);
  162.             $this->em->flush();
  163.             $this->addFlash('info''Quater succesfully deleted');
  164.         }
  165.         return $this->redirectToRoute('admin_quaters');
  166.     }
  167. }