2021-11-06 17:03:08 +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-06 17:03:08 +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-06 17:03:08 +03:00
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
|
|
|
* @since v1.5.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appointments API v1 controller.
|
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Appointments_api_v1 extends EA_Controller
|
|
|
|
{
|
2021-11-06 17:03:08 +03:00
|
|
|
/**
|
|
|
|
* Appointments_api_v1 constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('customers_model');
|
|
|
|
$this->load->model('providers_model');
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('settings_model');
|
|
|
|
|
|
|
|
$this->load->library('api');
|
|
|
|
$this->load->library('synchronization');
|
|
|
|
$this->load->library('notifications');
|
|
|
|
|
|
|
|
$this->api->auth();
|
|
|
|
|
|
|
|
$this->api->model('appointments_model');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an appointment collection.
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2021-11-06 17:03:08 +03:00
|
|
|
$keyword = $this->api->request_keyword();
|
|
|
|
|
|
|
|
$limit = $this->api->request_limit();
|
|
|
|
|
|
|
|
$offset = $this->api->request_offset();
|
|
|
|
|
|
|
|
$order_by = $this->api->request_order_by();
|
|
|
|
|
|
|
|
$fields = $this->api->request_fields();
|
|
|
|
|
2021-11-06 17:41:11 +03:00
|
|
|
$with = $this->api->request_with();
|
2021-11-06 17:34:54 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$where = null;
|
2022-10-16 22:17:36 +03:00
|
|
|
|
|
|
|
// Date query param.
|
|
|
|
|
|
|
|
$date = request('date');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($date)) {
|
2022-10-16 22:17:36 +03:00
|
|
|
$where['DATE(start_datetime)'] = (new DateTime($date))->format('Y-m-d');
|
|
|
|
}
|
|
|
|
|
|
|
|
// From query param.
|
|
|
|
|
|
|
|
$from = request('from');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($from)) {
|
2022-10-16 22:17:36 +03:00
|
|
|
$where['DATE(start_datetime) >='] = (new DateTime($from))->format('Y-m-d');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Till query param.
|
|
|
|
|
|
|
|
$till = request('till');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($till)) {
|
2022-10-16 22:17:36 +03:00
|
|
|
$where['DATE(end_datetime) <='] = (new DateTime($till))->format('Y-m-d');
|
|
|
|
}
|
|
|
|
|
2023-01-14 12:25:25 +03:00
|
|
|
// Service ID query param.
|
|
|
|
|
|
|
|
$service_id = request('serviceId');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($service_id)) {
|
2023-01-14 12:25:25 +03:00
|
|
|
$where['id_services'] = $service_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provider ID query param.
|
|
|
|
|
|
|
|
$provider_id = request('providerId');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($provider_id)) {
|
2023-01-14 12:25:25 +03:00
|
|
|
$where['id_users_provider'] = $provider_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Customer ID query param.
|
|
|
|
|
|
|
|
$customer_id = request('customerId');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($customer_id)) {
|
2023-01-14 12:25:25 +03:00
|
|
|
$where['id_users_customer'] = $customer_id;
|
|
|
|
}
|
|
|
|
|
2021-11-06 17:03:08 +03:00
|
|
|
$appointments = empty($keyword)
|
2022-10-16 22:17:36 +03:00
|
|
|
? $this->appointments_model->get($where, $limit, $offset, $order_by)
|
2021-11-06 17:03:08 +03:00
|
|
|
: $this->appointments_model->search($keyword, $limit, $offset, $order_by);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($appointments as &$appointment) {
|
2021-11-06 17:03:08 +03:00
|
|
|
$this->appointments_model->api_encode($appointment);
|
|
|
|
|
|
|
|
$this->aggregates($appointment);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($fields)) {
|
2021-11-06 17:03:08 +03:00
|
|
|
$this->appointments_model->only($appointment, $fields);
|
|
|
|
}
|
2021-11-06 17:34:54 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($with)) {
|
2021-11-06 17:41:11 +03:00
|
|
|
$this->appointments_model->load($appointment, $with);
|
2021-11-06 17:34:54 +03:00
|
|
|
}
|
2021-11-06 17:03:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
json_response($appointments);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-06 17:03:08 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
/**
|
|
|
|
* Load the relations of the current appointment if the "aggregates" query parameter is present.
|
|
|
|
*
|
|
|
|
* This is a compatibility addition to the appointment resource which was the only one to support it.
|
|
|
|
*
|
|
|
|
* Use the "attach" query parameter instead as this one will be removed.
|
|
|
|
*
|
|
|
|
* @param array $appointment Appointment data.
|
|
|
|
*
|
|
|
|
* @deprecated Since 1.5
|
|
|
|
*/
|
|
|
|
private function aggregates(array &$appointment)
|
|
|
|
{
|
|
|
|
$aggregates = request('aggregates') !== null;
|
|
|
|
|
|
|
|
if ($aggregates) {
|
|
|
|
$appointment['service'] = $this->services_model->find(
|
|
|
|
$appointment['id_services'] ?? ($appointment['serviceId'] ?? null),
|
2023-12-22 13:35:41 +03:00
|
|
|
true,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
|
|
|
$appointment['provider'] = $this->providers_model->find(
|
|
|
|
$appointment['id_users_provider'] ?? ($appointment['providerId'] ?? null),
|
2023-12-22 13:35:41 +03:00
|
|
|
true,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
|
|
|
$appointment['customer'] = $this->customers_model->find(
|
|
|
|
$appointment['id_users_customer'] ?? ($appointment['customerId'] ?? null),
|
2023-12-22 13:35:41 +03:00
|
|
|
true,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
|
|
|
$this->services_model->api_encode($appointment['service']);
|
|
|
|
$this->providers_model->api_encode($appointment['provider']);
|
|
|
|
$this->customers_model->api_encode($appointment['customer']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 17:03:08 +03:00
|
|
|
/**
|
|
|
|
* Get a single appointment.
|
|
|
|
*
|
|
|
|
* @param int|null $id Appointment ID.
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public function show(int $id = null)
|
2021-11-06 17:03:08 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2021-11-06 17:03:08 +03:00
|
|
|
$fields = $this->api->request_fields();
|
2022-10-16 22:17:36 +03:00
|
|
|
|
2021-11-06 17:41:11 +03:00
|
|
|
$with = $this->api->request_with();
|
2021-11-06 17:03:08 +03:00
|
|
|
|
|
|
|
$appointment = $this->appointments_model->find($id);
|
|
|
|
|
|
|
|
$this->appointments_model->api_encode($appointment);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($fields)) {
|
2021-11-06 17:03:08 +03:00
|
|
|
$this->appointments_model->only($appointment, $fields);
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($with)) {
|
2021-11-06 17:41:11 +03:00
|
|
|
$this->appointments_model->load($appointment, $with);
|
2021-11-06 17:34:54 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$appointment) {
|
2021-11-06 17:03:08 +03:00
|
|
|
response('Not Found', 404);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_response($appointment);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-06 17:03:08 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-26 06:41:25 +03:00
|
|
|
* Store a new appointment.
|
2021-11-06 17:03:08 +03:00
|
|
|
*/
|
|
|
|
public function store()
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2021-11-06 17:03:08 +03:00
|
|
|
$appointment = request();
|
|
|
|
|
|
|
|
$this->appointments_model->api_decode($appointment);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('id', $appointment)) {
|
2021-11-06 17:03:08 +03:00
|
|
|
unset($appointment['id']);
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!array_key_exists('end_datetime', $appointment)) {
|
2021-11-06 17:03:08 +03:00
|
|
|
$appointment['end_datetime'] = $this->calculate_end_datetime($appointment);
|
|
|
|
}
|
|
|
|
|
|
|
|
$appointment_id = $this->appointments_model->save($appointment);
|
|
|
|
|
|
|
|
$created_appointment = $this->appointments_model->find($appointment_id);
|
|
|
|
|
|
|
|
$this->notify_and_sync_appointment($created_appointment);
|
|
|
|
|
|
|
|
$this->appointments_model->api_encode($created_appointment);
|
|
|
|
|
|
|
|
json_response($created_appointment, 201);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-06 17:03:08 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
/**
|
|
|
|
* Calculate the end date time of an appointment based on the selected service.
|
|
|
|
*
|
|
|
|
* @param array $appointment Appointment data.
|
|
|
|
*
|
|
|
|
* @return string Returns the end date time value.
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
private function calculate_end_datetime(array $appointment): string
|
|
|
|
{
|
|
|
|
$duration = $this->services_model->value($appointment['id_services'], 'duration');
|
|
|
|
|
|
|
|
$end = new DateTime($appointment['start_datetime']);
|
|
|
|
|
|
|
|
$end->add(new DateInterval('PT' . $duration . 'M'));
|
|
|
|
|
|
|
|
return $end->format('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the required notifications and trigger syncing after saving an appointment.
|
|
|
|
*
|
|
|
|
* @param array $appointment Appointment data.
|
|
|
|
* @param string $action Performed action ("store" or "update").
|
|
|
|
*/
|
|
|
|
private function notify_and_sync_appointment(array $appointment, string $action = 'store')
|
|
|
|
{
|
|
|
|
$manage_mode = $action === 'update';
|
|
|
|
|
|
|
|
$service = $this->services_model->find($appointment['id_services'], true);
|
|
|
|
|
|
|
|
$provider = $this->providers_model->find($appointment['id_users_provider'], true);
|
|
|
|
|
|
|
|
$customer = $this->customers_model->find($appointment['id_users_customer'], true);
|
|
|
|
|
|
|
|
$settings = [
|
|
|
|
'company_name' => setting('company_name'),
|
|
|
|
'company_email' => setting('company_email'),
|
|
|
|
'company_link' => setting('company_link'),
|
|
|
|
'date_format' => setting('date_format'),
|
2023-12-22 13:35:41 +03:00
|
|
|
'time_format' => setting('time_format'),
|
2023-11-29 12:24:09 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->synchronization->sync_appointment_saved($appointment, $service, $provider, $customer, $settings);
|
|
|
|
|
|
|
|
$this->notifications->notify_appointment_saved(
|
|
|
|
$appointment,
|
|
|
|
$service,
|
|
|
|
$provider,
|
|
|
|
$customer,
|
|
|
|
$settings,
|
2023-12-22 13:35:41 +03:00
|
|
|
$manage_mode,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-06 17:03:08 +03:00
|
|
|
/**
|
|
|
|
* Update an appointment.
|
|
|
|
*
|
|
|
|
* @param int $id Appointment ID.
|
|
|
|
*/
|
|
|
|
public function update(int $id)
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2021-11-06 17:03:08 +03:00
|
|
|
$occurrences = $this->appointments_model->get(['id' => $id]);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($occurrences)) {
|
2021-11-06 17:03:08 +03:00
|
|
|
response('', 404);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$original_appointment = $occurrences[0];
|
|
|
|
|
|
|
|
$appointment = request();
|
|
|
|
|
|
|
|
$this->appointments_model->api_decode($appointment, $original_appointment);
|
|
|
|
|
|
|
|
$appointment_id = $this->appointments_model->save($appointment);
|
|
|
|
|
|
|
|
$updated_appointment = $this->appointments_model->find($appointment_id);
|
|
|
|
|
|
|
|
$this->notify_and_sync_appointment($updated_appointment, 'update');
|
|
|
|
|
|
|
|
$this->appointments_model->api_encode($updated_appointment);
|
|
|
|
|
|
|
|
json_response($updated_appointment);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-06 17:03:08 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an appointment.
|
|
|
|
*
|
|
|
|
* @param int $id Appointment ID.
|
|
|
|
*/
|
|
|
|
public function destroy(int $id)
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2021-11-06 17:03:08 +03:00
|
|
|
$occurrences = $this->appointments_model->get(['id' => $id]);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($occurrences)) {
|
2021-11-06 17:03:08 +03:00
|
|
|
response('', 404);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$deleted_appointment = $occurrences[0];
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$service = $this->services_model->find($deleted_appointment['id_services'], true);
|
2021-11-06 17:03:08 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$provider = $this->providers_model->find($deleted_appointment['id_users_provider'], true);
|
2021-11-06 17:03:08 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$customer = $this->customers_model->find($deleted_appointment['id_users_customer'], true);
|
2021-11-06 17:03:08 +03:00
|
|
|
|
|
|
|
$settings = [
|
|
|
|
'company_name' => setting('company_name'),
|
|
|
|
'company_email' => setting('company_email'),
|
|
|
|
'company_link' => setting('company_link'),
|
|
|
|
'date_format' => setting('date_format'),
|
2023-12-22 13:35:41 +03:00
|
|
|
'time_format' => setting('time_format'),
|
2021-11-06 17:03:08 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->appointments_model->delete($id);
|
|
|
|
|
|
|
|
$this->synchronization->sync_appointment_deleted($deleted_appointment, $provider);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$this->notifications->notify_appointment_deleted(
|
|
|
|
$deleted_appointment,
|
|
|
|
$service,
|
|
|
|
$provider,
|
|
|
|
$customer,
|
2023-12-22 13:35:41 +03:00
|
|
|
$settings,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2021-11-06 17:03:08 +03:00
|
|
|
|
|
|
|
response('', 204);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-06 17:03:08 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|