2020-04-22 22:48:56 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
2016-07-07 23:05:10 +03:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2020-03-11 12:10:59 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
|
2020-11-14 22:36:25 +03:00
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2016-07-07 23:05:10 +03:00
|
|
|
* @since v1.2.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2016-07-08 22:17:06 +03:00
|
|
|
require_once __DIR__ . '/API_V1_Controller.php';
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
use EA\Engine\Api\V1\Request;
|
|
|
|
use EA\Engine\Api\V1\Response;
|
|
|
|
use EA\Engine\Types\NonEmptyText;
|
2016-07-09 14:14:08 +03:00
|
|
|
|
2016-07-07 23:05:10 +03:00
|
|
|
/**
|
|
|
|
* Appointments Controller
|
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
2016-07-08 22:17:06 +03:00
|
|
|
class Appointments extends API_V1_Controller {
|
2016-07-09 14:14:08 +03:00
|
|
|
/**
|
2016-07-09 22:53:28 +03:00
|
|
|
* Appointments Resource Parser
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2016-07-09 22:53:28 +03:00
|
|
|
* @var \EA\Engine\Api\V1\Parsers\Appointments
|
2016-07-09 14:14:08 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
protected $parser;
|
2016-07-09 14:14:08 +03:00
|
|
|
|
2016-07-08 22:17:06 +03:00
|
|
|
/**
|
|
|
|
* Class Constructor
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function __construct()
|
|
|
|
{
|
2016-07-08 22:17:06 +03:00
|
|
|
parent::__construct();
|
2016-07-09 14:14:08 +03:00
|
|
|
$this->load->model('appointments_model');
|
2020-10-21 21:37:47 +03:00
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('providers_model');
|
|
|
|
$this->load->model('customers_model');
|
|
|
|
$this->load->model('settings_model');
|
|
|
|
$this->load->library('synchronization');
|
|
|
|
$this->load->library('notifications');
|
2016-07-09 22:53:28 +03:00
|
|
|
$this->parser = new \EA\Engine\Api\V1\Parsers\Appointments;
|
2016-07-08 22:17:06 +03:00
|
|
|
}
|
2016-07-07 23:05:10 +03:00
|
|
|
|
2016-07-08 22:17:06 +03:00
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* GET API Method
|
|
|
|
*
|
2016-07-08 22:17:06 +03:00
|
|
|
* @param int $id Optional (null), the record ID to be returned.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function get($id = NULL)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-04-13 15:54:50 +03:00
|
|
|
$conditions = [
|
|
|
|
'is_unavailable' => FALSE
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($id !== NULL)
|
|
|
|
{
|
|
|
|
$conditions['id'] = $id;
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:40:32 +03:00
|
|
|
$appointments = $this->appointments_model->get_batch($conditions, NULL, NULL, NULL, array_key_exists('aggregates', $_GET));
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
|
|
if ($id !== NULL && count($appointments) === 0)
|
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$this->throw_record_not_found();
|
2016-07-09 23:07:26 +03:00
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$response = new Response($appointments);
|
2016-07-10 14:00:42 +03:00
|
|
|
|
2016-07-10 12:38:22 +03:00
|
|
|
$response->encode($this->parser)
|
2017-09-15 14:36:37 +03:00
|
|
|
->search()
|
|
|
|
->sort()
|
|
|
|
->paginate()
|
|
|
|
->minimize()
|
|
|
|
->singleEntry($id)
|
|
|
|
->output();
|
|
|
|
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
catch (Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
exit($this->handle_exception($exception));
|
2017-09-15 14:36:37 +03:00
|
|
|
}
|
2016-07-07 23:05:10 +03:00
|
|
|
}
|
|
|
|
|
2016-07-08 22:17:06 +03:00
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* POST API Method
|
2016-07-08 22:17:06 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function post()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-04-06 21:40:32 +03:00
|
|
|
// Insert the appointment to the database.
|
2017-09-15 14:36:37 +03:00
|
|
|
$request = new Request();
|
2020-11-16 12:54:59 +03:00
|
|
|
$appointment = $request->get_body();
|
2017-09-15 14:36:37 +03:00
|
|
|
$this->parser->decode($appointment);
|
2016-07-10 12:12:16 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if (isset($appointment['id']))
|
|
|
|
{
|
2016-07-10 12:12:16 +03:00
|
|
|
unset($appointment['id']);
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:17:31 +03:00
|
|
|
// Generate end_datetime based on service duration if this field is not defined
|
2020-05-04 21:59:37 +03:00
|
|
|
if ( ! isset($appointment['end_datetime']))
|
2020-05-03 23:17:31 +03:00
|
|
|
{
|
|
|
|
$service = $this->services_model->get_row($appointment['id_services']);
|
2020-05-04 21:59:37 +03:00
|
|
|
|
2020-05-03 23:17:31 +03:00
|
|
|
if (isset($service['duration']))
|
|
|
|
{
|
2020-05-04 21:59:37 +03:00
|
|
|
$end_datetime = new DateTime($appointment['start_datetime']);
|
|
|
|
$end_datetime->add(new DateInterval('PT' . $service['duration'] . 'M'));
|
|
|
|
$appointment['end_datetime'] = $end_datetime->format('Y-m-d H:i:s');
|
2020-05-03 23:17:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 12:12:16 +03:00
|
|
|
$id = $this->appointments_model->add($appointment);
|
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
$service = $this->services_model->get_row($appointment['id_services']);
|
|
|
|
$provider = $this->providers_model->get_row($appointment['id_users_provider']);
|
|
|
|
$customer = $this->customers_model->get_row($appointment['id_users_customer']);
|
|
|
|
$settings = [
|
|
|
|
'company_name' => $this->settings_model->get_setting('company_name'),
|
|
|
|
'company_email' => $this->settings_model->get_setting('company_email'),
|
|
|
|
'company_link' => $this->settings_model->get_setting('company_link'),
|
|
|
|
'date_format' => $this->settings_model->get_setting('date_format'),
|
|
|
|
'time_format' => $this->settings_model->get_setting('time_format')
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->synchronization->sync_appointment_saved($appointment, $service, $provider, $customer, $settings, FALSE);
|
|
|
|
$this->notifications->notify_appointment_saved($appointment, $service, $provider, $customer, $settings, FALSE);
|
|
|
|
|
2016-07-10 12:12:16 +03:00
|
|
|
// Fetch the new object from the database and return it to the client.
|
2020-12-05 12:09:25 +03:00
|
|
|
$batch = $this->appointments_model->get_batch(['id' => $id]);
|
2017-09-15 14:36:37 +03:00
|
|
|
$response = new Response($batch);
|
2016-11-09 21:56:24 +03:00
|
|
|
$status = new NonEmptyText('201 Created');
|
2017-09-15 14:36:37 +03:00
|
|
|
$response->encode($this->parser)->singleEntry(TRUE)->output($status);
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
catch (Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
exit($this->handle_exception($exception));
|
2017-09-15 14:36:37 +03:00
|
|
|
}
|
2016-07-08 22:17:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* PUT API Method
|
2016-07-08 22:17:06 +03:00
|
|
|
*
|
|
|
|
* @param int $id The record ID to be updated.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function put($id)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-04-06 21:40:32 +03:00
|
|
|
// Update the appointment record.
|
2020-12-05 12:09:25 +03:00
|
|
|
$batch = $this->appointments_model->get_batch(['id' => $id]);
|
2016-07-08 22:17:06 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($id !== NULL && count($batch) === 0)
|
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$this->throw_record_not_found();
|
2016-07-09 23:07:26 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
|
|
$request = new Request();
|
2020-11-16 12:54:59 +03:00
|
|
|
$updated_appointment = $request->get_body();
|
2020-10-21 21:49:05 +03:00
|
|
|
$base_appointment = $batch[0];
|
|
|
|
$this->parser->decode($updated_appointment, $base_appointment);
|
|
|
|
$updated_appointment['id'] = $id;
|
|
|
|
$id = $this->appointments_model->add($updated_appointment);
|
|
|
|
|
|
|
|
$service = $this->services_model->get_row($updated_appointment['id_services']);
|
|
|
|
$provider = $this->providers_model->get_row($updated_appointment['id_users_provider']);
|
|
|
|
$customer = $this->customers_model->get_row($updated_appointment['id_users_customer']);
|
2020-10-21 21:37:47 +03:00
|
|
|
$settings = [
|
|
|
|
'company_name' => $this->settings_model->get_setting('company_name'),
|
|
|
|
'company_email' => $this->settings_model->get_setting('company_email'),
|
|
|
|
'company_link' => $this->settings_model->get_setting('company_link'),
|
|
|
|
'date_format' => $this->settings_model->get_setting('date_format'),
|
|
|
|
'time_format' => $this->settings_model->get_setting('time_format')
|
|
|
|
];
|
|
|
|
|
2020-10-21 21:49:05 +03:00
|
|
|
$this->synchronization->sync_appointment_saved($updated_appointment, $service, $provider, $customer, $settings, TRUE);
|
|
|
|
$this->notifications->notify_appointment_saved($updated_appointment, $service, $provider, $customer, $settings, TRUE);
|
2020-10-21 21:37:47 +03:00
|
|
|
|
|
|
|
|
2016-07-10 12:12:16 +03:00
|
|
|
// Fetch the updated object from the database and return it to the client.
|
2020-12-05 12:09:25 +03:00
|
|
|
$batch = $this->appointments_model->get_batch(['id' => $id]);
|
2017-09-15 14:36:37 +03:00
|
|
|
$response = new Response($batch);
|
|
|
|
$response->encode($this->parser)->singleEntry($id)->output();
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
catch (Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
exit($this->handle_exception($exception));
|
2017-09-15 14:36:37 +03:00
|
|
|
}
|
2016-07-08 22:17:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* DELETE API Method
|
2016-07-08 22:17:06 +03:00
|
|
|
*
|
|
|
|
* @param int $id The record ID to be deleted.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function delete($id)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-10-21 21:37:47 +03:00
|
|
|
$appointment = $this->appointments_model->get_row($id);
|
|
|
|
$service = $this->services_model->get_row($appointment['id_services']);
|
|
|
|
$provider = $this->providers_model->get_row($appointment['id_users_provider']);
|
|
|
|
$customer = $this->customers_model->get_row($appointment['id_users_customer']);
|
|
|
|
$settings = [
|
|
|
|
'company_name' => $this->settings_model->get_setting('company_name'),
|
|
|
|
'company_email' => $this->settings_model->get_setting('company_email'),
|
|
|
|
'company_link' => $this->settings_model->get_setting('company_link'),
|
|
|
|
'date_format' => $this->settings_model->get_setting('date_format'),
|
|
|
|
'time_format' => $this->settings_model->get_setting('time_format')
|
|
|
|
];
|
|
|
|
|
2016-11-06 20:30:03 +03:00
|
|
|
$this->appointments_model->delete($id);
|
2016-07-09 23:07:26 +03:00
|
|
|
|
2020-10-21 21:37:47 +03:00
|
|
|
$this->synchronization->sync_appointment_deleted($appointment, $provider);
|
|
|
|
$this->notifications->notify_appointment_deleted($appointment, $service, $provider, $customer, $settings);
|
|
|
|
|
2016-07-09 23:07:26 +03:00
|
|
|
$response = new Response([
|
2017-09-15 14:36:37 +03:00
|
|
|
'code' => 200,
|
2016-07-10 14:24:05 +03:00
|
|
|
'message' => 'Record was deleted successfully!'
|
2016-07-09 23:07:26 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$response->output();
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
catch (Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
exit($this->handle_exception($exception));
|
2017-09-15 14:36:37 +03:00
|
|
|
}
|
2016-07-07 23:05:10 +03:00
|
|
|
}
|
|
|
|
}
|