src/Controller/SectionController.php line 51

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