From fec42278c80bd8de42242526c5b728eebf5f3174 Mon Sep 17 00:00:00 2001 From: alextselegidis Date: Thu, 18 Nov 2021 06:34:07 +0100 Subject: [PATCH] Created a new customers resource controller --- application/controllers/Customers.php | 189 ++++++++++++++++++++++++++ assets/js/backend_customers_helper.js | 14 +- 2 files changed, 196 insertions(+), 7 deletions(-) create mode 100644 application/controllers/Customers.php diff --git a/application/controllers/Customers.php b/application/controllers/Customers.php new file mode 100644 index 00000000..6e3f98ec --- /dev/null +++ b/application/controllers/Customers.php @@ -0,0 +1,189 @@ + + * @copyright Copyright (c) 2013 - 2020, Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +/** + * Customers controller. + * + * Handles the customers related operations. + * + * @package Controllers + */ +class Customers extends EA_Controller { + /** + * Customers constructor. + */ + public function __construct() + { + parent::__construct(); + + $this->load->model('appointments_model'); + $this->load->model('customers_model'); + $this->load->model('roles_model'); + + $this->load->library('accounts'); + $this->load->library('timezones'); + } + + /** + * Render the backend customers page. + * + * On this page admin users will be able to manage customers, which are eventually selected by customers during the + * booking process. + */ + public function index() + { + session(['dest_url' => site_url('customers')]); + + if (cannot('view', 'customers')) + { + show_error('Forbidden', 403); + } + + $user_id = session('user_id'); + + $role_slug = session('role_slug'); + + $this->load->view('pages/customers/customers_page', [ + 'page_title' => lang('customers'), + 'active_menu' => PRIV_CUSTOMERS, + '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 customers by the provided keyword. + */ + public function search() + { + try + { + if (cannot('view', 'customers')) + { + show_error('Forbidden', 403); + } + + $keyword = request('keyword', ''); + + $order_by = 'first_name ASC, last_name ASC, email ASC'; + + $limit = request('limit', 1000); + + $offset = 0; + + $customers = $this->customers_model->search($keyword, $limit, $offset, $order_by); + + foreach ($customers as &$customer) + { + $appointments = $this->appointments_model->get(['id_users_customer' => $customer['id']]); + + foreach ($appointments as &$appointment) + { + $this->appointments_model->load($appointment, [ + 'service', + 'provider', + ]); + } + + $customer['appointments'] = $appointments; + } + + json_response($customers); + } + catch (Throwable $e) + { + json_exception($e); + } + } + + /** + * Create a customer. + */ + public function create() + { + try + { + $customer = json_decode(request('customer'), TRUE); + + if (cannot('add', 'customers')) + { + show_error('Forbidden', 403); + } + + $customer_id = $this->customers_model->save($customer); + + json_response([ + 'success' => TRUE, + 'id' => $customer_id + ]); + } + catch (Throwable $e) + { + json_exception($e); + } + } + + /** + * Update a customer. + */ + public function update() + { + try + { + $customer = json_decode(request('customer'), TRUE); + + if (cannot('edit', 'customers')) + { + show_error('Forbidden', 403); + } + + $customer_id = $this->customers_model->save($customer); + + json_response([ + 'success' => TRUE, + 'id' => $customer_id + ]); + } + catch (Throwable $e) + { + json_exception($e); + } + } + + /** + * Remove a customer. + */ + public function destroy() + { + try + { + if (cannot('delete', 'customers')) + { + show_error('Forbidden', 403); + } + + $customer_id = request('customer_id'); + + $this->customers_model->delete($customer_id); + + json_response([ + 'success' => TRUE, + ]); + } + catch (Throwable $e) + { + json_exception($e); + } + } +} diff --git a/assets/js/backend_customers_helper.js b/assets/js/backend_customers_helper.js index c0c07288..f3d727dd 100644 --- a/assets/js/backend_customers_helper.js +++ b/assets/js/backend_customers_helper.js @@ -168,7 +168,7 @@ * @param {Object} customer Contains the customer data. */ CustomersHelper.prototype.save = function (customer) { - var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_customer'; + var url = GlobalVariables.baseUrl + '/index.php/customers/' + (customer.id ? 'update' : 'create'); var data = { csrfToken: GlobalVariables.csrfToken, @@ -191,7 +191,7 @@ * @param {Number} id Record id to be deleted. */ CustomersHelper.prototype.delete = function (id) { - var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_customer'; + var url = GlobalVariables.baseUrl + '/index.php/customers/destroy'; var data = { csrfToken: GlobalVariables.csrfToken, @@ -367,19 +367,19 @@ /** * Filter customer records. * - * @param {String} key This key string is used to filter the customer records. + * @param {String} keyword This keyword string is used to filter the customer 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. */ - CustomersHelper.prototype.filter = function (key, selectId, display) { + CustomersHelper.prototype.filter = function (keyword, selectId, display) { display = display || false; - var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_customers'; + var url = GlobalVariables.baseUrl + '/index.php/customers/search'; var data = { csrfToken: GlobalVariables.csrfToken, - key: key, + keyword: keyword, limit: this.filterLimit }; @@ -408,7 +408,7 @@ 'text': EALang.load_more, 'click': function () { this.filterLimit += 20; - this.filter(key, selectId, display); + this.filter(keyword, selectId, display); }.bind(this) }).appendTo('#filter-customers .results'); }