Skip to content

Commit

Permalink
Update the blocked period CRUD (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
alextselegidis committed Nov 17, 2023
1 parent 9e2c482 commit 1c3ae95
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 24 deletions.
2 changes: 2 additions & 0 deletions application/config/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@
const WEBHOOK_SECRETARY_DELETE = 'secretary_delete';
const WEBHOOK_ADMIN_SAVE = 'admin_save';
const WEBHOOK_ADMIN_DELETE = 'admin_delete';
const WEBHOOK_BLOCKED_PERIOD_SAVE = 'blocked_period_save';
const WEBHOOK_BLOCKED_PERIOD_DELETE = 'blocked_period_delete';

/* End of file constants.php */
/* Location: ./application/config/constants.php */
45 changes: 26 additions & 19 deletions application/controllers/Blocked_periods.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public function index()
script_vars([
'user_id' => $user_id,
'role_slug' => $role_slug,
'date_format' => setting('date_format'),
'time_format' => setting('time_format'),
'first_weekday' => setting('first_weekday'),
]);

html_vars([
Expand Down Expand Up @@ -118,22 +121,24 @@ public function store()
abort(403, 'Forbidden');
}

$service_category = request('service_category');
$blocked_period = request('blocked_period');

$this->blocked_periods_model->only($service_category, [
$this->blocked_periods_model->only($blocked_period, [
'name',
'start_datetime',
'end_datetime',
'description'
]);

$service_category_id = $this->blocked_periods_model->save($service_category);
$blocked_period_id = $this->blocked_periods_model->save($blocked_period);

$service_category = $this->blocked_periods_model->find($service_category_id);
$blocked_period = $this->blocked_periods_model->find($blocked_period_id);

$this->webhooks_client->trigger(WEBHOOK_SERVICE_CATEGORY_SAVE, $service_category);
$this->webhooks_client->trigger(WEBHOOK_BLOCKED_PERIOD_SAVE, $blocked_period);

json_response([
'success' => TRUE,
'id' => $service_category_id
'id' => $blocked_period_id
]);
}
catch (Throwable $e)
Expand All @@ -154,23 +159,25 @@ public function update()
abort(403, 'Forbidden');
}

$service_category = request('service_category');
$blocked_period = request('blocked_period');

$this->blocked_periods_model->only($service_category, [
$this->blocked_periods_model->only($blocked_period, [
'id',
'name',
'start_datetime',
'end_datetime',
'description'
]);

$service_category_id = $this->blocked_periods_model->save($service_category);
$blocked_period_id = $this->blocked_periods_model->save($blocked_period);

$service_category = $this->blocked_periods_model->find($service_category_id);
$blocked_period = $this->blocked_periods_model->find($blocked_period_id);

$this->webhooks_client->trigger(WEBHOOK_SERVICE_CATEGORY_SAVE, $service_category);
$this->webhooks_client->trigger(WEBHOOK_BLOCKED_PERIOD_SAVE, $blocked_period);

json_response([
'success' => TRUE,
'id' => $service_category_id
'id' => $blocked_period_id
]);
}
catch (Throwable $e)
Expand All @@ -191,13 +198,13 @@ public function destroy()
abort(403, 'Forbidden');
}

$service_category_id = request('service_category_id');
$blocked_period_id = request('blocked_period_id');

$service_category = $this->blocked_periods_model->find($service_category_id);
$blocked_period = $this->blocked_periods_model->find($blocked_period_id);

$this->blocked_periods_model->delete($service_category_id);
$this->blocked_periods_model->delete($blocked_period_id);

$this->webhooks_client->trigger(WEBHOOK_SERVICE_CATEGORY_DELETE, $service_category);
$this->webhooks_client->trigger(WEBHOOK_BLOCKED_PERIOD_DELETE, $blocked_period);

json_response([
'success' => TRUE,
Expand All @@ -221,11 +228,11 @@ public function find()
abort(403, 'Forbidden');
}

$service_category_id = request('service_category_id');
$blocked_period_id = request('blocked_period_id');

$service_category = $this->blocked_periods_model->find($service_category_id);
$blocked_period = $this->blocked_periods_model->find($blocked_period_id);

json_response($service_category);
json_response($blocked_period);
}
catch (Throwable $e)
{
Expand Down
9 changes: 5 additions & 4 deletions application/views/pages/blocked_periods.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@
</div>

<div class="mb-3">
<label class="form-label" for="start-datetime">
<label class="form-label" for="start-date-time">
<?= lang('start') ?>
<span class="text-danger" hidden>*</span>
</label>
<input id="start-datetime" class="form-control required" disabled>
<input id="start-date-time" class="form-control required" disabled>
</div>

<div class="mb-3">
<label class="form-label" for="end-datetime">
<label class="form-label" for="end-date-time">
<?= lang('end') ?>
<span class="text-danger" hidden>*</span>
</label>
<input id="end-datetime" class="form-control required" disabled>
<input id="end-date-time" class="form-control required" disabled>
</div>

<div class="mb-3">
Expand All @@ -104,6 +104,7 @@
<script src="<?= asset_url('assets/js/utils/message.js') ?>"></script>
<script src="<?= asset_url('assets/js/utils/validation.js') ?>"></script>
<script src="<?= asset_url('assets/js/utils/url.js') ?>"></script>
<script src="<?= asset_url('assets/js/utils/ui.js') ?>"></script>
<script src="<?= asset_url('assets/js/http/blocked_periods_http_client.js') ?>"></script>
<script src="<?= asset_url('assets/js/pages/blocked_periods.js') ?>"></script>

Expand Down
17 changes: 16 additions & 1 deletion assets/js/pages/blocked_periods.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ App.Pages.BlockedPeriods = (function () {
const $filterBlockedPeriods = $('#filter-blocked-periods');
const $id = $('#id');
const $name = $('#name');
const $startDateTime = $('#start-date-time');
const $endDateTime = $('#end-date-time');
const $description = $('#description');
const moment = window.moment;

let filterResults = {};
let filterLimit = 20;

Expand Down Expand Up @@ -117,8 +121,15 @@ App.Pages.BlockedPeriods = (function () {
* Event: Blocked period Save Button "Click"
*/
$blockedPeriods.on('click', '#save-blocked-period', () => {
const startDateTimeObject = App.Utils.UI.getDatetimepickerValue($startDateTime);
const startDateTimeMoment = moment(startDateTimeObject);
const endDateTimeObject = App.Utils.UI.getDatetimepickerValue($endDateTime);
const endDateTimeMoment = moment(endDateTimeObject);

const blockedPeriod = {
name: $name.val(),
start_datetime: startDateTimeMoment.format('YYYY-MM-DD HH:mm:ss'),
end_datetime: endDateTimeMoment.format('YYYY-MM-DD HH:mm:ss'),
description: $description.val()
};

Expand Down Expand Up @@ -149,7 +160,7 @@ App.Pages.BlockedPeriods = (function () {
* Filter blocked periods records.
*
* @param {String} keyword This key string is used to filter the blocked-period records.
* @param {Number} selectId Optional, if set then after the filter operation the record with the given ID will be
* @param {Number} selectId Optional, if set then after the filter operation the record with the given ID will be
* selected (but not displayed).
* @param {Boolean} show Optional (false), if true then the selected record will be displayed on the form.
*/
Expand Down Expand Up @@ -222,6 +233,8 @@ App.Pages.BlockedPeriods = (function () {
function display(blockedPeriod) {
$id.val(blockedPeriod.id);
$name.val(blockedPeriod.name);
App.Utils.UI.setDatetimepickerValue($startDateTime, new Date(blockedPeriod.start_datetime));
App.Utils.UI.setDatetimepickerValue($endDateTime, new Date(blockedPeriod.end_datetime));
$description.val(blockedPeriod.description);
}

Expand Down Expand Up @@ -322,6 +335,8 @@ App.Pages.BlockedPeriods = (function () {
resetForm();
filter('');
addEventListeners();
App.Utils.UI.initializeDatetimepicker($startDateTime);
App.Utils.UI.initializeDatetimepicker($endDateTime);
}

document.addEventListener('DOMContentLoaded', initialize);
Expand Down
31 changes: 31 additions & 0 deletions assets/js/utils/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,42 @@ window.App.Utils.UI = (function () {
$target.trumbowyg(params);
}

/**
* Get Date, Date-Time or Time picker value.
*
* @param {jQuery} $target
*
* @return {Date}
*/
function getDatetimepickerValue($target) {
if (!$target?.length) {
throw new Error('Empty $target argument provided.')
}

return $target[0]._flatpickr.selectedDates[0];
}

/**
* Set Date, Date-Time or Time picker value.
*
* @param {jQuery} $target
* @param {Date} value
*/
function setDatetimepickerValue($target, value) {
if (!$target?.length) {
throw new Error('Empty $target argument provided.')
}

return $target[0]._flatpickr.setDate(value);
}

return {
initializeDatetimepicker,
initializeDatepicker,
initializeTimepicker,
initializeDropdown,
initializeTextEditor,
getDatetimepickerValue,
setDatetimepickerValue,
};
})();

0 comments on commit 1c3ae95

Please sign in to comment.