src/Controller/SchoolYearController.php line 40

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\SchoolYearRepository;
  11. use App\Entity\SchoolYear;
  12. use App\Form\SchoolYearType;
  13. /**
  14. * SchoolYear controller.
  15. *
  16. * @Route("/admin/years")
  17. */
  18. class SchoolYearController extends AbstractController
  19. {
  20. private $em;
  21. private $scRepo;
  22. public function __construct(EntityManagerInterface $em, SchoolYearRepository $scRepo)
  23. {
  24. $this->scRepo = $scRepo;
  25. $this->em = $em;
  26. }
  27. /**
  28. * Lists all SchoolYearme entities.
  29. *
  30. * @Route("/", name="admin_school_years")
  31. * @Method("GET")
  32. * @Template()
  33. */
  34. public function indexAction(SchoolYearRepository $repo)
  35. {
  36. $schoolyears = $repo->findAll();
  37. return $this->render('school_year/index.html.twig', compact("schoolyears"));
  38. }
  39. /**
  40. * Finds and displays a SchoolYearme entity.
  41. *
  42. * @Route("/{id}/show", name="admin_schoolyears_show", requirements={"id"="\d+"})
  43. * @Method("GET")
  44. * @Template()
  45. */
  46. public function showAction(SchoolYear $school_year, SchoolYearRepository $schoolYearRepository): Response
  47. {
  48. $em = $this->getDoctrine()->getManager();
  49. return $this->render('school_year/show.html.twig', compact("school_year"));
  50. }
  51. public function uniqueness(SchoolYear $schoolyear = null)
  52. {
  53. $allSchoolYears = ($schoolyear != null) ? $this->scRepo->findAllExcept($schoolyear) : $this->scRepo->findAll();
  54. if ($schoolyear != null) {
  55. if ($schoolyear->getActivated()) {
  56. foreach ($allSchoolYears as $year) {
  57. $year->disable();
  58. }
  59. $schoolyear->unable();
  60. } else {
  61. if ($this->scRepo->countActivatedExcept($schoolyear)[0]["count"] == 0) {
  62. $this->addFlash('warning', 'You cannot deactivate all the solar years, one must be activated at a time.');
  63. return $this->redirectToRoute('admin_school_years');
  64. }
  65. }
  66. } else {
  67. foreach ($allSchoolYears as $year) {
  68. $year->disable();
  69. }
  70. }
  71. }
  72. /**
  73. * @Route("/create",name= "admin_schoolyears_new", methods={"GET","POST"})
  74. */
  75. public function create(Request $request): Response
  76. {
  77. if (!$this->getUser()) {
  78. $this->addFlash('warning', 'You need login first!');
  79. return $this->redirectToRoute('app_login');
  80. }
  81. if (!$this->getUser()->isVerified()) {
  82. $this->addFlash('warning', 'You need to have a verified account!');
  83. return $this->redirectToRoute('app_login');
  84. }
  85. $schoolyear = new SchoolYear();
  86. $form = $this->createForm(SchoolYearType::class, $schoolyear);
  87. $form->handleRequest($request);
  88. if ($form->isSubmitted() && $form->isValid()) {
  89. if(($schoolyear->getStartDate() <= $schoolyear->getEndDate())
  90. && ($schoolyear->getStartDate() <= $schoolyear->getRegistrationDeadline())
  91. && ($schoolyear->getRegistrationDeadline() <= $schoolyear->getEndDate()))
  92. {
  93. $this->uniqueness();
  94. $this->em->persist($schoolyear);
  95. $this->em->flush();
  96. $this->addFlash('success', 'SchoolYear succesfully created');
  97. return $this->redirectToRoute('admin_school_years');
  98. } else {
  99. if($schoolyear->getStartDate() > $schoolyear->getEndDate()){
  100. $this->addFlash('warning', 'The start date is greater than the end date');
  101. }
  102. if($schoolyear->getRegistrationDeadline() > $schoolyear->getEndDate()){
  103. $this->addFlash('warning', 'The registration deadline occurs after the end date');
  104. }
  105. if($schoolyear->getRegistrationDeadline() < $schoolyear->getStartDate()){
  106. $this->addFlash('warning', 'The deadline for registrations occurs before the end date');
  107. }
  108. }
  109. }
  110. return $this->render(
  111. 'school_year/new.html.twig',
  112. ['form' => $form->createView()]
  113. );
  114. }
  115. /**
  116. * Displays a form to edit an existing SchoolYearme entity.
  117. *
  118. * @Route("/{id}/edt", name="admin_schoolyears_edit", requirements={"id"="\d+"}, methods={"GET","PUT"})
  119. * @Template()
  120. */
  121. public function edit(Request $request, SchoolYear $schoolyear): Response
  122. {
  123. $form = $this->createForm(SchoolYearType::class, $schoolyear, [
  124. 'method' => 'GET',
  125. ]);
  126. $form->handleRequest($request);
  127. if ($form->isSubmitted() && $form->isValid()) {
  128. $this->uniqueness($schoolyear);
  129. $this->em->persist($schoolyear);
  130. $this->em->flush();
  131. $this->addFlash('success', 'SchoolYear succesfully updated');
  132. return $this->redirectToRoute('admin_school_years');
  133. }
  134. return $this->render('school_year/edit.html.twig', [
  135. 'schoolyear' => $schoolyear,
  136. 'form' => $form->createView()
  137. ]);
  138. }
  139. /**
  140. * Deletes a SchoolYearme entity.
  141. *
  142. * @Route("/{id}/delete", name="admin_schoolyears_delete", requirements={"id"="\d+"}, methods={"DELETE"})
  143. */
  144. public function delete(SchoolYear $schoolyear, Request $request): Response
  145. {
  146. if ($this->isCsrfTokenValid('school_years_deletion' . $schoolyear->getId(), $request->request->get('csrf_token'))) {
  147. if ($this->scRepo->countActivatedExcept($schoolyear)[0]["count"] == 0) {
  148. $this->addFlash('warning', 'You cannot deactivate all the solar years, one must be activated at a time.');
  149. return $this->redirectToRoute('admin_school_years');
  150. }
  151. $this->em->remove($schoolyear);
  152. $this->em->flush();
  153. $this->addFlash('info', 'SchoolYear succesfully deleted');
  154. }
  155. return $this->redirectToRoute('admin_school_years');
  156. }
  157. }