Created a new customers resource controller

This commit is contained in:
alextselegidis 2021-11-18 06:34:07 +01:00
parent 957865ba23
commit fec42278c8
2 changed files with 196 additions and 7 deletions

View file

@ -0,0 +1,189 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @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);
}
}
}

View file

@ -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');
}