Skip to content

Commit

Permalink
WIP update event serie and event serie deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
catdesu committed Mar 14, 2024
1 parent d5edf15 commit e1ef49c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 8 deletions.
3 changes: 3 additions & 0 deletions orif/timbreuse/Config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@

$routes->group('event-series', function($routes) {
$routes->get('', '\Timbreuse\Controllers\EventSeries');

$routes->get('update/(:num)', '\Timbreuse\Controllers\EventSeries::update/$1');
$routes->post('update/(:num)', '\Timbreuse\Controllers\EventSeries::update/$1');

$routes->get('delete/(:num)', '\Timbreuse\Controllers\EventSeries::delete/$1');
$routes->post('delete/(:num)/(:num)', '\Timbreuse\Controllers\EventSeries::delete/$1/$2');
Expand Down
63 changes: 63 additions & 0 deletions orif/timbreuse/Controllers/EventSeries.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,69 @@ private function createEventPlannings(array $eventSerie, array $eventPlanning) :
return $planningErrors;
}

public function update(int $id) {
$eventSeriesModel = model(EventSeriesModel::class);

$eventSerie = $eventSeriesModel->find($id);

if (is_null($eventSerie)) {
return redirect()->to(base_url('admin/event-series'));
}

// Todo: replace the title on the update page

$data = [
'daysOfWeek' => $this->getDaysOfWeek(),
'recurrenceFrequencies' => $this->getEnumValues(),
'eventSerie' => $eventSerie
];

if (isset($_POST) && !empty($_POST)) {
$errors = $this->updateSerieAndGetErrors($id, $_POST);

if (empty($errors)) {
$newEventSerie = $eventSeriesModel->find($id);
dd($eventSerie, $newEventSerie);
$this->updateEventSeriesAndPlannings($eventSerie, $newEventSerie);
}
}

return $this->display_view('\Timbreuse\Views\eventSeries\update_form', $data);
}

public function updateSerieAndGetErrors(int $id, array $eventSerie) {
$eventSeriesModel = model(EventSeriesModel::class);

$eventSeriesModel->update($id, $eventSerie);

return $eventSeriesModel->errors();
}

public function updateEventSeriesAndPlannings(array $existingEventSeries, array $modifiedEventSeries) {
// Todo: implement and test this method
// Update event plannings
$planningErrors = [];

foreach ($existingEventSeries['plannings'] as $eventPlanning) {
$eventDate = new DateTime($eventPlanning['event_date']);

if ($eventDate < $modifiedEventSeries['start_date'] || $eventDate > $modifiedEventSeries['end_date']) {
// Deletion logic goes here
} else {
$dayOfWeek = strtolower($eventDate->format('l'));
if (!in_array($dayOfWeek, $modifiedEventSeries['days_of_week'])) {
// Deletion logic goes here
} else {
// Update logic goes here
}
}
}

// Creation logic goes here

return $planningErrors;
}

/**
* Display the delete form and delete the corresponding event planning
*
Expand Down
18 changes: 11 additions & 7 deletions orif/timbreuse/Models/EventSeriesModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,26 @@ public function __construct(ConnectionInterface &$db = null, ValidationInterface
}

protected function encodeDays(array $data) {
if (isset($data['data'][0]['days_of_week'])) {
$data['data'][0]['days_of_week'] = json_encode($data['data'][0]['days_of_week']);
if (isset($data['data']['days_of_week'])) {
$data['data']['days_of_week'] = json_encode($data['data']['days_of_week']);
}
return $data;
}

protected function decodeDays(array $data) {
if (isset($data) && count($data['data']) > 0) {
foreach($data['data'] as &$row) {
if (is_array($row) && array_key_exists('days_of_week', $row)) {
$row['days_of_week'] = json_decode($row['days_of_week']);
if (isset($data) && !empty($data['data'])) {
if (is_array($data['data']) && array_key_exists('days_of_week', $data['data'])) {
$data['data']['days_of_week'] = json_decode($data['data']['days_of_week']);
} else if (is_array($data['data'])) {
foreach ($data['data'] as &$row) {
if (is_array($row) && array_key_exists('days_of_week', $row)) {
$row['days_of_week'] = json_decode($row['days_of_week']);
}
}
}
}
return $data;
}
}

public function findAllSeries(?int $eventSeriesId = null) : array {
return $this
Expand Down
2 changes: 1 addition & 1 deletion orif/timbreuse/Views/eventSeries/create_series.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
foreach ($daysOfWeek as $key => $day) :
?>
<div class="form-check form-check-inline">
<?= form_checkbox('days[]', $key, $eventSerie[$key] ?? false, ['id' => $key, 'class' => 'form-check-input']); ?>
<?= form_checkbox('days[]', $key, in_array($key, $eventSerie['days_of_week']), ['id' => $key, 'class' => 'form-check-input']); ?>
<?= form_label(ucfirst($day), $key, ['class' => 'form-check-label']); ?>
</div>
<?php endforeach; ?>
Expand Down
13 changes: 13 additions & 0 deletions orif/timbreuse/Views/eventSeries/update_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="container">
<?= form_open(current_url()) ?>
<?php require 'create_series.php' ?>
<div class="row mb-3 mt-3">
<div class="col text-right">
<a class="btn btn-secondary" href="<?= base_url('admin/event-series'); ?>">
<?= lang('common_lang.btn_cancel'); ?>
</a>
<?= form_submit('save', lang('common_lang.btn_save'), ['class' => 'btn btn-primary']); ?>
</div>
</div>
<?= form_close() ?>
</div>

0 comments on commit e1ef49c

Please sign in to comment.