Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

155 lines
5.3KB

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