2021-11-18 09:29:41 +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 09:29:41 +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 09:29:41 +03:00
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unavailabilities controller.
|
|
|
|
*
|
|
|
|
* Handles the unavailabilities related operations.
|
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Unavailabilities extends EA_Controller
|
|
|
|
{
|
2024-05-11 17:42:01 +03:00
|
|
|
public array $allowed_unavailability_fields = [
|
|
|
|
'id',
|
|
|
|
'start_datetime',
|
|
|
|
'end_datetime',
|
|
|
|
'location',
|
|
|
|
'notes',
|
|
|
|
'is_unavailability',
|
|
|
|
'id_users_provider',
|
|
|
|
];
|
|
|
|
|
2021-11-18 09:29:41 +03:00
|
|
|
/**
|
|
|
|
* Unavailabilities constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->load->model('unavailabilities_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 09:29:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter unavailabilities by the provided keyword.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function search(): void
|
2021-11-18 09:29:41 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('view', PRIV_APPOINTMENTS)) {
|
2021-12-18 19:32:05 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 09:29:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$keyword = request('keyword', '');
|
|
|
|
|
2024-05-11 16:02:12 +03:00
|
|
|
$order_by = request('order_by', 'update_datetime DESC');
|
2021-11-18 09:29:41 +03:00
|
|
|
|
|
|
|
$limit = request('limit', 1000);
|
2022-06-19 20:05:45 +03:00
|
|
|
|
2024-05-11 16:02:12 +03:00
|
|
|
$offset = (int) request('offset', '0');
|
2021-11-18 09:29:41 +03:00
|
|
|
|
|
|
|
$unavailabilities = $this->unavailabilities_model->search($keyword, $limit, $offset, $order_by);
|
|
|
|
|
|
|
|
json_response($unavailabilities);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:29:41 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-26 06:41:25 +03:00
|
|
|
* Store a new unavailability.
|
2021-11-18 09:29:41 +03:00
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function store(): void
|
2021-11-18 09:29:41 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('add', PRIV_APPOINTMENTS)) {
|
2021-12-18 19:32:05 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 09:29:41 +03:00
|
|
|
}
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$unavailability = request('unavailability');
|
2022-03-25 14:50:40 +03:00
|
|
|
|
2024-05-11 17:42:01 +03:00
|
|
|
$this->unavailabilities_model->only($unavailability, $this->allowed_unavailability_fields);
|
2023-01-21 14:51:06 +03:00
|
|
|
|
2021-11-18 09:29:41 +03:00
|
|
|
$unavailability_id = $this->unavailabilities_model->save($unavailability);
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$unavailability = $this->unavailabilities_model->find($unavailability_id);
|
|
|
|
|
|
|
|
$provider = $this->providers_model->find($unavailability['id_users_provider']);
|
|
|
|
|
|
|
|
$this->synchronization->sync_unavailability_saved($unavailability, $provider);
|
|
|
|
|
|
|
|
$this->webhooks_client->trigger(WEBHOOK_UNAVAILABILITY_SAVE, $unavailability);
|
|
|
|
|
2021-11-18 09:29:41 +03:00
|
|
|
json_response([
|
2023-11-29 12:24:09 +03:00
|
|
|
'success' => true,
|
2023-12-22 13:35:41 +03:00
|
|
|
'id' => $unavailability_id,
|
2021-11-18 09:29:41 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
json_exception($e);
|
2021-11-18 09:29:41 +03:00
|
|
|
}
|
2023-11-29 12:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find an unavailability.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function find(): void
|
2023-11-29 12:24:09 +03:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (cannot('view', PRIV_APPOINTMENTS)) {
|
|
|
|
abort(403, 'Forbidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
$unavailability_id = request('unavailability_id');
|
|
|
|
|
|
|
|
$unavailability = $this->unavailabilities_model->find($unavailability_id);
|
|
|
|
|
|
|
|
json_response($unavailability);
|
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:29:41 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a unavailability.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function update(): void
|
2021-11-18 09:29:41 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('edit', PRIV_APPOINTMENTS)) {
|
2021-12-18 19:32:05 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 09:29:41 +03:00
|
|
|
}
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$unavailability = request('unavailability');
|
2022-03-25 14:50:40 +03:00
|
|
|
|
2024-05-11 17:42:01 +03:00
|
|
|
$this->unavailabilities_model->only($unavailability, $this->allowed_unavailability_fields);
|
|
|
|
|
2021-11-18 09:29:41 +03:00
|
|
|
$unavailability_id = $this->unavailabilities_model->save($unavailability);
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$unavailability = $this->unavailabilities_model->find($unavailability_id);
|
|
|
|
|
|
|
|
$provider = $this->providers_model->find($unavailability['id_users_provider']);
|
|
|
|
|
|
|
|
$this->synchronization->sync_unavailability_saved($unavailability, $provider);
|
|
|
|
|
|
|
|
$this->webhooks_client->trigger(WEBHOOK_UNAVAILABILITY_SAVE, $unavailability);
|
|
|
|
|
2021-11-18 09:29:41 +03:00
|
|
|
json_response([
|
2023-11-29 12:24:09 +03:00
|
|
|
'success' => true,
|
2023-12-22 13:35:41 +03:00
|
|
|
'id' => $unavailability_id,
|
2021-11-18 09:29:41 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:29:41 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a unavailability.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function destroy(): void
|
2021-11-18 09:29:41 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('delete', PRIV_APPOINTMENTS)) {
|
2021-12-18 19:32:05 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 09:29:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$unavailability_id = request('unavailability_id');
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$unavailability = $this->unavailabilities_model->find($unavailability_id);
|
|
|
|
|
2021-11-18 09:29:41 +03:00
|
|
|
$this->unavailabilities_model->delete($unavailability_id);
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->webhooks_client->trigger(WEBHOOK_UNAVAILABILITY_DELETE, $unavailability);
|
|
|
|
|
2021-11-18 09:29:41 +03:00
|
|
|
json_response([
|
2023-12-22 13:35:41 +03:00
|
|
|
'success' => true,
|
2021-11-18 09:29:41 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-12-14 09:18:46 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 09:29:41 +03:00
|
|
|
}
|