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
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
2020-03-11 12:10:59 +03:00
|
|
|
*
|
2015-07-20 22:41:24 +03:00
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2020-03-11 12:10:59 +03:00
|
|
|
* @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
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
/**
|
2016-07-17 13:49:27 +03:00
|
|
|
* Admins Model Class
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* Handles the database actions for admin users management.
|
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
|
|
|
*/
|
2020-11-16 11:29:36 +03:00
|
|
|
class Admins_model extends EA_Model {
|
2020-11-12 15:51:10 +03:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->load->helper('general');
|
|
|
|
$this->load->helper('data_validation');
|
|
|
|
}
|
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
/**
|
2013-08-26 14:53:11 +03:00
|
|
|
* Add (insert or update) an admin user record into database.
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @param array $admin Contains the admin user data.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @return int Returns the record id.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @throws Exception When the admin data are invalid (see validate() method).
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function add($admin)
|
|
|
|
{
|
2013-09-26 19:06:57 +03:00
|
|
|
$this->validate($admin);
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
|
|
if ($this->exists($admin) && ! isset($admin['id']))
|
|
|
|
{
|
2013-07-28 01:48:19 +03:00
|
|
|
$admin['id'] = $this->find_record_id($admin);
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
|
|
if ( ! isset($admin['id']))
|
|
|
|
{
|
2020-04-23 21:48:20 +03:00
|
|
|
$admin['id'] = $this->insert($admin);
|
2018-01-23 12:08:37 +03:00
|
|
|
}
|
|
|
|
else
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-23 21:48:20 +03:00
|
|
|
$admin['id'] = $this->update($admin);
|
2013-07-28 01:48:19 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2017-08-08 10:49:39 +03:00
|
|
|
return (int)$admin['id'];
|
2013-07-27 00:30:44 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
/**
|
|
|
|
* Validate admin user data before add() operation is executed.
|
|
|
|
*
|
|
|
|
* @param array $admin Contains the admin user data.
|
|
|
|
*
|
|
|
|
* @return bool Returns the validation result.
|
|
|
|
*
|
|
|
|
* @throws Exception When data are invalid.
|
|
|
|
*/
|
|
|
|
public function validate($admin)
|
|
|
|
{
|
|
|
|
// If a record id is provided then check whether the record exists in the database.
|
|
|
|
if (isset($admin['id']))
|
|
|
|
{
|
2020-04-23 21:48:20 +03:00
|
|
|
$num_rows = $this->db->get_where('users', ['id' => $admin['id']])->num_rows();
|
|
|
|
|
|
|
|
if ($num_rows === 0)
|
2020-04-22 22:48:56 +03:00
|
|
|
{
|
|
|
|
throw new Exception('Given admin id does not exist in database: ' . $admin['id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate required fields integrity.
|
2020-12-05 12:38:57 +03:00
|
|
|
if ( ! isset(
|
|
|
|
$admin['last_name'],
|
|
|
|
$admin['email'],
|
|
|
|
$admin['phone_number']
|
|
|
|
))
|
2020-04-22 22:48:56 +03:00
|
|
|
{
|
|
|
|
throw new Exception('Not all required fields are provided: ' . print_r($admin, TRUE));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate admin email address.
|
|
|
|
if ( ! filter_var($admin['email'], FILTER_VALIDATE_EMAIL))
|
|
|
|
{
|
|
|
|
throw new Exception('Invalid email address provided: ' . $admin['email']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if username exists.
|
|
|
|
if (isset($admin['settings']['username']))
|
|
|
|
{
|
|
|
|
$user_id = (isset($admin['id'])) ? $admin['id'] : '';
|
|
|
|
if ( ! $this->validate_username($admin['settings']['username'], $user_id))
|
|
|
|
{
|
|
|
|
throw new Exception ('Username already exists. Please select a different '
|
|
|
|
. 'username for this record.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate admin password
|
|
|
|
if (isset($admin['settings']['password']))
|
|
|
|
{
|
|
|
|
if (strlen($admin['settings']['password']) < MIN_PASSWORD_LENGTH)
|
|
|
|
{
|
|
|
|
throw new Exception('The user password must be at least '
|
|
|
|
. MIN_PASSWORD_LENGTH . ' characters long.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate calendar view mode.
|
|
|
|
if (isset($admin['settings']['calendar_view']) && ($admin['settings']['calendar_view'] !== CALENDAR_VIEW_DEFAULT
|
|
|
|
&& $admin['settings']['calendar_view'] !== CALENDAR_VIEW_TABLE))
|
|
|
|
{
|
|
|
|
throw new Exception('The calendar view setting must be either "' . CALENDAR_VIEW_DEFAULT
|
|
|
|
. '" or "' . CALENDAR_VIEW_TABLE . '", given: ' . $admin['settings']['calendar_view']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// When inserting a record the email address must be unique.
|
2020-12-05 12:38:57 +03:00
|
|
|
$admin_id = isset($admin['id']) ? $admin['id'] : '';
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
|
|
$num_rows = $this->db
|
|
|
|
->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();
|
|
|
|
|
|
|
|
if ($num_rows > 0)
|
|
|
|
{
|
|
|
|
throw new Exception('Given email address belongs to another admin record. '
|
|
|
|
. 'Please use a different email.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE; // Operation completed successfully.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate Records Username
|
|
|
|
*
|
|
|
|
* @param string $username The provider records username.
|
|
|
|
* @param int $user_id The user record id.
|
|
|
|
*
|
|
|
|
* @return bool Returns the validation result.
|
|
|
|
*/
|
|
|
|
public function validate_username($username, $user_id)
|
|
|
|
{
|
2020-12-08 12:00:31 +03:00
|
|
|
if ( ! empty($user_id))
|
|
|
|
{
|
|
|
|
$this->db->where('id_users !=', $user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db->where('username', $username);
|
2020-12-05 12:38:57 +03:00
|
|
|
|
2020-12-08 12:00:31 +03:00
|
|
|
return $this->db->get('user_settings')->num_rows() === 0;
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
/**
|
|
|
|
* Check whether a particular admin record exists in the database.
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2017-09-08 15:03:45 +03:00
|
|
|
* @param array $admin Contains the admin data. The 'email' value is required to be present at the moment.
|
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @return bool Returns whether the record exists or not.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @throws Exception When the 'email' value is not present on the $admin argument.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function exists($admin)
|
|
|
|
{
|
|
|
|
if ( ! isset($admin['email']))
|
|
|
|
{
|
2013-07-28 01:48:19 +03:00
|
|
|
throw new Exception('Admin email is not provided: ' . print_r($admin, TRUE));
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
// This method shouldn't depend on another method of this class.
|
|
|
|
$num_rows = $this->db
|
2017-09-15 14:36:37 +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('users.email', $admin['email'])
|
|
|
|
->where('roles.slug', DB_SLUG_ADMIN)
|
2017-09-15 14:36:37 +03:00
|
|
|
->get()->num_rows();
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
return $num_rows > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the database record id of an admin user.
|
|
|
|
*
|
|
|
|
* @param array $admin Contains the admin data. The 'email' value is required in order to find the record id.
|
|
|
|
*
|
|
|
|
* @return int Returns the record id
|
|
|
|
*
|
|
|
|
* @throws Exception When the 'email' value is not present on the $admin array.
|
|
|
|
*/
|
|
|
|
public function find_record_id($admin)
|
|
|
|
{
|
|
|
|
if ( ! isset($admin['email']))
|
|
|
|
{
|
|
|
|
throw new Exception('Admin email was not provided: ' . print_r($admin, TRUE));
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->db
|
2020-04-23 21:48:20 +03:00
|
|
|
->select('users.id')
|
|
|
|
->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('users.email', $admin['email'])
|
|
|
|
->where('roles.slug', DB_SLUG_ADMIN)
|
2020-04-22 22:48:56 +03:00
|
|
|
->get();
|
|
|
|
|
|
|
|
if ($result->num_rows() == 0)
|
|
|
|
{
|
|
|
|
throw new Exception('Could not find admin record id.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)$result->row()->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
|
|
|
/**
|
|
|
|
* Insert a new admin record into the database.
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @param array $admin Contains the admin data.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @return int Returns the new record id.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @throws Exception When the insert operation fails.
|
|
|
|
*/
|
2020-04-23 21:48:20 +03:00
|
|
|
protected function insert($admin)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2013-07-28 01:48:19 +03:00
|
|
|
$admin['id_roles'] = $this->get_admin_role_id();
|
2013-09-03 21:58:56 +03:00
|
|
|
$settings = $admin['settings'];
|
|
|
|
unset($admin['settings']);
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
|
|
$this->db->trans_begin();
|
|
|
|
|
2020-04-23 21:48:20 +03:00
|
|
|
if ( ! $this->db->insert('users', $admin))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2013-07-28 01:48:19 +03:00
|
|
|
throw new Exception('Could not insert admin into the database.');
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2017-08-08 10:49:39 +03:00
|
|
|
$admin['id'] = (int)$this->db->insert_id();
|
2013-09-03 21:58:56 +03:00
|
|
|
$settings['id_users'] = $admin['id'];
|
2013-09-23 18:42:36 +03:00
|
|
|
$settings['salt'] = generate_salt();
|
|
|
|
$settings['password'] = hash_password($settings['salt'], $settings['password']);
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2020-03-11 12:10:59 +03:00
|
|
|
// Insert admin settings.
|
2020-04-23 21:48:20 +03:00
|
|
|
if ( ! $this->db->insert('user_settings', $settings))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2013-09-03 21:58:56 +03:00
|
|
|
$this->db->trans_rollback();
|
|
|
|
throw new Exception('Could not insert admin settings into the database.');
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
$this->db->trans_complete();
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
return $admin['id'];
|
2017-09-15 14:36:37 +03:00
|
|
|
}
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
/**
|
|
|
|
* Get the admin users role id.
|
|
|
|
*
|
|
|
|
* @return int Returns the role record id.
|
|
|
|
*/
|
|
|
|
public function get_admin_role_id()
|
|
|
|
{
|
2020-04-23 21:48:20 +03:00
|
|
|
return (int)$this->db->get_where('roles', ['slug' => DB_SLUG_ADMIN])->row()->id;
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
/**
|
|
|
|
* Update an existing admin record in the database.
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @param array $admin Contains the admin record data.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2016-10-10 19:29:48 +03:00
|
|
|
* @return int Returns the record id.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @throws Exception When the update operation fails.
|
|
|
|
*/
|
2020-04-23 21:48:20 +03:00
|
|
|
protected function update($admin)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2013-09-03 21:58:56 +03:00
|
|
|
$settings = $admin['settings'];
|
|
|
|
unset($admin['settings']);
|
|
|
|
$settings['id_users'] = $admin['id'];
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
|
|
if (isset($settings['password']))
|
|
|
|
{
|
2020-04-23 21:48:20 +03:00
|
|
|
$salt = $this->db->get_where('user_settings', ['id_users' => $admin['id']])->row()->salt;
|
2013-09-23 18:42:36 +03:00
|
|
|
$settings['password'] = hash_password($salt, $settings['password']);
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
$this->db->where('id', $admin['id']);
|
2020-04-23 21:48:20 +03:00
|
|
|
if ( ! $this->db->update('users', $admin))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2013-07-28 01:48:19 +03:00
|
|
|
throw new Exception('Could not update admin record.');
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
$this->db->where('id_users', $settings['id_users']);
|
2020-04-23 21:48:20 +03:00
|
|
|
if ( ! $this->db->update('user_settings', $settings))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2013-09-03 21:58:56 +03:00
|
|
|
throw new Exception('Could not update admin settings.');
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2017-08-08 10:49:39 +03:00
|
|
|
return (int)$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
|
|
|
/**
|
|
|
|
* Delete an existing admin record from the database.
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2017-09-08 15:03:45 +03:00
|
|
|
* @param int $admin_id The admin record id to be deleted.
|
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @return bool Returns the delete operation result.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
|
|
|
* @throws Exception When the $admin_id is not a valid int value.
|
|
|
|
* @throws Exception When the record to be deleted is the only one admin user left on the system.
|
2013-07-28 01:48:19 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function delete($admin_id)
|
|
|
|
{
|
|
|
|
if ( ! is_numeric($admin_id))
|
|
|
|
{
|
2016-07-17 13:49:27 +03:00
|
|
|
throw new Exception('Invalid argument type $admin_id: ' . $admin_id);
|
2013-07-28 01:48:19 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
// There must be always at least one admin user. If this is the only admin
|
|
|
|
// the system, it cannot be deleted.
|
2020-04-23 21:48:20 +03:00
|
|
|
$admin_count = $this->db->get_where('users',
|
2017-09-15 14:36:37 +03:00
|
|
|
['id_roles' => $this->get_admin_role_id()])->num_rows();
|
|
|
|
if ($admin_count == 1)
|
|
|
|
{
|
2013-07-28 01:48:19 +03:00
|
|
|
throw new Exception('Record could not be deleted. The system requires at least '
|
2017-09-15 14:36:37 +03:00
|
|
|
. 'one admin user.');
|
2013-07-28 01:48:19 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2020-04-23 21:48:20 +03:00
|
|
|
$num_rows = $this->db->get_where('users', ['id' => $admin_id])->num_rows();
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($num_rows == 0)
|
|
|
|
{
|
2013-07-28 01:48:19 +03:00
|
|
|
return FALSE; // Record does not exist in database.
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2020-04-23 21:48:20 +03:00
|
|
|
return $this->db->delete('users', ['id' => $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
|
|
|
/**
|
|
|
|
* Get a specific admin record from the database.
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2017-09-08 15:03:45 +03:00
|
|
|
* @param int $admin_id The id of the record to be returned.
|
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @return array Returns an array with the admin user data.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
|
|
|
* @throws Exception When the $admin_id is not a valid int value.
|
2013-07-28 01:48:19 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function get_row($admin_id)
|
|
|
|
{
|
|
|
|
if ( ! is_numeric($admin_id))
|
|
|
|
{
|
2013-07-28 01:48:19 +03:00
|
|
|
throw new Exception('$admin_id argument is not a valid numeric value: ' . $admin_id);
|
|
|
|
}
|
2013-09-13 16:21:03 +03:00
|
|
|
|
|
|
|
// Check if record exists
|
2020-04-23 21:48:20 +03:00
|
|
|
if ($this->db->get_where('users', ['id' => $admin_id])->num_rows() == 0)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2013-09-13 16:21:03 +03:00
|
|
|
throw new Exception('The given admin id does not match a record in the database.');
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2020-04-23 21:48:20 +03:00
|
|
|
$admin = $this->db->get_where('users', ['id' => $admin_id])->row_array();
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2020-04-23 21:48:20 +03:00
|
|
|
$admin['settings'] = $this->db->get_where('user_settings',
|
2017-09-15 14:36:37 +03:00
|
|
|
['id_users' => $admin_id])->row_array();
|
2013-09-03 21:58:56 +03:00
|
|
|
unset($admin['settings']['id_users']);
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
return $admin;
|
2013-07-27 00:30:44 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
/**
|
|
|
|
* Get a specific field value from the database.
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @param string $field_name The field name of the value to be returned.
|
2017-09-08 15:03:45 +03:00
|
|
|
* @param int $admin_id Record id of the value to be returned.
|
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @return string Returns the selected record value from the database.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @throws Exception When the $field_name argument is not a valid string.
|
2017-09-08 15:03:45 +03:00
|
|
|
* @throws Exception When the $admin_id is not a valid int.
|
2013-07-28 01:48:19 +03:00
|
|
|
* @throws Exception When the admin record does not exist in the database.
|
|
|
|
* @throws Exception When the selected field value is not present on database.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function get_value($field_name, $admin_id)
|
|
|
|
{
|
|
|
|
if ( ! is_string($field_name))
|
|
|
|
{
|
2016-07-17 13:49:27 +03:00
|
|
|
throw new Exception('$field_name argument is not a string: ' . $field_name);
|
2013-07-28 01:48:19 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
|
|
if ( ! is_numeric($admin_id))
|
|
|
|
{
|
2013-07-28 01:48:19 +03:00
|
|
|
throw new Exception('$admin_id argument is not a valid numeric value: ' . $admin_id);
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2020-03-11 12:10:59 +03:00
|
|
|
// Check whether the admin record exists.
|
2020-04-23 21:48:20 +03:00
|
|
|
$result = $this->db->get_where('users', ['id' => $admin_id]);
|
2020-12-05 12:43:38 +03:00
|
|
|
|
|
|
|
if ($result->num_rows() === 0)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2013-07-28 01:48:19 +03:00
|
|
|
throw new Exception('The record with the given id does not exist in the '
|
2017-09-15 14:36:37 +03:00
|
|
|
. 'database: ' . $admin_id);
|
2013-07-28 01:48:19 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
// Check if the required field name exist in database.
|
2020-12-05 12:43:38 +03:00
|
|
|
$row_data = $result->row_array();
|
|
|
|
|
|
|
|
if ( ! array_key_exists($field_name, $row_data))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
throw new Exception('The given $field_name argument does not exist in the '
|
|
|
|
. 'database: ' . $field_name);
|
2013-07-28 01:48:19 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2020-12-05 12:43:38 +03:00
|
|
|
return $row_data[$field_name];
|
2013-07-27 00:30:44 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
/**
|
|
|
|
* Get all, or specific admin records from database.
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2020-12-05 12:38:57 +03:00
|
|
|
* @param mixed|null $where (OPTIONAL) The WHERE clause of the query to be executed.
|
2020-04-06 21:34:32 +03:00
|
|
|
* @param int|null $limit
|
|
|
|
* @param int|null $offset
|
2020-12-05 12:38:57 +03:00
|
|
|
* @param mixed|null $order_by
|
|
|
|
*
|
2013-07-28 01:48:19 +03:00
|
|
|
* @return array Returns an array with admin records.
|
|
|
|
*/
|
2020-12-05 12:38:57 +03:00
|
|
|
public function get_batch($where = NULL, $limit = NULL, $offset = NULL, $order_by = NULL)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2013-08-26 14:53:11 +03:00
|
|
|
$role_id = $this->get_admin_role_id();
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2020-04-06 21:34:32 +03:00
|
|
|
if ($where !== NULL)
|
|
|
|
{
|
|
|
|
$this->db->where($where);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($order_by !== NULL)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-06 21:34:32 +03:00
|
|
|
$this->db->order_by($order_by);
|
2013-07-28 01:48:19 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2020-04-23 21:48:20 +03:00
|
|
|
$batch = $this->db->get_where('users', ['id_roles' => $role_id], $limit, $offset)->result_array();
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
// Get every admin settings.
|
2017-09-15 14:36:37 +03:00
|
|
|
foreach ($batch as &$admin)
|
|
|
|
{
|
2020-04-23 21:48:20 +03:00
|
|
|
$admin['settings'] = $this->db->get_where('user_settings',
|
2017-09-15 14:36:37 +03:00
|
|
|
['id_users' => $admin['id']])->row_array();
|
2013-09-03 21:58:56 +03:00
|
|
|
unset($admin['settings']['id_users']);
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2013-07-28 01:48:19 +03:00
|
|
|
return $batch;
|
2013-07-27 00:30:44 +03:00
|
|
|
}
|
|
|
|
}
|