entityManager = $entityManager; $this->reminderRepositoryQuery = $reminderRepositoryQuery; $this->formFactory = $formFactory; $this->urlGenerator = $urlGenerator; $this->parameterBag = $parameterBag; } /** * @Route("/admin/reminder/modal", name="sov_admin_reminder_render_modal") */ 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() ] ); } /** * @Route("/admin/reminder/new", name="sov_admin_reminder_new") */ 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')); } /** * @Route("/admin/reminder/edit/{id}", name="sov_admin_reminder_edit") */ 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')); } /** * @Route("/admin/reminder/done", name="sov_admin_reminder_done") */ 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); } }