You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReminderAdminController.php 5.1KB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Lc\SovBundle\Controller\Reminder;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\SovBundle\Container\Reminder\ReminderContainer;
  5. use Lc\SovBundle\Controller\ControllerTrait;
  6. use Lc\SovBundle\Factory\Reminder\ReminderFactory;
  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\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class ReminderAdminController extends AbstractController
  17. {
  18. use ControllerTrait;
  19. protected EntityManagerInterface $entityManager;
  20. protected ReminderContainer $reminderContainer;
  21. protected FormFactoryInterface $formFactory;
  22. protected UrlGeneratorInterface $urlGenerator;
  23. protected ParameterBagInterface $parameterBag;
  24. public function __construct(
  25. EntityManagerInterface $entityManager,
  26. ReminderContainer $reminderContainer,
  27. FormFactoryInterface $formFactory,
  28. UrlGeneratorInterface $urlGenerator,
  29. ParameterBagInterface $parameterBag
  30. ) {
  31. $this->entityManager = $entityManager;
  32. $this->reminderContainer = $reminderContainer;
  33. $this->formFactory = $formFactory;
  34. $this->urlGenerator = $urlGenerator;
  35. $this->parameterBag = $parameterBag;
  36. }
  37. #[Route(path: '/admin/reminder/modal', name: 'sov_admin_reminder_render_modal')]
  38. public function renderModal(Request $request): Response
  39. {
  40. $id = $request->get('id');
  41. if($id) {
  42. $reminder = $this->reminderContainer->getRepositoryQuery()->getRepository()->find($id);
  43. $action = $this->urlGenerator->generate(
  44. $this->parameterBag->get('app.reminder.route_edit'),
  45. ['id' => $id]
  46. );
  47. }
  48. else {
  49. $crudAction = $request->get('crudAction');
  50. $crudControllerFqcn = $request->get('crudControllerFqcn');
  51. $entityId = $request->get('entityId') ? $request->get('entityId') : null ;
  52. $reminder = $this->createEntity($crudAction, $crudControllerFqcn, $entityId);
  53. $action = $this->urlGenerator->generate(
  54. $this->parameterBag->get('app.reminder.route_new')
  55. );
  56. }
  57. $form = $this->formFactory->create(
  58. ReminderAdminFormType::class,
  59. $reminder,
  60. [
  61. 'action' => $action
  62. ]
  63. );
  64. return $this->render(
  65. '@LcSov/admin/reminder/form_modal.html.twig',
  66. [
  67. 'form_reminder' => $form->createView()
  68. ]
  69. );
  70. }
  71. #[Route(path: '/admin/reminder/new', name: 'sov_admin_reminder_new')]
  72. public function new(Request $request)
  73. {
  74. $reminder = $this->createEntity();
  75. $form = $this->formFactory->create(ReminderAdminFormType::class, $reminder);
  76. $form->handleRequest($request);
  77. if ($form->isSubmitted() && $form->isValid()) {
  78. $reminder = $form->getData();
  79. $this->entityManager->persist($reminder);
  80. $this->entityManager->flush();
  81. $this->addFlashTranslator('success', 'added', 'Reminder');
  82. }
  83. return $this->redirect($request->headers->get('referer'));
  84. }
  85. #[Route(path: '/admin/reminder/edit/{id}', name: 'sov_admin_reminder_edit')]
  86. public function edit(Request $request)
  87. {
  88. $id = $request->get('id');
  89. $reminder = $this->reminderContainer->getRepositoryQuery()->getRepository()->find($id);
  90. $form = $this->formFactory->create(ReminderAdminFormType::class, $reminder);
  91. $form->handleRequest($request);
  92. if ($form->isSubmitted() && $form->isValid()) {
  93. $reminder = $form->getData();
  94. $this->entityManager->update($reminder);
  95. $this->entityManager->flush();
  96. $this->addFlashTranslator('success', 'updated');
  97. }
  98. return $this->redirect($request->headers->get('referer'));
  99. }
  100. #[Route(path: '/admin/reminder/done', name: 'sov_admin_reminder_done')]
  101. public function done(Request $request): JsonResponse
  102. {
  103. $id = $request->get('id');
  104. $reminder = $this->reminderContainer->getRepositoryQuery()->getRepository()->find($id);
  105. $done = $request->get('done');
  106. if($done == 'true') {
  107. $reminder->setDone(true);
  108. }
  109. else {
  110. $reminder->setDone(false);
  111. }
  112. $this->entityManager->update($reminder);
  113. $this->entityManager->flush();
  114. return new JsonResponse(['success' => true]);
  115. }
  116. public function createEntity(string $crudAction = null, string $crudControllerFqcn = null, int $entityId = null)
  117. {
  118. $factory = new ReminderFactory();
  119. return $factory->create($crudAction, $crudControllerFqcn, $entityId);
  120. }
  121. }