You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 line
5.3KB

  1. <?php
  2. namespace Lc\SovBundle\Controller\Reminder;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\SovBundle\Factory\Reminder\ReminderFactory;
  5. use Lc\SovBundle\Factory\Reminder\ReminderFactoryInterface;
  6. use Lc\SovBundle\Form\Reminder\ReminderAdminFormType;
  7. use Lc\SovBundle\Repository\Reminder\ReminderRepositoryQueryInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Symfony\Component\Form\FormFactoryInterface;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class ReminderAdminController extends AbstractController
  17. {
  18. protected EntityManagerInterface $entityManager;
  19. protected ReminderRepositoryQueryInterface $reminderRepositoryQuery;
  20. protected FormFactoryInterface $formFactory;
  21. protected UrlGeneratorInterface $urlGenerator;
  22. protected MerchantResolver $merchantResolver;
  23. protected SectionResolver $sectionResolver;
  24. protected ParameterBagInterface $parameterBag;
  25. public function __construct(
  26. EntityManagerInterface $entityManager,
  27. ReminderRepositoryQueryInterface $reminderRepositoryQuery,
  28. FormFactoryInterface $formFactory,
  29. UrlGeneratorInterface $urlGenerator,
  30. ParameterBagInterface $parameterBag
  31. ) {
  32. $this->entityManager = $entityManager;
  33. $this->reminderRepositoryQuery = $reminderRepositoryQuery;
  34. $this->formFactory = $formFactory;
  35. $this->urlGenerator = $urlGenerator;
  36. $this->parameterBag = $parameterBag;
  37. }
  38. /**
  39. * @Route("/admin/reminder/modal", name="sov_admin_reminder_render_modal")
  40. */
  41. public function renderModal(Request $request): Response
  42. {
  43. $id = $request->get('id');
  44. if($id) {
  45. $reminder = $this->reminderRepositoryQuery->getRepository()->find($id);
  46. $action = $this->urlGenerator->generate(
  47. $this->parameterBag->get('app.reminder.route_edit'),
  48. ['id' => $id]
  49. );
  50. }
  51. else {
  52. $crudAction = $request->get('crudAction');
  53. $crudControllerFqcn = $request->get('crudControllerFqcn');
  54. $entityId = $request->get('entityId') ? $request->get('entityId') : null ;
  55. $reminder = $this->createEntity($crudAction, $crudControllerFqcn, $entityId);
  56. $action = $this->urlGenerator->generate(
  57. $this->parameterBag->get('app.reminder.route_new')
  58. );
  59. }
  60. $form = $this->formFactory->create(
  61. ReminderAdminFormType::class,
  62. $reminder,
  63. [
  64. 'action' => $action
  65. ]
  66. );
  67. return $this->render(
  68. '@LcSov/admin/reminder/form_modal.html.twig',
  69. [
  70. 'form_reminder' => $form->createView()
  71. ]
  72. );
  73. }
  74. /**
  75. * @Route("/admin/reminder/new", name="sov_admin_reminder_new")
  76. */
  77. public function new(Request $request)
  78. {
  79. $reminder = $this->createEntity();
  80. $form = $this->formFactory->create(ReminderAdminFormType::class, $reminder);
  81. $form->handleRequest($request);
  82. if ($form->isSubmitted() && $form->isValid()) {
  83. $reminder = $form->getData();
  84. $this->entityManager->persist($reminder);
  85. $this->entityManager->flush();
  86. $this->addFlash('success', 'Le pense-bête a bien été ajouté');
  87. }
  88. return $this->redirect($request->headers->get('referer'));
  89. }
  90. /**
  91. * @Route("/admin/reminder/edit/{id}", name="sov_admin_reminder_edit")
  92. */
  93. public function edit(Request $request)
  94. {
  95. $id = $request->get('id');
  96. $reminder = $this->reminderRepositoryQuery->getRepository()->find($id);
  97. $form = $this->formFactory->create(ReminderAdminFormType::class, $reminder);
  98. $form->handleRequest($request);
  99. if ($form->isSubmitted() && $form->isValid()) {
  100. $reminder = $form->getData();
  101. $this->entityManager->update($reminder);
  102. $this->entityManager->flush();
  103. $this->addFlash('success', 'Le pense-bête a bien été mis à jour');
  104. }
  105. return $this->redirect($request->headers->get('referer'));
  106. }
  107. /**
  108. * @Route("/admin/reminder/done", name="sov_admin_reminder_done")
  109. */
  110. public function done(Request $request): JsonResponse
  111. {
  112. $id = $request->get('id');
  113. $reminder = $this->reminderRepositoryQuery->getRepository()->find($id);
  114. $done = $request->get('done');
  115. if($done == 'true') {
  116. $reminder->setDone(true);
  117. }
  118. else {
  119. $reminder->setDone(false);
  120. }
  121. $this->entityManager->update($reminder);
  122. $this->entityManager->flush();
  123. return new JsonResponse(['success' => true]);
  124. }
  125. public function createEntity(string $crudAction = null, string $crudControllerFqcn = null, int $entityId = null)
  126. {
  127. $factory = new ReminderFactory();
  128. return $factory->create($crudAction, $crudControllerFqcn, $entityId);
  129. }
  130. }