src/Controller/ModuleController.php line 83

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use App\Repository\ModuleRepository;
  11. use App\Entity\Module;
  12. use App\Form\ModuleType;
  13. use App\Service\SchoolYearService;
  14. use App\Repository\AttributionRepository;
  15. /**
  16.  * Module controller.
  17.  *
  18.  * @Route("/admin/modules")
  19.  */
  20. class ModuleController extends AbstractController
  21. {
  22.     private $em;
  23.     private SchoolYearService $schoolYearService;
  24.     private AttributionRepository $attRepo;
  25.     public function __construct(AttributionRepository $attRepo,SchoolYearService $schoolYearService,EntityManagerInterface $em)
  26.     {
  27.         $this->em $em;
  28.         $this->schoolYearService $schoolYearService;
  29.         $this->attRepo $attRepo;
  30.     }
  31.     /**
  32.      * Lists all Programme entities.
  33.      *
  34.      * @Route("/", name="admin_modules")
  35.      * @Method("GET")
  36.      * @Template()
  37.      */
  38.     public function indexAction(ModuleRepository $repo)
  39.     {
  40.         $modules $repo->findAll();
  41.         return $this->render('module/index.html.twig'compact("modules"));
  42.     }
  43.     /**
  44.      * @Route("/create",name="admin_modules_new", methods={"GET","POST"})
  45.      */
  46.     public function create(Request $request): Response
  47.     {
  48.         $module = new Module();
  49.         $form $this->createForm(ModuleType::class, $module);
  50.         $form->handleRequest($request);
  51.         if ($form->isSubmitted() && $form->isValid()) {
  52.             $this->em->persist($module);
  53.             $this->em->flush();
  54.             $this->addFlash('success''Module succesfully created');
  55.             return $this->redirectToRoute('admin_modules');
  56.         }
  57.         return $this->render(
  58.             'module/new.html.twig',
  59.             ['form' => $form->createView()]
  60.         );
  61.     }
  62.     /**
  63.      * Finds and displays a Module entity.
  64.      *
  65.      * @Route("/{id}/show", name="admin_modules_show", requirements={"id"="\d+"})
  66.      * @Method("GET")
  67.      * @Template()
  68.      */
  69.     public function showAction(Module $module)
  70.     {
  71.         $year $this->schoolYearService->sessionYearById();
  72.         $attributions $this->attRepo->findByYearAndByModule($year$module);
  73.         $attributionsMapCourses null;
  74.         foreach($attributions as $att){
  75.               $attributionsMapCourses[$att->getCourse()->getId()] = $att;
  76.         }
  77.         $attributions $attributionsMapCourses;
  78.         return $this->render('module/show.html.twig'compact("module""attributions"));
  79.     }
  80.     /**
  81.      * Creates a new Module entity.
  82.      *
  83.      * @Route("/create", name="admin_modules_create")
  84.      * @Method("POST")
  85.      * @Template("AppBundle:Module:new.html.twig")
  86.      */
  87.     public function createAction(Request $request)
  88.     {
  89.         if (!$this->getUser()) {
  90.             $this->addFlash('warning''You need login first!');
  91.             return $this->redirectToRoute('app_login');
  92.         }
  93.         if (!$this->getUser()->isVerified()) {
  94.             $this->addFlash('warning''You need to have a verified account!');
  95.             return $this->redirectToRoute('app_login');
  96.         }
  97.         $module = new Module();
  98.         $form $this->createForm(new ModuleType(), $module);
  99.         if ($form->handleRequest($request)->isValid()) {
  100.             $em $this->getDoctrine()->getManager();
  101.             $em->persist($module);
  102.             $em->flush();
  103.             return $this->redirect($this->generateUrl('admin_modules'));
  104.         }
  105.         return array(
  106.             'module' => $module,
  107.             'form'   => $form->createView(),
  108.         );
  109.     }
  110.     /**
  111.      * Displays a form to edit an existing Programme entity.
  112.      *
  113.      * @Route("/{id}/edit", name="admin_modules_edit", requirements={"id"="\d+"}, methods={"GET","PUT"})
  114.      * @Template()
  115.      */
  116.     public function edit(Request $requestModule $module): Response
  117.     {
  118.         $form $this->createForm(ModuleType::class, $module, [
  119.             'method' => 'PUT'
  120.         ]);
  121.         $form->handleRequest($request);
  122.         if ($form->isSubmitted() && $form->isValid()) {
  123.             $this->em->flush();
  124.             $this->addFlash('success''Module succesfully updated');
  125.             return $this->redirectToRoute('admin_modules');
  126.         }
  127.         return $this->render('module/edit.html.twig', [
  128.             'module' => $module,
  129.             'form' => $form->createView()
  130.         ]);
  131.     }
  132.     /**
  133.      * Deletes a Programme entity.
  134.      *
  135.      * @Route("/{id}/delete", name="admin_modules_delete", requirements={"id"="\d+"}, methods={"DELETE"})
  136.      */
  137.     public function delete(Module $moduleRequest $request): Response
  138.     {
  139.         if ($this->isCsrfTokenValid('modules_deletion' $module->getId(), $request->request->get('csrf_token'))) {
  140.             $this->em->remove($module);
  141.             $this->em->flush();
  142.             $this->addFlash('info''Module succesfully deleted');
  143.         }
  144.         return $this->redirectToRoute('admin_modules');
  145.     }
  146. }