Updated the secretaries model class.

This commit is contained in:
Alex Tselegidis 2021-10-27 10:04:35 +02:00
parent a4cd74863b
commit 8022ab28af
1 changed files with 364 additions and 349 deletions

View File

@ -12,136 +12,142 @@
* ---------------------------------------------------------------------------- */
/**
* Secretaries Model
* Secretaries model
*
* Handles the db actions that have to do with secretaries.
* Handles all the database operations of the secretary resource.
*
* @package Models
*/
class Secretaries_model extends EA_Model {
/**
* Secretaries_Model constructor.
* Secretaries_model constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->helper('general');
$this->load->helper('data_validation');
$this->load->helper('password');
$this->load->helper('validation');
}
/**
* Add (insert or update) a secretary user record into database.
* Save (insert or update) a secretary.
*
* @param array $secretary Contains the secretary user data.
* @param array $secretary Associative array with the secretary data.
*
* @return int Returns the record id.
* @return int Returns the secretary ID.
*
* @throws Exception When the secretary data are invalid (see validate() method).
* @throws InvalidArgumentException
*/
public function add($secretary)
public function save(array $secretary): int
{
$this->validate($secretary);
if ($this->exists($secretary) && ! isset($secretary['id']))
if (empty($secretary['id']))
{
$secretary['id'] = $this->find_record_id($secretary);
}
if ( ! isset($secretary['id']))
{
$secretary['id'] = $this->insert($secretary);
return $this->insert($secretary);
}
else
{
$secretary['id'] = $this->update($secretary);
return $this->update($secretary);
}
return (int)$secretary['id'];
}
/**
* Validate secretary user data before add() operation is executed.
* Validate the secretary data.
*
* @param array $secretary Contains the secretary user data.
* @param array $secretary Associative array with the secretary data.
*
* @return bool Returns the validation result.
*
* @throws Exception If secretary validation fails.
* @throws InvalidArgumentException
*/
public function validate($secretary)
public function validate(array $secretary): void
{
// If a record id is provided then check whether the record exists in the database.
if (isset($secretary['id']))
// If a secretary ID is provided then check whether the record really exists in the database.
if ( ! empty($provider['id']))
{
$num_rows = $this->db->get_where('users', ['id' => $secretary['id']])->num_rows();
$count = $this->db->get_where('users', ['id' => $secretary['id']])->num_rows();
if ($num_rows === 0)
if ( ! $count)
{
throw new Exception('Given secretary id does not exist in database: ' . $secretary['id']);
throw new InvalidArgumentException('The provided secretary ID does not exist in the database: ' . $secretary['id']);
}
}
// Validate 'providers' value data type (must be array)
if (isset($secretary['providers']) && ! is_array($secretary['providers']))
// Make sure all required fields are provided.
if (
empty($secretary['first_name'])
|| empty($secretary['last_name'])
|| empty($secretary['email'])
|| empty($secretary['phone_number'])
)
{
throw new Exception('Secretary providers value is not an array.');
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($secretary, TRUE));
}
// Validate required fields integrity.
if ( ! isset(
$secretary['last_name'],
$secretary['email'],
$secretary['phone_number']
))
{
throw new Exception('Not all required fields are provided: ' . print_r($secretary, TRUE));
}
// Validate secretary email address.
// Validate the email address.
if ( ! filter_var($secretary['email'], FILTER_VALIDATE_EMAIL))
{
throw new Exception('Invalid email address provided: ' . $secretary['email']);
throw new InvalidArgumentException('Invalid email address provided: ' . $secretary['email']);
}
// Check if username exists.
if (isset($secretary['settings']['username']))
// Validate secretary providers.
if (empty($provider['providers']) || ! is_array($provider['providers']))
{
$user_id = (isset($secretary['id'])) ? $secretary['id'] : '';
if ( ! $this->validate_username($secretary['settings']['username'], $user_id))
throw new InvalidArgumentException('The provided secretary providers are invalid: ' . print_r($secretary, TRUE));
}
else
{
throw new Exception ('Username already exists. Please select a different '
. 'username for this record.');
// Make sure the provided provider entries are numeric values.
foreach ($secretary['providers'] as $secretary_id)
{
if ( ! is_numeric($secretary_id))
{
throw new InvalidArgumentException('The provided secretary providers are invalid: ' . print_r($secretary, TRUE));
}
}
}
// Validate secretary password.
if (isset($secretary['settings']['password']))
// Make sure the username is unique.
if ( ! empty($secretary['settings']['username']))
{
$secretary_id = $secretary['id'] ?? NULL;
if ( ! $this->validate_username($secretary['settings']['username'], $secretary_id))
{
throw new InvalidArgumentException('The provided username is already in use, please use a different one.');
}
}
// Validate the password.
if ( ! empty($secretary['settings']['password']))
{
if (strlen($secretary['settings']['password']) < MIN_PASSWORD_LENGTH)
{
throw new Exception('The user password must be at least '
. MIN_PASSWORD_LENGTH . ' characters long.');
throw new InvalidArgumentException('The secretary password must be at least ' . MIN_PASSWORD_LENGTH . ' characters long.');
}
}
if ( ! isset($secretary['id']) && ! isset($secretary['settings']['password']))
// New users must always have a password value set.
if (empty($secretary['id']) && empty($secretary['settings']['password']))
{
throw new Exception('The user password cannot be empty for new users.');
throw new InvalidArgumentException('The provider password cannot be empty when inserting a new record.');
}
// Validate calendar view mode.
if (isset($secretary['settings']['calendar_view']) && ($secretary['settings']['calendar_view'] !== CALENDAR_VIEW_DEFAULT
&& $secretary['settings']['calendar_view'] !== CALENDAR_VIEW_TABLE))
// Validate calendar view type value.
if (
! empty($secretary['settings']['calendar_view'])
&& ! in_array($secretary['settings']['calendar_view'], [CALENDAR_VIEW_DEFAULT, CALENDAR_VIEW_TABLE])
)
{
throw new Exception('The calendar view setting must be either "' . CALENDAR_VIEW_DEFAULT
. '" or "' . CALENDAR_VIEW_TABLE . '", given: ' . $secretary['settings']['calendar_view']);
throw new InvalidArgumentException('The provided calendar view is invalid: ' . $secretary['settings']['calendar_view']);
}
// When inserting a record the email address must be unique.
$secretary_id = (isset($secretary['id'])) ? $secretary['id'] : '';
// Make sure the email address is unique.
$secretary_id = $secretary['id'] ?? NULL;
$num_rows = $this->db
->select('*')
$count = $this
->db
->select()
->from('users')
->join('roles', 'roles.id = users.id_roles', 'inner')
->where('roles.slug', DB_SLUG_SECRETARY)
@ -150,315 +156,154 @@ class Secretaries_model extends EA_Model {
->get()
->num_rows();
if ($num_rows > 0)
if ($count > 0)
{
throw new Exception('Given email address belongs to another secretary record. '
. 'Please use a different email.');
throw new InvalidArgumentException('The provided email address is already in use, please use a different one.');
}
return TRUE;
}
/**
* Validate Records Username
* Validate the secretary username.
*
* @param string $username The secretary records username.
* @param int $user_id The user record id.
* @param string $username Secretary username.
* @param int|null $secretary_id Secretary ID.
*
* @return bool Returns the validation result.
*/
public function validate_username($username, $user_id)
public function validate_username(string $username, int $secretary_id = NULL): bool
{
if ( ! empty($user_id))
if ( ! empty($secretary_id))
{
$this->db->where('id_users !=', $user_id);
$this->db->where('id_users !=', $secretary_id);
}
$this->db->where('username', $username);
return $this->db->get('user_settings')->num_rows() === 0;
return $this->db->get_where('user_settings', ['username' => $username])->num_rows() === 0;
}
/**
* Check whether a particular secretary record exists in the database.
* Insert a new secretary into the database.
*
* @param array $secretary Contains the secretary data. The 'email' value is required to be present at the moment.
* @param array $secretary Associative array with the secretary data.
*
* @return bool Returns whether the record exists or not.
* @return int Returns the secretary ID.
*
* @throws Exception When the 'email' value is not present on the $secretary argument.
* @throws RuntimeException
*/
public function exists($secretary)
protected function insert(array $secretary): int
{
if ( ! isset($secretary['email']))
{
throw new Exception('Secretary email is not provided: ' . print_r($secretary, TRUE));
}
$secretary['id_roles'] = $this->get_secretary_role_id();
// This method shouldn't depend on another method of this class.
$num_rows = $this->db
->select('*')
->from('users')
->join('roles', 'roles.id = users.id_roles', 'inner')
->where('users.email', $secretary['email'])
->where('roles.slug', DB_SLUG_SECRETARY)
->get()->num_rows();
return $num_rows > 0;
}
/**
* Find the database record id of a secretary.
*
* @param array $secretary Contains the secretary 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 $secretary array.
*/
public function find_record_id($secretary)
{
if ( ! isset($secretary['email']))
{
throw new Exception('Secretary email was not provided: ' . print_r($secretary, TRUE));
}
$result = $this->db
->select('users.id')
->from('users')
->join('roles', 'roles.id = users.id_roles', 'inner')
->where('users.email', $secretary['email'])
->where('roles.slug', DB_SLUG_SECRETARY)
->get();
if ($result->num_rows() == 0)
{
throw new Exception('Could not find secretary record id.');
}
return (int)$result->row()->id;
}
/**
* Insert a new secretary record into the database.
*
* @param array $secretary Contains the secretary data.
*
* @return int Returns the new record id.
*
* @throws Exception When the insert operation fails.
*/
protected function insert($secretary)
{
$providers = $secretary['providers'];
unset($secretary['providers']);
$settings = $secretary['settings'];
unset($secretary['settings']);
$secretary['id_roles'] = $this->get_secretary_role_id();
if ( ! $this->db->insert('users', $secretary))
{
throw new Exception('Could not insert secretary into the database.');
throw new RuntimeException('Could not insert secretary.');
}
$secretary['id'] = (int)$this->db->insert_id();
$secretary['id'] = $this->db->insert_id();
$settings['salt'] = generate_salt();
$settings['password'] = hash_password($settings['salt'], $settings['password']);
$this->save_providers($providers, $secretary['id']);
$this->save_settings($settings, $secretary['id']);
$this->save_settings($secretary['id'], $settings);
$this->save_provider_ids($secretary['id'], $providers);
return $secretary['id'];
}
/**
* Get the secretary users role id.
* Update an existing secretary.
*
* @return int Returns the role record id.
* @param array $secretary Associative array with the secretary data.
*
* @return int Returns the secretary ID.
*
* @throws RuntimeException
*/
public function get_secretary_role_id()
protected function update(array $secretary): int
{
return (int)$this->db->get_where('roles', ['slug' => DB_SLUG_SECRETARY])->row()->id;
}
/**
* Save a secretary handling users.
*
* @param array $providers Contains the provider ids that are handled by the secretary.
* @param int $secretary_id The selected secretary record.
*
* @throws Exception If $providers argument is invalid.
*/
protected function save_providers($providers, $secretary_id)
{
if ( ! is_array($providers))
{
throw new Exception('Invalid argument given $providers: ' . print_r($providers, TRUE));
}
// Delete old connections
$this->db->delete('secretaries_providers', ['id_users_secretary' => $secretary_id]);
if (count($providers) > 0)
{
foreach ($providers as $provider_id)
{
$this->db->insert('secretaries_providers', [
'id_users_secretary' => $secretary_id,
'id_users_provider' => $provider_id
]);
}
}
}
/**
* Save the secretary settings (used from insert or update operation).
*
* @param array $settings Contains the setting values.
* @param int $secretary_id Record id of the secretary.
*
* @throws Exception If $secretary_id argument is invalid.
* @throws Exception If $settings argument is invalid.
*/
protected function save_settings($settings, $secretary_id)
{
if ( ! is_numeric($secretary_id))
{
throw new Exception('Invalid $secretary_id argument given:' . $secretary_id);
}
if (count($settings) == 0 || ! is_array($settings))
{
throw new Exception('Invalid $settings argument given:' . print_r($settings, TRUE));
}
// Check if the setting record exists in db.
$num_rows = $this->db->get_where('user_settings',
['id_users' => $secretary_id])->num_rows();
if ($num_rows == 0)
{
$this->db->insert('user_settings', ['id_users' => $secretary_id]);
}
foreach ($settings as $name => $value)
{
$this->set_setting($name, $value, $secretary_id);
}
}
/**
* Set a secretary's setting value in the database.
*
* The secretary and settings record must already exist.
*
* @param string $setting_name The setting's name.
* @param string $value The setting's value.
* @param int $secretary_id The selected secretary id.
*
* @return bool
*/
public function set_setting($setting_name, $value, $secretary_id)
{
$this->db->where(['id_users' => $secretary_id]);
return $this->db->update('user_settings', [$setting_name => $value]);
}
/**
* Update an existing secretary record in the database.
*
* @param array $secretary Contains the secretary record data.
*
* @return int Returns the record id.
*
* @throws Exception When the update operation fails.
*/
protected function update($secretary)
{
$providers = $secretary['providers'];
$provider_ids = $secretary['providers'];
unset($secretary['providers']);
$settings = $secretary['settings'];
unset($secretary['settings']);
if (isset($settings['password']))
{
$salt = $this->db->get_where('user_settings', ['id_users' => $secretary['id']])->row()->salt;
$settings['password'] = hash_password($salt, $settings['password']);
}
$existing_settings = $this->db->get_where('user_settings', ['id_users' => $secretary['id']])->row_array();
$this->db->where('id', $secretary['id']);
if ( ! $this->db->update('users', $secretary))
if (empty($existing_settings))
{
throw new Exception('Could not update secretary record.');
throw new RuntimeException('No settings record found for secretary with ID: ' . $secretary['id']);
}
$this->save_providers($providers, $secretary['id']);
$this->save_settings($settings, $secretary['id']);
$settings['password'] = hash_password($existing_settings['salt'], $settings['password']);
}
if ( ! $this->db->update('users', $secretary, ['id' => $secretary['id']]))
{
throw new RuntimeException('Could not update secretary.');
}
$this->save_settings($secretary['id'], $settings);
$this->save_provider_ids($secretary['id'], $provider_ids);
return (int)$secretary['id'];
}
/**
* Delete an existing secretary record from the database.
* Remove an existing secretary from the database.
*
* @param int $secretary_id The secretary record id to be deleted.
* @param int $secretary_id Provider ID.
*
* @return bool Returns the delete operation result.
*
* @throws Exception When the $secretary_id is not a valid int value.
* @throws RuntimeException
*/
public function delete($secretary_id)
public function delete(int $secretary_id): void
{
if ( ! is_numeric($secretary_id))
if ( ! $this->db->delete('users', ['id' => $secretary_id]))
{
throw new Exception('Invalid argument type $secretary_id: ' . $secretary_id);
throw new RuntimeException('Could not delete secretary.');
}
$num_rows = $this->db->get_where('users', ['id' => $secretary_id])->num_rows();
if ($num_rows == 0)
{
return FALSE; // Record does not exist in database.
}
return $this->db->delete('users', ['id' => $secretary_id]);
}
/**
* Get a specific secretary record from the database.
* Get a specific secretary from the database.
*
* @param int $secretary_id The id of the record to be returned.
* @param int $secretary_id The ID of the record to be returned.
*
* @return array Returns an array with the secretary user data.
* @return array Returns an array with the secretary data.
*
* @throws Exception When the $secretary_id is not a valid int value.
* @throws Exception When given record id does not exist in the database.
* @throws InvalidArgumentException
*/
public function get_row($secretary_id)
public function find(int $secretary_id): array
{
if ( ! is_numeric($secretary_id))
if ( ! $this->db->get_where('users', ['id' => $secretary_id])->num_rows())
{
throw new Exception('$secretary_id argument is not a valid numeric value: ' . $secretary_id);
}
// Check if record exists
if ($this->db->get_where('users', ['id' => $secretary_id])->num_rows() == 0)
{
throw new Exception('The given secretary id does not match a record in the database.');
throw new InvalidArgumentException('The provided secretary ID was not found in the database: ' . $provider_id);
}
$secretary = $this->db->get_where('users', ['id' => $secretary_id])->row_array();
$secretary_providers = $this->db->get_where('secretaries_providers',
['id_users_secretary' => $secretary['id']])->result_array();
$secretary['providers'] = [];
foreach ($secretary_providers as $secretary_provider)
{
$secretary['providers'][] = $secretary_provider['id_users_provider'];
}
$secretary['settings'] = $this->db->get_where('user_settings', ['id_users' => $secretary_id])->row_array();
$secretary['settings'] = $this->db->get_where('user_settings',
['id_users' => $secretary['id']])->row_array();
unset($secretary['settings']['id_users'], $secretary['settings']['salt']);
unset(
$secretary['settings']['id_users'],
$secretary['settings']['password'],
$secretary['settings']['salt']
);
$secretary_provider_connections = $this->db->get_where('secretaries_providers', ['id_users_secretary' => $secretary_id])->result_array();
$secretary['providers'] = [];
foreach ($secretary_provider_connections as $secretary_provider_connection)
{
$secretary['providers'][] = (int)$secretary_provider_connection['id_users_provider'];
}
return $secretary;
}
@ -466,60 +311,55 @@ class Secretaries_model extends EA_Model {
/**
* Get a specific field value from the database.
*
* @param string $field_name The field name of the value to be returned.
* @param int $secretary_id Record id of the value to be returned.
* @param int $secretary_id Secretary ID.
* @param string $field Name of the value to be returned.
*
* @return string Returns the selected record value from the database.
* @return string Returns the selected secretary 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 int.
* @throws Exception When the secretary record does not exist in the database.
* @throws Exception When the selected field value is not present on database.
* @throws InvalidArgumentException
*/
public function get_value($field_name, $secretary_id)
public function value(int $secretary_id, string $field): string
{
if ( ! is_string($field_name))
if (empty($field))
{
throw new Exception('$field_name argument is not a string: ' . $field_name);
throw new InvalidArgumentException('The field argument is cannot be empty.');
}
if ( ! is_numeric($secretary_id))
if (empty($secretary_id))
{
throw new Exception('$secretary_id argument is not a valid numeric value: ' . $secretary_id);
throw new InvalidArgumentException('The secretary ID argument cannot be empty.');
}
// Check whether the secretary record exists.
$result = $this->db->get_where('users', ['id' => $secretary_id]);
// Check whether the secretary exists.
$query = $this->db->get_where('users', ['id' => $secretary_id]);
if ($result->num_rows() === 0)
if ( ! $query->num_rows())
{
throw new Exception('The record with the given id does not exist in the '
. 'database: ' . $secretary_id);
throw new InvalidArgumentException('The provided secretary ID was not found in the database: ' . $provider_id);
}
// Check if the required field name exist in database.
$row_data = $result->row_array();
// Check if the required field is part of the secretary data.
$secretary = $query->row_array();
if ( ! array_key_exists($field_name, $row_data))
if ( ! array_key_exists($field, $secretary))
{
throw new Exception('The given $field_name argument does not exist in the database: '
. $field_name);
throw new InvalidArgumentException('The requested field was not found in the secretary data: ' . $field);
}
return $row_data[$field_name];
return $secretary[$field];
}
/**
* Get all, or specific secretary records from database.
* Get all secretaries that match the provided criteria.
*
* @param mixed|null $where (OPTIONAL) The WHERE clause of the query to be executed.
* @param int|null $limit
* @param int|null $offset
* @param mixed|null $order_by
* @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.
*
* @return array Returns an array with secretary records.
* @return array Returns an array of secretaries.
*/
public function get_batch($where = NULL, $limit = NULL, $offset = NULL, $order_by = NULL)
public function get($where = NULL, int $limit = NULL, int $offset = NULL, string $order_by = NULL)
{
$role_id = $this->get_secretary_role_id();
@ -533,40 +373,215 @@ class Secretaries_model extends EA_Model {
$this->db->order_by($order_by);
}
$batch = $this->db->get_where('users', ['id_roles' => $role_id], $limit, $offset)->result_array();
$secretaries = $this->db->get_where('users', ['id_roles' => $role_id], $limit, $offset)->result_array();
// Include every secretary providers.
foreach ($batch as &$secretary)
foreach ($secretaries as &$secretary)
{
$secretary_providers = $this->db->get_where('secretaries_providers',
['id_users_secretary' => $secretary['id']])->result_array();
$secretary['settings'] = $this->db->get_where('user_settings', ['id_users' => $secretary['id']])->row_array();
unset(
$secretary['settings']['id_users'],
$secretary['settings']['password'],
$secretary['settings']['salt']
);
$secretary_provider_connections = $this->db->get_where('secretaries_providers', ['id_users_secretary' => $secretary['id']])->result_array();
$secretary['providers'] = [];
foreach ($secretary_providers as $secretary_provider)
foreach ($secretary_provider_connections as $secretary_provider_connection)
{
$secretary['providers'][] = $secretary_provider['id_users_provider'];
$secretary['providers'][] = (int)$secretary_provider_connection['id_users_provider'];
}
}
$secretary['settings'] = $this->db->get_where('user_settings',
['id_users' => $secretary['id']])->row_array();
unset($secretary['settings']['id_users']);
}
return $batch;
return $secretaries;
}
/**
* Get a secretaries setting from the database.
* Get the secretary role ID.
*
* @param string $setting_name The setting name that is going to be returned.
* @param int $secretary_id The selected provider id.
*
* @return string Returns the value of the selected user setting.
* @return int Returns the role ID.
*/
public function get_setting($setting_name, $secretary_id)
public function get_secretary_role_id(): int
{
$secretary_settings = $this->db->get_where('user_settings',
['id_users' => $secretary_id])->row_array();
return $secretary_settings[$setting_name];
$role = $this->db->get_where('roles', ['slug' => DB_SLUG_SECRETARY])->row_array();
if (empty($role))
{
throw new RuntimeException('The secretary role was not found in the database.');
}
return $role['id'];
}
/**
* Save the secretary settings.
*
* @param int $secretary_id Secretary ID.
* @param array $settings Associative array with the settings data.
*
* @throws InvalidArgumentException
*/
protected function save_settings(int $secretary_id, array $settings): void
{
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' => $secretary_id])->num_rows();
if ( ! $count)
{
$this->db->insert('user_settings', ['id_users' => $secretary_id]);
}
foreach ($settings as $name => $value)
{
$this->set_setting($secretary_id, $name, $value);
}
}
/**
* Set the value of a secretary setting.
*
* @param int $secretary_id Secretary ID.
* @param string $name Setting name.
* @param string $value Setting value.
*/
public function set_setting(int $secretary_id, string $name, string $value): void
{
if ( ! $this->db->update('user_settings', [$name => $value], ['id_users' => $secretary_id]))
{
throw new RuntimeException('Could not set the new secretary setting value: ' . $name);
}
}
/**
* Get the value of a secretary setting.
*
* @param int $secretary_id Secretary ID.
* @param string $name Setting name.
*
* @return string Returns the value of the requested user setting.
*/
public function get_setting(int $secretary_id, string $name): string
{
$settings = $this->db->get_where('user_settings', ['id_users' => $secretary_id])->row_array();
if (empty($settings[$name]))
{
throw new RuntimeException('The requested setting value was not found: ' . $secretary_id);
}
return $settings[$name];
}
/**
* Save the secretary provider IDs.
*
* @param int $secretary_id Secretary ID.
* @param array $provider_ids Provider IDs.
*/
protected function save_provider_ids(int $secretary_id, array $provider_ids): void
{
// Re-insert the secretary-provider connections.
$this->db->delete('secretary_providers', ['id_users_secretary' => $secretary_id]);
foreach ($provider_ids as $provider_id)
{
$secretary_provider_connection = [
'id_users_secretary' => $secretary_id,
'id_users_provider' => $provider_id
];
$this->db->insert('secretaries_providers', $secretary_provider_connection);
}
}
/**
* Get the query builder interface, configured for use with the users (secretary-filtered) table.
*
* @return CI_DB_query_builder
*/
public function query(): CI_DB_query_builder
{
$role_id = $this->get_secretary_role_id();
return $this->db->from('users')->where('id_roles', $role_id);
}
/**
* Search secretaries 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 secretaries.
*/
public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
{
$role_id = $this->get_secretary_role_id();
return $this
->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();
}
/**
* Attach related resources to a secretary.
*
* @param array $secretary Associative array with the secretary data.
* @param array $resources Resource names to be attached ("providers" supported).
*
* @throws InvalidArgumentException
*/
public function attach(array &$secretary, array $resources): void
{
if (empty($secretary) || empty($resources))
{
return;
}
foreach ($resources as $resource)
{
switch ($resource)
{
case 'providers':
$secretary['providers'] = $this
->db
->select('users.*')
->from('users')
->join('secretaries_providers', 'secretaries_providers.id_users_provider = users.id', 'inner')
->where('id_users_secretary', $secretary['id'])
->get()
->result_array();
break;
default:
throw new InvalidArgumentException('The requested secretary relation is not supported: ' . $resource);
}
}
}
}