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