您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ReminderAdminController.php 5.2KB

3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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->addFlashTranslator('success', 'added');
  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->addFlashTranslator('success', 'updated');
  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. }