From d96c3891ee1f163f4d36f77d77c2dfd07802ac38 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Tue, 22 Aug 2023 19:48:09 +0200 Subject: [PATCH] Create AdminOpeningHourController. Move everything except widget --- .../openinghour/widget_generator.html.twig | 4 +- .../Controller/AdminOpeningHourController.php | 183 ++++++++++++++++++ .../Controller/OpeningHourController.php | 171 +--------------- 3 files changed, 187 insertions(+), 171 deletions(-) create mode 100644 src/AppBundle/Controller/AdminOpeningHourController.php diff --git a/app/Resources/views/admin/openinghour/widget_generator.html.twig b/app/Resources/views/admin/openinghour/widget_generator.html.twig index 7f78ff774..bce39d701 100644 --- a/app/Resources/views/admin/openinghour/widget_generator.html.twig +++ b/app/Resources/views/admin/openinghour/widget_generator.html.twig @@ -44,12 +44,12 @@

Résultat

- +
texte à copier-coller 👇
-
<iframe src="{{ absolute_url(path('admin_openinghour_widget')) }}?{{ query_string }}" frameborder="0"></iframe>
+
<iframe src="{{ absolute_url(path('openinghour_widget')) }}?{{ query_string }}" frameborder="0"></iframe>
diff --git a/src/AppBundle/Controller/AdminOpeningHourController.php b/src/AppBundle/Controller/AdminOpeningHourController.php new file mode 100644 index 000000000..9013ebc4a --- /dev/null +++ b/src/AppBundle/Controller/AdminOpeningHourController.php @@ -0,0 +1,183 @@ +getDoctrine()->getManager(); + + $openingHours = $em->getRepository('AppBundle:OpeningHour')->findAll(); + + return $this->render('admin/openinghour/index.html.twig', array( + 'openingHours' => $openingHours + )); + } + + /** + * Add new opening hour + * + * @Route("/new", name="admin_openinghour_new", methods={"GET","POST"}) + * @Security("has_role('ROLE_ADMIN')") + */ + public function newAction(Request $request) + { + $session = new Session(); + $em = $this->getDoctrine()->getManager(); + + $openingHour = new OpeningHour(); + + $form = $this->createForm(OpeningHourType::class, $openingHour); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $start = $form->get('start')->getData(); + $openingHour->setStart(new \DateTime($start)); + $end = $form->get('end')->getData(); + $openingHour->setEnd(new \DateTime($end)); + + $em->persist($openingHour); + $em->flush(); + + $session->getFlashBag()->add('success', "L'horaire a bien été crée !"); + return $this->redirectToRoute('admin_openinghour_index'); + } + + return $this->render('admin/openinghour/new.html.twig', array( + 'form' => $form->createView() + )); + } + + /** + * Edit opening hour + * + * @Route("/edit/{id}", name="admin_openinghour_edit", methods={"GET","POST"}) + * @Security("has_role('ROLE_ADMIN')") + */ + public function editAction(Request $request, OpeningHour $openingHour) + { + $session = new Session(); + $em = $this->getDoctrine()->getManager(); + + $form = $this->createForm(OpeningHourType::class, $openingHour); + $form->handleRequest($request); + + if ($request->isMethod('GET')) { + $form->get('start')->setData($openingHour->getStart()->format('H:i')); + $form->get('end')->setData($openingHour->getEnd()->format('H:i')); + } + + if ($form->isSubmitted() && $form->isValid()) { + $start = $form->get('start')->getData(); + $openingHour->setStart(new \DateTime($start)); + $end = $form->get('end')->getData(); + $openingHour->setEnd(new \DateTime($end)); + + $em->persist($openingHour); + $em->flush(); + + $session->getFlashBag()->add('success', "L'horaire a bien été éditée !"); + return $this->redirectToRoute('admin_openinghour_index'); + } + + return $this->render('admin/openinghour/edit.html.twig', array( + 'form' => $form->createView(), + 'delete_form' => $this->getDeleteForm($openingHour)->createView() + )); + } + + /** + * Delete opening hour + * + * @Route("/{id}", name="admin_openinghour_delete", methods={"DELETE"}) + * @Security("has_role('ROLE_ADMIN')") + */ + public function deleteAction(Request $request, OpeningHour $openingHour) + { + $session = new Session(); + + $form = $this->getDeleteForm($openingHour); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->remove($openingHour); + $em->flush(); + + $session->getFlashBag()->add('success', "L'horaire a bien été supprimée !"); + return $this->redirectToRoute('admin_openinghour_index'); + } + + return $this->redirectToRoute('admin_openinghour_index'); + } + + /** + * Opening hours widget generator + * + * @Route("/widget_generator", name="admin_openinghour_widget_generator", methods={"GET","POST"}) + * @Security("has_role('ROLE_ADMIN')") + */ + public function widgetGeneratorAction(Request $request) + { + $form = $this->createFormBuilder() + ->add('title', CheckboxType::class, array('required' => false, 'data' => true, 'label' => 'Afficher le titre du widget ?')) + ->add('align', ChoiceType::class, array( + 'label' => 'Alignement', + 'choices' => array('centré' => 'center', 'gauche' => 'left'), + 'data' => 'center' + )) + ->add('generate', SubmitType::class, array('label' => 'Générer')) + ->getForm(); + + if ($form->handleRequest($request)->isValid()) { + $data = $form->getData(); + + $widgetQueryString = 'title='.($data['title'] ? 1 : 0) . '&align=' . $data['align']; + + return $this->render('admin/openinghour/widget_generator.html.twig', array( + 'query_string' => $widgetQueryString, + 'form' => $form->createView(), + )); + } + + return $this->render('admin/openinghour/widget_generator.html.twig', array( + 'form' => $form->createView(), + )); + } + + /** + * @param OpeningHour $openingHour + * @return \Symfony\Component\Form\FormInterface + */ + protected function getDeleteForm(OpeningHour $openingHour) + { + return $this->createFormBuilder() + ->setAction($this->generateUrl('admin_openinghour_delete', array('id' => $openingHour->getId()))) + ->setMethod('DELETE') + ->getForm(); + } +} diff --git a/src/AppBundle/Controller/OpeningHourController.php b/src/AppBundle/Controller/OpeningHourController.php index 88e3d6d42..55e6795c6 100644 --- a/src/AppBundle/Controller/OpeningHourController.php +++ b/src/AppBundle/Controller/OpeningHourController.php @@ -3,28 +3,21 @@ namespace AppBundle\Controller; use AppBundle\Entity\OpeningHour; -use AppBundle\Form\OpeningHourType; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Routing\Annotation\Route; -use Symfony\Component\Form\Form; -use Symfony\Component\Form\Extension\Core\Type\CheckboxType; -use Symfony\Component\Form\Extension\Core\Type\SubmitType; -use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Session\Session; -use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; /** * OpeningHour controller * - * @Route("admin/openinghours") + * @Route("openinghours") */ class OpeningHourController extends Controller { /** * Opening hours widget display * - * @Route("/widget", name="admin_openinghour_widget", methods={"GET"}) + * @Route("/widget", name="openinghour_widget", methods={"GET"}) */ public function widgetAction(Request $request) { @@ -41,164 +34,4 @@ public function widgetAction(Request $request) 'align' => $filter_align, ]); } - - /** - * List all opening hours - * - * @Route("/", name="admin_openinghour_index", methods={"GET"}) - * @Security("has_role('ROLE_ADMIN')") - */ - public function indexAction(Request $request) - { - $em = $this->getDoctrine()->getManager(); - - $openingHours = $em->getRepository('AppBundle:OpeningHour')->findAll(); - - return $this->render('admin/openinghour/index.html.twig', array( - 'openingHours' => $openingHours - )); - } - - /** - * Add new opening hour - * - * @Route("/new", name="admin_openinghour_new", methods={"GET","POST"}) - * @Security("has_role('ROLE_ADMIN')") - */ - public function newAction(Request $request) - { - $session = new Session(); - $em = $this->getDoctrine()->getManager(); - - $openingHour = new OpeningHour(); - - $form = $this->createForm(OpeningHourType::class, $openingHour); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $start = $form->get('start')->getData(); - $openingHour->setStart(new \DateTime($start)); - $end = $form->get('end')->getData(); - $openingHour->setEnd(new \DateTime($end)); - - $em->persist($openingHour); - $em->flush(); - - $session->getFlashBag()->add('success', "L'horaire a bien été crée !"); - return $this->redirectToRoute('admin_openinghour_index'); - } - - return $this->render('admin/openinghour/new.html.twig', array( - 'form' => $form->createView() - )); - } - - /** - * Edit opening hour - * - * @Route("/edit/{id}", name="admin_openinghour_edit", methods={"GET","POST"}) - * @Security("has_role('ROLE_ADMIN')") - */ - public function editAction(Request $request, OpeningHour $openingHour) - { - $session = new Session(); - $em = $this->getDoctrine()->getManager(); - - $form = $this->createForm(OpeningHourType::class, $openingHour); - $form->handleRequest($request); - - if ($request->isMethod('GET')) { - $form->get('start')->setData($openingHour->getStart()->format('H:i')); - $form->get('end')->setData($openingHour->getEnd()->format('H:i')); - } - - if ($form->isSubmitted() && $form->isValid()) { - $start = $form->get('start')->getData(); - $openingHour->setStart(new \DateTime($start)); - $end = $form->get('end')->getData(); - $openingHour->setEnd(new \DateTime($end)); - - $em->persist($openingHour); - $em->flush(); - - $session->getFlashBag()->add('success', "L'horaire a bien été éditée !"); - return $this->redirectToRoute('admin_openinghour_index'); - } - - return $this->render('admin/openinghour/edit.html.twig', array( - 'form' => $form->createView(), - 'delete_form' => $this->getDeleteForm($openingHour)->createView() - )); - } - - /** - * Delete opening hour - * - * @Route("/{id}", name="admin_openinghour_delete", methods={"DELETE"}) - * @Security("has_role('ROLE_ADMIN')") - */ - public function deleteAction(Request $request, OpeningHour $openingHour) - { - $session = new Session(); - - $form = $this->getDeleteForm($openingHour); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $em = $this->getDoctrine()->getManager(); - $em->remove($openingHour); - $em->flush(); - - $session->getFlashBag()->add('success', "L'horaire a bien été supprimée !"); - return $this->redirectToRoute('admin_openinghour_index'); - } - - return $this->redirectToRoute('admin_openinghour_index'); - } - - /** - * Opening hours widget generator - * - * @Route("/widget_generator", name="admin_openinghour_widget_generator", methods={"GET","POST"}) - * @Security("has_role('ROLE_ADMIN')") - */ - public function widgetGeneratorAction(Request $request) - { - $form = $this->createFormBuilder() - ->add('title', CheckboxType::class, array('required' => false, 'data' => true, 'label' => 'Afficher le titre du widget ?')) - ->add('align', ChoiceType::class, array( - 'label' => 'Alignement', - 'choices' => array('centré' => 'center', 'gauche' => 'left'), - 'data' => 'center' - )) - ->add('generate', SubmitType::class, array('label' => 'Générer')) - ->getForm(); - - if ($form->handleRequest($request)->isValid()) { - $data = $form->getData(); - - $widgetQueryString = 'title='.($data['title'] ? 1 : 0) . '&align=' . $data['align']; - - return $this->render('admin/openinghour/widget_generator.html.twig', array( - 'query_string' => $widgetQueryString, - 'form' => $form->createView(), - )); - } - - return $this->render('admin/openinghour/widget_generator.html.twig', array( - 'form' => $form->createView(), - )); - } - - /** - * @param OpeningHour $openingHour - * @return \Symfony\Component\Form\FormInterface - */ - protected function getDeleteForm(OpeningHour $openingHour) - { - return $this->createFormBuilder() - ->setAction($this->generateUrl('admin_openinghour_delete', array('id' => $openingHour->getId()))) - ->setMethod('DELETE') - ->getForm(); - } }