easyappointments/application/models/Consents_model.php

266 lines
7.3 KiB
PHP
Raw Normal View History

<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
2022-01-18 15:05:42 +03:00
* Easy!Appointments - Online Appointment Scheduler
*
* @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
* @since v1.3.2
* ---------------------------------------------------------------------------- */
/**
2021-11-06 18:21:27 +03:00
* Consents model.
2021-10-27 11:04:09 +03:00
*
* Handles all the database operations of the consent resource.
*
* @package Models
*/
class Consents_model extends EA_Model
{
/**
* @var array
*/
2023-03-13 11:06:18 +03:00
protected array $casts = [
'id' => 'integer',
];
/**
2021-10-27 11:04:09 +03:00
* Save (insert or update) a consent.
*
2021-10-27 11:04:09 +03:00
* @param array $consent Associative array with the consent data.
*
* @return int Returns the consent ID.
*
2021-10-27 11:04:09 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:04:09 +03:00
public function save(array $consent): int
{
$this->validate($consent);
if (empty($consent['id'])) {
2021-10-27 11:04:09 +03:00
return $this->insert($consent);
} else {
2021-10-27 11:04:09 +03:00
return $this->update($consent);
}
}
/**
2021-10-27 11:04:09 +03:00
* Validate the consent data.
*
2021-10-27 11:04:09 +03:00
* @param array $consent Associative array with the consent data.
*
2021-10-27 11:04:09 +03:00
* @throws InvalidArgumentException
*/
2024-04-26 17:36:14 +03:00
public function validate(array $consent): void
{
if (empty($consent['ip']) || empty($consent['type'])) {
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($consent, true));
}
}
/**
2021-10-27 11:04:09 +03:00
* Insert a new consent into the database.
*
2021-10-27 11:04:09 +03:00
* @param array $consent Associative array with the consent data.
*
2021-10-27 11:04:09 +03:00
* @return int Returns the consent ID.
*
2021-10-27 11:04:09 +03:00
* @throws RuntimeException
*/
2021-10-27 11:04:09 +03:00
protected function insert(array $consent): int
{
$consent['create_datetime'] = date('Y-m-d H:i:s');
$consent['update_datetime'] = date('Y-m-d H:i:s');
if (!$this->db->insert('consents', $consent)) {
2021-10-27 11:04:09 +03:00
throw new RuntimeException('Could not insert consent.');
}
2021-10-27 11:04:09 +03:00
return $this->db->insert_id();
}
/**
2021-10-27 11:04:09 +03:00
* Update an existing consent.
*
2021-10-27 11:04:09 +03:00
* @param array $consent Associative array with the consent data.
*
2021-10-27 11:04:09 +03:00
* @return int Returns the consent ID.
*
2021-10-27 11:04:09 +03:00
* @throws RuntimeException
*/
2021-10-27 11:04:09 +03:00
protected function update(array $consent): int
{
$consent['update_datetime'] = date('Y-m-d H:i:s');
if (!$this->db->update('consents', $consent, ['id' => $consent['id']])) {
2021-10-27 11:04:09 +03:00
throw new RuntimeException('Could not update consent.');
}
return $consent['id'];
}
/**
* Remove an existing consent from the database.
*
* @param int $consent_id Consent ID.
*
* @throws RuntimeException
*/
public function delete(int $consent_id): void
2021-10-27 11:04:09 +03:00
{
$this->db->delete('consents', ['id' => $consent_id]);
2021-10-27 11:04:09 +03:00
}
/**
* Get a specific consent from the database.
*
* @param int $consent_id The ID of the record to be returned.
*
* @return array Returns an array with the consent data.
*/
public function find(int $consent_id): array
2021-10-27 11:04:09 +03:00
{
$consent = $this->db->get_where('consents', ['id' => $consent_id])->row_array();
if (!$consent) {
throw new InvalidArgumentException('The provided consent ID was not found in the database: ' . $consent_id);
}
$this->cast($consent);
return $consent;
2021-10-27 11:04:09 +03:00
}
/**
* Get a specific field value from the database.
*
* @param int $consent_id Consent ID.
* @param string $field Name of the value to be returned.
*
2023-01-14 11:39:14 +03:00
* @return mixed Returns the selected consent value from the database.
2021-10-27 11:04:09 +03:00
*
* @throws InvalidArgumentException
*/
2023-01-14 11:39:14 +03:00
public function value(int $consent_id, string $field): mixed
2021-10-27 11:04:09 +03:00
{
if (empty($field)) {
2021-10-27 11:04:09 +03:00
throw new InvalidArgumentException('The field argument is cannot be empty.');
}
if (empty($consent_id)) {
2021-10-27 11:04:09 +03:00
throw new InvalidArgumentException('The consent ID argument cannot be empty.');
}
// Check whether the consent exists.
2021-10-27 11:04:09 +03:00
$query = $this->db->get_where('consents', ['id' => $consent_id]);
if (!$query->num_rows()) {
2021-10-27 11:04:09 +03:00
throw new InvalidArgumentException('The provided consent ID was not found in the database: ' . $consent_id);
}
// Check if the required field is part of the consent data.
$consent = $query->row_array();
$this->cast($consent);
2021-10-27 11:04:09 +03:00
if (!array_key_exists($field, $consent)) {
throw new InvalidArgumentException('The requested field was not found in the consent data: ' . $field);
2021-10-27 11:04:09 +03:00
}
return $consent[$field];
2021-10-27 11:04:09 +03:00
}
/**
* Get the query builder interface, configured for use with the consents table.
*
* @return CI_DB_query_builder
*/
public function query(): CI_DB_query_builder
{
return $this->db->from('consents');
}
/**
* Search consents 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 consents.
*/
public function search(string $keyword, int $limit = null, int $offset = null, string $order_by = null): array
2021-10-27 11:04:09 +03:00
{
$consents = $this->db
2021-10-27 11:04:09 +03:00
->select()
->from('consents')
->group_start()
2021-10-27 11:04:09 +03:00
->like('first_name', $keyword)
->or_like('last_name', $keyword)
->or_like('email', $keyword)
->or_like('ip', $keyword)
->group_end()
2021-10-27 11:04:09 +03:00
->limit($limit)
->offset($offset)
->order_by($order_by)
->get()
->result_array();
foreach ($consents as &$consent) {
$this->cast($consent);
}
return $consents;
}
/**
* Get all consents 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 consents.
*/
public function get(
array|string $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);
}
$consents = $this->db->get('consents', $limit, $offset)->result_array();
foreach ($consents as &$consent) {
$this->cast($consent);
}
return $consents;
2021-10-27 11:04:09 +03:00
}
/**
* Load related resources to a consent.
2021-10-27 11:04:09 +03:00
*
* @param array $consent Associative array with the consent data.
* @param array $resources Resource names to be attached.
*
* @throws InvalidArgumentException
*/
public function load(array &$consent, array $resources)
2021-10-27 11:04:09 +03:00
{
// Consents do not currently have any related resources.
}
}