iflrandevu/application/models/Admins_model.php

527 lines
15 KiB
PHP
Raw Normal View History

<?php defined('BASEPATH') or exit('No direct script access allowed');
2015-07-20 22:41:24 +03:00
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
2015-07-20 22:41:24 +03:00
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
2015-07-20 22:41:24 +03:00
* @link http://easyappointments.org
* @since v1.0.0
* ---------------------------------------------------------------------------- */
/**
2021-10-27 11:03:52 +03:00
* Admins model
*
2021-10-27 11:03:52 +03:00
* Handles all the database operations of the admin resource.
*
2015-07-08 01:36:16 +03:00
* @package Models
*/
class Admins_model extends EA_Model {
2021-10-29 13:38:41 +03:00
/**
* @var array
*/
protected $casts = [
'id' => 'integer',
'id_roles' => 'integer',
];
/**
2021-10-27 11:03:52 +03:00
* Save (insert or update) an admin.
*
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
*/
2021-10-27 11:03:52 +03:00
public function save(array $admin): int
{
$this->validate($admin);
2021-10-27 11:03:52 +03:00
if (empty($admin['id']))
{
2021-10-27 11:03:52 +03:00
return $this->insert($admin);
2018-01-23 12:08:37 +03:00
}
else
{
2021-10-27 11:03:52 +03:00
return $this->update($admin);
}
}
/**
2021-10-27 11:03:52 +03:00
* Validate the admin data.
*
2021-10-27 11:03:52 +03:00
* @param array $admin Associative array with the admin data.
*
2021-10-27 11:03:52 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:49:42 +03:00
public function validate(array $admin)
{
2021-10-27 11:03:52 +03:00
// If an admin ID is provided then check whether the record really exists in the database.
if ( ! empty($admin['id']))
{
2021-10-27 11:03:52 +03:00
$count = $this->db->get_where('users', ['id' => $admin['id']])->num_rows();
2021-10-27 11:03:52 +03:00
if ( ! $count)
{
2021-10-27 11:03:52 +03:00
throw new InvalidArgumentException('The provided admin ID does not exist in the database: ' . $admin['id']);
}
}
2021-10-27 11:03:52 +03:00
// Make sure all required fields are provided.
if (
empty($admin['first_name'])
|| empty($admin['last_name'])
|| empty($admin['email'])
|| empty($admin['phone_number'])
)
{
2021-10-27 11:03:52 +03:00
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($admin, TRUE));
}
2021-10-27 11:03:52 +03:00
// Validate the email address.
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']);
}
2021-10-27 11:03:52 +03:00
// Make sure the username is unique.
if ( ! empty($admin['settings']['username']))
{
2021-10-27 11:03:52 +03:00
$admin_id = $admin['id'] ?? NULL;
if ( ! $this->validate_username($admin['settings']['username'], $admin_id))
{
2021-10-27 11:03:52 +03:00
throw new InvalidArgumentException('The provided username is already in use, please use a different one.');
}
}
2021-10-27 11:03:52 +03:00
// Validate the password.
if ( ! empty($admin['settings']['password']))
{
if (strlen($admin['settings']['password']) < MIN_PASSWORD_LENGTH)
{
2021-10-27 11:03:52 +03:00
throw new InvalidArgumentException('The admin password must be at least ' . MIN_PASSWORD_LENGTH . ' characters long.');
}
}
2021-10-27 11:03:52 +03:00
// New users must always have a password value set.
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.');
}
2021-10-27 11:03:52 +03:00
// Validate calendar view type value.
if (
! empty($admin['settings']['calendar_view'])
&& ! in_array($admin['settings']['calendar_view'], [CALENDAR_VIEW_DEFAULT, CALENDAR_VIEW_TABLE])
)
{
2021-10-27 11:03:52 +03:00
throw new InvalidArgumentException('The provided calendar view is invalid: ' . $admin['settings']['calendar_view']);
}
2021-10-27 11:03:52 +03:00
// Make sure the email address is unique.
$admin_id = $admin['id'] ?? NULL;
2021-10-27 11:03:52 +03:00
$count = $this
->db
->select()
->from('users')
->join('roles', 'roles.id = users.id_roles', 'inner')
->where('roles.slug', DB_SLUG_ADMIN)
->where('users.email', $admin['email'])
2020-12-05 12:38:57 +03:00
->where('users.id !=', $admin_id)
->get()
->num_rows();
2021-10-27 11:03:52 +03:00
if ($count > 0)
{
2021-10-27 11:03:52 +03:00
throw new InvalidArgumentException('The provided email address is already in use, please use a different one.');
}
}
/**
2021-10-27 11:03:52 +03:00
* Validate the admin username.
*
2021-10-27 11:03:52 +03:00
* @param string $username Admin username.
* @param int|null $admin_id Admin ID.
*
* @return bool Returns the validation result.
*/
2021-10-27 11:03:52 +03:00
public function validate_username(string $username, int $admin_id = NULL): bool
{
2021-10-27 11:03:52 +03:00
if ( ! empty($admin_id))
{
2021-10-27 11:03:52 +03:00
$this->db->where('id_users !=', $admin_id);
}
2021-10-27 11:03:52 +03:00
return $this->db->get_where('user_settings', ['username' => $username])->num_rows() === 0;
}
/**
2021-10-27 11:03:52 +03:00
* Insert a new admin into the database.
*
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 RuntimeException
*/
2021-10-27 11:03:52 +03:00
protected function insert(array $admin): int
{
2021-10-27 11:03:52 +03:00
$admin['id_roles'] = $this->get_admin_role_id();
$settings = $admin['settings'];
unset($admin['settings']);
if ( ! $this->db->insert('users', $admin))
{
2021-10-27 11:03:52 +03:00
throw new RuntimeException('Could not insert admin.');
}
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);
2021-10-27 11:03:52 +03:00
return $admin['id'];
}
/**
2021-10-27 11:03:52 +03:00
* Update an existing admin.
*
2021-10-27 11:03:52 +03:00
* @param array $admin Associative array with the admin data.
*
2021-10-27 11:03:52 +03:00
* @return int Returns the admin ID.
*
2021-10-27 11:03:52 +03:00
* @throws RuntimeException
*/
2021-10-27 11:03:52 +03:00
protected function update(array $admin): int
{
2021-10-27 11:03:52 +03:00
$settings = $admin['settings'];
$settings['id_users'] = $admin['id'];
unset($admin['settings']);
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();
2021-10-27 11:03:52 +03:00
if (empty($existing_settings))
{
throw new RuntimeException('No settings record found for admin with ID: ' . $admin['id']);
}
2021-10-27 11:03:52 +03:00
$settings['password'] = hash_password($existing_settings['salt'], $settings['password']);
}
if ( ! $this->db->update('users', $admin, ['id' => $admin['id']]))
{
2021-10-27 11:03:52 +03:00
throw new RuntimeException('Could not update admin.');
}
2021-10-27 11:03:52 +03:00
$this->save_settings($admin['id'], $settings);
return $admin['id'];
}
/**
2021-10-27 11:03:52 +03:00
* Remove an existing admin from the database.
*
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
*/
2021-10-27 11:49:42 +03:00
public function delete(int $admin_id)
{
2021-10-27 11:03:52 +03:00
$role_id = $this->get_admin_role_id();
2021-10-27 11:03:52 +03:00
$count = $this->db->get_where('users', ['id_roles' => $role_id])->num_rows();
2021-10-27 11:03:52 +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.');
}
2021-10-27 11:03:52 +03:00
if ( ! $this->db->delete('users', ['id' => $admin_id]))
{
2021-10-27 11:03:52 +03:00
throw new RuntimeException('Could not delete admin.');
}
}
/**
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.
*
2021-10-27 11:03:52 +03:00
* @return array Returns an array with the admin data.
*
* @throws InvalidArgumentException
*/
2021-10-27 11:03:52 +03:00
public function find(int $admin_id): array
{
2021-10-27 11:03:52 +03:00
if ( ! $this->db->get_where('users', ['id' => $admin_id])->num_rows())
{
throw new InvalidArgumentException('The provided admin ID was not found in the database: ' . $admin_id);
}
$admin = $this->db->get_where('users', ['id' => $admin_id])->row_array();
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();
unset(
$admin['settings']['id_users'],
$admin['settings']['password'],
$admin['settings']['salt']
);
return $admin;
}
/**
2021-10-27 11:03:52 +03:00
* Get a specific field value from the database.
*
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
*
2021-10-27 11:03:52 +03:00
* @return string 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
*/
2021-10-27 11:03:52 +03:00
public function value(int $admin_id, string $field): string
{
2021-10-27 11:03:52 +03:00
if (empty($field))
{
throw new InvalidArgumentException('The field argument is cannot be empty.');
}
2021-10-27 11:03:52 +03:00
if (empty($admin_id))
{
2021-10-27 11:03:52 +03:00
throw new InvalidArgumentException('The admin ID argument cannot be empty.');
}
2021-10-27 11:03:52 +03:00
// Check whether the admin exists.
$query = $this->db->get_where('users', ['id' => $admin_id]);
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);
}
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);
2021-10-27 11:03:52 +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);
}
2021-10-27 11:03:52 +03:00
return $admin[$field];
}
/**
2021-10-27 11:03:52 +03:00
* Get all admins that match the provided criteria.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:03:52 +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.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:03:52 +03:00
* @return array Returns an array of admins.
*/
2021-10-27 11:03:52 +03:00
public function get($where = NULL, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
{
2021-10-27 11:03:52 +03:00
$role_id = $this->get_admin_role_id();
if ($where !== NULL)
{
2021-10-27 11:03:52 +03:00
$this->db->where($where);
}
2021-10-27 11:03:52 +03:00
if ($order_by !== NULL)
{
2021-10-27 11:03:52 +03:00
$this->db->order_by($order_by);
}
2021-10-27 11:03:52 +03:00
$admins = $this->db->get_where('users', ['id_roles' => $role_id], $limit, $offset)->result_array();
foreach ($admins as &$admin)
{
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();
unset(
$admin['settings']['id_users'],
$admin['settings']['password'],
$admin['settings']['salt']
);
}
2021-10-27 11:03:52 +03:00
return $admins;
}
/**
2021-10-27 11:03:52 +03:00
* Get the admin role ID.
*
2021-10-27 11:03:52 +03:00
* @return int Returns the role ID.
*/
2021-10-27 11:03:52 +03:00
public function get_admin_role_id(): int
{
2021-10-27 11:03:52 +03:00
$role = $this->db->get_where('roles', ['slug' => DB_SLUG_ADMIN])->row_array();
2021-10-27 11:03:52 +03:00
if (empty($role))
{
2021-10-27 11:03:52 +03:00
throw new RuntimeException('The admin role was not found in the database.');
}
2021-10-27 11:03:52 +03:00
return $role['id'];
}
/**
2021-10-27 11:03:52 +03:00
* Save the admin settings.
*
2021-10-27 11:03:52 +03:00
* @param int $admin_id Admin ID.
* @param array $settings Associative array with the settings data.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:03:52 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:49:42 +03:00
protected function save_settings(int $admin_id, array $settings)
{
2021-10-27 11:03:52 +03:00
if (empty($settings))
{
2021-10-27 11:03:52 +03:00
throw new InvalidArgumentException('The settings argument cannot be empty.');
}
2021-10-27 11:03:52 +03:00
// Make sure the settings record exists in the database.
$count = $this->db->get_where('user_settings', ['id_users' => $admin_id])->num_rows();
if ( ! $count)
{
2021-10-27 11:03:52 +03:00
$this->db->insert('user_settings', ['id_users' => $admin_id]);
}
2021-10-27 11:03:52 +03:00
foreach ($settings as $name => $value)
{
2021-10-27 11:03:52 +03:00
$this->set_setting($admin_id, $name, $value);
}
2021-10-27 11:03:52 +03:00
}
2021-10-27 11:03:52 +03:00
/**
* Set the value of an admin setting.
*
* @param int $admin_id Admin ID.
* @param string $name Setting name.
* @param string $value Setting value.
*/
2021-10-27 11:49:42 +03:00
public function set_setting(int $admin_id, string $name, string $value)
2021-10-27 11:03:52 +03:00
{
if ( ! $this->db->update('user_settings', [$name => $value], ['id_users' => $admin_id]))
{
2021-10-27 11:03:52 +03:00
throw new RuntimeException('Could not set the new admin setting value: ' . $name);
}
}
/**
2021-10-27 11:03:52 +03:00
* Get the value of an admin setting.
*
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.
*/
2021-10-27 11:03:52 +03:00
public function get_setting(int $admin_id, string $name): string
{
2021-10-27 11:03:52 +03:00
$settings = $this->db->get_where('user_settings', ['id_users' => $admin_id])->row_array();
2021-10-27 11:03:52 +03:00
if (empty($settings))
{
2021-10-27 11:03:52 +03:00
throw new RuntimeException('The requested setting value was not found: ' . $admin_id);
}
2021-10-27 11:03:52 +03:00
return $settings[$name];
}
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);
}
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.
*/
public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
{
2021-10-29 13:38:41 +03:00
$role_id = $this->get_admin_role_id();
$admins = $this
2021-10-27 11:03:52 +03:00
->db
->select()
->from('users')
->where('id_roles', $role_id)
->like('first_name', $keyword)
->or_like('last_name', $keyword)
->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)
->limit($limit)
->offset($offset)
->order_by($order_by)
->get()
->result_array();
2021-10-29 13:38:41 +03:00
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;
2021-10-27 11:03:52 +03:00
}
2021-10-27 11:03:52 +03:00
/**
* Attach related resources to an admin.
*
* @param array $admin Associative array with the admin data.
* @param array $resources Resource names to be attached.
*
* @throws InvalidArgumentException
*/
2021-10-27 11:49:42 +03:00
public function attach(array &$admin, array $resources)
2021-10-27 11:03:52 +03:00
{
// Admins do not currently have any related resources (settings are already part of the admins).
}
}