src/Controller/FAQController.php line 103

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\FAQ;
  4. use App\Form\FAQType;
  5. use App\Annotation\CmsAdminDash;
  6. use App\Annotation\CmsComponent;
  7. use App\Repository\FAQRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. #[Route('/takeflight/faq')]
  14. class FAQController extends AbstractController
  15. {
  16. /**
  17. * @CmsAdminDash("FAQs", active=true, routeName="app_f_a_q_index", icon="fa fa-question-circle", menuPosition=40, parentRouteName="control_content_index")
  18. */
  19. #[Route('/', name: 'app_f_a_q_index', methods: ['GET'])]
  20. public function index(FAQRepository $fAQRepository): Response
  21. {
  22. return $this->render('faq/index.html.twig', [
  23. 'f_a_qs' => $fAQRepository->findBy(['deleted' => false]),
  24. ]);
  25. }
  26. #[Route('/new', name: 'app_f_a_q_new', methods: ['GET', 'POST'])]
  27. public function new(Request $request): Response
  28. {
  29. $fAQ = new FAQ();
  30. $form = $this->createForm(FAQType::class, $fAQ);
  31. $form->handleRequest($request);
  32. if ($form->isSubmitted() && $form->isValid()) {
  33. // REQUIRED IF USING UPLOAD FILE TRAIT
  34. // $fAQ->uploadFile();
  35. $entityManager = $this->getDoctrine()->getManager();
  36. $entityManager->persist($fAQ);
  37. $entityManager->flush();
  38. return $this->redirectToRoute('app_f_a_q_index', [], Response::HTTP_SEE_OTHER);
  39. }
  40. return $this->render('faq/new.html.twig', [
  41. 'f_a_q' => $fAQ,
  42. 'form' => $form->createView(),
  43. ]);
  44. }
  45. #[Route('/{id}', name: 'app_f_a_q_show', methods: ['GET'])]
  46. public function show(FAQ $fAQ): Response
  47. {
  48. return $this->render('faq/show.html.twig', [
  49. 'f_a_q' => $fAQ,
  50. ]);
  51. }
  52. #[Route('/{id}/edit', name: 'app_f_a_q_edit', methods: ['GET', 'POST'])]
  53. public function edit(Request $request, FAQ $fAQ): Response
  54. {
  55. $form = $this->createForm(FAQType::class, $fAQ);
  56. $form->handleRequest($request);
  57. if ($form->isSubmitted() && $form->isValid()) {
  58. // REQUIRED IF USING UPLOAD FILE TRAIT
  59. // $fAQ->uploadFile();
  60. $this->getDoctrine()->getManager()->flush();
  61. return $this->redirectToRoute('app_f_a_q_index', [], Response::HTTP_SEE_OTHER);
  62. }
  63. return $this->render('faq/edit.html.twig', [
  64. 'f_a_q' => $fAQ,
  65. 'form' => $form->createView(),
  66. ]);
  67. }
  68. #[Route('/{id}', name: 'app_f_a_q_delete', methods: ['POST'])]
  69. public function delete(Request $request, FAQ $fAQ): Response
  70. {
  71. if ($this->isCsrfTokenValid('delete'.$fAQ->getId(), $request->request->get('_token'))) {
  72. $entityManager = $this->getDoctrine()->getManager();
  73. $fAQ->setActive(false);
  74. $fAQ->setDeleted(true);
  75. $entityManager->persist($fAQ);
  76. $entityManager->flush();
  77. $this->addFlash('success', 'Success - FAQ deleted');
  78. }
  79. return $this->redirectToRoute('app_f_a_q_index', [], Response::HTTP_SEE_OTHER);
  80. }
  81. /**
  82. * @CmsComponent("FAQs (All)", active=true, routeName="embed_faqs_all")
  83. *
  84. * @param mixed $request
  85. */
  86. #[Route(path: '/test-faqs/all', name: 'embed_faqs_all')]
  87. public function embedFaqsAll(EntityManagerInterface $em, Request $request): Response
  88. {
  89. // Get all FAQs grouped by service category
  90. $faqsEmployer = $em->getRepository(FAQ::class)->findBy(['deleted' => false, 'active' => true, 'serviceCategory' => 1], ['sortOrder' => 'ASC']);
  91. $faqsEmployee = $em->getRepository(FAQ::class)->findBy(['deleted' => false, 'active' => true, 'serviceCategory' => 2], ['sortOrder' => 'ASC']);
  92. return $this->render('@theme/faq/faqs.html.twig', [
  93. 'faqsEmployer' => $faqsEmployer,
  94. 'faqsEmployee' => $faqsEmployee,
  95. ]);
  96. }
  97. }