Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

156 lines
5.4KB

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