src/Controller/DomainController.php line 91

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