Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

150 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\Factory\Reminder\ReminderFactoryInterface;
  7. use Lc\SovBundle\Form\Reminder\ReminderAdminFormType;
  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\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class ReminderAdminController extends AbstractController
  16. {
  17. protected EntityManagerInterface $entityManager;
  18. protected ReminderFactoryInterface $reminderFactory;
  19. protected FormFactoryInterface $formFactory;
  20. protected UrlGeneratorInterface $urlGenerator;
  21. protected MerchantResolver $merchantResolver;
  22. protected SectionResolver $sectionResolver;
  23. protected ParameterBagInterface $parameterBag;
  24. public function __construct(
  25. EntityManagerInterface $entityManager,
  26. ReminderFactoryInterface $reminderFactory,
  27. FormFactoryInterface $formFactory,
  28. UrlGeneratorInterface $urlGenerator,
  29. MerchantResolver $merchantResolver,
  30. SectionResolver $sectionResolver,
  31. ParameterBagInterface $parameterBag
  32. ) {
  33. $this->entityManager = $entityManager;
  34. $this->reminderFactory = $reminderFactory;
  35. $this->formFactory = $formFactory;
  36. $this->urlGenerator = $urlGenerator;
  37. $this->merchantResolver = $merchantResolver;
  38. $this->sectionResolver = $sectionResolver;
  39. $this->parameterBag = $parameterBag;
  40. }
  41. /**
  42. * @Route("/admin/reminder/modal", name="sov_admin_reminder_render_modal")
  43. */
  44. public function renderModal(Request $request): Response
  45. {
  46. $crudAction = $request->get('crudAction');
  47. $crudControllerFqcn = $request->get('crudControllerFqcn');
  48. $entityId = $request->get('entityId');
  49. $reminder = $this->createEntity($crudAction, $crudControllerFqcn, $entityId);
  50. $form = $this->formFactory->create(
  51. ReminderAdminFormType::class,
  52. $reminder,
  53. [
  54. 'action' => $this->urlGenerator->generate(
  55. $this->parameterBag->get('app.reminder.route_new')
  56. )
  57. ]
  58. );
  59. return $this->render(
  60. '@LcSov/admin/reminder/form_modal.html.twig',
  61. [
  62. 'form_reminder' => $form->createView()
  63. ]
  64. );
  65. }
  66. /**
  67. * @Route("/admin/reminder/new", name="sov_admin_reminder_new")
  68. */
  69. public function new(Request $request)
  70. {
  71. $reminder = $this->createEntity();
  72. $form = $this->formFactory->create(ReminderAdminFormType::class, $reminder);
  73. $form->handleRequest($request);
  74. if ($form->isSubmitted() && $form->isValid()) {
  75. $reminder = $form->getData();
  76. $this->entityManager->persist($reminder);
  77. $this->entityManager->flush();
  78. $this->addFlash('success', 'Le pense-bête a bien été ajouté');
  79. }
  80. return $this->redirect($request->headers->get('referer'));
  81. }
  82. public function createEntity(string $crudAction = null, string $crudControllerFqcn = null, int $entityId = null)
  83. {
  84. return $this->reminderFactory->create($crudAction, $crudControllerFqcn, $entityId);
  85. }
  86. /*
  87. public function renderTemplate($actionName, $templatePath, array $parameters = [])
  88. {
  89. if ($this->request->isXmlHttpRequest() && ($actionName == 'new' || $actionName == 'edit')) {
  90. $response['flashMessages'] = $this->utils->getFlashMessages();
  91. $response['data'] = $this->render('@LcShop/backend/default/modal/edit_reminder.twig', $parameters)->getContent();
  92. return new Response(json_encode($response));
  93. } else {
  94. return parent::renderTemplate($actionName, $templatePath, $parameters);
  95. }
  96. }
  97. protected function redirectToReferrer()
  98. {
  99. $action = $this->request->query->get('action');
  100. if ($action == 'new') {
  101. $this->utils->addFlash('success', 'success.reminder.add');
  102. } elseif ($action == 'edit') {
  103. $this->utils->addFlash('success', 'success.reminder.edit');
  104. } elseif ($action == ' setReminderDone') {
  105. $this->utils->addFlash('success', 'success.reminder.done');
  106. }
  107. if ($this->request->isXmlHttpRequest()) {
  108. $response['flashMessages'] = $this->utils->getFlashMessages();
  109. return new Response(json_encode($response));
  110. } else {
  111. return parent::redirectToReferrer();
  112. }
  113. }
  114. public function setReminderDoneAction()
  115. {
  116. $id = $this->request->query->get('id');
  117. $done = $this->request->query->get('done');
  118. $easyadmin = $this->request->attributes->get('easyadmin');
  119. $reminder = $easyadmin['item'];
  120. $reminder->setDone($done);
  121. $this->em->persist($reminder);
  122. $this->em->flush();
  123. return $this->redirectToReferrer();
  124. }*/
  125. }