2016-04-27 09:21:40 +03:00
|
|
|
<?php if ( ! defined('BASEPATH')) 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
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2015-07-20 22:41:24 +03:00
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2016-01-02 15:47:04 +02:00
|
|
|
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
2016-04-27 09:21:40 +03:00
|
|
|
* @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-08-26 14:53:11 +03:00
|
|
|
/**
|
|
|
|
* Secretaries Model
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2013-08-26 14:53:11 +03:00
|
|
|
* Handles the db actions that have to do with secretaries.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2013-08-26 14:53:11 +03:00
|
|
|
* Data Structure
|
|
|
|
* 'first_name'
|
|
|
|
* 'last_name'
|
|
|
|
* 'email'
|
|
|
|
* 'mobile_number'
|
|
|
|
* 'phone_number'
|
|
|
|
* 'address'
|
|
|
|
* 'city'
|
|
|
|
* 'state'
|
|
|
|
* 'zip_code'
|
|
|
|
* 'notes'
|
|
|
|
* 'id_roles'
|
2013-09-03 21:58:56 +03:00
|
|
|
* 'providers' >> array with provider ids that the secretary handles
|
2013-09-13 16:21:03 +03:00
|
|
|
* 'settings' >> array with the secretary settings
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2015-07-08 01:36:16 +03:00
|
|
|
* @package Models
|
2013-08-26 14:53:11 +03:00
|
|
|
*/
|
|
|
|
class Secretaries_Model extends CI_Model {
|
2013-07-27 00:30:44 +03:00
|
|
|
/**
|
|
|
|
* Class Constructor
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
/**
|
|
|
|
* Add (insert or update) a secretary user record into database.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2013-08-26 14:53:11 +03:00
|
|
|
* @param array $secretary Contains the secretary user data.
|
|
|
|
* @return int Returns the record id.
|
|
|
|
* @throws Exception When the secretary data are invalid (see validate() method).
|
|
|
|
*/
|
2013-07-27 00:30:44 +03:00
|
|
|
public function add($secretary) {
|
2013-09-26 19:06:57 +03:00
|
|
|
$this->validate($secretary);
|
2013-09-03 21:58:56 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
if ($this->exists($secretary) && !isset($secretary['id'])) {
|
|
|
|
$secretary['id'] = $this->find_record_id($secretary);
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
if (!isset($secretary['id'])) {
|
2016-04-27 09:21:40 +03:00
|
|
|
$secretary['id'] = $this->_insert($secretary);
|
2013-08-26 14:53:11 +03:00
|
|
|
} else {
|
2016-04-27 09:21:40 +03:00
|
|
|
$secretary['id'] = $this->_update($secretary);
|
2013-08-26 14:53:11 +03:00
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
return intval($secretary['id']);
|
2013-07-27 00:30:44 +03:00
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
/**
|
|
|
|
* Check whether a particular secretary record exists in the database.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
|
|
|
* @param array $secretary Contains the secretary data. The 'email' value is required to
|
2013-08-26 14:53:11 +03:00
|
|
|
* be present at the moment.
|
|
|
|
* @return bool Returns whether the record exists or not.
|
|
|
|
* @throws Exception When the 'email' value is not present on the $secretary argument.
|
|
|
|
*/
|
2013-07-27 00:30:44 +03:00
|
|
|
public function exists($secretary) {
|
2013-08-26 14:53:11 +03:00
|
|
|
if (!isset($secretary['email'])) {
|
|
|
|
throw new Exception('Secretary email is not provided: ' . print_r($secretary, TRUE));
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
// This method shouldn't depend on another method of this class.
|
|
|
|
$num_rows = $this->db
|
|
|
|
->select('*')
|
|
|
|
->from('ea_users')
|
|
|
|
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
|
|
|
|
->where('ea_users.email', $secretary['email'])
|
|
|
|
->where('ea_roles.slug', DB_SLUG_SECRETARY)
|
|
|
|
->get()->num_rows();
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
return ($num_rows > 0) ? TRUE : FALSE;
|
2013-07-27 00:30:44 +03:00
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
/**
|
|
|
|
* Insert a new sercretary record into the database.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2013-08-26 14:53:11 +03:00
|
|
|
* @param array $secretary Contains the secretary data.
|
|
|
|
* @return int Returns the new record id.
|
|
|
|
* @throws Exception When the insert operation fails.
|
|
|
|
*/
|
2016-04-27 09:21:40 +03:00
|
|
|
protected function _insert($secretary) {
|
2013-09-23 18:42:36 +03:00
|
|
|
$this->load->helper('general');
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
$providers = $secretary['providers'];
|
|
|
|
unset($secretary['providers']);
|
2013-09-03 21:58:56 +03:00
|
|
|
$settings = $secretary['settings'];
|
2016-04-27 09:21:40 +03:00
|
|
|
unset($secretary['settings']);
|
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
$secretary['id_roles'] = $this->get_secretary_role_id();
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
if (!$this->db->insert('ea_users', $secretary)) {
|
|
|
|
throw new Exception('Could not insert secretary into the database.');
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
$secretary['id'] = intval($this->db->insert_id());
|
2013-09-23 18:42:36 +03:00
|
|
|
$settings['salt'] = generate_salt();
|
|
|
|
$settings['password'] = hash_password($settings['salt'], $settings['password']);
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
$this->save_providers($providers, $secretary['id']);
|
|
|
|
$this->save_settings($settings, $secretary['id']);
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
return $secretary['id'];
|
2016-04-27 09:21:40 +03:00
|
|
|
}
|
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
/**
|
|
|
|
* Update an existing secretary record in the database.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2013-08-26 14:53:11 +03:00
|
|
|
* @param array $secretary Contains the secretary record data.
|
|
|
|
* @return int Retuns the record id.
|
|
|
|
* @throws Exception When the update operation fails.
|
|
|
|
*/
|
2016-04-27 09:21:40 +03:00
|
|
|
protected function _update($secretary) {
|
2013-09-23 18:42:36 +03:00
|
|
|
$this->load->helper('general');
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
$providers = $secretary['providers'];
|
|
|
|
unset($secretary['providers']);
|
2013-09-03 21:58:56 +03:00
|
|
|
$settings = $secretary['settings'];
|
2016-04-27 09:21:40 +03:00
|
|
|
unset($secretary['settings']);
|
|
|
|
|
2013-09-23 18:42:36 +03:00
|
|
|
if (isset($settings['password'])) {
|
|
|
|
$salt = $this->db->get_where('ea_user_settings', array('id_users' => $secretary['id']))->row()->salt;
|
|
|
|
$settings['password'] = hash_password($salt, $settings['password']);
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
$this->db->where('id', $secretary['id']);
|
|
|
|
if (!$this->db->update('ea_users', $secretary)){
|
|
|
|
throw new Exception('Could not update secretary record.');
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
$this->save_providers($providers, $secretary['id']);
|
2013-09-03 21:58:56 +03:00
|
|
|
$this->save_settings($settings, $secretary['id']);
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
return intval($secretary['id']);
|
2013-07-27 00:30:44 +03:00
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
/**
|
|
|
|
* Find the database record id of a secretary.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
|
|
|
* @param array $secretary Contains the secretary data. The 'email' value is required
|
2013-08-26 14:53:11 +03:00
|
|
|
* in order to find the record id.
|
|
|
|
* @return int Returns the record id
|
|
|
|
* @throws Exception When the 'email' value is not present on the $secretary array.
|
|
|
|
*/
|
2013-07-27 00:30:44 +03:00
|
|
|
public function find_record_id($secretary) {
|
2013-08-26 14:53:11 +03:00
|
|
|
if (!isset($secretary['email'])) {
|
|
|
|
throw new Exception('Secretary email was not provided: ' . print_r($secretary, TRUE));
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
$result = $this->db
|
|
|
|
->select('ea_users.id')
|
|
|
|
->from('ea_users')
|
|
|
|
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
|
|
|
|
->where('ea_users.email', $secretary['email'])
|
|
|
|
->where('ea_roles.slug', DB_SLUG_SECRETARY)
|
|
|
|
->get();
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
if ($result->num_rows() == 0) {
|
|
|
|
throw new Exception('Could not find secretary record id.');
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
return intval($result->row()->id);
|
2013-07-27 00:30:44 +03:00
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
/**
|
|
|
|
* Validate secretary user data before add() operation is executed.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2013-08-26 14:53:11 +03:00
|
|
|
* @param array $secretary Contains the secretary user data.
|
|
|
|
* @return bool Returns the validation result.
|
|
|
|
*/
|
2013-07-27 00:30:44 +03:00
|
|
|
public function validate($secretary) {
|
2013-08-26 14:53:11 +03:00
|
|
|
$this->load->helper('data_validation');
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-09-26 19:06:57 +03:00
|
|
|
// If a record id is provided then check whether the record exists in the database.
|
|
|
|
if (isset($secretary['id'])) {
|
|
|
|
$num_rows = $this->db->get_where('ea_users', array('id' => $secretary['id']))
|
|
|
|
->num_rows();
|
|
|
|
if ($num_rows == 0) {
|
|
|
|
throw new Exception('Given secretary id does not exist in database: ' . $secretary['id']);
|
2013-08-26 14:53:11 +03:00
|
|
|
}
|
2013-09-26 19:06:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate 'providers' value datatype (must be array)
|
|
|
|
if (isset($secretary['providers']) && !is_array($secretary['providers'])) {
|
|
|
|
throw new Exception('Secretary providers value is not an array.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate required fields integrity.
|
|
|
|
if (!isset($secretary['last_name'])
|
|
|
|
|| !isset($secretary['email'])
|
2016-04-27 09:21:40 +03:00
|
|
|
|| !isset($secretary['phone_number'])) {
|
2013-09-26 19:06:57 +03:00
|
|
|
throw new Exception('Not all required fields are provided : ' . print_r($secretary, TRUE));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate secretary email address.
|
|
|
|
if (!filter_var($secretary['email'], FILTER_VALIDATE_EMAIL)) {
|
|
|
|
throw new Exception('Invalid email address provided : ' . $secretary['email']);
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-11-23 21:10:31 +02:00
|
|
|
// Check if username exists.
|
|
|
|
if (isset($secretary['settings']['username'])) {
|
|
|
|
$user_id = (isset($secretary['id'])) ? $secretary['id'] : '';
|
|
|
|
if (!$this->validate_username($secretary['settings']['username'], $user_id)) {
|
2016-04-27 09:21:40 +03:00
|
|
|
throw new Exception ('Username already exists. Please select a different '
|
2013-11-23 21:10:31 +02:00
|
|
|
. 'username for this record.');
|
|
|
|
}
|
|
|
|
}
|
2013-09-26 19:06:57 +03:00
|
|
|
|
2013-11-23 21:10:31 +02:00
|
|
|
// Validate secretary password.
|
2013-09-26 19:06:57 +03:00
|
|
|
if (isset($secretary['settings']['password'])) {
|
|
|
|
if (strlen($secretary['settings']['password']) < MIN_PASSWORD_LENGTH) {
|
2016-04-27 09:21:40 +03:00
|
|
|
throw new Exception('The user password must be at least '
|
2013-09-26 19:06:57 +03:00
|
|
|
. MIN_PASSWORD_LENGTH . ' characters long.');
|
2013-09-23 18:42:36 +03:00
|
|
|
}
|
2013-08-26 14:53:11 +03:00
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-11-23 21:10:31 +02:00
|
|
|
// When inserting a record the email address must be unique.
|
2013-11-24 19:01:46 +02:00
|
|
|
$secretary_id = (isset($secretary['id'])) ? $secretary['id'] : '';
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-11-23 21:10:31 +02:00
|
|
|
$num_rows = $this->db
|
|
|
|
->select('*')
|
|
|
|
->from('ea_users')
|
|
|
|
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
|
|
|
|
->where('ea_roles.slug', DB_SLUG_SECRETARY)
|
|
|
|
->where('ea_users.email', $secretary['email'])
|
2013-11-24 19:01:46 +02:00
|
|
|
->where('ea_users.id <>', $secretary_id)
|
2013-11-23 21:10:31 +02:00
|
|
|
->get()
|
|
|
|
->num_rows();
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-11-24 19:01:46 +02:00
|
|
|
if ($num_rows > 0) {
|
2016-04-27 09:21:40 +03:00
|
|
|
throw new Exception('Given email address belongs to another secretary record. '
|
2013-11-23 21:10:31 +02:00
|
|
|
. 'Please use a different email.');
|
|
|
|
}
|
2013-09-26 19:06:57 +03:00
|
|
|
|
|
|
|
return TRUE;
|
2013-07-27 00:30:44 +03:00
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
/**
|
|
|
|
* Delete an existing secretary record from the database.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2013-08-26 14:53:11 +03:00
|
|
|
* @param numeric $secretary_id The secretary record id to be deleted.
|
|
|
|
* @return bool Returns the delete operation result.
|
|
|
|
* @throws Exception When the $secretary_id is not a valid numeric value.
|
|
|
|
*/
|
|
|
|
public function delete($secretary_id) {
|
|
|
|
if (!is_numeric($secretary_id)) {
|
|
|
|
throw new Exception('Invalid argument type $secretary_id : ' . $secretary_id);
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
$num_rows = $this->db->get_where('ea_users', array('id' => $secretary_id))->num_rows();
|
|
|
|
if ($num_rows == 0) {
|
|
|
|
return FALSE; // Record does not exist in database.
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
return $this->db->delete('ea_users', array('id' => $secretary_id));
|
2013-07-27 00:30:44 +03:00
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
/**
|
|
|
|
* Get a specific secretary record from the database.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2013-08-26 14:53:11 +03:00
|
|
|
* @param numeric $secretary_id The id of the record to be returned.
|
|
|
|
* @return array Returns an array with the secretary user data.
|
|
|
|
* @throws Exception When the $secretary_id is not a valid numeric value.
|
2013-09-03 21:58:56 +03:00
|
|
|
* @throws Exception When given record id does not exist in the database.
|
2013-08-26 14:53:11 +03:00
|
|
|
*/
|
2013-07-27 00:30:44 +03:00
|
|
|
public function get_row($secretary_id) {
|
2013-08-26 14:53:11 +03:00
|
|
|
if (!is_numeric($secretary_id)) {
|
|
|
|
throw new Exception('$secretary_id argument is not a valid numeric value: ' . $secretary_id);
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
// Check if record exists
|
|
|
|
if ($this->db->get_where('ea_users', array('id' => $secretary_id))->num_rows() == 0) {
|
|
|
|
throw new Exception('The given secretary id does not match a record in the database.');
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
$secretary = $this->db->get_where('ea_users', array('id' => $secretary_id))->row_array();
|
2016-04-27 09:21:40 +03:00
|
|
|
|
|
|
|
$secretary_providers = $this->db->get_where('ea_secretaries_providers',
|
2013-09-03 21:58:56 +03:00
|
|
|
array('id_users_secretary' => $secretary['id']))->result_array();
|
|
|
|
$secretary['providers'] = array();
|
|
|
|
foreach($secretary_providers as $secretary_provider) {
|
|
|
|
$secretary['providers'][] = $secretary_provider['id_users_provider'];
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
|
|
|
$secretary['settings'] = $this->db->get_where('ea_user_settings',
|
2013-09-03 21:58:56 +03:00
|
|
|
array('id_users' => $secretary['id']))->row_array();
|
2013-10-19 13:34:32 +03:00
|
|
|
unset($secretary['settings']['id_users'], $secretary['settings']['salt']);
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
return $secretary;
|
2013-07-27 00:30:44 +03:00
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
/**
|
|
|
|
* Get a specific field value from the database.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2013-08-26 14:53:11 +03:00
|
|
|
* @param string $field_name The field name of the value to be returned.
|
|
|
|
* @param numeric $secretary_id Record id of the value to be returned.
|
|
|
|
* @return string Returns the selected record value from the database.
|
|
|
|
* @throws Exception When the $field_name argument is not a valid string.
|
|
|
|
* @throws Exception When the $secretary_id is not a valid numeric.
|
|
|
|
* @throws Exception When the secretary record does not exist in the database.
|
|
|
|
* @throws Exception When the selected field value is not present on database.
|
|
|
|
*/
|
2013-07-27 00:30:44 +03:00
|
|
|
public function get_value($field_name, $secretary_id) {
|
2013-08-26 14:53:11 +03:00
|
|
|
if (!is_string($field_name)) {
|
|
|
|
throw new Exception('$field_name argument is not a string : ' . $field_name);
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
if (!is_numeric($secretary_id)) {
|
|
|
|
throw new Exception('$secretary_id argument is not a valid numeric value: ' . $secretary_id);
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
|
|
|
// Check whether the secretary record exists.
|
2013-08-26 14:53:11 +03:00
|
|
|
$result = $this->db->get_where('ea_users', array('id' => $secretary_id));
|
|
|
|
if ($result->num_rows() == 0) {
|
|
|
|
throw new Exception('The record with the given id does not exist in the '
|
|
|
|
. 'database : ' . $secretary_id);
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
// Check if the required field name exist in database.
|
|
|
|
$provider = $result->row_array();
|
|
|
|
if (!isset($provider[$field_name])) {
|
2016-04-27 09:21:40 +03:00
|
|
|
throw new Exception('The given $field_name argument does not exist in the '
|
2013-08-26 14:53:11 +03:00
|
|
|
. 'database: ' . $field_name);
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
return $provider[$field_name];
|
2013-07-27 00:30:44 +03:00
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
/**
|
|
|
|
* Get all, or specific secretary records from database.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
|
|
|
* @param string|array $where_clause (OPTIONAL) The WHERE clause of the query to be executed.
|
2013-08-26 14:53:11 +03:00
|
|
|
* Use this to get specific secretary records.
|
|
|
|
* @return array Returns an array with secretary records.
|
|
|
|
*/
|
2013-07-27 00:30:44 +03:00
|
|
|
public function get_batch($where_clause = '') {
|
2013-08-26 14:53:11 +03:00
|
|
|
$role_id = $this->get_secretary_role_id();
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
if ($where_clause != '') {
|
|
|
|
$this->db->where($where_clause);
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
$this->db->where('id_roles', $role_id);
|
|
|
|
$batch = $this->db->get('ea_users')->result_array();
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
// Include every secretary providers.
|
2013-08-26 14:53:11 +03:00
|
|
|
foreach ($batch as &$secretary) {
|
2016-04-27 09:21:40 +03:00
|
|
|
$secretary_providers = $this->db->get_where('ea_secretaries_providers',
|
2013-08-26 14:53:11 +03:00
|
|
|
array('id_users_secretary' => $secretary['id']))->result_array();
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
$secretary['providers'] = array();
|
|
|
|
foreach($secretary_providers as $secretary_provider) {
|
|
|
|
$secretary['providers'][] = $secretary_provider['id_users_provider'];
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
|
|
|
$secretary['settings'] = $this->db->get_where('ea_user_settings',
|
2013-09-03 21:58:56 +03:00
|
|
|
array('id_users' => $secretary['id']))->row_array();
|
2013-09-13 16:21:03 +03:00
|
|
|
unset($secretary['settings']['id_users']);
|
2016-04-27 09:21:40 +03:00
|
|
|
}
|
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
return $batch;
|
2013-07-27 00:30:44 +03:00
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
/**
|
2016-04-27 09:21:40 +03:00
|
|
|
* Get the secretary users role id.
|
|
|
|
*
|
|
|
|
* @return int Returns the role record id.
|
2013-08-26 14:53:11 +03:00
|
|
|
*/
|
|
|
|
public function get_secretary_role_id() {
|
|
|
|
return intval($this->db->get_where('ea_roles', array('slug' => DB_SLUG_SECRETARY))->row()->id);
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
/**
|
|
|
|
* Save a secretary hasndling users.
|
|
|
|
* @param array $providers Contains the provider ids that are handled by the secretary.
|
|
|
|
* @param numeric $secretary_id The selected secretary record.
|
|
|
|
*/
|
2016-04-27 09:21:40 +03:00
|
|
|
protected function save_providers($providers, $secretary_id) {
|
2013-08-26 14:53:11 +03:00
|
|
|
if (!is_array($providers)) {
|
|
|
|
throw new Exception('Invalid argument given $providers: ' . print_r($providers, TRUE));
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
// Delete old connections
|
|
|
|
$this->db->delete('ea_secretaries_providers', array('id_users_secretary' => $secretary_id));
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-08-26 14:53:11 +03:00
|
|
|
if (count($providers) > 0) {
|
|
|
|
foreach ($providers as $provider_id) {
|
|
|
|
$this->db->insert('ea_secretaries_providers', array(
|
|
|
|
'id_users_secretary' => $secretary_id,
|
|
|
|
'id_users_provider' => $provider_id
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
/**
|
|
|
|
* Save the secretary settings (used from insert or update operation).
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @param array $settings Contains the setting values.
|
|
|
|
* @param numeric $secretary_id Record id of the secretary.
|
|
|
|
*/
|
2016-04-27 09:21:40 +03:00
|
|
|
protected function save_settings($settings, $secretary_id) {
|
2013-09-03 21:58:56 +03:00
|
|
|
if (!is_numeric($secretary_id)) {
|
|
|
|
throw new Exception('Invalid $provider_id argument given :' . $secretary_id);
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
if (count($settings) == 0 || !is_array($settings)) {
|
|
|
|
throw new Exception('Invalid $settings argument given:' . print_r($settings, TRUE));
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
// Check if the setting record exists in db.
|
2016-04-27 09:21:40 +03:00
|
|
|
$num_rows = $this->db->get_where('ea_user_settings',
|
2013-09-13 16:21:03 +03:00
|
|
|
array('id_users' => $secretary_id))->num_rows();
|
|
|
|
if ($num_rows == 0) {
|
2013-09-03 21:58:56 +03:00
|
|
|
$this->db->insert('ea_user_settings', array('id_users' => $secretary_id));
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-09-13 16:21:03 +03:00
|
|
|
foreach($settings as $name => $value) {
|
2013-09-03 21:58:56 +03:00
|
|
|
$this->set_setting($name, $value, $secretary_id);
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
/**
|
|
|
|
* Get a providers setting from the database.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2013-09-13 16:21:03 +03:00
|
|
|
* @param string $setting_name The setting name that is going to be returned.
|
2013-09-03 21:58:56 +03:00
|
|
|
* @param int $secretary_id The selected provider id.
|
|
|
|
* @return string Returs the value of the selected user setting.
|
|
|
|
*/
|
|
|
|
public function get_setting($setting_name, $secretary_id) {
|
2016-04-27 09:21:40 +03:00
|
|
|
$provider_settings = $this->db->get_where('ea_user_settings',
|
2013-09-03 21:58:56 +03:00
|
|
|
array('id_users' => $secretary_id))->row_array();
|
|
|
|
return $provider_settings[$setting_name];
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
/**
|
2016-04-27 09:21:40 +03:00
|
|
|
* Set a provider's setting value in the database.
|
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* The provider and settings record must already exist.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @param string $setting_name The setting's name.
|
|
|
|
* @param string $value The setting's value.
|
|
|
|
* @param numeric $secretary_id The selected provider id.
|
|
|
|
*/
|
|
|
|
public function set_setting($setting_name, $value, $secretary_id) {
|
|
|
|
$this->db->where(array('id_users' => $secretary_id));
|
|
|
|
return $this->db->update('ea_user_settings', array($setting_name => $value));
|
|
|
|
}
|
2016-04-27 09:21:40 +03:00
|
|
|
|
2013-11-23 21:10:31 +02:00
|
|
|
/**
|
2016-04-27 09:21:40 +03:00
|
|
|
* Validate Records Username
|
|
|
|
*
|
2013-11-23 21:10:31 +02:00
|
|
|
* @param string $username The provider records username.
|
|
|
|
* @param numeric $user_id The user record id.
|
|
|
|
* @return bool Returns the validation result.
|
|
|
|
*/
|
|
|
|
public function validate_username($username, $user_id) {
|
2016-04-27 09:21:40 +03:00
|
|
|
$num_rows = $this->db->get_where('ea_user_settings',
|
2013-11-23 21:10:31 +02:00
|
|
|
array('username' => $username, 'id_users <> ' => $user_id))->num_rows();
|
|
|
|
return ($num_rows > 0) ? FALSE : TRUE;
|
|
|
|
}
|
2013-07-27 00:30:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* End of file secretaries_model.php */
|
2016-04-27 09:21:40 +03:00
|
|
|
/* Location: ./application/models/secretaries_model.php */
|