Skip to content

Commit

Permalink
schedule for cudi
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrodevog committed Oct 13, 2023
1 parent 5ba0631 commit bfd60bf
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CudiBundle\Controller\Admin\Sale\Session;

use DateTime;
use CudiBundle\Entity\Sale\Session\OpeningHour;
use Laminas\View\Model\ViewModel;

Expand Down Expand Up @@ -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()
);
Expand Down Expand Up @@ -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();
Expand Down
66 changes: 66 additions & 0 deletions module/CudiBundle/Form/Admin/Sale/Session/OpeningHour/Schedule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace CudiBundle\Form\Admin\Sale\Session\OpeningHour;

use DateInterval;
use DatePeriod;
use DateTime;

/**
* Add Order
*
* @author Pedro Devogelaere <[email protected]>
*/
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;
}
}

2 changes: 1 addition & 1 deletion module/CudiBundle/Resources/config/install/acl.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<div class="title"><span class="clock">Opening Hours</span></div>
<nav id="controller_nav">
<ul>
{% if hasAccess('cudi_admin_sales_session_openinghour', 'schedule') %}
<li><a {% if 'add' == getParam('schedule') %} class="active" {% endif %} href="{{ url('cudi_admin_sales_session_openinghour', {"action": "schedule"}) }}">Schedule</a></li>
{% endif %}
{% if hasAccess('cudi_admin_sales_session_openinghour', 'add') %}
<li><a {% if 'add' == getParam('action') %} class="active" {% endif %} href="{{ url('cudi_admin_sales_session_openinghour', {"action": "add"}) }}">Add</a></li>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends 'admin/base.twig' %}

{% block content %}
{% include 'cudi/admin/sale/session/opening-hour/partials/navigation.twig' %}

{% include 'admin/partials/flashMessenger.twig' %}
<h1>Week: <b>{{ nextMonday.format('l d/m') }}</b> - <b>{{ nextMonday.modify('next thursday').format('l d/m') }}</b></h1>
<div id="controller_action">
{% import 'admin/partials/form.twig' as forms %}
{{ forms.renderForm(form) }}
</div>
{% endblock %}

0 comments on commit bfd60bf

Please sign in to comment.