easyappointments/application/models/Services_model.php

343 lines
11 KiB
PHP
Raw Normal View History

<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
* @link http://easyappointments.org
* @since v1.0.0
* ---------------------------------------------------------------------------- */
/**
2021-10-27 11:04:42 +03:00
* Services model
*
* Handles all the database operations of the service resource.
*
* @package Models
*/
class Services_model extends EA_Model {
/**
2021-10-27 11:04:42 +03:00
* Save (insert or update) a service.
*
2021-10-27 11:04:42 +03:00
* @param array $service Associative array with the service data.
*
2021-10-27 11:04:42 +03:00
* @return int Returns the service ID.
*
* @throws InvalidArgumentException
*/
2021-10-27 11:04:42 +03:00
public function save(array $service): int
{
$this->validate($service);
2021-10-27 11:04:42 +03:00
if (empty($service['id']))
{
2021-10-27 11:04:42 +03:00
return $this->insert($service);
2018-01-23 12:08:37 +03:00
}
else
{
2021-10-27 11:04:42 +03:00
return $this->update($service);
}
}
/**
2021-10-27 11:04:42 +03:00
* Validate the service data.
*
2021-10-27 11:04:42 +03:00
* @param array $service Associative array with the service data.
*
2021-10-27 11:04:42 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:04:42 +03:00
public function validate(array $service): void
{
2021-10-27 11:04:42 +03:00
// If a service ID is provided then check whether the record really exists in the database.
if ( ! empty($service['id']))
{
2021-10-27 11:04:42 +03:00
$count = $this->db->get_where('services', ['id' => $service['id']])->num_rows();
2021-10-27 11:04:42 +03:00
if ( ! $count)
{
2021-10-27 11:04:42 +03:00
throw new InvalidArgumentException('The provided service ID does not exist in the database: ' . $service['id']);
}
}
2021-10-27 11:04:42 +03:00
// Make sure all required fields are provided.
if (
empty($service['name'])
)
{
2021-10-27 11:04:42 +03:00
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($service, TRUE));
}
2021-10-27 11:04:42 +03:00
// If a category was provided then make sure it really exists in the database.
if ( ! empty($service['id_service_categories']))
{
2021-10-27 11:04:42 +03:00
$count = $this->db->get_where('service_categories', ['id' => $service['id_service_categories']])->num_rows();
2021-10-27 11:04:42 +03:00
if ( ! $count)
{
2021-10-27 11:04:42 +03:00
throw new InvalidArgumentException('The provided category ID was not found in the database: ' . $service['id_service_categories']);
}
}
2021-10-27 11:04:42 +03:00
// Make sure the duration value is valid.
if ( ! empty($service['duration']))
{
2021-10-27 11:04:42 +03:00
if ((int)$service['duration'] < EVENT_MINIMUM_DURATION)
{
2021-10-27 11:04:42 +03:00
throw new InvalidArgumentException('The service duration cannot be less than ' . EVENT_MINIMUM_DURATION . ' minutes long.');
}
}
// Availabilities type must have the correct value.
if ($service['availabilities_type'] !== NULL && $service['availabilities_type'] !== AVAILABILITIES_TYPE_FLEXIBLE
&& $service['availabilities_type'] !== AVAILABILITIES_TYPE_FIXED)
{
throw new Exception('Service availabilities type must be either ' . AVAILABILITIES_TYPE_FLEXIBLE
. ' or ' . AVAILABILITIES_TYPE_FIXED . ' (given ' . $service['availabilities_type'] . ')');
}
2021-10-27 11:04:42 +03:00
// Validate the availabilities type value.
if (
! empty($service['availabilities_type'])
&& ! in_array($service['availabilities_type'], [AVAILABILITIES_TYPE_FLEXIBLE, AVAILABILITIES_TYPE_FIXED])
)
{
2021-10-27 11:04:42 +03:00
throw new InvalidArgumentException('The provided availabilities type is invalid: ' . $service['availabilities_type']);
}
2021-10-27 11:04:42 +03:00
// Validate the attendants number value.
if (empty($service['attendants_number']) || (int)$service['attendants_number'] < 1)
{
2021-10-27 11:04:42 +03:00
throw new InvalidArgumentException('The provided attendants number is invalid: ' . $service['attendants_number']);
}
}
/**
2021-10-27 11:04:42 +03:00
* Insert a new service into the database.
*
2021-10-27 11:04:42 +03:00
* @param array $service Associative array with the service data.
*
2021-10-27 11:04:42 +03:00
* @return int Returns the service ID.
*
2021-10-27 11:04:42 +03:00
* @throws RuntimeException
*/
2021-10-27 11:04:42 +03:00
protected function insert(array $service): int
{
2021-10-27 11:04:42 +03:00
if ( ! $this->db->insert('services', $service))
{
2021-10-27 11:04:42 +03:00
throw new RuntimeException('Could not insert service.');
}
2021-10-27 11:04:42 +03:00
return $this->db->insert_id();
}
/**
2021-10-27 11:04:42 +03:00
* Update an existing service.
*
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.
*
2021-10-27 11:04:42 +03:00
* @throws RuntimeException
*/
2021-10-27 11:04:42 +03:00
protected function update(array $service): int
{
2021-10-27 11:04:42 +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.');
}
2021-10-27 11:04:42 +03:00
return $service['id'];
}
/**
2021-10-27 11:04:42 +03:00
* Remove an existing service from the database.
*
2021-10-27 11:04:42 +03:00
* @param int $service_id Service ID.
*
2021-10-27 11:04:42 +03:00
* @throws RuntimeException
*/
2021-10-27 11:04:42 +03:00
public function delete(int $service_id): void
{
2021-10-27 11:04:42 +03:00
if ( ! $this->db->delete('services', ['id' => $service_id]))
{
2021-10-27 11:04:42 +03:00
throw new RuntimeException('Could not delete service.');
}
}
/**
2021-10-27 11:04:42 +03:00
* Get a specific service from the database.
*
2021-10-27 11:04:42 +03:00
* @param int $service_id The ID of the record to be returned.
*
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
*/
2021-10-27 11:04:42 +03:00
public function find(int $service_id): array
{
2021-10-27 11:04:42 +03:00
if ( ! $this->db->get_where('services', ['id' => $service_id])->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);
}
2021-10-27 11:04:42 +03:00
return $this->db->get_where('services', ['id' => $service_id])->row_array();
}
/**
* 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.
*
2021-10-27 11:04:42 +03:00
* @return string 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
*/
2021-10-27 11:04:42 +03:00
public function value(int $service_id, string $field): string
{
2021-10-27 11:04:42 +03:00
if (empty($field))
{
2021-10-27 11:04:42 +03:00
throw new InvalidArgumentException('The field argument is cannot be empty.');
}
2021-10-27 11:04:42 +03:00
if (empty($service_id))
{
2021-10-27 11:04:42 +03:00
throw new InvalidArgumentException('The service ID argument cannot be empty.');
}
2021-10-27 11:04:42 +03:00
// Check whether the service exists.
$query = $this->db->get_where('services', ['id' => $service_id]);
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);
}
2021-10-27 11:04:42 +03:00
// Check if the required field is part of the service data.
$service = $query->row_array();
2021-10-27 11:04:42 +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);
}
2021-10-27 11:04:42 +03:00
return $service[$field];
}
/**
2021-10-27 11:04:42 +03:00
* Get all services that match the provided criteria.
*
2021-10-27 11:04:42 +03:00
* @param array|string $where Where conditions
* @param int|null $limit Record limit.
* @param int|null $offset Record offset.
* @param string|null $order_by Order by.
2020-11-16 11:41:04 +03:00
*
2021-10-27 11:04:42 +03:00
* @return array Returns an array of services.
*/
2021-10-27 11:04:42 +03:00
public function get($where = NULL, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
{
if ($where !== NULL)
{
$this->db->where($where);
}
if ($order_by !== NULL)
{
$this->db->order_by($order_by);
}
return $this->db->get('services', $limit, $offset)->result_array();
}
/**
2021-10-27 11:04:42 +03:00
* Get all the service records that are assigned to at least one provider.
*
2021-10-27 11:04:42 +03:00
* @return array Returns an array of services.
*/
2021-10-27 11:04:42 +03:00
public function get_available_services(): array
{
2021-10-27 11:04:42 +03:00
return $this
->db
->distinct()
->select('services.*, service_categories.name AS category_name, service_categories.id AS category_id')
->from('services')
2021-10-27 11:04:42 +03:00
->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')
2021-10-27 11:04:42 +03:00
->get()
->result_array();
}
/**
2021-10-27 11:04:42 +03:00
* Get the query builder interface, configured for use with the services table.
*
2021-10-27 11:04:42 +03:00
* @return CI_DB_query_builder
*/
2021-10-27 11:04:42 +03:00
public function query(): CI_DB_query_builder
{
2021-10-27 11:04:42 +03:00
return $this->db->from('services');
}
/**
2021-10-27 11:04:42 +03:00
* Search services by the provided keyword.
*
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.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:42 +03:00
* @return array Returns an array of services.
*/
2021-10-27 11:04:42 +03:00
public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
{
2021-10-27 11:04:42 +03:00
return $this
->db
->select()
->from('services')
->like('name', $keyword)
->or_like('description', $keyword)
->limit($limit)
->offset($offset)
->order_by($order_by)
->get()
->result_array();
}
/**
2021-10-27 11:04:42 +03:00
* Attach related resources to a service.
*
2021-10-27 11:04:42 +03:00
* @param array $service Associative array with the service data.
* @param array $resources Resource names to be attached ("service_category" supported).
2020-11-16 11:41:04 +03:00
*
2021-10-27 11:04:42 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:04:42 +03:00
public function attach(array &$service, array $resources): void
{
2021-10-27 11:04:42 +03:00
if (empty($service) || empty($resources))
{
2021-10-27 11:04:42 +03:00
return;
}
2021-10-27 11:04:42 +03:00
foreach ($resources as $resource)
{
2021-10-27 11:04:42 +03:00
switch ($resource)
{
case 'service_category':
$service['service_category'] = $this
->db
->get_where('service_categories', [
'id' => $service['id_service_categories']
])
->row_array();
break;
default:
throw new InvalidArgumentException('The requested appointment relation is not supported: ' . $resource);
}
}
}
}