From aadac6a06080ee4919e2c94237173d10ce64def5 Mon Sep 17 00:00:00 2001 From: alextselegidis Date: Thu, 18 Nov 2021 05:23:54 +0100 Subject: [PATCH] Created a new service categories resource controller --- .../controllers/Service_categories.php | 173 +++++++++++ .../service_categories_page.php | 279 ++++++++++++++++++ assets/js/backend_categories_helper.js | 20 +- 3 files changed, 462 insertions(+), 10 deletions(-) create mode 100644 application/controllers/Service_categories.php create mode 100755 application/views/pages/service_categories/service_categories_page.php diff --git a/application/controllers/Service_categories.php b/application/controllers/Service_categories.php new file mode 100644 index 00000000..0bcfd7bc --- /dev/null +++ b/application/controllers/Service_categories.php @@ -0,0 +1,173 @@ + + * @copyright Copyright (c) 2013 - 2020, Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +/** + * Service categories controller. + * + * Handles the service categories related operations. + * + * @package Controllers + */ +class Service_categories extends EA_Controller { + /** + * Service_categories constructor. + */ + public function __construct() + { + parent::__construct(); + + $this->load->model('service_categories_model'); + $this->load->model('roles_model'); + + $this->load->library('accounts'); + $this->load->library('timezones'); + } + + /** + * Render the backend services page. + * + * On this page admin users will be able to manage services, which are eventually selected by customers during the + * booking process. + */ + public function index() + { + session(['dest_url' => site_url('service_categories')]); + + if (cannot('view', 'services')) + { + show_error('Forbidden', 403); + } + + $user_id = session('user_id'); + + $role_slug = session('role_slug'); + + $this->load->view('pages/service_categories/service_categories_page', [ + 'page_title' => lang('services'), + 'active_menu' => PRIV_SERVICES, + '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), + ]); + } + + /** + * Filter services by the provided keyword. + */ + public function search() + { + try + { + if (cannot('view', 'services')) + { + show_error('Forbidden', 403); + } + + $keyword = request('keyword', ''); + + $order_by = 'name ASC'; + + $limit = request('limit', 1000); + + $offset = 0; + + $service_categories = $this->service_categories_model->search($keyword, $limit, $offset, $order_by); + + json_response($service_categories); + } + catch (Throwable $e) + { + json_exception($e); + } + } + + /** + * Create a service. + */ + public function create() + { + try + { + $service_category = json_decode(request('service_category'), TRUE); + + if (cannot('add', 'services')) + { + show_error('Forbidden', 403); + } + + $service_category_id = $this->service_categories_model->save($service_category); + + json_response([ + 'success' => TRUE, + 'id' => $service_category_id + ]); + } + catch (Throwable $e) + { + json_exception($e); + } + } + + /** + * Update a service. + */ + public function update() + { + try + { + $service_category = json_decode(request('service_category'), TRUE); + + if (cannot('edit', 'services')) + { + show_error('Forbidden', 403); + } + + $service_category_id = $this->service_categories_model->save($service_category); + + json_response([ + 'success' => TRUE, + 'id' => $service_category_id + ]); + } + catch (Throwable $e) + { + json_exception($e); + } + } + + /** + * Remove a service. + */ + public function destroy() + { + try + { + if (cannot('delete', 'services')) + { + show_error('Forbidden', 403); + } + + $service_category_id = request('service_category_id'); + + $this->service_categories_model->delete($service_category_id); + + json_response([ + 'success' => TRUE, + ]); + } + catch (Throwable $e) + { + json_exception($e); + } + } +} diff --git a/application/views/pages/service_categories/service_categories_page.php b/application/views/pages/service_categories/service_categories_page.php new file mode 100755 index 00000000..87026ff0 --- /dev/null +++ b/application/views/pages/service_categories/service_categories_page.php @@ -0,0 +1,279 @@ + + + + + + + + + + + +
+ + +
+ + + +
+
+
+
+
+ + +
+
+ + +
+
+
+
+ +

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

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

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

+ + + + + +
+ + +
+ +
+ + +
+
+
+
+
+
+ + diff --git a/assets/js/backend_categories_helper.js b/assets/js/backend_categories_helper.js index cefc95dc..6976ee06 100644 --- a/assets/js/backend_categories_helper.js +++ b/assets/js/backend_categories_helper.js @@ -174,17 +174,17 @@ /** * Filter service categories records. * - * @param {String} key This key string is used to filter the category records. + * @param {String} keyword This key string is used to filter the category 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} display Optional (false), if true then the selected record will be displayed on the form. */ - CategoriesHelper.prototype.filter = function (key, selectId, display) { - var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_service_categories'; + CategoriesHelper.prototype.filter = function (keyword, selectId, display) { + var url = GlobalVariables.baseUrl + '/index.php/service_categories/search'; var data = { csrfToken: GlobalVariables.csrfToken, - key: key, + keyword: keyword, limit: this.filterLimit }; @@ -213,7 +213,7 @@ 'text': EALang.load_more, 'click': function () { this.filterLimit += 20; - this.filter(key, selectId, display); + this.filter(keyword, selectId, display); }.bind(this) }).appendTo('#filter-categories .results'); } @@ -231,11 +231,11 @@ * @param {Object} category Contains the category data. */ CategoriesHelper.prototype.save = function (category) { - var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_service_category'; + var url = GlobalVariables.baseUrl + '/index.php/service_categories/' + (category.id ? 'update' : 'create'); var data = { csrfToken: GlobalVariables.csrfToken, - category: JSON.stringify(category) + service_category: JSON.stringify(category) }; $.post(url, data).done( @@ -252,14 +252,14 @@ /** * Delete category record. * - * @param Number} id Record ID to be deleted. + * @param {Number} id Record ID to be deleted. */ CategoriesHelper.prototype.delete = function (id) { - var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_service_category'; + var url = GlobalVariables.baseUrl + '/index.php/service_categories/destroy'; var data = { csrfToken: GlobalVariables.csrfToken, - category_id: id + service_category_id: id }; $.post(url, data).done(