2021-11-18 07:13:13 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2021-11-18 07:13:13 +03:00
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
2021-11-18 07:13:13 +03:00
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Services controller.
|
|
|
|
*
|
|
|
|
* Handles the services related operations.
|
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Services extends EA_Controller
|
|
|
|
{
|
2024-05-11 17:42:01 +03:00
|
|
|
public array $allowed_service_fields = [
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'duration',
|
|
|
|
'price',
|
|
|
|
'currency',
|
|
|
|
'description',
|
|
|
|
'color',
|
|
|
|
'location',
|
|
|
|
'availabilities_type',
|
|
|
|
'attendants_number',
|
|
|
|
'is_private',
|
|
|
|
'id_service_categories',
|
|
|
|
];
|
|
|
|
public array $optional_service_fields = [
|
|
|
|
'id_service_categories' => null,
|
|
|
|
];
|
|
|
|
|
2021-11-18 07:13:13 +03:00
|
|
|
/**
|
|
|
|
* Services constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('roles_model');
|
|
|
|
|
|
|
|
$this->load->library('accounts');
|
|
|
|
$this->load->library('timezones');
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->load->library('webhooks_client');
|
2021-11-18 07:13:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the backend services page.
|
|
|
|
*
|
2022-01-10 11:09:49 +03:00
|
|
|
* On this page admin users will be able to manage services, which are eventually selected by customers during the
|
2021-11-18 07:13:13 +03:00
|
|
|
* booking process.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function index(): void
|
2021-11-18 07:13:13 +03:00
|
|
|
{
|
|
|
|
session(['dest_url' => site_url('services')]);
|
|
|
|
|
2022-01-17 20:24:02 +03:00
|
|
|
$user_id = session('user_id');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (cannot('view', PRIV_SERVICES)) {
|
|
|
|
if ($user_id) {
|
2022-01-17 20:24:02 +03:00
|
|
|
abort(403, 'Forbidden');
|
|
|
|
}
|
2021-11-18 07:13:13 +03:00
|
|
|
|
2022-01-17 20:24:02 +03:00
|
|
|
redirect('login');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2021-11-18 07:13:13 +03:00
|
|
|
|
|
|
|
$role_slug = session('role_slug');
|
2022-01-10 11:09:49 +03:00
|
|
|
|
|
|
|
script_vars([
|
|
|
|
'user_id' => $user_id,
|
|
|
|
'role_slug' => $role_slug,
|
2022-01-25 00:09:30 +03:00
|
|
|
'event_minimum_duration' => EVENT_MINIMUM_DURATION,
|
2022-01-10 11:09:49 +03:00
|
|
|
]);
|
|
|
|
|
2021-12-18 19:22:40 +03:00
|
|
|
html_vars([
|
2021-11-18 07:13:13 +03:00
|
|
|
'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),
|
|
|
|
]);
|
2021-12-18 19:22:40 +03:00
|
|
|
|
2022-01-19 12:21:05 +03:00
|
|
|
$this->load->view('pages/services');
|
2021-11-18 07:13:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter services by the provided keyword.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function search(): void
|
2021-11-18 07:13:13 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('view', PRIV_SERVICES)) {
|
2021-12-18 19:22:40 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 07:13:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$keyword = request('keyword', '');
|
|
|
|
|
2024-05-16 08:25:33 +03:00
|
|
|
$order_by = request('order_by', 'row_order ASC, update_datetime DESC');
|
2021-11-18 07:13:13 +03:00
|
|
|
|
|
|
|
$limit = request('limit', 1000);
|
2022-01-10 11:09:49 +03:00
|
|
|
|
2024-05-11 16:02:12 +03:00
|
|
|
$offset = (int) request('offset', '0');
|
2021-11-18 07:13:13 +03:00
|
|
|
|
|
|
|
$services = $this->services_model->search($keyword, $limit, $offset, $order_by);
|
|
|
|
|
|
|
|
json_response($services);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 07:13:13 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-26 06:41:25 +03:00
|
|
|
* Store a new service.
|
2021-11-18 07:13:13 +03:00
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function store(): void
|
2021-11-18 07:13:13 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('add', PRIV_SERVICES)) {
|
2021-12-18 19:22:40 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 07:13:13 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 14:50:40 +03:00
|
|
|
$service = request('service');
|
|
|
|
|
2024-05-11 17:42:01 +03:00
|
|
|
$this->services_model->only($service, $this->allowed_service_fields);
|
2023-01-21 14:51:06 +03:00
|
|
|
|
2024-05-11 17:42:01 +03:00
|
|
|
$this->services_model->optional($service, $this->optional_service_fields);
|
2022-03-25 14:50:40 +03:00
|
|
|
|
2021-11-18 07:13:13 +03:00
|
|
|
$service_id = $this->services_model->save($service);
|
2023-01-21 14:51:06 +03:00
|
|
|
|
|
|
|
$service = $this->services_model->find($service_id);
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->webhooks_client->trigger(WEBHOOK_SERVICE_SAVE, $service);
|
2021-11-18 07:13:13 +03:00
|
|
|
|
|
|
|
json_response([
|
2023-11-29 12:24:09 +03:00
|
|
|
'success' => true,
|
2023-12-22 13:35:41 +03:00
|
|
|
'id' => $service_id,
|
2021-11-18 07:13:13 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
json_exception($e);
|
2021-11-18 07:13:13 +03:00
|
|
|
}
|
2023-11-29 12:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a service.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function find(): void
|
2023-11-29 12:24:09 +03:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (cannot('delete', PRIV_SERVICES)) {
|
|
|
|
abort(403, 'Forbidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
$service_id = request('service_id');
|
|
|
|
|
|
|
|
$service = $this->services_model->find($service_id);
|
|
|
|
|
|
|
|
json_response($service);
|
|
|
|
} catch (Throwable $e) {
|
2021-11-18 07:13:13 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a service.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function update(): void
|
2021-11-18 07:13:13 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('edit', PRIV_SERVICES)) {
|
2021-12-18 19:22:40 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 07:13:13 +03:00
|
|
|
}
|
2022-03-25 14:50:40 +03:00
|
|
|
|
|
|
|
$service = request('service');
|
|
|
|
|
2024-05-11 17:42:01 +03:00
|
|
|
$this->services_model->only($service, $this->allowed_service_fields);
|
2023-01-21 14:51:06 +03:00
|
|
|
|
2024-05-11 17:42:01 +03:00
|
|
|
$this->services_model->optional($service, $this->optional_service_fields);
|
2022-03-25 14:50:40 +03:00
|
|
|
|
2021-11-18 07:13:13 +03:00
|
|
|
$service_id = $this->services_model->save($service);
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$service = $this->services_model->find($service_id);
|
|
|
|
|
|
|
|
$this->webhooks_client->trigger(WEBHOOK_SERVICE_SAVE, $service);
|
|
|
|
|
2021-11-18 07:13:13 +03:00
|
|
|
json_response([
|
2023-11-29 12:24:09 +03:00
|
|
|
'success' => true,
|
2023-12-22 13:35:41 +03:00
|
|
|
'id' => $service_id,
|
2021-11-18 07:13:13 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 07:13:13 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a service.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function destroy(): void
|
2021-11-18 07:13:13 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('delete', PRIV_SERVICES)) {
|
2021-12-18 19:22:40 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 07:13:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$service_id = request('service_id');
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$service = $this->services_model->find($service_id);
|
|
|
|
|
2021-11-18 07:13:13 +03:00
|
|
|
$this->services_model->delete($service_id);
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->webhooks_client->trigger(WEBHOOK_SERVICE_DELETE, $service);
|
|
|
|
|
2021-11-18 07:13:13 +03:00
|
|
|
json_response([
|
2023-12-22 13:35:41 +03:00
|
|
|
'success' => true,
|
2021-11-18 07:13:13 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-12-14 09:18:46 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
2022-11-24 17:17:50 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Arrange service order
|
|
|
|
*/
|
|
|
|
public function sort()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (cannot('edit', PRIV_SERVICES))
|
|
|
|
{
|
|
|
|
abort(403, 'Forbidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
$service_id = request('service_id');
|
|
|
|
if (($service_id = filter_var($service_id, FILTER_VALIDATE_INT)) === FALSE)
|
|
|
|
{
|
|
|
|
abort(400,'Invalid ID value');
|
|
|
|
}
|
|
|
|
|
2022-11-28 10:39:28 +03:00
|
|
|
$insertAfterId = request('after');
|
|
|
|
if (($insertAfterId = filter_var($insertAfterId,FILTER_VALIDATE_INT)) === FALSE)
|
2022-11-24 17:17:50 +03:00
|
|
|
{
|
2022-11-28 10:39:28 +03:00
|
|
|
abort(400,'Invalid after value, must be ID or -1');
|
2022-11-24 17:17:50 +03:00
|
|
|
}
|
2022-11-28 10:39:28 +03:00
|
|
|
|
|
|
|
if ($insertAfterId <= 0)
|
2022-11-24 17:17:50 +03:00
|
|
|
{
|
2022-11-28 10:39:28 +03:00
|
|
|
$insertAfterId = FALSE;
|
2022-11-24 17:17:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$service = $this->services_model->find($service_id);
|
|
|
|
|
2022-11-28 10:39:28 +03:00
|
|
|
$service['row_order'] = $this->services_model->set_service_order($service['id'], $insertAfterId);
|
2022-11-24 17:17:50 +03:00
|
|
|
|
|
|
|
return json_response($service);
|
|
|
|
}
|
|
|
|
catch (Throwable $e)
|
|
|
|
{
|
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|