iflrandevu/application/models/Secretaries_model.php

607 lines
19 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:04:35 +03:00
* Secretaries model
*
2021-10-27 11:04:35 +03:00
* Handles all the database operations of the secretary resource.
*
2015-07-08 01:36:16 +03:00
* @package Models
*/
class Secretaries_model extends EA_Model {
/**
* @var array
*/
protected $casts = [
'id' => 'integer',
'id_roles' => 'integer',
];
/**
2021-10-27 11:04:35 +03:00
* Save (insert or update) a secretary.
*
2021-10-27 11:04:35 +03:00
* @param array $secretary Associative array with the secretary data.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:35 +03:00
* @return int Returns the secretary ID.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:35 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:04:35 +03:00
public function save(array $secretary): int
{
$this->validate($secretary);
2021-10-27 11:04:35 +03:00
if (empty($secretary['id']))
{
2021-10-27 11:04:35 +03:00
return $this->insert($secretary);
}
else
{
2021-10-27 11:04:35 +03:00
return $this->update($secretary);
}
}
/**
2021-10-27 11:04:35 +03:00
* Validate the secretary data.
*
2021-10-27 11:04:35 +03:00
* @param array $secretary Associative array with the secretary data.
*
2021-10-27 11:04:35 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:49:42 +03:00
public function validate(array $secretary)
{
2021-10-27 11:04:35 +03:00
// If a secretary ID is provided then check whether the record really exists in the database.
if ( ! empty($provider['id']))
{
2021-10-27 11:04:35 +03:00
$count = $this->db->get_where('users', ['id' => $secretary['id']])->num_rows();
2020-12-05 12:38:57 +03:00
2021-10-27 11:04:35 +03:00
if ( ! $count)
{
2021-10-27 11:04:35 +03:00
throw new InvalidArgumentException('The provided secretary ID does not exist in the database: ' . $secretary['id']);
}
}
2021-10-27 11:04:35 +03:00
// Make sure all required fields are provided.
if (
empty($secretary['first_name'])
|| empty($secretary['last_name'])
|| empty($secretary['email'])
|| empty($secretary['phone_number'])
)
{
2021-10-27 11:04:35 +03:00
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($secretary, TRUE));
}
2021-10-27 11:04:35 +03:00
// Validate the email address.
if ( ! filter_var($secretary['email'], FILTER_VALIDATE_EMAIL))
{
2021-10-27 11:04:35 +03:00
throw new InvalidArgumentException('Invalid email address provided: ' . $secretary['email']);
}
2021-10-27 11:04:35 +03:00
// Validate secretary providers.
2021-10-28 14:51:46 +03:00
if (empty($secretary['providers']) || ! is_array($secretary['providers']))
{
2021-10-27 11:04:35 +03:00
throw new InvalidArgumentException('The provided secretary providers are invalid: ' . print_r($secretary, TRUE));
}
else
{
// 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));
}
}
}
2021-10-27 11:04:35 +03:00
// Make sure the username is unique.
if ( ! empty($secretary['settings']['username']))
{
2021-10-27 11:04:35 +03:00
$secretary_id = $secretary['id'] ?? NULL;
if ( ! $this->validate_username($secretary['settings']['username'], $secretary_id))
{
2021-10-27 11:04:35 +03:00
throw new InvalidArgumentException('The provided username is already in use, please use a different one.');
}
}
2021-10-27 11:04:35 +03:00
// Validate the password.
if ( ! empty($secretary['settings']['password']))
{
if (strlen($secretary['settings']['password']) < MIN_PASSWORD_LENGTH)
{
2021-10-27 11:04:35 +03:00
throw new InvalidArgumentException('The secretary password must be at least ' . MIN_PASSWORD_LENGTH . ' characters long.');
}
}
2021-10-27 11:04:35 +03:00
// New users must always have a password value set.
if (empty($secretary['id']) && empty($secretary['settings']['password']))
{
2021-10-27 11:04:35 +03:00
throw new InvalidArgumentException('The provider password cannot be empty when inserting a new record.');
}
2021-10-27 11:04:35 +03:00
// Validate calendar view type value.
if (
! empty($secretary['settings']['calendar_view'])
&& ! in_array($secretary['settings']['calendar_view'], [CALENDAR_VIEW_DEFAULT, CALENDAR_VIEW_TABLE])
)
{
2021-10-27 11:04:35 +03:00
throw new InvalidArgumentException('The provided calendar view is invalid: ' . $secretary['settings']['calendar_view']);
}
2021-10-27 11:04:35 +03:00
// Make sure the email address is unique.
$secretary_id = $secretary['id'] ?? NULL;
2021-10-27 11:04:35 +03:00
$count = $this
->db
->select()
->from('users')
->join('roles', 'roles.id = users.id_roles', 'inner')
->where('roles.slug', DB_SLUG_SECRETARY)
->where('users.email', $secretary['email'])
2020-12-05 12:38:57 +03:00
->where('users.id !=', $secretary_id)
->get()
->num_rows();
2021-10-27 11:04:35 +03:00
if ($count > 0)
{
2021-10-27 11:04:35 +03:00
throw new InvalidArgumentException('The provided email address is already in use, please use a different one.');
}
}
/**
2021-10-27 11:04:35 +03:00
* Validate the secretary username.
*
2021-10-27 11:04:35 +03:00
* @param string $username Secretary username.
* @param int|null $secretary_id Secretary ID.
*
* @return bool Returns the validation result.
*/
2021-10-27 11:04:35 +03:00
public function validate_username(string $username, int $secretary_id = NULL): bool
{
2021-10-27 11:04:35 +03:00
if ( ! empty($secretary_id))
{
2021-10-27 11:04:35 +03:00
$this->db->where('id_users !=', $secretary_id);
}
2021-10-27 11:04:35 +03:00
return $this->db->get_where('user_settings', ['username' => $username])->num_rows() === 0;
}
/**
2021-10-27 11:04:35 +03:00
* Insert a new secretary into the database.
*
2021-10-27 11:04:35 +03:00
* @param array $secretary Associative array with the secretary data.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:35 +03:00
* @return int Returns the secretary ID.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:35 +03:00
* @throws RuntimeException
*/
2021-10-27 11:04:35 +03:00
protected function insert(array $secretary): int
{
2021-10-27 11:04:35 +03:00
$secretary['id_roles'] = $this->get_secretary_role_id();
2021-10-27 11:04:35 +03:00
$providers = $secretary['providers'];
unset($secretary['providers']);
2021-10-27 11:04:35 +03:00
$settings = $secretary['settings'];
unset($secretary['settings']);
2021-10-27 11:04:35 +03:00
if ( ! $this->db->insert('users', $secretary))
{
2021-10-27 11:04:35 +03:00
throw new RuntimeException('Could not insert secretary.');
}
2021-10-27 11:04:35 +03:00
$secretary['id'] = $this->db->insert_id();
$settings['salt'] = generate_salt();
$settings['password'] = hash_password($settings['salt'], $settings['password']);
2021-10-27 11:04:35 +03:00
$this->save_settings($secretary['id'], $settings);
$this->save_provider_ids($secretary['id'], $providers);
2021-10-27 11:04:35 +03:00
return $secretary['id'];
}
/**
2021-10-27 11:04:35 +03:00
* Update an existing secretary.
*
2021-10-27 11:04:35 +03:00
* @param array $secretary Associative array with the secretary data.
*
2021-10-27 11:04:35 +03:00
* @return int Returns the secretary ID.
*
2021-10-27 11:04:35 +03:00
* @throws RuntimeException
*/
2021-10-27 11:04:35 +03:00
protected function update(array $secretary): int
{
2021-10-27 11:04:35 +03:00
$provider_ids = $secretary['providers'];
unset($secretary['providers']);
2021-10-27 11:04:35 +03:00
$settings = $secretary['settings'];
unset($secretary['settings']);
2021-10-27 11:04:35 +03:00
if (isset($settings['password']))
{
2021-10-27 11:04:35 +03:00
$existing_settings = $this->db->get_where('user_settings', ['id_users' => $secretary['id']])->row_array();
if (empty($existing_settings))
{
throw new RuntimeException('No settings record found for secretary with ID: ' . $secretary['id']);
}
$settings['password'] = hash_password($existing_settings['salt'], $settings['password']);
}
2021-10-27 11:04:35 +03:00
if ( ! $this->db->update('users', $secretary, ['id' => $secretary['id']]))
{
throw new RuntimeException('Could not update secretary.');
}
2021-10-27 11:04:35 +03:00
$this->save_settings($secretary['id'], $settings);
$this->save_provider_ids($secretary['id'], $provider_ids);
2021-10-27 11:04:35 +03:00
return (int)$secretary['id'];
}
/**
2021-10-27 11:04:35 +03:00
* Remove an existing secretary from the database.
*
* @param int $secretary_id Provider ID.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:35 +03:00
* @throws RuntimeException
*/
2021-10-27 11:49:42 +03:00
public function delete(int $secretary_id)
{
2021-10-27 11:04:35 +03:00
if ( ! $this->db->delete('users', ['id' => $secretary_id]))
{
throw new RuntimeException('Could not delete secretary.');
}
}
/**
2021-10-27 11:04:35 +03:00
* Get a specific secretary from the database.
*
* @param int $secretary_id The ID of the record to be returned.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:35 +03:00
* @return array Returns an array with the secretary data.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:35 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:04:35 +03:00
public function find(int $secretary_id): array
{
2021-10-27 11:04:35 +03:00
if ( ! $this->db->get_where('users', ['id' => $secretary_id])->num_rows())
{
throw new InvalidArgumentException('The provided secretary ID was not found in the database: ' . $secretary_id);
}
2021-10-27 11:04:35 +03:00
$secretary = $this->db->get_where('users', ['id' => $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']['password'],
$secretary['settings']['salt']
);
$secretary_provider_connections = $this->db->get_where('secretaries_providers', ['id_users_secretary' => $secretary_id])->result_array();
$secretary['providers'] = [];
2021-10-27 11:04:35 +03:00
foreach ($secretary_provider_connections as $secretary_provider_connection)
{
2021-10-27 11:04:35 +03:00
$secretary['providers'][] = (int)$secretary_provider_connection['id_users_provider'];
}
2021-10-27 11:04:35 +03:00
return $secretary;
}
/**
2021-10-27 11:04:35 +03:00
* Get a specific field value from the database.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:35 +03:00
* @param int $secretary_id Secretary ID.
* @param string $field Name of the value to be returned.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:35 +03:00
* @return string Returns the selected secretary value from the database.
*
* @throws InvalidArgumentException
*/
2021-10-27 11:04:35 +03:00
public function value(int $secretary_id, string $field): string
{
2021-10-27 11:04:35 +03:00
if (empty($field))
{
2021-10-27 11:04:35 +03:00
throw new InvalidArgumentException('The field argument is cannot be empty.');
}
2021-10-27 11:04:35 +03:00
if (empty($secretary_id))
{
2021-10-27 11:04:35 +03:00
throw new InvalidArgumentException('The secretary ID argument cannot be empty.');
}
2021-10-27 11:04:35 +03:00
// Check whether the secretary exists.
$query = $this->db->get_where('users', ['id' => $secretary_id]);
if ( ! $query->num_rows())
{
throw new InvalidArgumentException('The provided secretary ID was not found in the database: ' . $secretary_id);
}
2021-10-27 11:04:35 +03:00
// Check if the required field is part of the secretary data.
$secretary = $query->row_array();
if ( ! array_key_exists($field, $secretary))
{
2021-10-27 11:04:35 +03:00
throw new InvalidArgumentException('The requested field was not found in the secretary data: ' . $field);
}
2021-10-27 11:04:35 +03:00
return $secretary[$field];
}
/**
2021-10-27 11:04:35 +03:00
* Get all secretaries that match the provided criteria.
*
2021-10-27 11:04:35 +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.
*
2021-10-27 11:04:35 +03:00
* @return array Returns an array of secretaries.
*/
2021-10-27 11:04:35 +03:00
public function get($where = NULL, int $limit = NULL, int $offset = NULL, string $order_by = NULL)
{
2021-10-27 11:04:35 +03:00
$role_id = $this->get_secretary_role_id();
2021-10-27 11:04:35 +03:00
if ($where !== NULL)
{
2021-10-27 11:04:35 +03:00
$this->db->where($where);
}
2021-10-27 11:04:35 +03:00
if ($order_by !== NULL)
{
2021-10-27 11:04:35 +03:00
$this->db->order_by($order_by);
2013-11-23 21:10:31 +02:00
}
2021-10-27 11:04:35 +03:00
$secretaries = $this->db->get_where('users', ['id_roles' => $role_id], $limit, $offset)->result_array();
2021-10-27 11:04:35 +03:00
foreach ($secretaries as &$secretary)
{
$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_provider_connections as $secretary_provider_connection)
{
$secretary['providers'][] = (int)$secretary_provider_connection['id_users_provider'];
}
}
return $secretaries;
}
/**
2021-10-27 11:04:35 +03:00
* Get the secretary role ID.
*
2021-10-27 11:04:35 +03:00
* @return int Returns the role ID.
*/
2021-10-27 11:04:35 +03:00
public function get_secretary_role_id(): int
{
2021-10-27 11:04:35 +03:00
$role = $this->db->get_where('roles', ['slug' => DB_SLUG_SECRETARY])->row_array();
2021-10-27 11:04:35 +03:00
if (empty($role))
{
2021-10-27 11:04:35 +03:00
throw new RuntimeException('The secretary role was not found in the database.');
}
2021-10-27 11:04:35 +03:00
return $role['id'];
}
/**
2021-10-27 11:04:35 +03:00
* Save the secretary settings.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:35 +03:00
* @param int $secretary_id Secretary ID.
* @param array $settings Associative array with the settings data.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:35 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:49:42 +03:00
protected function save_settings(int $secretary_id, array $settings)
{
2021-10-27 11:04:35 +03:00
if (empty($settings))
{
2021-10-27 11:04:35 +03:00
throw new InvalidArgumentException('The settings argument cannot be empty.');
}
2021-10-27 11:04:35 +03:00
// Make sure the settings record exists in the database.
$count = $this->db->get_where('user_settings', ['id_users' => $secretary_id])->num_rows();
if ( ! $count)
{
2021-10-27 11:04:35 +03:00
$this->db->insert('user_settings', ['id_users' => $secretary_id]);
}
2021-10-27 11:04:35 +03:00
foreach ($settings as $name => $value)
{
2021-10-27 11:04:35 +03:00
$this->set_setting($secretary_id, $name, $value);
}
}
/**
2021-10-27 11:04:35 +03:00
* Set the value of a secretary setting.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:35 +03:00
* @param int $secretary_id Secretary ID.
* @param string $name Setting name.
* @param string|null $value Setting value.
*/
public function set_setting(int $secretary_id, string $name, string $value = NULL)
{
2021-10-27 11:04:35 +03:00
if ( ! $this->db->update('user_settings', [$name => $value], ['id_users' => $secretary_id]))
{
2021-10-27 11:04:35 +03:00
throw new RuntimeException('Could not set the new secretary setting value: ' . $name);
}
2021-10-27 11:04:35 +03:00
}
2021-10-27 11:04:35 +03:00
/**
* 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();
2021-10-27 11:04:35 +03:00
if (empty($settings[$name]))
{
2021-10-27 11:04:35 +03:00
throw new RuntimeException('The requested setting value was not found: ' . $secretary_id);
}
2021-10-27 11:04:35 +03:00
return $settings[$name];
}
/**
2021-10-27 11:04:35 +03:00
* Save the secretary provider IDs.
2020-12-05 12:38:57 +03:00
*
2021-10-27 11:04:35 +03:00
* @param int $secretary_id Secretary ID.
* @param array $provider_ids Provider IDs.
*/
2021-10-27 11:49:42 +03:00
protected function save_provider_ids(int $secretary_id, array $provider_ids)
{
2021-10-27 11:04:35 +03:00
// Re-insert the secretary-provider connections.
2021-10-28 14:51:46 +03:00
$this->db->delete('secretaries_providers', ['id_users_secretary' => $secretary_id]);
2021-10-27 11:04:35 +03:00
foreach ($provider_ids as $provider_id)
{
2021-10-27 11:04:35 +03:00
$secretary_provider_connection = [
'id_users_secretary' => $secretary_id,
'id_users_provider' => $provider_id
];
2021-10-27 11:04:35 +03:00
$this->db->insert('secretaries_providers', $secretary_provider_connection);
}
2021-10-27 11:04:35 +03:00
}
2021-10-27 11:04:35 +03:00
/**
* 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();
2021-10-27 11:04:35 +03:00
return $this->db->from('users')->where('id_roles', $role_id);
}
2021-10-27 11:04:35 +03:00
/**
* 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();
$secretaries = $this
2021-10-27 11:04:35 +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();
foreach ($secretaries as &$secretary)
{
$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_provider_connections as $secretary_provider_connection)
{
$secretary['providers'][] = (int)$secretary_provider_connection['id_users_provider'];
}
}
return $secretaries;
}
/**
2021-10-27 11:04:35 +03:00
* Attach related resources to a secretary.
*
2021-10-27 11:04:35 +03:00
* @param array $secretary Associative array with the secretary data.
* @param array $resources Resource names to be attached ("providers" supported).
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:35 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:49:42 +03:00
public function attach(array &$secretary, array $resources)
{
2021-10-27 11:04:35 +03:00
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);
}
}
}
}