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
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2023-12-31 21:46:27 +03:00
|
|
|
|
* IFLRandevu - İzmir Fen Lisesi 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
|
|
|
|
* Settings model.
|
2021-10-27 11:04:48 +03:00
|
|
|
|
*
|
|
|
|
|
* Handles all the database operations of the setting resource.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
|
|
|
|
* @package Models
|
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
class Settings_model extends EA_Model
|
|
|
|
|
{
|
2021-10-29 13:39:32 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
protected array $casts = [
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'id' => 'integer',
|
2021-10-29 13:39:32 +03:00
|
|
|
|
];
|
|
|
|
|
|
2021-11-06 17:02:40 +03:00
|
|
|
|
/**
|
2023-10-19 17:25:45 +03:00
|
|
|
|
* @var array
|
2021-11-06 17:02:40 +03:00
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
protected array $api_resource = [
|
2023-10-19 17:25:45 +03:00
|
|
|
|
'name' => 'name',
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'value' => 'value',
|
2021-11-06 17:02:40 +03:00
|
|
|
|
];
|
|
|
|
|
|
2016-01-09 23:29:28 +02:00
|
|
|
|
/**
|
2021-10-27 11:04:48 +03:00
|
|
|
|
* Save (insert or update) a setting.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:48 +03:00
|
|
|
|
* @param array $setting Associative array with the setting data.
|
2016-04-27 09:21:40 +03:00
|
|
|
|
*
|
2021-10-27 11:04:48 +03:00
|
|
|
|
* @return int Returns the setting ID.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:04:48 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-10-27 11:04:48 +03:00
|
|
|
|
public function save(array $setting): int
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2021-10-27 11:04:48 +03:00
|
|
|
|
$this->validate($setting);
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($setting['id'])) {
|
2021-10-27 11:04:48 +03:00
|
|
|
|
return $this->insert($setting);
|
2023-11-29 12:24:09 +03:00
|
|
|
|
} else {
|
2021-10-27 11:04:48 +03:00
|
|
|
|
return $this->update($setting);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
2021-10-27 11:04:48 +03:00
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
2021-10-27 11:04:48 +03:00
|
|
|
|
/**
|
|
|
|
|
* Validate the setting data.
|
|
|
|
|
*
|
|
|
|
|
* @param array $setting Associative array with the setting data.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
2021-10-27 11:49:42 +03:00
|
|
|
|
public function validate(array $setting)
|
2021-10-27 11:04:48 +03:00
|
|
|
|
{
|
|
|
|
|
// If a setting ID is provided then check whether the record really exists in the database.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!empty($setting['id'])) {
|
2021-10-27 11:04:48 +03:00
|
|
|
|
$count = $this->db->get_where('settings', ['id' => $setting['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 setting ID does not exist in the database: ' . $setting['id'],
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2021-10-27 11:04:48 +03:00
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:48 +03:00
|
|
|
|
// Make sure all required fields are provided.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($setting['name'])) {
|
|
|
|
|
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($setting, true));
|
2021-10-27 11:04:48 +03:00
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:48 +03:00
|
|
|
|
* Insert a new setting into the database.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:48 +03:00
|
|
|
|
* @param array $setting Associative array with the setting data.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:48 +03:00
|
|
|
|
* @return int Returns the setting ID.
|
2016-04-27 09:21:40 +03:00
|
|
|
|
*
|
2021-10-27 11:04:48 +03:00
|
|
|
|
* @throws RuntimeException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-10-27 11:04:48 +03:00
|
|
|
|
protected function insert(array $setting): int
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2022-01-25 01:42:13 +03:00
|
|
|
|
$setting['create_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
|
$setting['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->insert('settings', $setting)) {
|
2021-10-27 11:04:48 +03:00
|
|
|
|
throw new RuntimeException('Could not insert setting.');
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:48 +03:00
|
|
|
|
return $this->db->insert_id();
|
|
|
|
|
}
|
2020-12-05 12:38:57 +03:00
|
|
|
|
|
2021-10-27 11:04:48 +03:00
|
|
|
|
/**
|
|
|
|
|
* Update an existing setting.
|
|
|
|
|
*
|
|
|
|
|
* @param array $setting Associative array with the setting data.
|
|
|
|
|
*
|
|
|
|
|
* @return int Returns the setting ID.
|
|
|
|
|
*
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
|
|
|
|
protected function update(array $setting): int
|
|
|
|
|
{
|
2022-01-25 01:42:13 +03:00
|
|
|
|
$setting['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('settings', $setting, ['id' => $setting['id']])) {
|
2021-10-27 11:04:48 +03:00
|
|
|
|
throw new RuntimeException('Could not update setting.');
|
2018-01-23 12:08:37 +03:00
|
|
|
|
}
|
2020-12-05 12:38:57 +03:00
|
|
|
|
|
2021-10-27 11:04:48 +03:00
|
|
|
|
return $setting['id'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove an existing setting from the database.
|
2023-10-19 17:25:45 +03:00
|
|
|
|
*
|
2021-10-27 11:04:48 +03:00
|
|
|
|
* @param int $setting_id Setting ID.
|
|
|
|
|
*
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
|
public function delete(int $setting_id): void
|
2021-10-27 11:04:48 +03:00
|
|
|
|
{
|
2023-10-19 17:25:45 +03:00
|
|
|
|
$this->db->delete('settings', ['id' => $setting_id]);
|
2021-10-27 11:04:48 +03:00
|
|
|
|
}
|
2020-12-05 12:38:57 +03:00
|
|
|
|
|
2021-10-27 11:04:48 +03:00
|
|
|
|
/**
|
|
|
|
|
* Get a specific setting from the database.
|
|
|
|
|
*
|
|
|
|
|
* @param int $setting_id The ID of the record to be returned.
|
|
|
|
|
*
|
|
|
|
|
* @return array Returns an array with the setting data.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
|
public function find(int $setting_id): array
|
2021-10-27 11:04:48 +03:00
|
|
|
|
{
|
2021-10-29 13:39:32 +03:00
|
|
|
|
$setting = $this->db->get_where('settings', ['id' => $setting_id])->row_array();
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$setting) {
|
2022-01-25 01:32:42 +03:00
|
|
|
|
throw new InvalidArgumentException('The provided setting ID was not found in the database: ' . $setting_id);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 13:39:32 +03:00
|
|
|
|
$this->cast($setting);
|
|
|
|
|
|
|
|
|
|
return $setting;
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:48 +03:00
|
|
|
|
* Get a specific field value from the database.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:48 +03:00
|
|
|
|
* @param int $setting_id Setting 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 setting value from the database.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:04:48 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2023-01-14 11:39:14 +03:00
|
|
|
|
public function value(int $setting_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:48 +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($setting_id)) {
|
2021-10-27 11:04:48 +03:00
|
|
|
|
throw new InvalidArgumentException('The setting ID argument cannot be empty.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check whether the setting exists.
|
|
|
|
|
$query = $this->db->get_where('settings', ['id' => $setting_id]);
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$query->num_rows()) {
|
2021-10-27 11:04:48 +03:00
|
|
|
|
throw new InvalidArgumentException('The provided setting ID was not found in the database: ' . $setting_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if the required field is part of the setting data.
|
|
|
|
|
$setting = $query->row_array();
|
|
|
|
|
|
2021-10-29 13:39:32 +03:00
|
|
|
|
$this->cast($setting);
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!array_key_exists($field, $setting)) {
|
2021-10-27 11:04:48 +03:00
|
|
|
|
throw new InvalidArgumentException('The requested field was not found in the setting data: ' . $field);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:48 +03:00
|
|
|
|
return $setting[$field];
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:48 +03:00
|
|
|
|
/**
|
|
|
|
|
* Get the query builder interface, configured for use with the settings table.
|
|
|
|
|
*
|
|
|
|
|
* @return CI_DB_query_builder
|
|
|
|
|
*/
|
|
|
|
|
public function query(): CI_DB_query_builder
|
|
|
|
|
{
|
|
|
|
|
return $this->db->from('settings');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Search settings 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.
|
2023-10-19 17:25:45 +03:00
|
|
|
|
*
|
2021-10-27 11:04:48 +03:00
|
|
|
|
* @return array Returns an array of settings.
|
|
|
|
|
*/
|
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-10-27 11:04:48 +03:00
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$settings = $this->db
|
2021-10-27 11:04:48 +03:00
|
|
|
|
->select()
|
|
|
|
|
->from('settings')
|
2022-07-07 16:27:10 +03:00
|
|
|
|
->group_start()
|
2021-10-27 11:04:48 +03:00
|
|
|
|
->like('name', $keyword)
|
|
|
|
|
->or_like('value', $keyword)
|
2022-07-07 16:27:10 +03:00
|
|
|
|
->group_end()
|
2021-10-27 11:04:48 +03:00
|
|
|
|
->limit($limit)
|
|
|
|
|
->offset($offset)
|
|
|
|
|
->order_by($order_by)
|
|
|
|
|
->get()
|
|
|
|
|
->result_array();
|
2021-10-29 13:39:32 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
foreach ($settings as &$setting) {
|
|
|
|
|
$this->cast($setting);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all settings 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 settings.
|
|
|
|
|
*/
|
|
|
|
|
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 !== null) {
|
|
|
|
|
$this->db->order_by($order_by);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$settings = $this->db->get('settings', $limit, $offset)->result_array();
|
|
|
|
|
|
|
|
|
|
foreach ($settings as &$setting) {
|
2021-10-29 13:39:32 +03:00
|
|
|
|
$this->cast($setting);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $settings;
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-06 17:40:55 +03:00
|
|
|
|
* Load related resources to a setting.
|
2021-10-27 11:04:48 +03:00
|
|
|
|
*
|
|
|
|
|
* @param array $setting Associative array with the setting data.
|
|
|
|
|
* @param array $resources Resource names to be attached.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:48 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-11-06 17:40:55 +03:00
|
|
|
|
public function load(array &$setting, array $resources)
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2023-10-19 17:25:45 +03:00
|
|
|
|
// Users do not currently have any related resources.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
2021-11-05 11:32:34 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert the database setting record to the equivalent API resource.
|
|
|
|
|
*
|
|
|
|
|
* @param array $setting Setting data.
|
|
|
|
|
*/
|
|
|
|
|
public function api_encode(array &$setting)
|
|
|
|
|
{
|
|
|
|
|
$encoded_resource = [
|
|
|
|
|
'name' => $setting['name'],
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'value' => $setting['value'],
|
2021-11-05 11:32:34 +03:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$setting = $encoded_resource;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert the API resource to the equivalent database setting record.
|
|
|
|
|
*
|
|
|
|
|
* @param array $setting API resource.
|
|
|
|
|
* @param array|null $base Base setting data to be overwritten with the provided values (useful for updates).
|
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
public function api_decode(array &$setting, 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('name', $setting)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['name'] = $setting['name'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('value', $setting)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['value'] = $setting['value'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$setting = $decoded_resource;
|
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|