Skip to content

Commit

Permalink
automatic registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrodevog committed Oct 14, 2023
1 parent 0cde35b commit 6d76125
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTime;
use CudiBundle\Entity\Sale\Session\OpeningHour;
use Laminas\View\Model\ViewModel;
use ShiftBundle\Hydrator;

/**
* OpeningHourController
Expand Down Expand Up @@ -126,9 +127,12 @@ public function editAction()
public function scheduleAction()
{
$form = $this->getForm('cudi_sale_session_opening-hour_schedule');
$registrationForm = $this->getForm('shift_registration-shift_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
$now = (new DateTime())->format('d/m/Y H:i');

$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());
Expand All @@ -138,17 +142,49 @@ public function scheduleAction()
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];
$startDate = $split[2] . ' ' . $startHour;
$endDate = $split[2] . ' ' . $endHour;

$data = array();
$data["start"] = $date . ' ' . $startHour;
$data["end"] = $date . ' ' . $endHour;
$data["start_date"] = $startDate;
$data["end_date"] = $endDate;

$this->getEntityManager()->persist(
$form->getHydrator()->hydrate($data)
);

$count = 0;
$startHour_ = $startHour; // creating dummy variable that is updated
$signoutDate = (DateTime::createFromFormat('d/m/Y', $split[2]))->modify('+1 day')->format('d/m/Y') . ' 00:00';

while ($count != 5 && $startHour_ != $endHour) {
$nextTime = $this->calculateNextTime($startHour_);
$registration = array(
'unit' => 1,
'event' => '',
'location' => 1,
'start_date' => $split[2] . ' ' . $startHour_,
'end_date' => $split[2] . ' ' . $nextTime,
'visible_date' => $now,
'signout_date' => $signoutDate,
'nb_registered' => 50,
'name' => 'Boekenverkoop',
'description' => 'Kom je boeken ophalen ;)',
'ticket_needed' => true,
'members_only' => false,
'members_visible' => true,
'final_signin_date' => $startDate,
'is_cudi_timeslot' => true,
);
$this->getEntityManager()->persist(
$registrationForm->getHydrator()->hydrate($registration)
);

$startHour_ = $nextTime;
$count += 1;
}
}
}

Expand Down Expand Up @@ -222,4 +258,21 @@ private function getOpeningHourEntity()

return $openingHour;
}

/**
* @return string
*/
private function calculateNextTime($time)
{
$hour = explode(':', $time)[0];
$minute = explode(':', $time)[1];
if ($minute == '00') {
$minute = '30';
} else {
$hour = strval($hour + 1);
$minute = '00';
}

return $hour . ':' . $minute;
}
}
6 changes: 3 additions & 3 deletions module/CudiBundle/Form/Admin/Sale/Session/OpeningHour/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected function initBeforeTabs()
$this->add(
array(
'type' => 'datetime',
'name' => 'start',
'name' => 'start_date',
'label' => 'Start',
'required' => true,
)
Expand All @@ -28,7 +28,7 @@ protected function initBeforeTabs()
$this->add(
array(
'type' => 'datetime',
'name' => 'end',
'name' => 'end_date',
'label' => 'End',
'required' => true,
'options' => array(
Expand All @@ -37,7 +37,7 @@ protected function initBeforeTabs()
array(
'name' => 'DateCompare',
'options' => array(
'first_date' => 'start',
'first_date' => 'start_date',
'format' => 'd/m/Y H:i',
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use DateTime;

/**
* Add Order
* Add multiple opening hours at once
*
* @author Pedro Devogelaere <[email protected]>
*/
Expand Down Expand Up @@ -53,10 +53,10 @@ public function init()
*/
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
$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');

Expand Down
10 changes: 5 additions & 5 deletions module/CudiBundle/Hydrator/Sale/Session/OpeningHour.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ protected function doExtract($object = null)
}

$data = array(
'start' => $object->getStart()->format('d/m/Y H:i'),
'end' => $object->getEnd()->format('d/m/Y H:i'),
'start_date' => $object->getStart()->format('d/m/Y H:i'),
'end_date' => $object->getEnd()->format('d/m/Y H:i'),
'tab_content' => array(),
);

Expand All @@ -32,9 +32,9 @@ protected function doHydrate(array $data, $object = null)
if ($object === null) {
$object = new OpeningHourEntity($this->getPersonEntity());
}

$object->setStart(self::loadDateTime($data['start']))
->setEnd(self::loadDateTime($data['end']));
error_log(json_encode($data));
$object->setStart(self::loadDateTime($data['start_date']))
->setEnd(self::loadDateTime($data['end_date']));

foreach ($this->getLanguages() as $language) {
$abbrev = $language->getAbbrev();
Expand Down
14 changes: 14 additions & 0 deletions module/ShiftBundle/Form/Admin/RegistrationShift/Schedule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace ShiftBundle\Form\Admin\RegistrationShift;

/**
* Add multiple registrations at once; only made to get the hydrator in OpeningHourController
*
* @author Pedro Devogelaere <[email protected]>
*/
class Schedule extends \CommonBundle\Component\Form\Admin\Form
{
protected $hydrator = 'ShiftBundle\Hydrator\RegistrationShift';
}

0 comments on commit 6d76125

Please sign in to comment.