From bfd60bfe465a08125c8a0443b0131cadc679b317 Mon Sep 17 00:00:00 2001 From: pedrod Date: Fri, 13 Oct 2023 23:19:27 +0000 Subject: [PATCH] schedule for cudi --- .../Sale/Session/OpeningHourController.php | 57 ++++++++++++++++ .../Sale/Session/OpeningHour/Schedule.php | 66 +++++++++++++++++++ .../Resources/config/install/acl.config.php | 2 +- .../opening-hour/partials/navigation.twig | 3 + .../sale/session/opening-hour/schedule.twig | 12 ++++ 5 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 module/CudiBundle/Form/Admin/Sale/Session/OpeningHour/Schedule.php create mode 100644 module/CudiBundle/Resources/views/cudi/admin/sale/session/opening-hour/schedule.twig diff --git a/module/CudiBundle/Controller/Admin/Sale/Session/OpeningHourController.php b/module/CudiBundle/Controller/Admin/Sale/Session/OpeningHourController.php index 948b8f495f..fdeb642011 100644 --- a/module/CudiBundle/Controller/Admin/Sale/Session/OpeningHourController.php +++ b/module/CudiBundle/Controller/Admin/Sale/Session/OpeningHourController.php @@ -2,6 +2,7 @@ namespace CudiBundle\Controller\Admin\Sale\Session; +use DateTime; use CudiBundle\Entity\Sale\Session\OpeningHour; use Laminas\View\Model\ViewModel; @@ -54,6 +55,7 @@ public function addAction() $form->setData($this->getRequest()->getPost()); if ($form->isValid()) { + error_log(json_encode($form->getData())); $this->getEntityManager()->persist( $form->hydrateObject() ); @@ -121,6 +123,61 @@ public function editAction() ); } + public function scheduleAction() + { + $form = $this->getForm('cudi_sale_session_opening-hour_schedule'); + + $monday = new DateTime(); // create DateTime object with current time + $monday->setISODate($monday->format('o'), $monday->format('W') + 1); // set object to Monday on next week + + if ($this->getRequest()->isPost()) { + $form->setData($this->getRequest()->getPost()); + + if ($form->isValid()) { + $formData = $form->getData(); + foreach ($formData as $formKey => $formValue) { + $split = explode("_", $formKey); + if ($split[0] == 'interval' && $formValue) { + $date = $split[2]; // for readability create extra variables, could also just plug it in $data array + $startHour = explode('-', $split[1])[0]; + $endHour = explode('-', $split[1])[1]; + + $data = array(); + $data["start"] = $date . ' ' . $startHour; + $data["end"] = $date . ' ' . $endHour; + + $this->getEntityManager()->persist( + $form->getHydrator()->hydrate($data) + ); + } + } + + $this->getEntityManager()->flush(); + + $this->flashMessenger()->success( + 'Succes', + 'This schedule was successfully added!' + ); + + $this->redirect()->toRoute( + 'cudi_admin_sales_session_openinghour', + array( + 'action' => 'manage', + ) + ); + + return new ViewModel(); + } + } + + return new ViewModel( + array( + 'form' => $form, + 'nextMonday' => $monday, + ) + ); + } + public function deleteAction() { $this->initAjax(); diff --git a/module/CudiBundle/Form/Admin/Sale/Session/OpeningHour/Schedule.php b/module/CudiBundle/Form/Admin/Sale/Session/OpeningHour/Schedule.php new file mode 100644 index 0000000000..902b665ae8 --- /dev/null +++ b/module/CudiBundle/Form/Admin/Sale/Session/OpeningHour/Schedule.php @@ -0,0 +1,66 @@ + + */ +class Schedule extends \CommonBundle\Component\Form\Admin\Form +{ + protected $hydrator = 'CudiBundle\Hydrator\Sale\Session\OpeningHour'; + + public function init() + { + parent::init(); + + $days = $this->createDaysArray(); + + foreach ($days as $day) { + $this->add( + array( + 'type' => 'checkbox', + 'name' => 'interval_12:30-14:00_' . $day->format('d/m/Y'), + 'label' => $day->format('l') . ' 12:30 - 14:00', + 'attributes' => array( + 'id' => 'interval_12:30-14:00_' . $day->format('d/m/Y'), + ), + ) + ); + + $this->add( + array( + 'type' => 'checkbox', + 'name' => 'interval_18:00-19:00_' . $day->format('d/m/Y'), + 'label' => $day->format('l') . ' 18:00 - 19:00', + 'attributes' => array( + 'id' => 'interval_18:00-19:00_' . $day->format('d/m/Y'), + ), + ) + ); + } + + $this->addSubmit('Add', 'clock_add'); + } + + /** + * @return array + */ + private function createDaysArray() + { + $dt = new DateTime(); // create DateTime object with current time + $dt->setISODate($dt->format('o'), $dt->format('W') + 1); // set object to Monday on next week + $periods = new DatePeriod($dt, new DateInterval('P1D'), 3); // get all 1day periods from Monday to +6 days + $days = iterator_to_array($periods); // convert DatePeriod object to array + // $days[0] is Monday, ..., $days[3] is Thursday + // to format selected date do: $days[1]->format('Y-m-d'); + + return $days; + } +} + diff --git a/module/CudiBundle/Resources/config/install/acl.config.php b/module/CudiBundle/Resources/config/install/acl.config.php index 115f9b7f91..9f8ad5c0c5 100644 --- a/module/CudiBundle/Resources/config/install/acl.config.php +++ b/module/CudiBundle/Resources/config/install/acl.config.php @@ -63,7 +63,7 @@ 'delete', 'manage', ), 'cudi_admin_sales_session_openinghour' => array( - 'add', 'edit', 'delete', 'manage', 'old', + 'add', 'edit', 'schedule', 'delete', 'manage', 'old', ), 'cudi_admin_sales_session_message' => array( 'add', 'edit', 'delete', 'manage', diff --git a/module/CudiBundle/Resources/views/cudi/admin/sale/session/opening-hour/partials/navigation.twig b/module/CudiBundle/Resources/views/cudi/admin/sale/session/opening-hour/partials/navigation.twig index 42ff3d0eca..688a6a596e 100644 --- a/module/CudiBundle/Resources/views/cudi/admin/sale/session/opening-hour/partials/navigation.twig +++ b/module/CudiBundle/Resources/views/cudi/admin/sale/session/opening-hour/partials/navigation.twig @@ -2,6 +2,9 @@
Opening Hours