2020-04-22 22:48:56 +03:00
|
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
2013-07-27 00:30:44 +03:00
|
|
|
|
|
2015-07-20 22:41:24 +03:00
|
|
|
|
/* ----------------------------------------------------------------------------
|
2024-01-01 01:14:38 +03:00
|
|
|
|
* MaketRandevu - MAKET Randevu Portalı
|
2020-03-11 12:10:59 +03:00
|
|
|
|
*
|
2015-07-20 22:41:24 +03: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
|
2015-07-20 22:41:24 +03:00
|
|
|
|
* @since v1.0.0
|
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
|
/**
|
2021-11-06 18:21:27 +03:00
|
|
|
|
* Admins model.
|
2017-09-15 14:36:37 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* Handles all the database operations of the admin resource.
|
2017-09-15 14:36:37 +03:00
|
|
|
|
*
|
2015-07-08 01:36:16 +03:00
|
|
|
|
* @package Models
|
2013-07-28 01:48:19 +03:00
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
class Admins_model extends EA_Model
|
|
|
|
|
{
|
2021-10-29 13:38:41 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
protected array $casts = [
|
2021-10-29 13:38:41 +03:00
|
|
|
|
'id' => 'integer',
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'id_roles' => 'integer',
|
2021-10-29 13:38:41 +03:00
|
|
|
|
];
|
|
|
|
|
|
2021-11-03 10:21:03 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
protected array $api_resource = [
|
2021-11-03 10:21:03 +03:00
|
|
|
|
'id' => 'id',
|
|
|
|
|
'firstName' => 'first_name',
|
|
|
|
|
'lastName' => 'last_name',
|
|
|
|
|
'email' => 'email',
|
|
|
|
|
'mobile' => 'mobile_number',
|
|
|
|
|
'phone' => 'phone_number',
|
|
|
|
|
'address' => 'address',
|
|
|
|
|
'city' => 'city',
|
|
|
|
|
'state' => 'state',
|
|
|
|
|
'zip' => 'zip_code',
|
|
|
|
|
'timezone' => 'timezone',
|
|
|
|
|
'language' => 'language',
|
2021-11-06 17:02:40 +03:00
|
|
|
|
'notes' => 'notes',
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'roleId' => 'id_roles',
|
2021-11-03 10:21:03 +03:00
|
|
|
|
];
|
|
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
|
/**
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* Save (insert or update) an admin.
|
2017-09-15 14:36:37 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @param array $admin Associative array with the admin data.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @return int Returns the admin ID.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
2023-03-13 11:06:18 +03:00
|
|
|
|
* @throws Exception
|
2013-07-28 01:48:19 +03:00
|
|
|
|
*/
|
2021-10-27 11:03:52 +03:00
|
|
|
|
public function save(array $admin): int
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2013-09-26 19:06:57 +03:00
|
|
|
|
$this->validate($admin);
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($admin['id'])) {
|
2021-10-27 11:03:52 +03:00
|
|
|
|
return $this->insert($admin);
|
2023-11-29 12:24:09 +03:00
|
|
|
|
} else {
|
2021-10-27 11:03:52 +03:00
|
|
|
|
return $this->update($admin);
|
2013-07-28 01:48:19 +03:00
|
|
|
|
}
|
2013-07-27 00:30:44 +03:00
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
|
/**
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* Validate the admin data.
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @param array $admin Associative array with the admin data.
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*/
|
2021-10-27 11:49:42 +03:00
|
|
|
|
public function validate(array $admin)
|
2020-04-22 22:48:56 +03:00
|
|
|
|
{
|
2021-10-27 11:03:52 +03:00
|
|
|
|
// If an admin ID is provided then check whether the record really exists in the database.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!empty($admin['id'])) {
|
2021-10-27 11:03:52 +03:00
|
|
|
|
$count = $this->db->get_where('users', ['id' => $admin['id']])->num_rows();
|
2020-04-23 21:48:20 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$count) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The provided admin ID does not exist in the database: ' . $admin['id'],
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 17:25:45 +03:00
|
|
|
|
// Make sure all required fields are provided.
|
2021-10-27 11:03:52 +03:00
|
|
|
|
if (
|
2023-11-29 12:24:09 +03:00
|
|
|
|
empty($admin['first_name']) ||
|
|
|
|
|
empty($admin['last_name']) ||
|
|
|
|
|
empty($admin['email']) ||
|
|
|
|
|
empty($admin['phone_number'])
|
|
|
|
|
) {
|
|
|
|
|
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($admin, true));
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
// Validate the email address.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!filter_var($admin['email'], FILTER_VALIDATE_EMAIL)) {
|
2021-10-27 11:03:52 +03:00
|
|
|
|
throw new InvalidArgumentException('Invalid email address provided: ' . $admin['email']);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 17:25:45 +03:00
|
|
|
|
// Make sure the username is unique.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!empty($admin['settings']['username'])) {
|
|
|
|
|
$admin_id = $admin['id'] ?? null;
|
2021-10-27 11:03:52 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$this->validate_username($admin['settings']['username'], $admin_id)) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The provided username is already in use, please use a different one.',
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 17:25:45 +03:00
|
|
|
|
// Validate the password.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!empty($admin['settings']['password'])) {
|
|
|
|
|
if (strlen($admin['settings']['password']) < MIN_PASSWORD_LENGTH) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The admin password must be at least ' . MIN_PASSWORD_LENGTH . ' characters long.',
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 17:25:45 +03:00
|
|
|
|
// New users must always have a password value set.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($admin['id']) && empty($admin['settings']['password'])) {
|
2021-10-27 11:03:52 +03:00
|
|
|
|
throw new InvalidArgumentException('The admin password cannot be empty when inserting a new record.');
|
2020-12-11 22:18:33 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
// Validate calendar view type value.
|
|
|
|
|
if (
|
2023-11-29 12:24:09 +03:00
|
|
|
|
!empty($admin['settings']['calendar_view']) &&
|
|
|
|
|
!in_array($admin['settings']['calendar_view'], [CALENDAR_VIEW_DEFAULT, CALENDAR_VIEW_TABLE])
|
|
|
|
|
) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The provided calendar view is invalid: ' . $admin['settings']['calendar_view'],
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
// Make sure the email address is unique.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$admin_id = $admin['id'] ?? null;
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$count = $this->db
|
2021-10-27 11:03:52 +03:00
|
|
|
|
->select()
|
2020-04-23 21:48:20 +03:00
|
|
|
|
->from('users')
|
2020-05-02 13:51:06 +03:00
|
|
|
|
->join('roles', 'roles.id = users.id_roles', 'inner')
|
2020-04-23 21:48:20 +03:00
|
|
|
|
->where('roles.slug', DB_SLUG_ADMIN)
|
|
|
|
|
->where('users.email', $admin['email'])
|
2020-12-05 12:38:57 +03:00
|
|
|
|
->where('users.id !=', $admin_id)
|
2020-04-22 22:48:56 +03:00
|
|
|
|
->get()
|
|
|
|
|
->num_rows();
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if ($count > 0) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The provided email address is already in use, please use a different one.',
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* Validate the admin username.
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @param string $username Admin username.
|
|
|
|
|
* @param int|null $admin_id Admin ID.
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*
|
|
|
|
|
* @return bool Returns the validation result.
|
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
public function validate_username(string $username, int $admin_id = null): bool
|
2020-04-22 22:48:56 +03:00
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!empty($admin_id)) {
|
2021-10-27 11:03:52 +03:00
|
|
|
|
$this->db->where('id_users !=', $admin_id);
|
2020-12-08 12:00:31 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
return $this->db
|
|
|
|
|
->from('users')
|
|
|
|
|
->join('user_settings', 'user_settings.id_users = users.id', 'inner')
|
|
|
|
|
->where(['username' => $username])
|
|
|
|
|
->get()
|
|
|
|
|
->num_rows() === 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all admins 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 admins.
|
|
|
|
|
*/
|
|
|
|
|
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 {
|
|
|
|
|
$role_id = $this->get_admin_role_id();
|
|
|
|
|
|
|
|
|
|
if ($where !== null) {
|
|
|
|
|
$this->db->where($where);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($order_by !== null) {
|
|
|
|
|
$this->db->order_by($order_by);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$admins = $this->db->get_where('users', ['id_roles' => $role_id], $limit, $offset)->result_array();
|
|
|
|
|
|
|
|
|
|
foreach ($admins as &$admin) {
|
|
|
|
|
$this->cast($admin);
|
|
|
|
|
|
|
|
|
|
$admin['settings'] = $this->db->get_where('user_settings', ['id_users' => $admin['id']])->row_array();
|
|
|
|
|
|
|
|
|
|
unset($admin['settings']['id_users'], $admin['settings']['password'], $admin['settings']['salt']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $admins;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the admin role ID.
|
|
|
|
|
*
|
|
|
|
|
* @return int Returns the role ID.
|
|
|
|
|
*/
|
|
|
|
|
public function get_admin_role_id(): int
|
|
|
|
|
{
|
|
|
|
|
$role = $this->db->get_where('roles', ['slug' => DB_SLUG_ADMIN])->row_array();
|
|
|
|
|
|
|
|
|
|
if (empty($role)) {
|
|
|
|
|
throw new RuntimeException('The admin role was not found in the database.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $role['id'];
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
|
/**
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* Insert a new admin into the database.
|
2017-09-15 14:36:37 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @param array $admin Associative array with the admin data.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @return int Returns the admin ID.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2023-03-13 11:06:18 +03:00
|
|
|
|
* @throws RuntimeException|Exception
|
2013-07-28 01:48:19 +03:00
|
|
|
|
*/
|
2021-10-27 11:03:52 +03:00
|
|
|
|
protected function insert(array $admin): int
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2021-10-27 11:03:52 +03:00
|
|
|
|
$admin['id_roles'] = $this->get_admin_role_id();
|
|
|
|
|
|
|
|
|
|
$settings = $admin['settings'];
|
2022-11-30 13:21:39 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
unset($admin['settings']);
|
2021-10-27 11:03:52 +03:00
|
|
|
|
|
2022-01-25 01:42:13 +03:00
|
|
|
|
$admin['create_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
|
$admin['update_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$this->db->insert('users', $admin)) {
|
2021-10-27 11:03:52 +03:00
|
|
|
|
throw new RuntimeException('Could not insert admin.');
|
2013-07-28 01:48:19 +03:00
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
$admin['id'] = $this->db->insert_id();
|
|
|
|
|
$settings['salt'] = generate_salt();
|
|
|
|
|
$settings['password'] = hash_password($settings['salt'], $settings['password']);
|
|
|
|
|
|
|
|
|
|
$this->save_settings($admin['id'], $settings);
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
return $admin['id'];
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
/**
|
|
|
|
|
* Save the admin settings.
|
|
|
|
|
*
|
|
|
|
|
* @param int $admin_id Admin ID.
|
|
|
|
|
* @param array $settings Associative array with the settings data.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
|
|
|
|
protected function save_settings(int $admin_id, array $settings)
|
|
|
|
|
{
|
|
|
|
|
if (empty($settings)) {
|
|
|
|
|
throw new InvalidArgumentException('The settings argument cannot be empty.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure the settings record exists in the database.
|
|
|
|
|
$count = $this->db->get_where('user_settings', ['id_users' => $admin_id])->num_rows();
|
|
|
|
|
|
|
|
|
|
if (!$count) {
|
|
|
|
|
$this->db->insert('user_settings', ['id_users' => $admin_id]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($settings as $name => $value) {
|
|
|
|
|
$this->set_setting($admin_id, $name, $value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the value of an admin setting.
|
|
|
|
|
*
|
|
|
|
|
* @param int $admin_id Admin ID.
|
|
|
|
|
* @param string $name Setting name.
|
|
|
|
|
* @param mixed|null $value Setting value.
|
|
|
|
|
*/
|
|
|
|
|
public function set_setting(int $admin_id, string $name, mixed $value = null)
|
|
|
|
|
{
|
|
|
|
|
if (!$this->db->update('user_settings', [$name => $value], ['id_users' => $admin_id])) {
|
|
|
|
|
throw new RuntimeException('Could not set the new admin setting value: ' . $name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
|
/**
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* Update an existing admin.
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @param array $admin Associative array with the admin data.
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @return int Returns the admin ID.
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @throws RuntimeException
|
2023-03-13 11:06:18 +03:00
|
|
|
|
* @throws Exception
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*/
|
2021-10-27 11:03:52 +03:00
|
|
|
|
protected function update(array $admin): int
|
2020-04-22 22:48:56 +03:00
|
|
|
|
{
|
2021-10-27 11:03:52 +03:00
|
|
|
|
$settings = $admin['settings'];
|
2022-11-30 13:21:39 +03:00
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
$settings['id_users'] = $admin['id'];
|
2022-11-30 13:21:39 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
unset($admin['settings']);
|
2021-10-27 11:03:52 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!empty($settings['password'])) {
|
2021-10-27 11:03:52 +03:00
|
|
|
|
$existing_settings = $this->db->get_where('user_settings', ['id_users' => $admin['id']])->row_array();
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($existing_settings)) {
|
2021-10-27 11:03:52 +03:00
|
|
|
|
throw new RuntimeException('No settings record found for admin with ID: ' . $admin['id']);
|
|
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
$settings['password'] = hash_password($existing_settings['salt'], $settings['password']);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-25 01:42:13 +03:00
|
|
|
|
$admin['update_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$this->db->update('users', $admin, ['id' => $admin['id']])) {
|
2021-10-27 11:03:52 +03:00
|
|
|
|
throw new RuntimeException('Could not update admin.');
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
$this->save_settings($admin['id'], $settings);
|
|
|
|
|
|
|
|
|
|
return $admin['id'];
|
2013-07-27 00:30:44 +03:00
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
|
/**
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* Remove an existing admin from the database.
|
2017-09-15 14:36:37 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @param int $admin_id Admin ID.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @throws RuntimeException
|
2013-07-28 01:48:19 +03:00
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
|
public function delete(int $admin_id): void
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2021-10-27 11:03:52 +03:00
|
|
|
|
$role_id = $this->get_admin_role_id();
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2023-10-19 17:25:45 +03:00
|
|
|
|
$count = $this->db->get_where('users', ['id_roles' => $role_id])->num_rows();
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if ($count <= 1) {
|
2021-10-27 11:03:52 +03:00
|
|
|
|
throw new RuntimeException('Record could not be deleted as the app requires at least one admin user.');
|
2013-07-28 01:48:19 +03:00
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2023-10-19 17:25:45 +03:00
|
|
|
|
$this->db->delete('users', ['id' => $admin_id]);
|
2017-09-15 14:36:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
|
/**
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* Get a specific admin from the database.
|
|
|
|
|
*
|
|
|
|
|
* @param int $admin_id The ID of the record to be returned.
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @return array Returns an array with the admin data.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
|
public function find(int $admin_id): array
|
2020-04-22 22:48:56 +03:00
|
|
|
|
{
|
2021-10-27 11:03:52 +03:00
|
|
|
|
$admin = $this->db->get_where('users', ['id' => $admin_id])->row_array();
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$admin) {
|
2022-01-25 01:32:42 +03:00
|
|
|
|
throw new InvalidArgumentException('The provided admin ID was not found in the database: ' . $admin_id);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 13:38:41 +03:00
|
|
|
|
$this->cast($admin);
|
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
$admin['settings'] = $this->db->get_where('user_settings', ['id_users' => $admin_id])->row_array();
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
unset($admin['settings']['id_users'], $admin['settings']['password'], $admin['settings']['salt']);
|
2021-10-27 11:03:52 +03:00
|
|
|
|
|
|
|
|
|
return $admin;
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
|
/**
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* Get a specific field value from the database.
|
2017-09-15 14:36:37 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @param int $admin_id Admin ID.
|
|
|
|
|
* @param string $field Name of the value to be returned.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2023-01-14 11:39:14 +03:00
|
|
|
|
* @return mixed Returns the selected admin value from the database.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
2013-07-28 01:48:19 +03:00
|
|
|
|
*/
|
2023-01-14 11:39:14 +03:00
|
|
|
|
public function value(int $admin_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:03:52 +03:00
|
|
|
|
throw new InvalidArgumentException('The field argument is cannot be empty.');
|
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($admin_id)) {
|
2021-10-27 11:03:52 +03:00
|
|
|
|
throw new InvalidArgumentException('The admin ID argument cannot be empty.');
|
2013-09-23 18:42:36 +03:00
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
// Check whether the admin exists.
|
|
|
|
|
$query = $this->db->get_where('users', ['id' => $admin_id]);
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$query->num_rows()) {
|
2021-10-27 11:03:52 +03:00
|
|
|
|
throw new InvalidArgumentException('The provided admin ID was not found in the database: ' . $admin_id);
|
2013-07-28 01:48:19 +03:00
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
// Check if the required field is part of the admin data.
|
|
|
|
|
$admin = $query->row_array();
|
|
|
|
|
|
2021-10-29 13:38:41 +03:00
|
|
|
|
$this->cast($admin);
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!array_key_exists($field, $admin)) {
|
2021-10-27 11:03:52 +03:00
|
|
|
|
throw new InvalidArgumentException('The requested field was not found in the admin data: ' . $field);
|
2013-09-03 21:58:56 +03:00
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
return $admin[$field];
|
2013-07-27 00:30:44 +03:00
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
|
/**
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* Get the value of an admin setting.
|
2017-09-15 14:36:37 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @param int $admin_id Admin ID.
|
|
|
|
|
* @param string $name Setting name.
|
2020-12-05 12:38:57 +03:00
|
|
|
|
*
|
2021-10-27 11:03:52 +03:00
|
|
|
|
* @return string Returns the value of the requested user setting.
|
2013-07-28 01:48:19 +03:00
|
|
|
|
*/
|
2021-10-27 11:03:52 +03:00
|
|
|
|
public function get_setting(int $admin_id, string $name): string
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2021-10-27 11:03:52 +03:00
|
|
|
|
$settings = $this->db->get_where('user_settings', ['id_users' => $admin_id])->row_array();
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!array_key_exists($name, $settings)) {
|
2021-10-27 11:03:52 +03:00
|
|
|
|
throw new RuntimeException('The requested setting value was not found: ' . $admin_id);
|
2020-04-06 21:34:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
return $settings[$name];
|
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
/**
|
|
|
|
|
* Get the query builder interface, configured for use with the users (admin-filtered) table.
|
|
|
|
|
*
|
|
|
|
|
* @return CI_DB_query_builder
|
|
|
|
|
*/
|
|
|
|
|
public function query(): CI_DB_query_builder
|
|
|
|
|
{
|
2021-10-29 13:38:41 +03:00
|
|
|
|
$role_id = $this->get_admin_role_id();
|
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
return $this->db->from('users')->where('id_roles', $role_id);
|
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
/**
|
|
|
|
|
* Search admins 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 admins.
|
|
|
|
|
*/
|
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:03:52 +03:00
|
|
|
|
{
|
2021-10-29 13:38:41 +03:00
|
|
|
|
$role_id = $this->get_admin_role_id();
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$admins = $this->db
|
2021-10-27 11:03:52 +03:00
|
|
|
|
->select()
|
|
|
|
|
->from('users')
|
|
|
|
|
->where('id_roles', $role_id)
|
2021-11-06 17:02:40 +03:00
|
|
|
|
->group_start()
|
2021-10-27 11:03:52 +03:00
|
|
|
|
->like('first_name', $keyword)
|
|
|
|
|
->or_like('last_name', $keyword)
|
2022-12-12 11:15:55 +03:00
|
|
|
|
->or_like('CONCAT_WS(" ", first_name, last_name)', $keyword)
|
2021-10-27 11:03:52 +03:00
|
|
|
|
->or_like('email', $keyword)
|
|
|
|
|
->or_like('phone_number', $keyword)
|
|
|
|
|
->or_like('mobile_number', $keyword)
|
|
|
|
|
->or_like('address', $keyword)
|
|
|
|
|
->or_like('city', $keyword)
|
|
|
|
|
->or_like('state', $keyword)
|
|
|
|
|
->or_like('zip_code', $keyword)
|
|
|
|
|
->or_like('notes', $keyword)
|
2021-11-06 17:02:40 +03:00
|
|
|
|
->group_end()
|
2021-10-27 11:03:52 +03:00
|
|
|
|
->limit($limit)
|
|
|
|
|
->offset($offset)
|
|
|
|
|
->order_by($order_by)
|
|
|
|
|
->get()
|
|
|
|
|
->result_array();
|
2021-10-29 13:38:41 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
foreach ($admins as &$admin) {
|
2021-10-29 13:38:41 +03:00
|
|
|
|
$this->cast($admin);
|
|
|
|
|
|
|
|
|
|
$admin['settings'] = $this->db->get_where('user_settings', ['id_users' => $admin['id']])->row_array();
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
unset($admin['settings']['id_users'], $admin['settings']['password'], $admin['settings']['salt']);
|
2021-10-29 13:38:41 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $admins;
|
2021-10-27 11:03:52 +03:00
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2021-10-27 11:03:52 +03:00
|
|
|
|
/**
|
2021-11-06 17:40:55 +03:00
|
|
|
|
* Load related resources to an admin.
|
2021-10-27 11:03:52 +03:00
|
|
|
|
*
|
|
|
|
|
* @param array $admin Associative array with the admin data.
|
|
|
|
|
* @param array $resources Resource names to be attached.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
2021-11-06 17:40:55 +03:00
|
|
|
|
public function load(array &$admin, array $resources)
|
2021-10-27 11:03:52 +03:00
|
|
|
|
{
|
2023-10-19 17:25:45 +03:00
|
|
|
|
// Admins do not currently have any related resources (settings are already part of the admins).
|
2013-07-27 00:30:44 +03:00
|
|
|
|
}
|
2021-11-02 12:45:05 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert the database admin record to the equivalent API resource.
|
|
|
|
|
*
|
|
|
|
|
* @param array $admin Admin data.
|
|
|
|
|
*/
|
|
|
|
|
public function api_encode(array &$admin)
|
|
|
|
|
{
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$encoded_resource = [
|
2023-11-29 12:24:09 +03:00
|
|
|
|
'id' => array_key_exists('id', $admin) ? (int) $admin['id'] : null,
|
2021-11-02 12:45:05 +03:00
|
|
|
|
'firstName' => $admin['first_name'],
|
|
|
|
|
'lastName' => $admin['last_name'],
|
|
|
|
|
'email' => $admin['email'],
|
|
|
|
|
'mobile' => $admin['mobile_number'],
|
|
|
|
|
'phone' => $admin['phone_number'],
|
|
|
|
|
'address' => $admin['address'],
|
|
|
|
|
'city' => $admin['city'],
|
|
|
|
|
'state' => $admin['state'],
|
|
|
|
|
'zip' => $admin['zip_code'],
|
|
|
|
|
'notes' => $admin['notes'],
|
|
|
|
|
'timezone' => $admin['timezone'],
|
|
|
|
|
'settings' => [
|
|
|
|
|
'username' => $admin['settings']['username'],
|
|
|
|
|
'notifications' => filter_var($admin['settings']['notifications'], FILTER_VALIDATE_BOOLEAN),
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'calendarView' => $admin['settings']['calendar_view'],
|
|
|
|
|
],
|
2021-11-02 12:45:05 +03:00
|
|
|
|
];
|
|
|
|
|
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$admin = $encoded_resource;
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert the API resource to the equivalent database admin record.
|
|
|
|
|
*
|
2021-11-05 11:32:34 +03:00
|
|
|
|
* @param array $admin API resource.
|
2021-11-02 12:45:05 +03:00
|
|
|
|
* @param array|null $base Base admin data to be overwritten with the provided values (useful for updates).
|
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
public function api_decode(array &$admin, array $base = null)
|
2021-11-02 12:45:05 +03:00
|
|
|
|
{
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource = $base ?? [];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('id', $admin)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['id'] = $admin['id'];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('firstName', $admin)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['first_name'] = $admin['firstName'];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('lastName', $admin)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['last_name'] = $admin['lastName'];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('email', $admin)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['email'] = $admin['email'];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('mobile', $admin)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['mobile_number'] = $admin['mobile'];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('phone', $admin)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['phone_number'] = $admin['phone'];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('address', $admin)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['address'] = $admin['address'];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('city', $admin)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['city'] = $admin['city'];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('state', $admin)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['state'] = $admin['state'];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('zip', $admin)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['zip_code'] = $admin['zip'];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('notes', $admin)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['notes'] = $admin['notes'];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('timezone', $admin)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['timezone'] = $admin['timezone'];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('settings', $admin)) {
|
|
|
|
|
if (empty($decoded_resource['settings'])) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['settings'] = [];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('username', $admin['settings'])) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['settings']['username'] = $admin['settings']['username'];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('password', $admin['settings'])) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['settings']['password'] = $admin['settings']['password'];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('notifications', $admin['settings'])) {
|
|
|
|
|
$decoded_resource['settings']['notifications'] = filter_var(
|
|
|
|
|
$admin['settings']['notifications'],
|
2023-12-22 13:35:41 +03:00
|
|
|
|
FILTER_VALIDATE_BOOLEAN,
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (array_key_exists('calendarView', $admin['settings'])) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['settings']['calendar_view'] = $admin['settings']['calendarView'];
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$admin = $decoded_resource;
|
2021-11-02 12:45:05 +03:00
|
|
|
|
}
|
2013-07-27 00:30:44 +03:00
|
|
|
|
}
|