MaketRandevu/application/controllers/Customers.php

214 lines
5.1 KiB
PHP
Raw Normal View History

<?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_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);
}
}
2021-12-14 09:18:46 +03:00
/**
* Find a customer.
*/
public function find()
{
try
{
if (cannot('delete', PRIV_USERS))
{
show_error('Forbidden', 403);
}
$customer_id = request('customer_id');
$customer = $this->customers_model->find($customer_id);
json_response($customer);
}
catch (Throwable $e)
{
json_exception($e);
}
}
}