2023-10-26 05:54:13 +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
|
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Service-Categories model.
|
|
|
|
*
|
|
|
|
* Handles all the database operations of the service-category resource.
|
|
|
|
*
|
|
|
|
* @package Models
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Service_categories_model extends EA_Model
|
|
|
|
{
|
2023-10-26 05:54:13 +03:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected array $casts = [
|
2023-12-22 13:35:41 +03:00
|
|
|
'id' => 'integer',
|
2023-10-26 05:54:13 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected array $api_resource = [
|
|
|
|
'id' => 'id',
|
|
|
|
'name' => 'name',
|
2023-12-22 13:35:41 +03:00
|
|
|
'description' => 'description',
|
2023-10-26 05:54:13 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save (insert or update) a service-category.
|
|
|
|
*
|
|
|
|
* @param array $service_category Associative array with the service-category data.
|
|
|
|
*
|
|
|
|
* @return int Returns the service-category ID.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function save(array $service_category): int
|
|
|
|
{
|
|
|
|
$this->validate($service_category);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($service_category['id'])) {
|
2023-10-26 05:54:13 +03:00
|
|
|
return $this->insert($service_category);
|
2023-11-29 12:24:09 +03:00
|
|
|
} else {
|
2023-10-26 05:54:13 +03:00
|
|
|
return $this->update($service_category);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the service-category data.
|
|
|
|
*
|
|
|
|
* @param array $service_category Associative array with the service-category data.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function validate(array $service_category): void
|
2023-10-26 05:54:13 +03:00
|
|
|
{
|
|
|
|
// If a service-category ID is provided then check whether the record really exists in the database.
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($service_category['id'])) {
|
2023-10-26 05:54:13 +03:00
|
|
|
$count = $this->db->get_where('service_categories', ['id' => $service_category['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 service-category ID does not exist in the database: ' . $service_category['id'],
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2023-10-26 05:54:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure all required fields are provided.
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($service_category['name'])) {
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
'Not all required fields are provided: ' . print_r($service_category, true),
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2023-10-26 05:54:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new service-category into the database.
|
|
|
|
*
|
|
|
|
* @param array $service_category Associative array with the service-category data.
|
|
|
|
*
|
|
|
|
* @return int Returns the service-category ID.
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
|
|
|
protected function insert(array $service_category): int
|
|
|
|
{
|
|
|
|
$service_category['create_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
$service_category['update_datetime'] = date('Y-m-d H:i:s');
|
2023-11-29 12:24:09 +03:00
|
|
|
|
|
|
|
if (!$this->db->insert('service_categories', $service_category)) {
|
2023-10-26 05:54:13 +03:00
|
|
|
throw new RuntimeException('Could not insert service-category.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->db->insert_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an existing service-category.
|
|
|
|
*
|
|
|
|
* @param array $service_category Associative array with the service-category data.
|
|
|
|
*
|
|
|
|
* @return int Returns the service-category ID.
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
|
|
|
protected function update(array $service_category): int
|
|
|
|
{
|
|
|
|
$service_category['update_datetime'] = date('Y-m-d H:i:s');
|
2023-11-29 12:24:09 +03:00
|
|
|
|
|
|
|
if (!$this->db->update('service_categories', $service_category, ['id' => $service_category['id']])) {
|
2023-10-26 05:54:13 +03:00
|
|
|
throw new RuntimeException('Could not update service categories.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $service_category['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an existing service-category from the database.
|
|
|
|
*
|
2023-10-26 07:07:24 +03:00
|
|
|
* @param int $service_category_id Service-Category ID.
|
2023-10-26 05:54:13 +03:00
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
2023-11-03 20:24:49 +03:00
|
|
|
public function delete(int $service_category_id): void
|
2023-10-26 05:54:13 +03:00
|
|
|
{
|
2023-11-03 20:24:49 +03:00
|
|
|
$this->db->delete('service_categories', ['id' => $service_category_id]);
|
2023-10-26 05:54:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific service-category from the database.
|
|
|
|
*
|
2023-10-26 07:07:24 +03:00
|
|
|
* @param int $service_category_id The ID of the record to be returned.
|
2023-10-26 05:54:13 +03:00
|
|
|
*
|
|
|
|
* @return array Returns an array with the service-category data.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2023-11-03 20:24:49 +03:00
|
|
|
public function find(int $service_category_id): array
|
2023-10-26 05:54:13 +03:00
|
|
|
{
|
2023-10-26 07:07:24 +03:00
|
|
|
$service_category = $this->db->get_where('service_categories', ['id' => $service_category_id])->row_array();
|
2023-10-26 05:54:13 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$service_category) {
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
'The provided service-category ID was not found in the database: ' . $service_category_id,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2023-10-26 05:54:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->cast($service_category);
|
|
|
|
|
|
|
|
return $service_category;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific field value from the database.
|
|
|
|
*
|
2023-10-26 07:07:24 +03:00
|
|
|
* @param int $service_category_id Service-Category ID.
|
2023-10-26 05:54:13 +03:00
|
|
|
* @param string $field Name of the value to be returned.
|
|
|
|
*
|
|
|
|
* @return mixed Returns the selected service-category value from the database.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2023-10-26 07:07:24 +03:00
|
|
|
public function value(int $service_category_id, string $field): mixed
|
2023-10-26 05:54:13 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($field)) {
|
2023-10-26 05:54:13 +03:00
|
|
|
throw new InvalidArgumentException('The field argument is cannot be empty.');
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($service_category_id)) {
|
2023-10-26 05:54:13 +03:00
|
|
|
throw new InvalidArgumentException('The service-category ID argument cannot be empty.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the service exists.
|
2023-10-26 07:07:24 +03:00
|
|
|
$query = $this->db->get_where('service_categories', ['id' => $service_category_id]);
|
2023-10-26 05:54:13 +03:00
|
|
|
|
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 service-category ID was not found in the database: ' . $service_category_id,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2023-10-26 05:54:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the required field is part of the service-category data.
|
|
|
|
$service_category = $query->row_array();
|
|
|
|
|
|
|
|
$this->cast($service_category);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!array_key_exists($field, $service_category)) {
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
'The requested field was not found in the service-category data: ' . $field,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2023-10-26 05:54:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $service_category[$field];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the query builder interface, configured for use with the service categories table.
|
|
|
|
*
|
|
|
|
* @return CI_DB_query_builder
|
|
|
|
*/
|
|
|
|
public function query(): CI_DB_query_builder
|
|
|
|
{
|
|
|
|
return $this->db->from('service_categories');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search service categories 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 service categories.
|
|
|
|
*/
|
2024-12-19 21:13:51 +03:00
|
|
|
public function search(string $keyword, ?int $limit = null, ?int $offset = null, ?string $order_by = null): array
|
2023-10-26 05:54:13 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
$service_categories = $this->db
|
2023-10-26 05:54:13 +03:00
|
|
|
->select()
|
|
|
|
->from('service_categories')
|
|
|
|
->group_start()
|
|
|
|
->like('name', $keyword)
|
|
|
|
->or_like('description', $keyword)
|
|
|
|
->group_end()
|
|
|
|
->limit($limit)
|
|
|
|
->offset($offset)
|
|
|
|
->order_by($order_by)
|
|
|
|
->get()
|
|
|
|
->result_array();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($service_categories as &$service_category) {
|
|
|
|
$this->cast($service_category);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $service_categories;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all services 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 service categories.
|
|
|
|
*/
|
|
|
|
public function get(
|
2024-12-19 21:13:51 +03:00
|
|
|
array|string|null $where = null,
|
|
|
|
?int $limit = null,
|
|
|
|
?int $offset = null,
|
|
|
|
?string $order_by = null,
|
2023-11-29 12:24:09 +03:00
|
|
|
): array {
|
|
|
|
if ($where !== null) {
|
|
|
|
$this->db->where($where);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($order_by !== null) {
|
|
|
|
$this->db->order_by($order_by);
|
|
|
|
}
|
|
|
|
|
|
|
|
$service_categories = $this->db->get('service_categories', $limit, $offset)->result_array();
|
|
|
|
|
|
|
|
foreach ($service_categories as &$service_category) {
|
2023-10-26 05:54:13 +03:00
|
|
|
$this->cast($service_category);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $service_categories;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load related resources to a service-category.
|
|
|
|
*
|
|
|
|
* @param array $service_category Associative array with the service-category data.
|
|
|
|
* @param array $resources Resource names to be attached.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function load(array &$service_category, array $resources)
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
// Service categories do not currently have any related resources.
|
2023-10-26 05:54:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the database service-category record to the equivalent API resource.
|
|
|
|
*
|
|
|
|
* @param array $service_category Category data.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function api_encode(array &$service_category): void
|
2023-10-26 05:54:13 +03:00
|
|
|
{
|
|
|
|
$encoded_resource = [
|
2023-11-29 12:24:09 +03:00
|
|
|
'id' => array_key_exists('id', $service_category) ? (int) $service_category['id'] : null,
|
2023-10-26 05:54:13 +03:00
|
|
|
'name' => $service_category['name'],
|
2023-11-29 12:24:09 +03:00
|
|
|
'description' => array_key_exists('description', $service_category)
|
|
|
|
? $service_category['description']
|
2023-12-22 13:35:41 +03:00
|
|
|
: null,
|
2023-10-26 05:54:13 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$service_category = $encoded_resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the API resource to the equivalent database service-category record.
|
|
|
|
*
|
|
|
|
* @param array $service_category API resource.
|
|
|
|
* @param array|null $base Base service-category data to be overwritten with the provided values (useful for updates).
|
|
|
|
*/
|
2024-12-19 21:13:51 +03:00
|
|
|
public function api_decode(array &$service_category, ?array $base = null): void
|
2023-10-26 05:54:13 +03:00
|
|
|
{
|
|
|
|
$decoded_resource = $base ?: [];
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('id', $service_category)) {
|
2023-10-26 05:54:13 +03:00
|
|
|
$decoded_resource['id'] = $service_category['id'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('name', $service_category)) {
|
2023-10-26 05:54:13 +03:00
|
|
|
$decoded_resource['name'] = $service_category['name'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('description', $service_category)) {
|
2023-10-26 05:54:13 +03:00
|
|
|
$decoded_resource['description'] = $service_category['description'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$service_category = $decoded_resource;
|
|
|
|
}
|
|
|
|
}
|