easyappointments/application/controllers/Appointments.php

201 lines
5.3 KiB
PHP
Raw Normal View History

<?php defined('BASEPATH') or exit('No direct script access allowed');
2015-10-05 01:32:47 +03:00
/* ----------------------------------------------------------------------------
2022-01-18 15:05:42 +03:00
* Easy!Appointments - Online Appointment Scheduler
2015-10-05 01:32:47 +03:00
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
2021-12-18 19:43:45 +03:00
* @copyright Copyright (c) Alex Tselegidis
2020-11-14 22:36:25 +03:00
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
2015-10-05 01:32:47 +03:00
* @since v1.0.0
* ---------------------------------------------------------------------------- */
/**
2021-11-06 18:21:27 +03:00
* Appointments controller.
*
* Handles the appointments related operations.
*
* Notice: This file used to have the booking page related code which since v1.5 has now moved to the Booking.php
* controller for improved consistency.
2015-10-05 01:32:47 +03:00
*
* @package Controllers
*/
class Appointments extends EA_Controller
{
public array $allowed_appointment_fields = [
'id',
'start_datetime',
'end_datetime',
'location',
'notes',
'color',
2024-07-15 17:22:17 +03:00
'status',
'is_unavailability',
'id_users_provider',
'id_users_customer',
'id_services',
];
public array $optional_appointment_fields = [
//
];
/**
2021-11-06 18:21:27 +03:00
* Appointments constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->model('appointments_model');
$this->load->model('roles_model');
$this->load->library('accounts');
$this->load->library('timezones');
$this->load->library('webhooks_client');
}
2015-10-05 01:32:47 +03:00
/**
* Support backwards compatibility for appointment links that still point to this URL.
*
* @param string $appointment_hash
*
* @deprecated Since 1.5
2015-10-05 01:32:47 +03:00
*/
2024-12-19 21:13:51 +03:00
public function index(string $appointment_hash = ''): void
{
redirect('booking/' . $appointment_hash);
2015-10-05 01:32:47 +03:00
}
/**
* Filter appointments by the provided keyword.
2015-10-05 01:32:47 +03:00
*/
2024-04-26 17:36:14 +03:00
public function search(): void
{
try {
if (cannot('view', PRIV_APPOINTMENTS)) {
abort(403, 'Forbidden');
2015-10-05 01:32:47 +03:00
}
$keyword = request('keyword', '');
$order_by = request('order_by', 'update_datetime DESC');
2015-10-05 01:32:47 +03:00
$limit = request('limit', 1000);
$offset = (int) request('offset', '0');
2015-10-05 01:32:47 +03:00
$appointments = $this->appointments_model->search($keyword, $limit, $offset, $order_by);
json_response($appointments);
} catch (Throwable $e) {
json_exception($e);
2015-11-23 23:58:32 +02:00
}
}
2015-10-05 01:32:47 +03:00
/**
* Store a new appointment.
2015-10-05 01:32:47 +03:00
*/
2024-04-26 17:36:14 +03:00
public function store(): void
{
try {
if (cannot('add', PRIV_APPOINTMENTS)) {
abort(403, 'Forbidden');
}
$appointment = json_decode(request('appointment'), true);
$this->appointments_model->only($appointment, $this->allowed_appointment_fields);
$this->appointments_model->optional($appointment, $this->optional_appointment_fields);
$appointment_id = $this->appointments_model->save($appointment);
$appointment = $this->appointments_model->find($appointment);
$this->webhooks_client->trigger(WEBHOOK_APPOINTMENT_SAVE, $appointment);
json_response([
'success' => true,
'id' => $appointment_id,
]);
} catch (Throwable $e) {
json_exception($e);
2017-09-23 02:30:22 +03:00
}
}
/**
* Find an appointment.
*/
2024-04-26 17:36:14 +03:00
public function find(): void
{
try {
if (cannot('view', PRIV_APPOINTMENTS)) {
abort(403, 'Forbidden');
}
$appointment_id = request('appointment_id');
$appointment = $this->appointments_model->find($appointment_id);
json_response($appointment);
} catch (Throwable $e) {
json_exception($e);
2015-10-05 01:32:47 +03:00
}
}
/**
* Update a appointment.
*/
2024-04-26 17:36:14 +03:00
public function update(): void
{
try {
if (cannot('edit', PRIV_APPOINTMENTS)) {
abort(403, 'Forbidden');
}
$appointment = json_decode(request('appointment'), true);
$this->appointments_model->only($appointment, $this->allowed_appointment_fields);
$this->appointments_model->optional($appointment, $this->optional_appointment_fields);
$appointment_id = $this->appointments_model->save($appointment);
json_response([
'success' => true,
'id' => $appointment_id,
]);
} catch (Throwable $e) {
json_exception($e);
}
}
/**
* Remove a appointment.
*/
2024-04-26 17:36:14 +03:00
public function destroy(): void
{
try {
if (cannot('delete', PRIV_APPOINTMENTS)) {
abort(403, 'Forbidden');
}
$appointment_id = request('appointment_id');
$appointment = $this->appointments_model->find($appointment_id);
$this->appointments_model->delete($appointment_id);
$this->webhooks_client->trigger(WEBHOOK_APPOINTMENT_DELETE, $appointment);
json_response([
'success' => true,
]);
} catch (Throwable $e) {
2021-12-14 09:18:46 +03:00
json_exception($e);
}
}
2015-10-05 01:32:47 +03:00
}