From 1c3ae956aafdf7b5f198a100a72ef37589388913 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Fri, 17 Nov 2023 08:04:27 +0100 Subject: [PATCH] Update the blocked period CRUD (#432) --- application/config/constants.php | 2 + application/controllers/Blocked_periods.php | 45 ++++++++++++--------- application/views/pages/blocked_periods.php | 9 +++-- assets/js/pages/blocked_periods.js | 17 +++++++- assets/js/utils/ui.js | 31 ++++++++++++++ 5 files changed, 80 insertions(+), 24 deletions(-) diff --git a/application/config/constants.php b/application/config/constants.php index 29ff6b99..1f2dbad6 100644 --- a/application/config/constants.php +++ b/application/config/constants.php @@ -120,6 +120,8 @@ const WEBHOOK_SECRETARY_SAVE = 'secretary_save'; 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 */ diff --git a/application/controllers/Blocked_periods.php b/application/controllers/Blocked_periods.php index 44092a04..b86b5b3f 100644 --- a/application/controllers/Blocked_periods.php +++ b/application/controllers/Blocked_periods.php @@ -63,6 +63,9 @@ class Blocked_periods extends EA_Controller { 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([ @@ -118,22 +121,24 @@ class Blocked_periods extends EA_Controller { 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) @@ -154,23 +159,25 @@ class Blocked_periods extends EA_Controller { 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) @@ -191,13 +198,13 @@ class Blocked_periods extends EA_Controller { 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, @@ -221,11 +228,11 @@ class Blocked_periods extends EA_Controller { 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) { diff --git a/application/views/pages/blocked_periods.php b/application/views/pages/blocked_periods.php index 83162c96..253c9f07 100644 --- a/application/views/pages/blocked_periods.php +++ b/application/views/pages/blocked_periods.php @@ -71,19 +71,19 @@
-
-
@@ -104,6 +104,7 @@ + diff --git a/assets/js/pages/blocked_periods.js b/assets/js/pages/blocked_periods.js index 693fdc31..647e4741 100644 --- a/assets/js/pages/blocked_periods.js +++ b/assets/js/pages/blocked_periods.js @@ -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; @@ -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() }; @@ -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. */ @@ -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); } @@ -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); diff --git a/assets/js/utils/ui.js b/assets/js/utils/ui.js index a4bef6e6..3599cbcf 100644 --- a/assets/js/utils/ui.js +++ b/assets/js/utils/ui.js @@ -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, }; })();