|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
-
- namespace Lc\SovBundle\Controller\Reminder;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Resolver\MerchantResolver;
- use Lc\CaracoleBundle\Resolver\SectionResolver;
- use Lc\SovBundle\Factory\Reminder\ReminderFactory;
- use Lc\SovBundle\Factory\Reminder\ReminderFactoryInterface;
- use Lc\SovBundle\Form\Reminder\ReminderAdminFormType;
- use Lc\SovBundle\Repository\Reminder\ReminderRepositoryQueryInterface;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Symfony\Component\Form\FormFactoryInterface;
- use Symfony\Component\HttpFoundation\JsonResponse;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
- use Symfony\Component\Routing\Annotation\Route;
-
- class ReminderAdminController extends AbstractController
- {
-
- protected EntityManagerInterface $entityManager;
- protected ReminderRepositoryQueryInterface $reminderRepositoryQuery;
- protected FormFactoryInterface $formFactory;
- protected UrlGeneratorInterface $urlGenerator;
- protected MerchantResolver $merchantResolver;
- protected SectionResolver $sectionResolver;
- protected ParameterBagInterface $parameterBag;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- ReminderRepositoryQueryInterface $reminderRepositoryQuery,
- FormFactoryInterface $formFactory,
- UrlGeneratorInterface $urlGenerator,
- ParameterBagInterface $parameterBag
- ) {
- $this->entityManager = $entityManager;
- $this->reminderRepositoryQuery = $reminderRepositoryQuery;
- $this->formFactory = $formFactory;
- $this->urlGenerator = $urlGenerator;
- $this->parameterBag = $parameterBag;
- }
-
-
-
- public function renderModal(Request $request): Response
- {
- $id = $request->get('id');
-
- if($id) {
- $reminder = $this->reminderRepositoryQuery->getRepository()->find($id);
- $action = $this->urlGenerator->generate(
- $this->parameterBag->get('app.reminder.route_edit'),
- ['id' => $id]
- );
- }
- else {
- $crudAction = $request->get('crudAction');
- $crudControllerFqcn = $request->get('crudControllerFqcn');
- $entityId = $request->get('entityId') ? $request->get('entityId') : null ;
- $reminder = $this->createEntity($crudAction, $crudControllerFqcn, $entityId);
- $action = $this->urlGenerator->generate(
- $this->parameterBag->get('app.reminder.route_new')
- );
- }
-
- $form = $this->formFactory->create(
- ReminderAdminFormType::class,
- $reminder,
- [
- 'action' => $action
- ]
- );
-
- return $this->render(
- '@LcSov/admin/reminder/form_modal.html.twig',
- [
- 'form_reminder' => $form->createView()
- ]
- );
- }
-
-
-
- public function new(Request $request)
- {
- $reminder = $this->createEntity();
-
- $form = $this->formFactory->create(ReminderAdminFormType::class, $reminder);
- $form->handleRequest($request);
-
- if ($form->isSubmitted() && $form->isValid()) {
- $reminder = $form->getData();
- $this->entityManager->persist($reminder);
- $this->entityManager->flush();
-
- $this->addFlash('success', 'Le pense-bête a bien été ajouté');
- }
-
- return $this->redirect($request->headers->get('referer'));
- }
-
-
-
- public function edit(Request $request)
- {
- $id = $request->get('id');
- $reminder = $this->reminderRepositoryQuery->getRepository()->find($id);
-
- $form = $this->formFactory->create(ReminderAdminFormType::class, $reminder);
- $form->handleRequest($request);
-
- if ($form->isSubmitted() && $form->isValid()) {
- $reminder = $form->getData();
- $this->entityManager->update($reminder);
- $this->entityManager->flush();
-
- $this->addFlash('success', 'Le pense-bête a bien été mis à jour');
- }
-
- return $this->redirect($request->headers->get('referer'));
- }
-
-
-
- public function done(Request $request): JsonResponse
- {
- $id = $request->get('id');
- $reminder = $this->reminderRepositoryQuery->getRepository()->find($id);
- $done = $request->get('done');
-
- if($done == 'true') {
- $reminder->setDone(true);
- }
- else {
- $reminder->setDone(false);
- }
-
- $this->entityManager->update($reminder);
- $this->entityManager->flush();
- return new JsonResponse(['success' => true]);
- }
-
- public function createEntity(string $crudAction = null, string $crudControllerFqcn = null, int $entityId = null)
- {
- $factory = new ReminderFactory();
- return $factory->create($crudAction, $crudControllerFqcn, $entityId);
- }
- }
|