2023-11-03 20:25:23 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2024-05-14 11:37:43 +03:00
|
|
|
* @since v1.5.0
|
2023-11-03 20:25:23 +03:00
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Blocked-Periods model.
|
|
|
|
*
|
|
|
|
* Handles all the database operations of the blocked-period resource.
|
|
|
|
*
|
|
|
|
* @package Models
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Blocked_periods_model extends EA_Model
|
|
|
|
{
|
2023-11-03 20:25:23 +03:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected array $casts = [
|
2023-12-22 13:35:41 +03:00
|
|
|
'id' => 'integer',
|
2023-11-03 20:25:23 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected array $api_resource = [
|
|
|
|
'id' => 'id',
|
|
|
|
'name' => 'name',
|
|
|
|
'start' => 'start_datetime',
|
|
|
|
'end' => 'end_datetime',
|
2023-12-22 13:35:41 +03:00
|
|
|
'notes' => 'notes',
|
2023-11-03 20:25:23 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save (insert or update) a blocked-period.
|
|
|
|
*
|
|
|
|
* @param array $blocked_period Associative array with the blocked-period data.
|
|
|
|
*
|
|
|
|
* @return int Returns the blocked-period ID.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
2024-04-26 17:36:14 +03:00
|
|
|
* @throws Exception
|
2023-11-03 20:25:23 +03:00
|
|
|
*/
|
|
|
|
public function save(array $blocked_period): int
|
|
|
|
{
|
|
|
|
$this->validate($blocked_period);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($blocked_period['id'])) {
|
2023-11-03 20:25:23 +03:00
|
|
|
return $this->insert($blocked_period);
|
2023-11-29 12:24:09 +03:00
|
|
|
} else {
|
2023-11-03 20:25:23 +03:00
|
|
|
return $this->update($blocked_period);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the blocked-period data.
|
|
|
|
*
|
|
|
|
* @param array $blocked_period Associative array with the blocked-period data.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
2024-04-26 17:36:14 +03:00
|
|
|
* @throws Exception
|
2023-11-03 20:25:23 +03:00
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function validate(array $blocked_period): void
|
2023-11-03 20:25:23 +03:00
|
|
|
{
|
|
|
|
// If a blocked-period ID is provided then check whether the record really exists in the database.
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($blocked_period['id'])) {
|
2023-11-03 20:25:23 +03:00
|
|
|
$count = $this->db->get_where('blocked_periods', ['id' => $blocked_period['id']])->num_rows();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$count) {
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
'The provided blocked-period ID does not exist in the database: ' . $blocked_period['id'],
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2023-11-03 20:25:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure all required fields are provided.
|
|
|
|
if (
|
2023-11-29 12:24:09 +03:00
|
|
|
empty($blocked_period['name']) ||
|
|
|
|
empty($blocked_period['start_datetime']) ||
|
|
|
|
empty($blocked_period['end_datetime'])
|
|
|
|
) {
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
'Not all required fields are provided: ' . print_r($blocked_period, true),
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2023-11-03 20:25:23 +03:00
|
|
|
}
|
2023-11-17 10:10:03 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
// Make sure that the start date time is before the end.
|
2023-11-17 10:10:03 +03:00
|
|
|
$start_date_time_object = new DateTime($blocked_period['start_datetime']);
|
|
|
|
$end_date_time_object = new DateTime($blocked_period['end_datetime']);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($start_date_time_object >= $end_date_time_object) {
|
2023-11-17 10:10:03 +03:00
|
|
|
throw new InvalidArgumentException('The start must be before the end date time value.');
|
|
|
|
}
|
2023-11-03 20:25:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new blocked-period into the database.
|
|
|
|
*
|
|
|
|
* @param array $blocked_period Associative array with the blocked-period data.
|
|
|
|
*
|
|
|
|
* @return int Returns the blocked-period ID.
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
|
|
|
protected function insert(array $blocked_period): int
|
|
|
|
{
|
|
|
|
$blocked_period['create_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
$blocked_period['update_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$this->db->insert('blocked_periods', $blocked_period)) {
|
2023-11-03 20:25:23 +03:00
|
|
|
throw new RuntimeException('Could not insert blocked-period.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->db->insert_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an existing blocked-period.
|
|
|
|
*
|
|
|
|
* @param array $blocked_period Associative array with the blocked-period data.
|
|
|
|
*
|
|
|
|
* @return int Returns the blocked-period ID.
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
|
|
|
protected function update(array $blocked_period): int
|
|
|
|
{
|
|
|
|
$blocked_period['update_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$this->db->update('blocked_periods', $blocked_period, ['id' => $blocked_period['id']])) {
|
2023-11-03 20:25:23 +03:00
|
|
|
throw new RuntimeException('Could not update blocked periods.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $blocked_period['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an existing blocked-period from the database.
|
|
|
|
*
|
|
|
|
* @param int $blocked_period_id Blocked period ID.
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
|
|
|
public function delete(int $blocked_period_id): void
|
|
|
|
{
|
|
|
|
$this->db->delete('blocked_periods', ['id' => $blocked_period_id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific blocked-period from the database.
|
|
|
|
*
|
|
|
|
* @param int $blocked_period_id The ID of the record to be returned.
|
|
|
|
*
|
|
|
|
* @return array Returns an array with the blocked-period data.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function find(int $blocked_period_id): array
|
|
|
|
{
|
|
|
|
$blocked_period = $this->db->get_where('blocked_periods', ['id' => $blocked_period_id])->row_array();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$blocked_period) {
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
'The provided blocked-period ID was not found in the database: ' . $blocked_period_id,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2023-11-03 20:25:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->cast($blocked_period);
|
|
|
|
|
|
|
|
return $blocked_period;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific field value from the database.
|
|
|
|
*
|
|
|
|
* @param int $blocked_period_id Blocked period ID.
|
|
|
|
* @param string $field Name of the value to be returned.
|
|
|
|
*
|
|
|
|
* @return mixed Returns the selected blocked-period value from the database.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function value(int $blocked_period_id, string $field): mixed
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($field)) {
|
2023-11-03 20:25:23 +03:00
|
|
|
throw new InvalidArgumentException('The field argument is cannot be empty.');
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($blocked_period_id)) {
|
2023-11-03 20:25:23 +03:00
|
|
|
throw new InvalidArgumentException('The blocked-period ID argument cannot be empty.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the service exists.
|
|
|
|
$query = $this->db->get_where('blocked_periods', ['id' => $blocked_period_id]);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$query->num_rows()) {
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
'The provided blocked-period ID was not found in the database: ' . $blocked_period_id,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2023-11-03 20:25:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the required field is part of the blocked-period data.
|
|
|
|
$blocked_period = $query->row_array();
|
|
|
|
|
|
|
|
$this->cast($blocked_period);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!array_key_exists($field, $blocked_period)) {
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
'The requested field was not found in the blocked-period data: ' . $field,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2023-11-03 20:25:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $blocked_period[$field];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-29 12:24:09 +03:00
|
|
|
* Search blocked periods by the provided keyword.
|
2023-11-03 20:25:23 +03:00
|
|
|
*
|
2023-11-29 12:24:09 +03:00
|
|
|
* @param string $keyword Search keyword.
|
2023-11-03 20:25:23 +03:00
|
|
|
* @param int|null $limit Record limit.
|
|
|
|
* @param int|null $offset Record offset.
|
|
|
|
* @param string|null $order_by Order by.
|
|
|
|
*
|
|
|
|
* @return array Returns an array of blocked periods.
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public function search(string $keyword, int $limit = null, int $offset = null, string $order_by = null): array
|
2023-11-03 20:25:23 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
$blocked_periods = $this->db
|
|
|
|
->select()
|
|
|
|
->from('blocked_periods')
|
|
|
|
->group_start()
|
|
|
|
->like('name', $keyword)
|
|
|
|
->or_like('notes', $keyword)
|
|
|
|
->group_end()
|
|
|
|
->limit($limit)
|
|
|
|
->offset($offset)
|
|
|
|
->order_by($order_by)
|
|
|
|
->get()
|
|
|
|
->result_array();
|
2023-11-03 20:25:23 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($blocked_periods as &$blocked_period) {
|
2023-11-03 20:25:23 +03:00
|
|
|
$this->cast($blocked_period);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $blocked_periods;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-29 12:24:09 +03:00
|
|
|
* Get all services that match the provided criteria.
|
2023-11-03 20:25:23 +03:00
|
|
|
*
|
2023-11-29 12:24:09 +03:00
|
|
|
* @param array|string|null $where Where conditions
|
2023-11-03 20:25:23 +03:00
|
|
|
* @param int|null $limit Record limit.
|
|
|
|
* @param int|null $offset Record offset.
|
|
|
|
* @param string|null $order_by Order by.
|
|
|
|
*
|
|
|
|
* @return array Returns an array of blocked periods.
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public function get(
|
|
|
|
array|string $where = null,
|
|
|
|
int $limit = null,
|
|
|
|
int $offset = null,
|
2023-12-22 13:35:41 +03:00
|
|
|
string $order_by = null,
|
2023-11-29 12:24:09 +03:00
|
|
|
): array {
|
|
|
|
if ($where !== null) {
|
|
|
|
$this->db->where($where);
|
|
|
|
}
|
2023-11-03 20:25:23 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($order_by !== null) {
|
|
|
|
$this->db->order_by($order_by);
|
|
|
|
}
|
|
|
|
|
|
|
|
$blocked_periods = $this->db->get('blocked_periods', $limit, $offset)->result_array();
|
|
|
|
|
|
|
|
foreach ($blocked_periods as &$blocked_period) {
|
2023-11-03 20:25:23 +03:00
|
|
|
$this->cast($blocked_period);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $blocked_periods;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load related resources to a blocked-period.
|
|
|
|
*
|
|
|
|
* @param array $blocked_period Associative array with the blocked-period data.
|
|
|
|
* @param array $resources Resource names to be attached.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function load(array &$blocked_period, array $resources)
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
// Blocked periods do not currently have any related resources.
|
2023-11-03 20:25:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the database blocked-period record to the equivalent API resource.
|
|
|
|
*
|
|
|
|
* @param array $blocked_period Blocked period data.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function api_encode(array &$blocked_period): void
|
2023-11-03 20:25:23 +03:00
|
|
|
{
|
|
|
|
$encoded_resource = [
|
2023-11-29 12:24:09 +03:00
|
|
|
'id' => array_key_exists('id', $blocked_period) ? (int) $blocked_period['id'] : null,
|
2023-11-03 20:25:23 +03:00
|
|
|
'name' => $blocked_period['name'],
|
2023-11-29 12:24:09 +03:00
|
|
|
'start' => array_key_exists('start_datetime', $blocked_period) ? $blocked_period['start_datetime'] : null,
|
|
|
|
'end' => array_key_exists('end_datetime', $blocked_period) ? $blocked_period['end_datetime'] : null,
|
2023-12-22 13:35:41 +03:00
|
|
|
'notes' => array_key_exists('notes', $blocked_period) ? $blocked_period['notes'] : null,
|
2023-11-03 20:25:23 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$blocked_period = $encoded_resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the API resource to the equivalent database blocked-period record.
|
|
|
|
*
|
|
|
|
* @param array $blocked_period API resource.
|
|
|
|
* @param array|null $base Base blocked-period data to be overwritten with the provided values (useful for updates).
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function api_decode(array &$blocked_period, array $base = null): void
|
2023-11-03 20:25:23 +03:00
|
|
|
{
|
|
|
|
$decoded_resource = $base ?: [];
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('id', $blocked_period)) {
|
2023-11-03 20:25:23 +03:00
|
|
|
$decoded_resource['id'] = $blocked_period['id'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('name', $blocked_period)) {
|
2023-11-03 20:25:23 +03:00
|
|
|
$decoded_resource['name'] = $blocked_period['name'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('start', $blocked_period)) {
|
2023-11-03 20:25:23 +03:00
|
|
|
$decoded_resource['start_datetime'] = $blocked_period['start'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('end', $blocked_period)) {
|
2023-11-03 20:25:23 +03:00
|
|
|
$decoded_resource['end_datetime'] = $blocked_period['end'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('notes', $blocked_period)) {
|
2023-11-17 11:37:38 +03:00
|
|
|
$decoded_resource['notes'] = $blocked_period['notes'];
|
2023-11-03 20:25:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$blocked_period = $decoded_resource;
|
|
|
|
}
|
2023-11-17 10:40:29 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the blocked periods that are within the provided period.
|
|
|
|
*
|
|
|
|
* @param string $start_date
|
|
|
|
* @param string $end_date
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_for_period(string $start_date, string $end_date): array
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
return $this->query()
|
2023-11-17 10:40:29 +03:00
|
|
|
//
|
|
|
|
->group_start()
|
|
|
|
->where('DATE(start_datetime) <=', $start_date)
|
|
|
|
->where('DATE(end_datetime) >=', $end_date)
|
|
|
|
->group_end()
|
|
|
|
//
|
|
|
|
->or_group_start()
|
|
|
|
->where('DATE(start_datetime) >=', $start_date)
|
|
|
|
->where('DATE(end_datetime) <=', $end_date)
|
|
|
|
->group_end()
|
|
|
|
//
|
|
|
|
->or_group_start()
|
|
|
|
->where('DATE(end_datetime) >', $start_date)
|
|
|
|
->where('DATE(end_datetime) <', $end_date)
|
|
|
|
->group_end()
|
|
|
|
//
|
|
|
|
->or_group_start()
|
|
|
|
->where('DATE(start_datetime) >', $start_date)
|
|
|
|
->where('DATE(start_datetime) <', $end_date)
|
|
|
|
->group_end()
|
|
|
|
//
|
|
|
|
->get()
|
|
|
|
->result_array();
|
|
|
|
}
|
2023-11-17 13:38:08 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
/**
|
|
|
|
* Get the query builder interface, configured for use with the blocked periods table.
|
|
|
|
*
|
|
|
|
* @return CI_DB_query_builder
|
|
|
|
*/
|
|
|
|
public function query(): CI_DB_query_builder
|
|
|
|
{
|
|
|
|
return $this->db->from('blocked_periods');
|
|
|
|
}
|
|
|
|
|
2023-11-17 13:38:08 +03:00
|
|
|
/**
|
|
|
|
* Check if a date is blocked by a blocked period.
|
|
|
|
*
|
|
|
|
* @param string $date
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function is_entire_date_blocked(string $date): bool
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
return $this->query()
|
|
|
|
->where('DATE(start_datetime) <=', $date)
|
|
|
|
->where('DATE(end_datetime) >=', $date)
|
|
|
|
->get()
|
|
|
|
->num_rows() > 1;
|
2023-11-17 13:38:08 +03:00
|
|
|
}
|
2023-11-03 20:25:23 +03:00
|
|
|
}
|