From fbcd35f1977222b45ceb7e01c7b4ce5025de2645 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Fri, 3 Nov 2023 18:25:23 +0100 Subject: [PATCH] Add blocked period CRUD operations to the app (#432) --- application/config/constants.php | 1 + application/controllers/Blocked_periods.php | 235 ++++++++++++ application/core/EA_Controller.php | 1 + .../language/arabic/translations_lang.php | 5 + .../language/bulgarian/translations_lang.php | 5 + .../language/catalan/translations_lang.php | 5 + .../language/chinese/translations_lang.php | 5 + .../language/croatian/translations_lang.php | 5 + .../language/czech/translations_lang.php | 5 + .../language/danish/translations_lang.php | 5 + .../language/dutch/translations_lang.php | 5 + .../language/english/translations_lang.php | 5 + .../language/estonian/translations_lang.php | 5 + .../language/finnish/translations_lang.php | 5 + .../language/french/translations_lang.php | 5 + .../language/german/translations_lang.php | 5 + .../language/greek/translations_lang.php | 5 + .../language/hebrew/translations_lang.php | 5 + .../language/hindi/translations_lang.php | 5 + .../language/hungarian/translations_lang.php | 5 + .../language/italian/translations_lang.php | 5 + .../language/japanese/translations_lang.php | 5 + .../luxembourgish/translations_lang.php | 5 + .../language/marathi/translations_lang.php | 5 + .../language/persian/translations_lang.php | 5 + .../language/polish/translations_lang.php | 5 + .../portuguese-br/translations_lang.php | 5 + .../language/portuguese/translations_lang.php | 5 + .../language/romanian/translations_lang.php | 5 + .../language/russian/translations_lang.php | 5 + .../language/serbian/translations_lang.php | 5 + .../language/slovak/translations_lang.php | 5 + .../language/spanish/translations_lang.php | 5 + .../language/swedish/translations_lang.php | 5 + .../language/turkish/translations_lang.php | 5 + ...delete_datetime_column_from_all_tables.php | 2 +- ...ervice_categories_table_to_categories.php} | 0 ...e_categories_column_of_services_table.php} | 0 .../048_create_blocked_periods_table.php | 71 ++++ ..._blocked_periods_column_to_roles_table.php | 48 +++ application/models/Blocked_periods_model.php | 357 ++++++++++++++++++ application/views/pages/blocked_periods.php | 110 ++++++ assets/js/http/blocked_periods_http_client.js | 133 +++++++ assets/js/pages/blocked_periods.js | 337 +++++++++++++++++ 44 files changed, 1454 insertions(+), 1 deletion(-) create mode 100644 application/controllers/Blocked_periods.php rename application/migrations/{045_revert_rename_service_categories_table_to_categories.php => 046_revert_rename_service_categories_table_to_categories.php} (100%) rename application/migrations/{046_revert_rename_id_service_categories_column_of_services_table.php => 047_revert_rename_id_service_categories_column_of_services_table.php} (100%) create mode 100644 application/migrations/048_create_blocked_periods_table.php create mode 100644 application/migrations/049_add_blocked_periods_column_to_roles_table.php create mode 100644 application/models/Blocked_periods_model.php create mode 100644 application/views/pages/blocked_periods.php create mode 100644 assets/js/http/blocked_periods_http_client.js create mode 100644 assets/js/pages/blocked_periods.js diff --git a/application/config/constants.php b/application/config/constants.php index 1e30f6d081..29ff6b99ef 100644 --- a/application/config/constants.php +++ b/application/config/constants.php @@ -72,6 +72,7 @@ const PRIV_SYSTEM_SETTINGS = 'system_settings'; const PRIV_USER_SETTINGS = 'user_settings'; const PRIV_WEBHOOKS = 'webhooks'; +const PRIV_BLOCKED_PERIODS = 'blocked_periods'; const DATE_FORMAT_DMY = 'DMY'; const DATE_FORMAT_MDY = 'MDY'; diff --git a/application/controllers/Blocked_periods.php b/application/controllers/Blocked_periods.php new file mode 100644 index 0000000000..44092a04c9 --- /dev/null +++ b/application/controllers/Blocked_periods.php @@ -0,0 +1,235 @@ + + * @copyright Copyright (c) Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +/** + * Blocked_periods controller. + * + * Handles the blocked-periods related operations. + * + * @package Controllers + */ +class Blocked_periods extends EA_Controller { + /** + * Blocked_periods constructor. + */ + public function __construct() + { + parent::__construct(); + + $this->load->model('blocked_periods_model'); + $this->load->model('roles_model'); + + $this->load->library('accounts'); + $this->load->library('timezones'); + $this->load->library('webhooks_client'); + } + + /** + * Render the backend blocked-periods page. + * + * On this page admin users will be able to manage blocked-periods, which are eventually selected by customers during the + * booking process. + */ + public function index() + { + session(['dest_url' => site_url('blocked_periods')]); + + $user_id = session('user_id'); + + if (cannot('view', PRIV_BLOCKED_PERIODS)) + { + if ($user_id) + { + abort(403, 'Forbidden'); + } + + redirect('login'); + + return; + } + + $role_slug = session('role_slug'); + + script_vars([ + 'user_id' => $user_id, + 'role_slug' => $role_slug, + ]); + + html_vars([ + 'page_title' => lang('blocked_periods'), + 'active_menu' => PRIV_BLOCKED_PERIODS, + 'user_display_name' => $this->accounts->get_user_display_name($user_id), + 'timezones' => $this->timezones->to_array(), + 'privileges' => $this->roles_model->get_permissions_by_slug($role_slug), + ]); + + $this->load->view('pages/blocked_periods'); + } + + /** + * Filter blocked-periods by the provided keyword. + */ + public function search() + { + try + { + if (cannot('view', PRIV_BLOCKED_PERIODS)) + { + abort(403, 'Forbidden'); + } + + $keyword = request('keyword', ''); + + $order_by = 'update_datetime DESC'; + + $limit = request('limit', 1000); + + $offset = 0; + + $blocked_periods = $this->blocked_periods_model->search($keyword, $limit, $offset, $order_by); + + json_response($blocked_periods); + } + catch (Throwable $e) + { + json_exception($e); + } + } + + /** + * Store a new service-category. + */ + public function store() + { + try + { + if (cannot('add', PRIV_BLOCKED_PERIODS)) + { + abort(403, 'Forbidden'); + } + + $service_category = request('service_category'); + + $this->blocked_periods_model->only($service_category, [ + 'name', + 'description' + ]); + + $service_category_id = $this->blocked_periods_model->save($service_category); + + $service_category = $this->blocked_periods_model->find($service_category_id); + + $this->webhooks_client->trigger(WEBHOOK_SERVICE_CATEGORY_SAVE, $service_category); + + json_response([ + 'success' => TRUE, + 'id' => $service_category_id + ]); + } + catch (Throwable $e) + { + json_exception($e); + } + } + + /** + * Update a service-category. + */ + public function update() + { + try + { + if (cannot('edit', PRIV_BLOCKED_PERIODS)) + { + abort(403, 'Forbidden'); + } + + $service_category = request('service_category'); + + $this->blocked_periods_model->only($service_category, [ + 'id', + 'name', + 'description' + ]); + + $service_category_id = $this->blocked_periods_model->save($service_category); + + $service_category = $this->blocked_periods_model->find($service_category_id); + + $this->webhooks_client->trigger(WEBHOOK_SERVICE_CATEGORY_SAVE, $service_category); + + json_response([ + 'success' => TRUE, + 'id' => $service_category_id + ]); + } + catch (Throwable $e) + { + json_exception($e); + } + } + + /** + * Remove a service-category. + */ + public function destroy() + { + try + { + if (cannot('delete', PRIV_BLOCKED_PERIODS)) + { + abort(403, 'Forbidden'); + } + + $service_category_id = request('service_category_id'); + + $service_category = $this->blocked_periods_model->find($service_category_id); + + $this->blocked_periods_model->delete($service_category_id); + + $this->webhooks_client->trigger(WEBHOOK_SERVICE_CATEGORY_DELETE, $service_category); + + json_response([ + 'success' => TRUE, + ]); + } + catch (Throwable $e) + { + json_exception($e); + } + } + + /** + * Find a service-category. + */ + public function find() + { + try + { + if (cannot('view', PRIV_BLOCKED_PERIODS)) + { + abort(403, 'Forbidden'); + } + + $service_category_id = request('service_category_id'); + + $service_category = $this->blocked_periods_model->find($service_category_id); + + json_response($service_category); + } + catch (Throwable $e) + { + json_exception($e); + } + } +} diff --git a/application/core/EA_Controller.php b/application/core/EA_Controller.php index 59cdf8d7bd..a7a568a63c 100644 --- a/application/core/EA_Controller.php +++ b/application/core/EA_Controller.php @@ -52,6 +52,7 @@ * @property Unavailabilities_model $unavailabilities_model * @property Users_model $users_model * @property Webhooks_model $webhooks_model + * @property Blocked_periods_model $blocked_periods_model * * @property Accounts $accounts * @property Api $api diff --git a/application/language/arabic/translations_lang.php b/application/language/arabic/translations_lang.php index 6980da69d5..85da6d7773 100755 --- a/application/language/arabic/translations_lang.php +++ b/application/language/arabic/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/bulgarian/translations_lang.php b/application/language/bulgarian/translations_lang.php index 7504c73b46..9af09eb08d 100755 --- a/application/language/bulgarian/translations_lang.php +++ b/application/language/bulgarian/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/catalan/translations_lang.php b/application/language/catalan/translations_lang.php index 9ad5a04fed..7114a37cb5 100644 --- a/application/language/catalan/translations_lang.php +++ b/application/language/catalan/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/chinese/translations_lang.php b/application/language/chinese/translations_lang.php index 880459f90c..fb23ec6c3a 100755 --- a/application/language/chinese/translations_lang.php +++ b/application/language/chinese/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/croatian/translations_lang.php b/application/language/croatian/translations_lang.php index baba51bd65..831ad3230c 100644 --- a/application/language/croatian/translations_lang.php +++ b/application/language/croatian/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/czech/translations_lang.php b/application/language/czech/translations_lang.php index e1ec07bbe9..7a10a28cf2 100644 --- a/application/language/czech/translations_lang.php +++ b/application/language/czech/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/danish/translations_lang.php b/application/language/danish/translations_lang.php index 079effbb1f..9d42f08c46 100755 --- a/application/language/danish/translations_lang.php +++ b/application/language/danish/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/dutch/translations_lang.php b/application/language/dutch/translations_lang.php index 5b6d094ff3..890196bf50 100755 --- a/application/language/dutch/translations_lang.php +++ b/application/language/dutch/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/english/translations_lang.php b/application/language/english/translations_lang.php index 680f6ecdde..c793ad7545 100755 --- a/application/language/english/translations_lang.php +++ b/application/language/english/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/estonian/translations_lang.php b/application/language/estonian/translations_lang.php index fae4cd1846..5349a188c5 100644 --- a/application/language/estonian/translations_lang.php +++ b/application/language/estonian/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/finnish/translations_lang.php b/application/language/finnish/translations_lang.php index 6c164a7b3b..877e14d50a 100755 --- a/application/language/finnish/translations_lang.php +++ b/application/language/finnish/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/french/translations_lang.php b/application/language/french/translations_lang.php index 0c3663ac04..1086370939 100755 --- a/application/language/french/translations_lang.php +++ b/application/language/french/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/german/translations_lang.php b/application/language/german/translations_lang.php index f1c57c3675..2e8e8bd08a 100755 --- a/application/language/german/translations_lang.php +++ b/application/language/german/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/greek/translations_lang.php b/application/language/greek/translations_lang.php index 21bc1ba565..eba704aaf0 100755 --- a/application/language/greek/translations_lang.php +++ b/application/language/greek/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/hebrew/translations_lang.php b/application/language/hebrew/translations_lang.php index dc0b90fa27..8e6768038a 100644 --- a/application/language/hebrew/translations_lang.php +++ b/application/language/hebrew/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/hindi/translations_lang.php b/application/language/hindi/translations_lang.php index a6b648664c..5ee9a83ac9 100755 --- a/application/language/hindi/translations_lang.php +++ b/application/language/hindi/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/hungarian/translations_lang.php b/application/language/hungarian/translations_lang.php index c0f5a5efe5..a86af978d5 100755 --- a/application/language/hungarian/translations_lang.php +++ b/application/language/hungarian/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/italian/translations_lang.php b/application/language/italian/translations_lang.php index e47ca12155..c681f35fc1 100755 --- a/application/language/italian/translations_lang.php +++ b/application/language/italian/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/japanese/translations_lang.php b/application/language/japanese/translations_lang.php index c68ef8a11a..f58b0ee7a3 100755 --- a/application/language/japanese/translations_lang.php +++ b/application/language/japanese/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/luxembourgish/translations_lang.php b/application/language/luxembourgish/translations_lang.php index 999bc2acc8..348fedcab5 100755 --- a/application/language/luxembourgish/translations_lang.php +++ b/application/language/luxembourgish/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/marathi/translations_lang.php b/application/language/marathi/translations_lang.php index e5640a7235..53da1c042b 100644 --- a/application/language/marathi/translations_lang.php +++ b/application/language/marathi/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/persian/translations_lang.php b/application/language/persian/translations_lang.php index ed189cf6b8..d22a3035c9 100644 --- a/application/language/persian/translations_lang.php +++ b/application/language/persian/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/polish/translations_lang.php b/application/language/polish/translations_lang.php index cc855ec0f1..4a358231b2 100755 --- a/application/language/polish/translations_lang.php +++ b/application/language/polish/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/portuguese-br/translations_lang.php b/application/language/portuguese-br/translations_lang.php index 0a3db192ea..613a54d62d 100755 --- a/application/language/portuguese-br/translations_lang.php +++ b/application/language/portuguese-br/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/portuguese/translations_lang.php b/application/language/portuguese/translations_lang.php index 8383d12923..4a62a0dd57 100755 --- a/application/language/portuguese/translations_lang.php +++ b/application/language/portuguese/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/romanian/translations_lang.php b/application/language/romanian/translations_lang.php index d06146546a..633c77e217 100755 --- a/application/language/romanian/translations_lang.php +++ b/application/language/romanian/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/russian/translations_lang.php b/application/language/russian/translations_lang.php index 0f5ea688ea..0a48556a86 100644 --- a/application/language/russian/translations_lang.php +++ b/application/language/russian/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/serbian/translations_lang.php b/application/language/serbian/translations_lang.php index 78a93f24a8..26aee29867 100644 --- a/application/language/serbian/translations_lang.php +++ b/application/language/serbian/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/slovak/translations_lang.php b/application/language/slovak/translations_lang.php index 546220ad13..732026106e 100755 --- a/application/language/slovak/translations_lang.php +++ b/application/language/slovak/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/spanish/translations_lang.php b/application/language/spanish/translations_lang.php index 9337da29a8..bdcd1d0c70 100755 --- a/application/language/spanish/translations_lang.php +++ b/application/language/spanish/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/swedish/translations_lang.php b/application/language/swedish/translations_lang.php index 7cca2a1731..a5796c3d95 100644 --- a/application/language/swedish/translations_lang.php +++ b/application/language/swedish/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/language/turkish/translations_lang.php b/application/language/turkish/translations_lang.php index 6bf0fd6d71..73dd8de95a 100755 --- a/application/language/turkish/translations_lang.php +++ b/application/language/turkish/translations_lang.php @@ -438,4 +438,9 @@ $lang['no_breaks'] = 'No Breaks'; $lang['service_categories'] = 'Service Categories'; $lang['service_category'] = 'Service Category'; +$lang['blocked_period_saved'] = 'Blocked period saved successfully.'; +$lang['blocked_period_deleted'] = 'Blocked period deleted successfully.'; +$lang['delete_blocked_period'] = 'Delete Blocked Period'; +$lang['blocked_period'] = 'Blocked Period'; +$lang['blocked_periods'] = 'Blocked Periods'; // End diff --git a/application/migrations/045_drop_delete_datetime_column_from_all_tables.php b/application/migrations/045_drop_delete_datetime_column_from_all_tables.php index c6b407b503..d3eb39f0dd 100644 --- a/application/migrations/045_drop_delete_datetime_column_from_all_tables.php +++ b/application/migrations/045_drop_delete_datetime_column_from_all_tables.php @@ -17,7 +17,7 @@ class Migration_Drop_delete_datetime_column_from_all_tables extends EA_Migration */ protected $tables = [ 'appointments', - 'categories', + 'service_categories', 'consents', 'roles', 'services', diff --git a/application/migrations/045_revert_rename_service_categories_table_to_categories.php b/application/migrations/046_revert_rename_service_categories_table_to_categories.php similarity index 100% rename from application/migrations/045_revert_rename_service_categories_table_to_categories.php rename to application/migrations/046_revert_rename_service_categories_table_to_categories.php diff --git a/application/migrations/046_revert_rename_id_service_categories_column_of_services_table.php b/application/migrations/047_revert_rename_id_service_categories_column_of_services_table.php similarity index 100% rename from application/migrations/046_revert_rename_id_service_categories_column_of_services_table.php rename to application/migrations/047_revert_rename_id_service_categories_column_of_services_table.php diff --git a/application/migrations/048_create_blocked_periods_table.php b/application/migrations/048_create_blocked_periods_table.php new file mode 100644 index 0000000000..1c62d733ef --- /dev/null +++ b/application/migrations/048_create_blocked_periods_table.php @@ -0,0 +1,71 @@ + + * @copyright Copyright (c) Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.4.0 + * ---------------------------------------------------------------------------- */ + +class Migration_Create_blocked_periods_table extends EA_Migration { + /** + * Upgrade method. + */ + public function up() + { + if ( ! $this->db->table_exists('blocked_periods')) + { + $this->dbforge->add_field([ + 'id' => [ + 'type' => 'INT', + 'constraint' => 11, + 'auto_increment' => TRUE + ], + 'create_datetime' => [ + 'type' => 'DATETIME', + 'null' => TRUE + ], + 'update_datetime' => [ + 'type' => 'DATETIME', + 'null' => TRUE + ], + 'name' => [ + 'type' => 'VARCHAR', + 'constraint' => '256', + 'null' => TRUE, + ], + 'start_datetime' => [ + 'type' => 'DATETIME', + 'null' => TRUE, + ], + 'end_datetime' => [ + 'type' => 'DATETIME', + 'null' => TRUE, + ], + 'description' => [ + 'type' => 'TEXT', + 'null' => TRUE, + ], + ]); + + $this->dbforge->add_key('id', TRUE); + + $this->dbforge->create_table('blocked_periods', TRUE, ['engine' => 'InnoDB']); + } + } + + /** + * Downgrade method. + */ + public function down() + { + if ($this->db->table_exists('blocked_periods')) + { + $this->dbforge->drop_table('blocked_periods'); + } + } +} diff --git a/application/migrations/049_add_blocked_periods_column_to_roles_table.php b/application/migrations/049_add_blocked_periods_column_to_roles_table.php new file mode 100644 index 0000000000..62662df40b --- /dev/null +++ b/application/migrations/049_add_blocked_periods_column_to_roles_table.php @@ -0,0 +1,48 @@ + + * @copyright Copyright (c) Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.4.0 + * ---------------------------------------------------------------------------- */ + +class Migration_Add_blocked_periods_column_to_roles_table extends EA_Migration { + /** + * Upgrade method. + */ + public function up() + { + if ( ! $this->db->field_exists('blocked_periods', 'roles')) + { + $fields = [ + 'blocked_periods' => [ + 'type' => 'INT', + 'constraint' => '11', + 'null' => TRUE + ] + ]; + + $this->dbforge->add_column('roles', $fields); + + $this->db->update('roles', ['blocked_periods' => '15'], ['slug' => 'admin']); + + $this->db->update('roles', ['blocked_periods' => '0'], ['slug !=' => 'admin']); + } + } + + /** + * Downgrade method. + */ + public function down() + { + if ($this->db->field_exists('blocked_periods', 'roles')) + { + $this->dbforge->drop_column('roles', 'blocked_periods'); + } + } +} diff --git a/application/models/Blocked_periods_model.php b/application/models/Blocked_periods_model.php new file mode 100644 index 0000000000..73089aad35 --- /dev/null +++ b/application/models/Blocked_periods_model.php @@ -0,0 +1,357 @@ + + * @copyright Copyright (c) Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +/** + * Blocked-Periods model. + * + * Handles all the database operations of the blocked-period resource. + * + * @package Models + */ +class Blocked_periods_model extends EA_Model { + /** + * @var array + */ + protected array $casts = [ + 'id' => 'integer', + ]; + + /** + * @var array + */ + protected array $api_resource = [ + 'id' => 'id', + 'name' => 'name', + 'start' => 'start_datetime', + 'end' => 'end_datetime', + 'description' => 'description', + ]; + + /** + * Save (insert or update) a blocked-period. + * + * @param array $blocked_period Associative array with the blocked-period data. + * + * @return int Returns the blocked-period ID. + * + * @throws InvalidArgumentException + */ + public function save(array $blocked_period): int + { + $this->validate($blocked_period); + + if (empty($blocked_period['id'])) + { + return $this->insert($blocked_period); + } + else + { + return $this->update($blocked_period); + } + } + + /** + * Validate the blocked-period data. + * + * @param array $blocked_period Associative array with the blocked-period data. + * + * @throws InvalidArgumentException + */ + public function validate(array $blocked_period) + { + // If a blocked-period ID is provided then check whether the record really exists in the database. + if ( ! empty($blocked_period['id'])) + { + $count = $this->db->get_where('blocked_periods', ['id' => $blocked_period['id']])->num_rows(); + + if ( ! $count) + { + throw new InvalidArgumentException('The provided blocked-period ID does not exist in the database: ' . $blocked_period['id']); + } + } + + // Make sure all required fields are provided. + if ( + empty($blocked_period['name']) + ) + { + throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($blocked_period, TRUE)); + } + } + + /** + * Insert a new blocked-period into the database. + * + * @param array $blocked_period Associative array with the blocked-period data. + * + * @return int Returns the blocked-period ID. + * + * @throws RuntimeException + */ + protected function insert(array $blocked_period): int + { + $blocked_period['create_datetime'] = date('Y-m-d H:i:s'); + $blocked_period['update_datetime'] = date('Y-m-d H:i:s'); + + if ( ! $this->db->insert('blocked_periods', $blocked_period)) + { + throw new RuntimeException('Could not insert blocked-period.'); + } + + return $this->db->insert_id(); + } + + /** + * Update an existing blocked-period. + * + * @param array $blocked_period Associative array with the blocked-period data. + * + * @return int Returns the blocked-period ID. + * + * @throws RuntimeException + */ + protected function update(array $blocked_period): int + { + $blocked_period['update_datetime'] = date('Y-m-d H:i:s'); + + if ( ! $this->db->update('blocked_periods', $blocked_period, ['id' => $blocked_period['id']])) + { + throw new RuntimeException('Could not update blocked periods.'); + } + + return $blocked_period['id']; + } + + /** + * Remove an existing blocked-period from the database. + * + * @param int $blocked_period_id Blocked period ID. + * + * @throws RuntimeException + */ + public function delete(int $blocked_period_id): void + { + $this->db->delete('blocked_periods', ['id' => $blocked_period_id]); + } + + /** + * Get a specific blocked-period from the database. + * + * @param int $blocked_period_id The ID of the record to be returned. + * + * @return array Returns an array with the blocked-period data. + * + * @throws InvalidArgumentException + */ + public function find(int $blocked_period_id): array + { + $blocked_period = $this->db->get_where('blocked_periods', ['id' => $blocked_period_id])->row_array(); + + if ( ! $blocked_period) + { + throw new InvalidArgumentException('The provided blocked-period ID was not found in the database: ' . $blocked_period_id); + } + + $this->cast($blocked_period); + + return $blocked_period; + } + + /** + * Get a specific field value from the database. + * + * @param int $blocked_period_id Blocked period ID. + * @param string $field Name of the value to be returned. + * + * @return mixed Returns the selected blocked-period value from the database. + * + * @throws InvalidArgumentException + */ + public function value(int $blocked_period_id, string $field): mixed + { + if (empty($field)) + { + throw new InvalidArgumentException('The field argument is cannot be empty.'); + } + + if (empty($blocked_period_id)) + { + throw new InvalidArgumentException('The blocked-period ID argument cannot be empty.'); + } + + // Check whether the service exists. + $query = $this->db->get_where('blocked_periods', ['id' => $blocked_period_id]); + + if ( ! $query->num_rows()) + { + throw new InvalidArgumentException('The provided blocked-period ID was not found in the database: ' . $blocked_period_id); + } + + // Check if the required field is part of the blocked-period data. + $blocked_period = $query->row_array(); + + $this->cast($blocked_period); + + if ( ! array_key_exists($field, $blocked_period)) + { + throw new InvalidArgumentException('The requested field was not found in the blocked-period data: ' . $field); + } + + return $blocked_period[$field]; + } + + /** + * Get all services that match the provided criteria. + * + * @param array|string|null $where Where conditions + * @param int|null $limit Record limit. + * @param int|null $offset Record offset. + * @param string|null $order_by Order by. + * + * @return array Returns an array of blocked periods. + */ + public function get(array|string $where = NULL, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array + { + if ($where !== NULL) + { + $this->db->where($where); + } + + if ($order_by !== NULL) + { + $this->db->order_by($order_by); + } + + $blocked_periods = $this->db->get('blocked_periods', $limit, $offset)->result_array(); + + foreach ($blocked_periods as &$blocked_period) + { + $this->cast($blocked_period); + } + + return $blocked_periods; + } + + /** + * Get the query builder interface, configured for use with the blocked periods table. + * + * @return CI_DB_query_builder + */ + public function query(): CI_DB_query_builder + { + return $this->db->from('blocked_periods'); + } + + /** + * Search blocked periods by the provided keyword. + * + * @param string $keyword Search keyword. + * @param int|null $limit Record limit. + * @param int|null $offset Record offset. + * @param string|null $order_by Order by. + * + * @return array Returns an array of blocked periods. + */ + public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array + { + $blocked_periods = $this + ->db + ->select() + ->from('blocked_periods') + ->group_start() + ->like('name', $keyword) + ->or_like('description', $keyword) + ->group_end() + ->limit($limit) + ->offset($offset) + ->order_by($order_by) + ->get() + ->result_array(); + + foreach ($blocked_periods as &$blocked_period) + { + $this->cast($blocked_period); + } + + return $blocked_periods; + } + + /** + * Load related resources to a blocked-period. + * + * @param array $blocked_period Associative array with the blocked-period data. + * @param array $resources Resource names to be attached. + * + * @throws InvalidArgumentException + */ + public function load(array &$blocked_period, array $resources) + { + // Blocked periods do not currently have any related resources. + } + + /** + * Convert the database blocked-period record to the equivalent API resource. + * + * @param array $blocked_period Blocked period data. + */ + public function api_encode(array &$blocked_period) + { + $encoded_resource = [ + 'id' => array_key_exists('id', $blocked_period) ? (int)$blocked_period['id'] : NULL, + 'name' => $blocked_period['name'], + 'start' => array_key_exists('start_datetime', $blocked_period) ? $blocked_period['start_datetime'] : NULL, + 'end' => array_key_exists('end_datetime', $blocked_period) ? $blocked_period['end_datetime'] : NULL, + 'description' => array_key_exists('description', $blocked_period) ? $blocked_period['description'] : NULL + ]; + + $blocked_period = $encoded_resource; + } + + /** + * Convert the API resource to the equivalent database blocked-period record. + * + * @param array $blocked_period API resource. + * @param array|null $base Base blocked-period data to be overwritten with the provided values (useful for updates). + */ + public function api_decode(array &$blocked_period, array $base = NULL) + { + $decoded_resource = $base ?: []; + + if (array_key_exists('id', $blocked_period)) + { + $decoded_resource['id'] = $blocked_period['id']; + } + + if (array_key_exists('name', $blocked_period)) + { + $decoded_resource['name'] = $blocked_period['name']; + } + + if (array_key_exists('start', $blocked_period)) + { + $decoded_resource['start_datetime'] = $blocked_period['start']; + } + + if (array_key_exists('end', $blocked_period)) + { + $decoded_resource['end_datetime'] = $blocked_period['end']; + } + + if (array_key_exists('description', $blocked_period)) + { + $decoded_resource['description'] = $blocked_period['description']; + } + + $blocked_period = $decoded_resource; + } +} diff --git a/application/views/pages/blocked_periods.php b/application/views/pages/blocked_periods.php new file mode 100644 index 0000000000..83162c96ab --- /dev/null +++ b/application/views/pages/blocked_periods.php @@ -0,0 +1,110 @@ + + + + +
+ +
+
+
+
+ + + +
+
+ +

+ +

+ +
+ +
+
+ +
+
+
+ + + +
+ + +
+ +

+ +

+ + + + + +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+
+
+ +
+ + + + + + + + + + + + diff --git a/assets/js/http/blocked_periods_http_client.js b/assets/js/http/blocked_periods_http_client.js new file mode 100644 index 0000000000..48421c0217 --- /dev/null +++ b/assets/js/http/blocked_periods_http_client.js @@ -0,0 +1,133 @@ +/* ---------------------------------------------------------------------------- + * Easy!Appointments - Online Appointment Scheduler + * + * @package EasyAppointments + * @author A.Tselegidis + * @copyright Copyright (c) Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.5.0 + * ---------------------------------------------------------------------------- */ + +/** + * Blocked-periods HTTP client. + * + * This module implements the blocked-periods related HTTP requests. + */ +App.Http.BlockedPeriods = (function () { + /** + * Save (create or update) a blocked-period. + * + * @param {Object} blockedPeriod + * + * @return {Object} + */ + function save(blockedPeriod) { + return blockedPeriod.id ? update(blockedPeriod) : store(blockedPeriod); + } + + /** + * Create a blocked-period. + * + * @param {Object} blockedPeriod + * + * @return {Object} + */ + function store(blockedPeriod) { + const url = App.Utils.Url.siteUrl('blocked_periods/store'); + + const data = { + csrf_token: vars('csrf_token'), + blocked_period: blockedPeriod + }; + + return $.post(url, data); + } + + /** + * Update a blocked-period. + * + * @param {Object} blockedPeriod + * + * @return {Object} + */ + function update(blockedPeriod) { + const url = App.Utils.Url.siteUrl('blocked_periods/update'); + + const data = { + csrf_token: vars('csrf_token'), + blocked_period: blockedPeriod + }; + + return $.post(url, data); + } + + /** + * Delete a blocked-period. + * + * @param {Number} blockedPeriodId + * + * @return {Object} + */ + function destroy(blockedPeriodId) { + const url = App.Utils.Url.siteUrl('blocked_periods/destroy'); + + const data = { + csrf_token: vars('csrf_token'), + blocked_period_id: blockedPeriodId + }; + + return $.post(url, data); + } + + /** + * Search blocked-periods by keyword. + * + * @param {String} keyword + * @param {Number} [limit] + * @param {Number} [offset] + * @param {String} [orderBy] + * + * @return {Object} + */ + function search(keyword, limit = null, offset = null, orderBy = null) { + const url = App.Utils.Url.siteUrl('blocked_periods/search'); + + const data = { + csrf_token: vars('csrf_token'), + keyword, + limit, + offset, + order_by: orderBy + }; + + return $.post(url, data); + } + + /** + * Find a blocked-period. + * + * @param {Number} blockedPeriodId + * + * @return {Object} + */ + function find(blockedPeriodId) { + const url = App.Utils.Url.siteUrl('blocked_periods/find'); + + const data = { + csrf_token: vars('csrf_token'), + blocked_period_id: blockedPeriodId + }; + + return $.post(url, data); + } + + return { + save, + store, + update, + destroy, + search, + find + }; +})(); diff --git a/assets/js/pages/blocked_periods.js b/assets/js/pages/blocked_periods.js new file mode 100644 index 0000000000..693fdc3117 --- /dev/null +++ b/assets/js/pages/blocked_periods.js @@ -0,0 +1,337 @@ +/* ---------------------------------------------------------------------------- + * Easy!Appointments - Online Appointment Scheduler + * + * @package EasyAppointments + * @author A.Tselegidis + * @copyright Copyright (c) Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.5.0 + * ---------------------------------------------------------------------------- */ + +/** + * Blocked-periods page. + * + * This module implements the functionality of the blocked-periods page. + */ +App.Pages.BlockedPeriods = (function () { + const $blockedPeriods = $('#blocked-periods'); + const $filterBlockedPeriods = $('#filter-blocked-periods'); + const $id = $('#id'); + const $name = $('#name'); + const $description = $('#description'); + let filterResults = {}; + let filterLimit = 20; + + /** + * Add the page event listeners. + */ + function addEventListeners() { + /** + * Event: Filter Blocked Periods Form "Submit" + * + * @param {jQuery.Event} event + */ + $blockedPeriods.on('submit', '#filter-blocked-periods form', (event) => { + event.preventDefault(); + const key = $('#filter-blocked-periods .key').val(); + $('.selected').removeClass('selected'); + resetForm(); + filter(key); + }); + + /** + * Event: Filter Blocked-Periods Row "Click" + * + * Displays the selected row data on the right side of the page. + * + * @param {jQuery.Event} event + */ + $blockedPeriods.on('click', '.blocked-period-row', (event) => { + if ($('#filter-blocked-periods .filter').prop('disabled')) { + $('#filter-blocked-periods .results').css('color', '#AAA'); + return; // exit because we are on edit mode + } + + const blockedPeriodId = $(event.currentTarget).attr('data-id'); + + const blockedPeriod = filterResults.find((filterResult) => Number(filterResult.id) === Number(blockedPeriodId)); + + display(blockedPeriod); + $('#filter-blocked-periods .selected').removeClass('selected'); + $(event.currentTarget).addClass('selected'); + $('#edit-blocked-period, #delete-blocked-period').prop('disabled', false); + }); + + /** + * Event: Add Blocked-Period Button "Click" + */ + $blockedPeriods.on('click', '#add-blocked-period', () => { + resetForm(); + $blockedPeriods.find('.add-edit-delete-group').hide(); + $blockedPeriods.find('.save-cancel-group').show(); + $blockedPeriods.find('.record-details').find('input, select, textarea').prop('disabled', false); + $blockedPeriods.find('.record-details .form-label span').prop('hidden', false); + $filterBlockedPeriods.find('button').prop('disabled', true); + $filterBlockedPeriods.find('.results').css('color', '#AAA'); + }); + + /** + * Event: Edit Blocked-Period Button "Click" + */ + $blockedPeriods.on('click', '#edit-blocked-period', () => { + $blockedPeriods.find('.add-edit-delete-group').hide(); + $blockedPeriods.find('.save-cancel-group').show(); + $blockedPeriods.find('.record-details').find('input, select, textarea').prop('disabled', false); + $blockedPeriods.find('.record-details .form-label span').prop('hidden', false); + $filterBlockedPeriods.find('button').prop('disabled', true); + $filterBlockedPeriods.find('.results').css('color', '#AAA'); + }); + + /** + * Event: Delete Blocked-Period Button "Click" + */ + $blockedPeriods.on('click', '#delete-blocked-period', () => { + const blockedPeriodId = $id.val(); + + const buttons = [ + { + text: lang('cancel'), + click: (event, messageModal) => { + messageModal.dispose(); + } + }, + { + text: lang('delete'), + click: (event, messageModal) => { + remove(blockedPeriodId); + messageModal.dispose(); + } + } + ]; + + App.Utils.Message.show(lang('delete_blocked_period'), lang('delete_record_prompt'), buttons); + }); + + /** + * Event: Blocked period Save Button "Click" + */ + $blockedPeriods.on('click', '#save-blocked-period', () => { + const blockedPeriod = { + name: $name.val(), + description: $description.val() + }; + + if ($id.val() !== '') { + blockedPeriod.id = $id.val(); + } + + if (!validate()) { + return; + } + + save(blockedPeriod); + }); + + /** + * Event: Cancel Blocked-Period Button "Click" + */ + $blockedPeriods.on('click', '#cancel-blocked-period', () => { + const id = $id.val(); + resetForm(); + if (id !== '') { + select(id, true); + } + }); + } + + /** + * 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 + * selected (but not displayed). + * @param {Boolean} show Optional (false), if true then the selected record will be displayed on the form. + */ + function filter(keyword, selectId = null, show = false) { + App.Http.BlockedPeriods.search(keyword, filterLimit).then((response) => { + filterResults = response; + + $('#filter-blocked-periods .results').empty(); + + response.forEach((blockedPeriod) => { + $('#filter-blocked-periods .results').append(getFilterHtml(blockedPeriod)).append($('
')); + }); + + if (response.length === 0) { + $('#filter-blocked-periods .results').append( + $('', { + 'text': lang('no_records_found') + }) + ); + } else if (response.length === filterLimit) { + $('