Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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