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

161 lines
5.6KB

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