2021-11-06 16:55:36 +03:00
|
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2024-01-01 01:14:38 +03:00
|
|
|
|
* MaketRandevu - MAKET Randevu Portalı
|
2021-11-06 16:55:36 +03:00
|
|
|
|
*
|
|
|
|
|
* @package EasyAppointments
|
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
2021-11-06 16:55:36 +03:00
|
|
|
|
* @link http://easyunavailabilities.org
|
|
|
|
|
* @since v1.0.0
|
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Unavailabilities model.
|
|
|
|
|
*
|
|
|
|
|
* @package Models
|
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
class Unavailabilities_model extends EA_Model
|
|
|
|
|
{
|
2021-11-06 16:55:36 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
protected array $casts = [
|
2021-11-06 16:55:36 +03:00
|
|
|
|
'id' => 'integer',
|
2022-01-18 14:54:41 +03:00
|
|
|
|
'is_unavailability' => 'boolean',
|
2021-11-06 16:55:36 +03:00
|
|
|
|
'id_users_provider' => 'integer',
|
|
|
|
|
'id_users_customer' => 'integer',
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'id_services' => 'integer',
|
2021-11-06 16:55:36 +03:00
|
|
|
|
];
|
|
|
|
|
|
2023-01-23 09:50:24 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
protected array $api_resource = [
|
2023-01-23 09:50:24 +03:00
|
|
|
|
'id' => 'id',
|
|
|
|
|
'book' => 'book_datetime',
|
|
|
|
|
'start' => 'start_datetime',
|
|
|
|
|
'end' => 'end_datetime',
|
|
|
|
|
'location' => 'location',
|
|
|
|
|
'color' => 'color',
|
|
|
|
|
'status' => 'status',
|
|
|
|
|
'notes' => 'notes',
|
|
|
|
|
'hash' => 'hash',
|
|
|
|
|
'providerId' => 'id_users_provider',
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'googleCalendarId' => 'id_google_calendar',
|
2023-01-23 09:50:24 +03:00
|
|
|
|
];
|
|
|
|
|
|
2021-11-06 16:55:36 +03:00
|
|
|
|
/**
|
|
|
|
|
* Save (insert or update) an unavailability.
|
|
|
|
|
*
|
|
|
|
|
* @param array $unavailability Associative array with the unavailability data.
|
|
|
|
|
*
|
|
|
|
|
* @return int Returns the unavailability ID.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
|
|
|
|
public function save(array $unavailability): int
|
|
|
|
|
{
|
|
|
|
|
$this->validate($unavailability);
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($unavailability['id'])) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
return $this->insert($unavailability);
|
2023-11-29 12:24:09 +03:00
|
|
|
|
} else {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
return $this->update($unavailability);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validate the unavailability data.
|
|
|
|
|
*
|
|
|
|
|
* @param array $unavailability Associative array with the unavailability data.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
|
|
|
|
public function validate(array $unavailability)
|
|
|
|
|
{
|
|
|
|
|
// If an unavailability ID is provided then check whether the record really exists in the database.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!empty($unavailability['id'])) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
$count = $this->db->get_where('appointments', ['id' => $unavailability['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 unavailability ID does not exist in the database: ' . $unavailability['id'],
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2021-11-06 16:55:36 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 17:25:45 +03:00
|
|
|
|
// Make sure all required fields are provided.
|
2021-11-06 16:55:36 +03:00
|
|
|
|
if (
|
2023-11-29 12:24:09 +03:00
|
|
|
|
empty($unavailability['start_datetime']) ||
|
|
|
|
|
empty($unavailability['end_datetime']) ||
|
|
|
|
|
empty($unavailability['id_users_provider'])
|
|
|
|
|
) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'Not all required fields are provided: ' . print_r($unavailability, true),
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2021-11-06 16:55:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure that the provided unavailability date time values are valid.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!validate_datetime($unavailability['start_datetime'])) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
throw new InvalidArgumentException('The unavailability start date time is invalid.');
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!validate_datetime($unavailability['end_datetime'])) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
throw new InvalidArgumentException('The unavailability end date time is invalid.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make the unavailability lasts longer than the minimum duration (in minutes).
|
|
|
|
|
$diff = (strtotime($unavailability['end_datetime']) - strtotime($unavailability['start_datetime'])) / 60;
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if ($diff < EVENT_MINIMUM_DURATION) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The unavailability duration cannot be less than ' . EVENT_MINIMUM_DURATION . ' minutes.',
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2021-11-06 16:55:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 17:25:45 +03:00
|
|
|
|
// Make sure the provider ID really exists in the database.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$count = $this->db
|
2021-11-06 16:55:36 +03:00
|
|
|
|
->select()
|
|
|
|
|
->from('users')
|
|
|
|
|
->join('roles', 'roles.id = users.id_roles', 'inner')
|
|
|
|
|
->where('users.id', $unavailability['id_users_provider'])
|
|
|
|
|
->where('roles.slug', DB_SLUG_PROVIDER)
|
|
|
|
|
->get()
|
|
|
|
|
->num_rows();
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$count) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The unavailability provider ID was not found in the database: ' . $unavailability['id_users_provider'],
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2021-11-06 16:55:36 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
/**
|
|
|
|
|
* Get all unavailabilities that match the provided criteria.
|
|
|
|
|
*
|
|
|
|
|
* @param array|string|null $where Where conditions.
|
|
|
|
|
* @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 unavailabilities.
|
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($order_by) {
|
|
|
|
|
$this->db->order_by($order_by);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$unavailabilities = $this->db
|
|
|
|
|
->get_where('appointments', ['is_unavailability' => true], $limit, $offset)
|
|
|
|
|
->result_array();
|
|
|
|
|
|
|
|
|
|
foreach ($unavailabilities as &$unavailability) {
|
|
|
|
|
$this->cast($unavailability);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $unavailabilities;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 16:55:36 +03:00
|
|
|
|
/**
|
|
|
|
|
* Insert a new unavailability into the database.
|
|
|
|
|
*
|
|
|
|
|
* @param array $unavailability Associative array with the unavailability data.
|
|
|
|
|
*
|
|
|
|
|
* @return int Returns the unavailability ID.
|
|
|
|
|
*
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
|
|
|
|
protected function insert(array $unavailability): int
|
|
|
|
|
{
|
|
|
|
|
$unavailability['book_datetime'] = date('Y-m-d H:i:s');
|
2022-01-25 01:42:13 +03:00
|
|
|
|
$unavailability['create_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
|
$unavailability['update_datetime'] = date('Y-m-d H:i:s');
|
2021-11-06 16:55:36 +03:00
|
|
|
|
$unavailability['hash'] = random_string('alnum', 12);
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$unavailability['is_unavailability'] = true;
|
2021-11-06 16:55:36 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$this->db->insert('appointments', $unavailability)) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
throw new RuntimeException('Could not insert unavailability.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->db->insert_id();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update an existing unavailability.
|
|
|
|
|
*
|
|
|
|
|
* @param array $unavailability Associative array with the unavailability data.
|
|
|
|
|
*
|
|
|
|
|
* @return int Returns the unavailability ID.
|
|
|
|
|
*
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
|
|
|
|
protected function update(array $unavailability): int
|
|
|
|
|
{
|
2022-01-25 01:42:13 +03:00
|
|
|
|
$unavailability['update_datetime'] = date('Y-m-d H:i:s');
|
2023-10-19 17:25:45 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$this->db->update('appointments', $unavailability, ['id' => $unavailability['id']])) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
throw new RuntimeException('Could not update unavailability record.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $unavailability['id'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove an existing unavailability from the database.
|
|
|
|
|
*
|
|
|
|
|
* @param int $unavailability_id Unavailability ID.
|
|
|
|
|
*
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
|
public function delete(int $unavailability_id): void
|
2021-11-06 16:55:36 +03:00
|
|
|
|
{
|
2023-10-19 17:25:45 +03:00
|
|
|
|
$this->db->delete('appointments', ['id' => $unavailability_id]);
|
2021-11-06 16:55:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a specific unavailability from the database.
|
|
|
|
|
*
|
|
|
|
|
* @param int $unavailability_id The ID of the record to be returned.
|
|
|
|
|
*
|
|
|
|
|
* @return array Returns an array with the unavailability data.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
|
public function find(int $unavailability_id): array
|
2021-11-06 16:55:36 +03:00
|
|
|
|
{
|
|
|
|
|
$unavailability = $this->db->get_where('appointments', ['id' => $unavailability_id])->row_array();
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$unavailability) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The provided unavailability ID was not found in the database: ' . $unavailability_id,
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2022-01-25 01:32:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 16:55:36 +03:00
|
|
|
|
$this->cast($unavailability);
|
|
|
|
|
|
|
|
|
|
return $unavailability;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a specific field value from the database.
|
|
|
|
|
*
|
|
|
|
|
* @param int $unavailability_id Unavailability ID.
|
|
|
|
|
* @param string $field Name of the value to be returned.
|
|
|
|
|
*
|
2023-01-14 11:39:14 +03:00
|
|
|
|
* @return mixed Returns the selected unavailability value from the database.
|
2021-11-06 16:55:36 +03:00
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
2023-01-14 11:39:14 +03:00
|
|
|
|
public function value(int $unavailability_id, string $field): mixed
|
2021-11-06 16:55:36 +03:00
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($field)) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
throw new InvalidArgumentException('The field argument is cannot be empty.');
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($unavailability_id)) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
throw new InvalidArgumentException('The unavailability ID argument cannot be empty.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check whether the unavailability exists.
|
|
|
|
|
$query = $this->db->get_where('appointments', ['id' => $unavailability_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 unavailability ID was not found in the database: ' . $unavailability_id,
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2021-11-06 16:55:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if the required field is part of the unavailability data.
|
|
|
|
|
$unavailability = $query->row_array();
|
|
|
|
|
|
|
|
|
|
$this->cast($unavailability);
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!array_key_exists($field, $unavailability)) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The requested field was not found in the unavailability data: ' . $field,
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2021-11-06 16:55:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $unavailability[$field];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the query builder interface, configured for use with the unavailabilities table.
|
|
|
|
|
*
|
|
|
|
|
* @return CI_DB_query_builder
|
|
|
|
|
*/
|
|
|
|
|
public function query(): CI_DB_query_builder
|
|
|
|
|
{
|
|
|
|
|
return $this->db->from('appointments');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Search unavailabilities by the provided keyword.
|
|
|
|
|
*
|
|
|
|
|
* @param string $keyword Search keyword.
|
|
|
|
|
* @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 unavailabilities.
|
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
public function search(string $keyword, int $limit = null, int $offset = null, string $order_by = null): array
|
2021-11-06 16:55:36 +03:00
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$unavailabilities = $this->db
|
2021-11-06 16:55:36 +03:00
|
|
|
|
->select()
|
|
|
|
|
->from('appointments')
|
|
|
|
|
->join('users AS providers', 'providers.id = appointments.id_users_provider', 'inner')
|
2023-11-29 12:24:09 +03:00
|
|
|
|
->where('is_unavailability', true)
|
2021-11-06 16:55:36 +03:00
|
|
|
|
->group_start()
|
|
|
|
|
->like('appointments.start_datetime', $keyword)
|
|
|
|
|
->or_like('appointments.end_datetime', $keyword)
|
|
|
|
|
->or_like('appointments.location', $keyword)
|
|
|
|
|
->or_like('appointments.hash', $keyword)
|
|
|
|
|
->or_like('appointments.notes', $keyword)
|
|
|
|
|
->or_like('providers.first_name', $keyword)
|
|
|
|
|
->or_like('providers.last_name', $keyword)
|
|
|
|
|
->or_like('providers.email', $keyword)
|
|
|
|
|
->or_like('providers.phone_number', $keyword)
|
|
|
|
|
->group_end()
|
|
|
|
|
->limit($limit)
|
|
|
|
|
->offset($offset)
|
|
|
|
|
->order_by($order_by)
|
|
|
|
|
->get()
|
|
|
|
|
->result_array();
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
foreach ($unavailabilities as &$unavailability) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
$this->cast($unavailability);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $unavailabilities;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-06 17:40:55 +03:00
|
|
|
|
* Load related resources to an unavailability.
|
2021-11-06 16:55:36 +03:00
|
|
|
|
*
|
|
|
|
|
* @param array $unavailability Associative array with the unavailability data.
|
|
|
|
|
* @param array $resources Resource names to be attached ("service", "provider", "customer" supported).
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
2021-11-06 17:40:55 +03:00
|
|
|
|
public function load(array &$unavailability, array $resources)
|
2021-11-06 16:55:36 +03:00
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($unavailability) || empty($resources)) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
foreach ($resources as $resource) {
|
|
|
|
|
$unavailability['provider'] = match ($resource) {
|
|
|
|
|
'provider' => $this->db
|
2023-03-13 11:06:18 +03:00
|
|
|
|
->get_where('users', [
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'id' => $unavailability['id_users_provider'] ?? ($unavailability['providerId'] ?? null),
|
2023-03-13 11:06:18 +03:00
|
|
|
|
])
|
|
|
|
|
->row_array(),
|
2023-11-29 12:24:09 +03:00
|
|
|
|
default => throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The requested unavailability relation is not supported: ' . $resource,
|
|
|
|
|
),
|
2023-03-13 11:06:18 +03:00
|
|
|
|
};
|
2021-11-06 16:55:36 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert the database unavailability record to the equivalent API resource.
|
|
|
|
|
*
|
|
|
|
|
* @param array $unavailability Unavailability data.
|
|
|
|
|
*/
|
|
|
|
|
public function api_encode(array &$unavailability)
|
|
|
|
|
{
|
|
|
|
|
$encoded_resource = [
|
2023-11-29 12:24:09 +03:00
|
|
|
|
'id' => array_key_exists('id', $unavailability) ? (int) $unavailability['id'] : null,
|
2021-11-06 16:55:36 +03:00
|
|
|
|
'book' => $unavailability['book_datetime'],
|
|
|
|
|
'start' => $unavailability['start_datetime'],
|
|
|
|
|
'end' => $unavailability['end_datetime'],
|
|
|
|
|
'hash' => $unavailability['hash'],
|
|
|
|
|
'location' => $unavailability['location'],
|
|
|
|
|
'notes' => $unavailability['notes'],
|
2023-11-29 12:24:09 +03:00
|
|
|
|
'providerId' =>
|
|
|
|
|
$unavailability['id_users_provider'] !== null ? (int) $unavailability['id_users_provider'] : null,
|
|
|
|
|
'googleCalendarId' =>
|
2023-12-22 13:35:41 +03:00
|
|
|
|
$unavailability['id_google_calendar'] !== null ? (int) $unavailability['id_google_calendar'] : null,
|
2021-11-06 16:55:36 +03:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$unavailability = $encoded_resource;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert the API resource to the equivalent database unavailability record.
|
|
|
|
|
*
|
|
|
|
|
* @param array $unavailability API resource.
|
|
|
|
|
* @param array|null $base Base unavailability data to be overwritten with the provided values (useful for updates).
|
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
public function api_decode(array &$unavailability, array $base = null)
|
2021-11-06 16:55:36 +03:00
|
|
|
|
{
|
|
|
|
|
$decoded_request = $base ?: [];
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('id', $unavailability)) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
$decoded_request['id'] = $unavailability['id'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('book', $unavailability)) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
$decoded_request['book_datetime'] = $unavailability['book'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('start', $unavailability)) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
$decoded_request['start_datetime'] = $unavailability['start'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('end', $unavailability)) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
$decoded_request['end_datetime'] = $unavailability['end'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('hash', $unavailability)) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
$decoded_request['hash'] = $unavailability['hash'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('location', $unavailability)) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
$decoded_request['location'] = $unavailability['location'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('notes', $unavailability)) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
$decoded_request['notes'] = $unavailability['notes'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('providerId', $unavailability)) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
$decoded_request['id_users_provider'] = $unavailability['providerId'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('googleCalendarId', $unavailability)) {
|
2021-11-06 16:55:36 +03:00
|
|
|
|
$decoded_request['id_google_calendar'] = $unavailability['googleCalendarId'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$decoded_request['is_unavailability'] = true;
|
2021-11-06 16:55:36 +03:00
|
|
|
|
|
|
|
|
|
$unavailability = $decoded_request;
|
|
|
|
|
}
|
|
|
|
|
}
|