2020-04-22 22:48:56 +03:00
|
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2024-01-01 01:14:38 +03:00
|
|
|
|
* MaketRandevu - MAKET Randevu Portalı
|
2016-01-09 23:29:28 +02: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
|
|
|
|
|
* @link https://easyappointments.org
|
2016-01-09 23:29:28 +02:00
|
|
|
|
* @since v1.0.0
|
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-06 18:21:27 +03:00
|
|
|
|
* Services model.
|
2021-10-27 11:04:42 +03:00
|
|
|
|
*
|
|
|
|
|
* Handles all the database operations of the service resource.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
|
|
|
|
* @package Models
|
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
class Services_model extends EA_Model
|
|
|
|
|
{
|
2021-10-29 13:39:27 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
protected array $casts = [
|
2021-10-29 13:39:27 +03:00
|
|
|
|
'id' => 'integer',
|
|
|
|
|
'price' => 'float',
|
|
|
|
|
'attendants_number' => 'integer',
|
2022-01-24 22:50:14 +03:00
|
|
|
|
'is_private' => 'boolean',
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'id_service_categories' => 'integer',
|
2021-10-29 13:39:27 +03:00
|
|
|
|
];
|
|
|
|
|
|
2021-11-06 17:02:40 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
protected array $api_resource = [
|
2021-11-06 17:02:40 +03:00
|
|
|
|
'id' => 'id',
|
|
|
|
|
'name' => 'name',
|
|
|
|
|
'duration' => 'duration',
|
|
|
|
|
'price' => 'price',
|
|
|
|
|
'currency' => 'currency',
|
|
|
|
|
'description' => 'description',
|
|
|
|
|
'location' => 'location',
|
2022-01-18 20:22:24 +03:00
|
|
|
|
'color' => 'color',
|
2021-11-06 17:02:40 +03:00
|
|
|
|
'availabilitiesType' => 'availabilities_type',
|
|
|
|
|
'attendantsNumber' => 'attendants_number',
|
2022-01-24 22:50:14 +03:00
|
|
|
|
'isPrivate' => 'is_private',
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'serviceCategoryId' => 'id_service_categories',
|
2021-11-06 17:02:40 +03:00
|
|
|
|
];
|
|
|
|
|
|
2020-11-12 15:51:10 +03:00
|
|
|
|
/**
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* Save (insert or update) a service.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @param array $service Associative array with the service data.
|
2016-04-27 09:21:40 +03:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @return int Returns the service ID.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-10-27 11:04:42 +03:00
|
|
|
|
public function save(array $service): int
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
|
$this->validate($service);
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($service['id'])) {
|
2021-10-27 11:04:42 +03:00
|
|
|
|
return $this->insert($service);
|
2023-11-29 12:24:09 +03:00
|
|
|
|
} else {
|
2021-10-27 11:04:42 +03:00
|
|
|
|
return $this->update($service);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* Validate the service data.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @param array $service Associative array with the service data.
|
2016-04-27 09:21:40 +03:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-10-27 11:49:42 +03:00
|
|
|
|
public function validate(array $service)
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2021-10-27 11:04:42 +03:00
|
|
|
|
// If a service ID is provided then check whether the record really exists in the database.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!empty($service['id'])) {
|
2021-10-27 11:04:42 +03:00
|
|
|
|
$count = $this->db->get_where('services', ['id' => $service['id']])->num_rows();
|
2020-11-12 15:51:10 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$count) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The provided service ID does not exist in the database: ' . $service['id'],
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:42 +03:00
|
|
|
|
// Make sure all required fields are provided.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($service['name'])) {
|
|
|
|
|
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($service, true));
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
// If a category was provided then make sure it really exists in the database.
|
|
|
|
|
if (!empty($service['id_service_categories'])) {
|
2023-10-26 05:54:13 +03:00
|
|
|
|
$count = $this->db->get_where('categories', ['id' => $service['id_service_categories']])->num_rows();
|
2021-03-25 13:32:10 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$count) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The provided category ID was not found in the database: ' . $service['id_service_categories'],
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2021-03-25 13:32:10 +03:00
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
// Make sure the duration value is valid.
|
|
|
|
|
if (!empty($service['duration'])) {
|
|
|
|
|
if ((int) $service['duration'] < EVENT_MINIMUM_DURATION) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The service duration cannot be less than ' . EVENT_MINIMUM_DURATION . ' minutes long.',
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 13:11:51 +03:00
|
|
|
|
// Availabilities type must have the correct value.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (
|
|
|
|
|
$service['availabilities_type'] !== null &&
|
|
|
|
|
$service['availabilities_type'] !== AVAILABILITIES_TYPE_FLEXIBLE &&
|
|
|
|
|
$service['availabilities_type'] !== AVAILABILITIES_TYPE_FIXED
|
|
|
|
|
) {
|
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
|
'Service availabilities type must be either ' .
|
|
|
|
|
AVAILABILITIES_TYPE_FLEXIBLE .
|
|
|
|
|
' or ' .
|
|
|
|
|
AVAILABILITIES_TYPE_FIXED .
|
|
|
|
|
' (given ' .
|
|
|
|
|
$service['availabilities_type'] .
|
2023-12-22 13:35:41 +03:00
|
|
|
|
')',
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2016-07-20 22:19:52 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:42 +03:00
|
|
|
|
// Validate the availabilities type value.
|
|
|
|
|
if (
|
2023-11-29 12:24:09 +03:00
|
|
|
|
!empty($service['availabilities_type']) &&
|
|
|
|
|
!in_array($service['availabilities_type'], [AVAILABILITIES_TYPE_FLEXIBLE, AVAILABILITIES_TYPE_FIXED])
|
|
|
|
|
) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The provided availabilities type is invalid: ' . $service['availabilities_type'],
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2016-07-21 21:33:43 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
// Validate the attendants number value.
|
|
|
|
|
if (empty($service['attendants_number']) || (int) $service['attendants_number'] < 1) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The provided attendants number is invalid: ' . $service['attendants_number'],
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* Insert a new service into the database.
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @param array $service Associative array with the service data.
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @return int Returns the service ID.
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @throws RuntimeException
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*/
|
2021-10-27 11:04:42 +03:00
|
|
|
|
protected function insert(array $service): int
|
2020-04-22 22:48:56 +03:00
|
|
|
|
{
|
2022-01-25 01:42:13 +03:00
|
|
|
|
$service['create_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
|
$service['update_datetime'] = date('Y-m-d H:i:s');
|
2022-06-20 12:31:11 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$this->db->insert('services', $service)) {
|
2021-10-27 11:04:42 +03:00
|
|
|
|
throw new RuntimeException('Could not insert service.');
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:42 +03:00
|
|
|
|
return $this->db->insert_id();
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-09 23:29:28 +02:00
|
|
|
|
/**
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* Update an existing service.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @param array $service Associative array with the service data.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @return int Returns the service ID.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @throws RuntimeException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-10-27 11:04:42 +03:00
|
|
|
|
protected function update(array $service): int
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2022-01-25 01:42:13 +03:00
|
|
|
|
$service['update_datetime'] = date('Y-m-d H:i:s');
|
2022-06-20 12:31:11 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$this->db->update('services', $service, ['id' => $service['id']])) {
|
2021-10-27 11:04:42 +03:00
|
|
|
|
throw new RuntimeException('Could not update service.');
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:42 +03:00
|
|
|
|
return $service['id'];
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* Remove an existing service from the database.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @param int $service_id Service ID.
|
2016-04-27 09:21:40 +03:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @throws RuntimeException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2023-11-03 20:24:49 +03:00
|
|
|
|
public function delete(int $service_id): void
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2023-11-03 20:24:49 +03:00
|
|
|
|
$this->db->delete('services', ['id' => $service_id]);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* Get a specific service from the database.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @param int $service_id The ID of the record to be returned.
|
2016-04-27 09:21:40 +03:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @return array Returns an array with the service data.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2023-11-03 20:24:49 +03:00
|
|
|
|
public function find(int $service_id): array
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2021-10-29 13:39:27 +03:00
|
|
|
|
$service = $this->db->get_where('services', ['id' => $service_id])->row_array();
|
2022-01-24 22:50:14 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$service) {
|
2022-01-25 01:32:42 +03:00
|
|
|
|
throw new InvalidArgumentException('The provided service ID was not found in the database: ' . $service_id);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 22:50:14 +03:00
|
|
|
|
$this->cast($service);
|
|
|
|
|
|
2021-10-29 13:39:27 +03:00
|
|
|
|
return $service;
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a specific field value from the database.
|
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @param int $service_id Service ID.
|
|
|
|
|
* @param string $field Name of the value to be returned.
|
2016-04-27 09:21:40 +03:00
|
|
|
|
*
|
2023-01-14 11:39:14 +03:00
|
|
|
|
* @return mixed Returns the selected service value from the database.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2023-01-14 11:39:14 +03:00
|
|
|
|
public function value(int $service_id, string $field): mixed
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($field)) {
|
2021-10-27 11:04:42 +03:00
|
|
|
|
throw new InvalidArgumentException('The field argument is cannot be empty.');
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($service_id)) {
|
2021-10-27 11:04:42 +03:00
|
|
|
|
throw new InvalidArgumentException('The service ID argument cannot be empty.');
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:42 +03:00
|
|
|
|
// Check whether the service exists.
|
|
|
|
|
$query = $this->db->get_where('services', ['id' => $service_id]);
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$query->num_rows()) {
|
2021-10-27 11:04:42 +03:00
|
|
|
|
throw new InvalidArgumentException('The provided service ID was not found in the database: ' . $service_id);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:42 +03:00
|
|
|
|
// Check if the required field is part of the service data.
|
|
|
|
|
$service = $query->row_array();
|
2022-01-24 22:50:14 +03:00
|
|
|
|
|
2021-10-29 13:39:27 +03:00
|
|
|
|
$this->cast($service);
|
2020-12-05 12:43:38 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!array_key_exists($field, $service)) {
|
2021-10-27 11:04:42 +03:00
|
|
|
|
throw new InvalidArgumentException('The requested field was not found in the service data: ' . $field);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:42 +03:00
|
|
|
|
return $service[$field];
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-11-29 12:24:09 +03:00
|
|
|
|
* Get all the service records that are assigned to at least one provider.
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*
|
2023-11-29 12:24:09 +03:00
|
|
|
|
* @param bool $without_private Only include the public services.
|
2022-06-20 12:31:11 +03:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @return array Returns an array of services.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
public function get_available_services(bool $without_private = false): array
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if ($without_private) {
|
|
|
|
|
$this->db->where('services.is_private', false);
|
2020-04-06 21:34:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$services = $this->db
|
|
|
|
|
->distinct()
|
|
|
|
|
->select(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'services.*, service_categories.name AS service_category_name, service_categories.id AS service_category_id',
|
2023-11-29 12:24:09 +03:00
|
|
|
|
)
|
|
|
|
|
->from('services')
|
|
|
|
|
->join('services_providers', 'services_providers.id_services = services.id', 'inner')
|
|
|
|
|
->join('service_categories', 'service_categories.id = services.id_service_categories', 'left')
|
|
|
|
|
->order_by('name ASC')
|
|
|
|
|
->get()
|
|
|
|
|
->result_array();
|
2022-01-24 22:50:14 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
foreach ($services as &$service) {
|
2021-10-29 13:39:27 +03:00
|
|
|
|
$this->cast($service);
|
|
|
|
|
}
|
2022-01-24 22:50:14 +03:00
|
|
|
|
|
2021-10-29 13:39:27 +03:00
|
|
|
|
return $services;
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-11-29 12:24:09 +03:00
|
|
|
|
* Get all services that match the provided criteria.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2023-11-29 12:24:09 +03:00
|
|
|
|
* @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.
|
2022-01-24 22:50:14 +03:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @return array Returns an array of services.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
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);
|
2022-01-24 22:50:14 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if ($order_by !== null) {
|
|
|
|
|
$this->db->order_by($order_by);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$services = $this->db->get('services', $limit, $offset)->result_array();
|
2021-10-29 13:39:27 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
foreach ($services as &$service) {
|
2021-10-29 13:39:27 +03:00
|
|
|
|
$this->cast($service);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $services;
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* Get the query builder interface, configured for use with the services table.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @return CI_DB_query_builder
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-10-27 11:04:42 +03:00
|
|
|
|
public function query(): CI_DB_query_builder
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2021-10-27 11:04:42 +03:00
|
|
|
|
return $this->db->from('services');
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* Search services by the provided keyword.
|
2016-04-27 09:21:40 +03:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @param string $keyword Search keyword.
|
|
|
|
|
* @param int|null $limit Record limit.
|
|
|
|
|
* @param int|null $offset Record offset.
|
|
|
|
|
* @param string|null $order_by Order by.
|
2022-06-20 12:31:11 +03:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @return array Returns an array of services.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
public function search(string $keyword, int $limit = null, int $offset = null, string $order_by = null): array
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$services = $this->db
|
2021-10-27 11:04:42 +03:00
|
|
|
|
->select()
|
|
|
|
|
->from('services')
|
2022-07-07 16:27:10 +03:00
|
|
|
|
->group_start()
|
2021-10-27 11:04:42 +03:00
|
|
|
|
->like('name', $keyword)
|
|
|
|
|
->or_like('description', $keyword)
|
2022-07-07 16:27:10 +03:00
|
|
|
|
->group_end()
|
2021-10-27 11:04:42 +03:00
|
|
|
|
->limit($limit)
|
|
|
|
|
->offset($offset)
|
|
|
|
|
->order_by($order_by)
|
|
|
|
|
->get()
|
|
|
|
|
->result_array();
|
2022-01-24 22:50:14 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
foreach ($services as &$service) {
|
2021-10-29 13:39:27 +03:00
|
|
|
|
$this->cast($service);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $services;
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-06 17:40:55 +03:00
|
|
|
|
* Load related resources to a service.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @param array $service Associative array with the service data.
|
2021-12-15 10:00:48 +03:00
|
|
|
|
* @param array $resources Resource names to be attached ("category" supported).
|
2020-11-16 11:41:04 +03:00
|
|
|
|
*
|
2021-10-27 11:04:42 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-11-06 17:40:55 +03:00
|
|
|
|
public function load(array &$service, array $resources)
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($service) || empty($resources)) {
|
2021-10-27 11:04:42 +03:00
|
|
|
|
return;
|
2017-09-15 14:36:37 +03:00
|
|
|
|
}
|
2020-04-06 21:34:32 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
foreach ($resources as $resource) {
|
|
|
|
|
$service['category'] = match ($resource) {
|
|
|
|
|
'category' => $this->db
|
2023-03-13 11:06:18 +03:00
|
|
|
|
->get_where('categories', [
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'id' => $service['id_service_categories'] ?? ($service['serviceCategoryId'] ?? 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 appointment relation is not supported: ' . $resource,
|
|
|
|
|
),
|
2023-03-13 11:06:18 +03:00
|
|
|
|
};
|
2020-04-06 21:34:32 +03:00
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
2021-11-05 11:32:34 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert the database service record to the equivalent API resource.
|
|
|
|
|
*
|
|
|
|
|
* @param array $service Service data.
|
|
|
|
|
*/
|
|
|
|
|
public function api_encode(array &$service)
|
|
|
|
|
{
|
|
|
|
|
$encoded_resource = [
|
2023-11-29 12:24:09 +03:00
|
|
|
|
'id' => array_key_exists('id', $service) ? (int) $service['id'] : null,
|
2021-11-05 11:32:34 +03:00
|
|
|
|
'name' => $service['name'],
|
2023-11-29 12:24:09 +03:00
|
|
|
|
'duration' => (int) $service['duration'],
|
|
|
|
|
'price' => (float) $service['price'],
|
2021-11-05 11:32:34 +03:00
|
|
|
|
'currency' => $service['currency'],
|
|
|
|
|
'description' => $service['description'],
|
|
|
|
|
'location' => $service['location'],
|
|
|
|
|
'availabilitiesType' => $service['availabilities_type'],
|
2023-11-29 12:24:09 +03:00
|
|
|
|
'attendantsNumber' => (int) $service['attendants_number'],
|
|
|
|
|
'serviceCategoryId' =>
|
2023-12-22 13:35:41 +03:00
|
|
|
|
$service['id_service_categories'] !== null ? (int) $service['id_service_categories'] : null,
|
2021-11-05 11:32:34 +03:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$service = $encoded_resource;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert the API resource to the equivalent database service record.
|
|
|
|
|
*
|
|
|
|
|
* @param array $service API resource.
|
|
|
|
|
* @param array|null $base Base service data to be overwritten with the provided values (useful for updates).
|
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
public function api_decode(array &$service, array $base = null)
|
2021-11-05 11:32:34 +03:00
|
|
|
|
{
|
|
|
|
|
$decoded_resource = $base ?: [];
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('id', $service)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['id'] = $service['id'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('name', $service)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['name'] = $service['name'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('duration', $service)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['duration'] = $service['duration'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('price', $service)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['price'] = $service['price'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('currency', $service)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['currency'] = $service['currency'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('description', $service)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['description'] = $service['description'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('location', $service)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['location'] = $service['location'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('availabilitiesType', $service)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['availabilities_type'] = $service['availabilitiesType'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('attendantsNumber', $service)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['attendants_number'] = $service['attendantsNumber'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('serviceCategoryId', $service)) {
|
2023-10-26 05:54:13 +03:00
|
|
|
|
$decoded_resource['id_service_categories'] = $service['serviceCategoryId'];
|
2021-11-05 11:32:34 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$service = $decoded_resource;
|
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|