iflrandevu/application/models/Settings_model.php

260 lines
7.3 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:48 +03:00
* Settings model
*
* Handles all the database operations of the setting resource.
*
* @package Models
*/
class Settings_model extends EA_Model {
/**
2021-10-27 11:04:48 +03:00
* Save (insert or update) a setting.
*
2021-10-27 11:04:48 +03:00
* @param array $setting Associative array with the setting data.
*
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
*/
2021-10-27 11:04:48 +03:00
public function save(array $setting): int
{
2021-10-27 11:04:48 +03:00
$this->validate($setting);
if (empty($setting['id']))
{
return $this->insert($setting);
}
else
2020-12-05 12:38:57 +03:00
{
2021-10-27 11:04:48 +03:00
return $this->update($setting);
}
2021-10-27 11:04:48 +03:00
}
2021-10-27 11:04:48 +03:00
/**
* Validate the setting data.
*
* @param array $setting Associative array with the setting data.
*
* @throws InvalidArgumentException
*/
public function validate(array $setting): void
{
// If a setting ID is provided then check whether the record really exists in the database.
if ( ! empty($setting['id']))
2020-12-05 12:38:57 +03:00
{
2021-10-27 11:04:48 +03:00
$count = $this->db->get_where('settings', ['id' => $setting['id']])->num_rows();
if ( ! $count)
{
throw new InvalidArgumentException('The provided setting ID does not exist in the database: ' . $setting['id']);
}
}
2021-10-27 11:04:48 +03:00
// Make sure all required fields are provided.
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
* Insert a new setting into the database.
*
2021-10-27 11:04:48 +03:00
* @param array $setting Associative array with the setting data.
*
2021-10-27 11:04:48 +03:00
* @return int Returns the setting ID.
*
2021-10-27 11:04:48 +03:00
* @throws RuntimeException
*/
2021-10-27 11:04:48 +03:00
protected function insert(array $setting): int
{
2021-10-27 11:04:48 +03:00
if ( ! $this->db->insert('settings', $setting))
{
2021-10-27 11:04:48 +03:00
throw new RuntimeException('Could not insert setting.');
}
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
{
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.
*
* @param int $setting_id Setting ID.
*
* @throws RuntimeException
*/
public function delete(int $setting_id): void
{
if ( ! $this->db->delete('settings', ['id' => $setting_id]))
{
throw new RuntimeException('Could not delete setting.');
}
}
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
*/
public function find(int $setting_id): array
{
if ( ! $this->db->get_where('settings', ['id' => $setting_id])->num_rows())
{
throw new InvalidArgumentException('The provided setting ID was not found in the database: ' . $setting_id);
}
2021-10-27 11:04:48 +03:00
return $this->db->get_where('settings', ['id' => $setting_id])->row_array();
}
/**
2021-10-27 11:04:48 +03:00
* Get a specific field value from the database.
*
2021-10-27 11:04:48 +03:00
* @param int $setting_id Setting ID.
* @param string $field Name of the value to be returned.
*
2021-10-27 11:04:48 +03:00
* @return string 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
*/
2021-10-27 11:04:48 +03:00
public function value(int $setting_id, string $field): string
{
2021-10-27 11:04:48 +03:00
if (empty($field))
{
2021-10-27 11:04:48 +03:00
throw new InvalidArgumentException('The field argument is cannot be empty.');
}
2021-10-27 11:04:48 +03:00
if (empty($setting_id))
{
throw new InvalidArgumentException('The setting ID argument cannot be empty.');
}
// Check whether the setting exists.
$query = $this->db->get_where('settings', ['id' => $setting_id]);
if ( ! $query->num_rows())
{
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();
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);
}
2021-10-27 11:04:48 +03:00
return $setting[$field];
}
/**
2021-10-27 11:04:48 +03:00
* Get all settings that match the provided criteria.
*
2021-10-27 11:04:48 +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.
*
2021-10-27 11:04:48 +03:00
* @return array Returns an array of settings.
*/
2021-10-27 11:04:48 +03:00
public function get($where = NULL, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
{
2021-10-27 11:04:48 +03:00
if ($where !== NULL)
{
2021-10-27 11:04:48 +03:00
$this->db->where($where);
}
2021-10-27 11:04:48 +03:00
if ($order_by !== NULL)
{
2021-10-27 11:04:48 +03:00
$this->db->order_by($order_by);
}
2021-10-27 11:04:48 +03:00
return $this->db->get('settings', $limit, $offset)->result_array();
}
/**
* 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.
*
* @return array Returns an array of settings.
*/
public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
{
return $this
->db
->select()
->from('settings')
->like('name', $keyword)
->or_like('value', $keyword)
->limit($limit)
->offset($offset)
->order_by($order_by)
->get()
->result_array();
}
/**
2021-10-27 11:04:48 +03:00
* Attach related resources to a setting.
*
* @param array $setting Associative array with the setting data.
* @param array $resources Resource names to be attached.
*
2021-10-27 11:04:48 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:04:48 +03:00
public function attach(array &$setting, array $resources): void
{
2021-10-27 11:04:48 +03:00
// Users do not currently have any related resources.
}
}