mirror of
https://github.com/alextselegidis/easyappointments.git
synced 2024-11-25 09:23:08 +03:00
Code enhancements in the model classes
This commit is contained in:
parent
14017a49f5
commit
9c3d253456
13 changed files with 363 additions and 341 deletions
|
@ -49,9 +49,9 @@ class Admins extends API_V1_Controller {
|
|||
{
|
||||
try
|
||||
{
|
||||
$conditions = $id !== NULL ? ['id' => $id] : NULL;
|
||||
$where = $id !== NULL ? ['id' => $id] : NULL;
|
||||
|
||||
$admins = $this->admins_model->get_batch($conditions);
|
||||
$admins = $this->admins_model->get_batch($where);
|
||||
|
||||
if ($id !== NULL && count($admins) === 0)
|
||||
{
|
||||
|
|
|
@ -55,16 +55,16 @@ class Appointments extends API_V1_Controller {
|
|||
{
|
||||
try
|
||||
{
|
||||
$conditions = [
|
||||
$where = [
|
||||
'is_unavailable' => FALSE
|
||||
];
|
||||
|
||||
if ($id !== NULL)
|
||||
{
|
||||
$conditions['id'] = $id;
|
||||
$where['id'] = $id;
|
||||
}
|
||||
|
||||
$appointments = $this->appointments_model->get_batch($conditions, NULL, NULL, NULL, array_key_exists('aggregates', $_GET));
|
||||
$appointments = $this->appointments_model->get_batch($where, NULL, NULL, NULL, array_key_exists('aggregates', $_GET));
|
||||
|
||||
if ($id !== NULL && count($appointments) === 0)
|
||||
{
|
||||
|
|
|
@ -49,9 +49,9 @@ class Unavailabilities extends API_V1_Controller {
|
|||
{
|
||||
try
|
||||
{
|
||||
$conditions = $id !== NULL ? ['id' => $id] : ['is_unavailable' => true];
|
||||
$where = $id !== NULL ? ['id' => $id] : ['is_unavailable' => true];
|
||||
|
||||
$unavailabilities = $this->appointments_model->get_batch($conditions);
|
||||
$unavailabilities = $this->appointments_model->get_batch($where);
|
||||
|
||||
if ($id !== NULL && count($unavailabilities) === 0)
|
||||
{
|
||||
|
|
|
@ -108,7 +108,7 @@ class Availability {
|
|||
// every reserved appointment is considered to be a taken space in the plan.
|
||||
$working_day = strtolower(date('l', strtotime($date)));
|
||||
|
||||
$date_working_plan = $working_plan[$working_day] ?? null;
|
||||
$date_working_plan = $working_plan[$working_day] ?? NULL;
|
||||
|
||||
// Search if the $date is an custom availability period added outside the normal working plan.
|
||||
if (isset($working_plan_exceptions[$date]))
|
||||
|
@ -349,7 +349,7 @@ class Availability {
|
|||
|
||||
$working_day = strtolower(date('l', strtotime($date)));
|
||||
|
||||
$date_working_plan = $working_plan[$working_day] ?? null;
|
||||
$date_working_plan = $working_plan[$working_day] ?? NULL;
|
||||
|
||||
// Search if the $date is an custom availability period added outside the normal working plan.
|
||||
if (isset($working_plan_exceptions[$date]))
|
||||
|
|
|
@ -80,9 +80,11 @@ class Admins_model extends EA_Model {
|
|||
}
|
||||
|
||||
// Validate required fields integrity.
|
||||
if ( ! isset($admin['last_name'])
|
||||
|| ! isset($admin['email'])
|
||||
|| ! isset($admin['phone_number']))
|
||||
if ( ! isset(
|
||||
$admin['last_name'],
|
||||
$admin['email'],
|
||||
$admin['phone_number']
|
||||
))
|
||||
{
|
||||
throw new Exception('Not all required fields are provided: ' . print_r($admin, TRUE));
|
||||
}
|
||||
|
@ -123,7 +125,7 @@ class Admins_model extends EA_Model {
|
|||
}
|
||||
|
||||
// When inserting a record the email address must be unique.
|
||||
$admin_id = (isset($admin['id'])) ? $admin['id'] : '';
|
||||
$admin_id = isset($admin['id']) ? $admin['id'] : '';
|
||||
|
||||
$num_rows = $this->db
|
||||
->select('*')
|
||||
|
@ -131,7 +133,7 @@ class Admins_model extends EA_Model {
|
|||
->join('roles', 'roles.id = users.id_roles', 'inner')
|
||||
->where('roles.slug', DB_SLUG_ADMIN)
|
||||
->where('users.email', $admin['email'])
|
||||
->where('users.id <>', $admin_id)
|
||||
->where('users.id !=', $admin_id)
|
||||
->get()
|
||||
->num_rows();
|
||||
|
||||
|
@ -154,8 +156,11 @@ class Admins_model extends EA_Model {
|
|||
*/
|
||||
public function validate_username($username, $user_id)
|
||||
{
|
||||
$num_rows = $this->db->get_where('user_settings',
|
||||
['username' => $username, 'id_users <> ' => $user_id])->num_rows();
|
||||
$num_rows = $this->db->get_where('user_settings', [
|
||||
'username' => $username,
|
||||
'id_users !=' => $user_id
|
||||
])->num_rows();
|
||||
|
||||
return $num_rows > 0 ? FALSE : TRUE;
|
||||
}
|
||||
|
||||
|
@ -419,14 +424,14 @@ class Admins_model extends EA_Model {
|
|||
/**
|
||||
* Get all, or specific admin records from database.
|
||||
*
|
||||
* @param mixed|null $where (OPTIONAL) The WHERE clause of the query to be executed. Use this to get
|
||||
* specific admin records.
|
||||
* @param mixed|null $order_by
|
||||
* @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
|
||||
*
|
||||
* @return array Returns an array with admin records.
|
||||
*/
|
||||
public function get_batch($where = NULL, $order_by = NULL, $limit = NULL, $offset = NULL)
|
||||
public function get_batch($where = NULL, $limit = NULL, $offset = NULL, $order_by = NULL)
|
||||
{
|
||||
$role_id = $this->get_admin_role_id();
|
||||
|
||||
|
|
|
@ -23,9 +23,7 @@ class Appointments_model extends EA_Model {
|
|||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->load->helper('data_validation');
|
||||
|
||||
$this->load->library('timezones');
|
||||
}
|
||||
|
||||
|
@ -109,7 +107,8 @@ class Appointments_model extends EA_Model {
|
|||
->where('users.id', $appointment['id_users_provider'])
|
||||
->where('roles.slug', DB_SLUG_PROVIDER)
|
||||
->get()->num_rows();
|
||||
if ($num_rows == 0)
|
||||
|
||||
if ($num_rows === 0)
|
||||
{
|
||||
throw new Exception('Appointment provider id is invalid.');
|
||||
}
|
||||
|
@ -124,15 +123,16 @@ class Appointments_model extends EA_Model {
|
|||
->where('users.id', $appointment['id_users_customer'])
|
||||
->where('roles.slug', DB_SLUG_CUSTOMER)
|
||||
->get()->num_rows();
|
||||
if ($num_rows == 0)
|
||||
|
||||
if ($num_rows === 0)
|
||||
{
|
||||
throw new Exception('Appointment customer id is invalid.');
|
||||
}
|
||||
|
||||
// Check if the service id is valid.
|
||||
$num_rows = $this->db->get_where('services',
|
||||
['id' => $appointment['id_services']])->num_rows();
|
||||
if ($num_rows == 0)
|
||||
$num_rows = $this->db->get_where('services', ['id' => $appointment['id_services']])->num_rows();
|
||||
|
||||
if ($num_rows === 0)
|
||||
{
|
||||
throw new Exception('Appointment service id is invalid.');
|
||||
}
|
||||
|
@ -213,11 +213,13 @@ class Appointments_model extends EA_Model {
|
|||
*/
|
||||
public function exists($appointment)
|
||||
{
|
||||
if ( ! isset($appointment['start_datetime'])
|
||||
|| ! isset($appointment['end_datetime'])
|
||||
|| ! isset($appointment['id_users_provider'])
|
||||
|| ! isset($appointment['id_users_customer'])
|
||||
|| ! isset($appointment['id_services']))
|
||||
if ( ! isset(
|
||||
$appointment['start_datetime'],
|
||||
$appointment['end_datetime'],
|
||||
$appointment['id_users_provider'],
|
||||
$appointment['id_users_customer'],
|
||||
$appointment['id_services']
|
||||
))
|
||||
{
|
||||
throw new Exception('Not all appointment field values are provided: '
|
||||
. print_r($appointment, TRUE));
|
||||
|
@ -371,19 +373,19 @@ class Appointments_model extends EA_Model {
|
|||
*
|
||||
* Example:
|
||||
*
|
||||
* $this->Model->getBatch('id = ' . $recordId);
|
||||
* $this->appointments_model->get_batch(['id' => $record_id]);
|
||||
*
|
||||
* @param mixed|null $where (OPTIONAL) The WHERE clause of the query to be executed. DO NOT INCLUDE 'WHERE'
|
||||
* KEYWORD.
|
||||
* @param mixed|null $order_by
|
||||
* @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 bool $aggregates (OPTIONAL) Defines whether to add aggregations or not.
|
||||
*
|
||||
* @return array Returns the rows from the database.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get_batch($where = NULL, $order_by = NULL, $limit = NULL, $offset = NULL, $aggregates = FALSE)
|
||||
public function get_batch($where = NULL, $limit = NULL, $offset = NULL, $order_by = NULL, $aggregates = FALSE)
|
||||
{
|
||||
if ($where !== NULL)
|
||||
{
|
||||
|
@ -419,12 +421,18 @@ class Appointments_model extends EA_Model {
|
|||
*/
|
||||
private function get_aggregates(array $appointment)
|
||||
{
|
||||
$appointment['service'] = $this->db->get_where('services',
|
||||
['id' => $appointment['id_services']])->row_array();
|
||||
$appointment['provider'] = $this->db->get_where('users',
|
||||
['id' => $appointment['id_users_provider']])->row_array();
|
||||
$appointment['customer'] = $this->db->get_where('users',
|
||||
['id' => $appointment['id_users_customer']])->row_array();
|
||||
$appointment['service'] = $this->db->get_where('services', [
|
||||
'id' => $appointment['id_services']
|
||||
])->row_array();
|
||||
|
||||
$appointment['provider'] = $this->db->get_where('users', [
|
||||
'id' => $appointment['id_users_provider']
|
||||
])->row_array();
|
||||
|
||||
$appointment['customer'] = $this->db->get_where('users', [
|
||||
'id' => $appointment['id_users_customer']
|
||||
])->row_array();
|
||||
|
||||
return $appointment;
|
||||
}
|
||||
|
||||
|
@ -443,6 +451,7 @@ class Appointments_model extends EA_Model {
|
|||
// Validate period
|
||||
$start = strtotime($unavailable['start_datetime']);
|
||||
$end = strtotime($unavailable['end_datetime']);
|
||||
|
||||
if ($start > $end)
|
||||
{
|
||||
throw new Exception('Unavailable period start must be prior to end.');
|
||||
|
@ -495,7 +504,7 @@ class Appointments_model extends EA_Model {
|
|||
|
||||
$num_rows = $this->db->get_where('appointments', ['id' => $unavailable_id])->num_rows();
|
||||
|
||||
if ($num_rows == 0)
|
||||
if ($num_rows === 0)
|
||||
{
|
||||
return FALSE; // Record does not exist.
|
||||
}
|
||||
|
@ -519,8 +528,9 @@ class Appointments_model extends EA_Model {
|
|||
throw new Exception('Invalid argument type $provider_id: ' . $provider_id);
|
||||
}
|
||||
|
||||
$this->db->update('appointments', ['id_google_calendar' => NULL],
|
||||
['id_users_provider' => $provider_id]);
|
||||
$this->db->update('appointments', ['id_google_calendar' => NULL], [
|
||||
'id_users_provider' => $provider_id
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -23,7 +23,6 @@ class Customers_model extends EA_Model {
|
|||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->load->helper('data_validation');
|
||||
}
|
||||
|
||||
|
@ -78,37 +77,36 @@ class Customers_model extends EA_Model {
|
|||
// If a customer id is provided, check whether the record exist in the database.
|
||||
if (isset($customer['id']))
|
||||
{
|
||||
$num_rows = $this->db->get_where('users',
|
||||
['id' => $customer['id']])->num_rows();
|
||||
if ($num_rows == 0)
|
||||
$num_rows = $this->db->get_where('users', ['id' => $customer['id']])->num_rows();
|
||||
|
||||
if ($num_rows === 0)
|
||||
{
|
||||
throw new Exception('Provided customer id does not '
|
||||
. 'exist in the database.');
|
||||
}
|
||||
}
|
||||
|
||||
$query = $this->db->get_where('settings', ['name' => 'require_phone_number']);
|
||||
$phone_number_required = $query->num_rows() > 0 ? $query->row() === '1' : FALSE;
|
||||
$phone_number_required = $this->db->get_where('settings', ['name' => 'require_phone_number'])->row()->value === '1';
|
||||
|
||||
// Validate required fields
|
||||
if (empty($customer['first_name'])
|
||||
|| empty($customer['last_name'])
|
||||
|| empty($customer['email'])
|
||||
|| (empty($customer['phone_number']) && $phone_number_required))
|
||||
if ( ! isset(
|
||||
$customer['first_name'],
|
||||
$customer['last_name'],
|
||||
$customer['email']
|
||||
)
|
||||
|| ( ! isset($customer['phone_number']) && $phone_number_required))
|
||||
{
|
||||
throw new Exception('Not all required fields are provided: '
|
||||
. print_r($customer, TRUE));
|
||||
throw new Exception('Not all required fields are provided: ' . print_r($customer, TRUE));
|
||||
}
|
||||
|
||||
// Validate email address
|
||||
if ( ! filter_var($customer['email'], FILTER_VALIDATE_EMAIL))
|
||||
{
|
||||
throw new Exception('Invalid email address provided: '
|
||||
. $customer['email']);
|
||||
throw new Exception('Invalid email address provided: ' . $customer['email']);
|
||||
}
|
||||
|
||||
// When inserting a record the email address must be unique.
|
||||
$customer_id = (isset($customer['id'])) ? $customer['id'] : '';
|
||||
$customer_id = isset($customer['id']) ? $customer['id'] : '';
|
||||
|
||||
$num_rows = $this->db
|
||||
->select('*')
|
||||
|
@ -116,7 +114,7 @@ class Customers_model extends EA_Model {
|
|||
->join('roles', 'roles.id = users.id_roles', 'inner')
|
||||
->where('roles.slug', DB_SLUG_CUSTOMER)
|
||||
->where('users.email', $customer['email'])
|
||||
->where('users.id <>', $customer_id)
|
||||
->where('users.id !=', $customer_id)
|
||||
->get()
|
||||
->num_rows();
|
||||
|
||||
|
@ -331,8 +329,8 @@ class Customers_model extends EA_Model {
|
|||
. 'does not exist in the database: ' . $customer_id);
|
||||
}
|
||||
|
||||
$row_data = $this->db->get_where('users', ['id' => $customer_id]
|
||||
)->row_array();
|
||||
$row_data = $this->db->get_where('users', ['id' => $customer_id])->row_array();
|
||||
|
||||
if ( ! isset($row_data[$field_name]))
|
||||
{
|
||||
throw new Exception('The given $field_name argument does not'
|
||||
|
@ -349,15 +347,16 @@ class Customers_model extends EA_Model {
|
|||
*
|
||||
* Example:
|
||||
*
|
||||
* $this->Model->getBatch('id = ' . $recordId);
|
||||
* $this->appointments_model->get_batch([$id => $record_id]);
|
||||
*
|
||||
* @param mixed|null $where
|
||||
* @param mixed|null $order_by
|
||||
* @param int|null $limit
|
||||
* @param int|null $offset
|
||||
* @param mixed|null $order_by
|
||||
*
|
||||
* @return array Returns the rows from the database.
|
||||
*/
|
||||
public function get_batch($where = NULL, $order_by = NULL, $limit = NULL, $offset = NULL)
|
||||
public function get_batch($where = NULL, $limit = NULL, $offset = NULL, $order_by = NULL)
|
||||
{
|
||||
$role_id = $this->get_customers_role_id();
|
||||
|
||||
|
|
|
@ -25,7 +25,6 @@ class Providers_model extends EA_Model {
|
|||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->load->helper('data_validation');
|
||||
$this->load->helper('general');
|
||||
}
|
||||
|
@ -87,9 +86,11 @@ class Providers_model extends EA_Model {
|
|||
}
|
||||
|
||||
// Validate required fields.
|
||||
if ( ! isset($provider['last_name'])
|
||||
|| ! isset($provider['email'])
|
||||
|| ! isset($provider['phone_number']))
|
||||
if ( ! isset(
|
||||
$provider['last_name'],
|
||||
$provider['email'],
|
||||
$provider['phone_number']
|
||||
))
|
||||
{
|
||||
throw new Exception('Not all required fields are provided: ' . print_r($provider, TRUE));
|
||||
}
|
||||
|
@ -106,7 +107,8 @@ class Providers_model extends EA_Model {
|
|||
throw new Exception('Invalid provider services given: ' . print_r($provider, TRUE));
|
||||
}
|
||||
else
|
||||
{ // Check if services are valid int values.
|
||||
{
|
||||
// Check if services are valid int values.
|
||||
foreach ($provider['services'] as $service_id)
|
||||
{
|
||||
if ( ! is_numeric($service_id))
|
||||
|
@ -118,7 +120,7 @@ class Providers_model extends EA_Model {
|
|||
}
|
||||
|
||||
// Validate provider settings.
|
||||
if ( ! isset($provider['settings']) || count($provider['settings']) == 0
|
||||
if ( ! isset($provider['settings']) || count($provider['settings']) === 0
|
||||
|| ! is_array($provider['settings']))
|
||||
{
|
||||
throw new Exception('Invalid provider settings given: ' . print_r($provider, TRUE));
|
||||
|
@ -162,7 +164,7 @@ class Providers_model extends EA_Model {
|
|||
->join('roles', 'roles.id = users.id_roles', 'inner')
|
||||
->where('roles.slug', DB_SLUG_PROVIDER)
|
||||
->where('users.email', $provider['email'])
|
||||
->where('users.id <>', $provider_id)
|
||||
->where('users.id !=', $provider_id)
|
||||
->get()
|
||||
->num_rows();
|
||||
|
||||
|
@ -186,7 +188,7 @@ class Providers_model extends EA_Model {
|
|||
public function validate_username($username, $user_id)
|
||||
{
|
||||
$num_rows = $this->db->get_where('user_settings',
|
||||
['username' => $username, 'id_users <> ' => $user_id])->num_rows();
|
||||
['username' => $username, 'id_users != ' => $user_id])->num_rows();
|
||||
return $num_rows > 0 ? FALSE : TRUE;
|
||||
}
|
||||
|
||||
|
@ -333,7 +335,7 @@ class Providers_model extends EA_Model {
|
|||
{
|
||||
$value = json_decode($value, TRUE);
|
||||
|
||||
if (!$value)
|
||||
if ( ! $value)
|
||||
{
|
||||
$value = [];
|
||||
}
|
||||
|
@ -388,6 +390,7 @@ class Providers_model extends EA_Model {
|
|||
|
||||
// Save provider services in the database (delete old records and add new).
|
||||
$this->db->delete('services_providers', ['id_users' => $provider_id]);
|
||||
|
||||
foreach ($services as $service_id)
|
||||
{
|
||||
$service_provider = [
|
||||
|
@ -452,7 +455,7 @@ class Providers_model extends EA_Model {
|
|||
}
|
||||
|
||||
$num_rows = $this->db->get_where('users', ['id' => $provider_id])->num_rows();
|
||||
if ($num_rows == 0)
|
||||
if ($num_rows === 0)
|
||||
{
|
||||
return FALSE; // Record does not exist in database.
|
||||
}
|
||||
|
@ -460,6 +463,244 @@ class Providers_model extends EA_Model {
|
|||
return $this->db->delete('users', ['id' => $provider_id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific field value from the database.
|
||||
*
|
||||
* @param string $field_name The field name of the value to be returned.
|
||||
* @param int $provider_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 $provider_id is not a valid int.
|
||||
* @throws Exception When the provider record does not exist in the database.
|
||||
* @throws Exception When the selected field value is not present on database.
|
||||
*/
|
||||
public function get_value($field_name, $provider_id)
|
||||
{
|
||||
if ( ! is_numeric($provider_id))
|
||||
{
|
||||
throw new Exception('Invalid argument provided as $provider_id: ' . $provider_id);
|
||||
}
|
||||
|
||||
if ( ! is_string($field_name))
|
||||
{
|
||||
throw new Exception('$field_name argument is not a string: ' . $field_name);
|
||||
}
|
||||
|
||||
// Check whether the provider record exists in database.
|
||||
$result = $this->db->get_where('users', ['id' => $provider_id]);
|
||||
|
||||
if ($result->num_rows() == 0)
|
||||
{
|
||||
throw new Exception('The record with the $provider_id argument does not exist in '
|
||||
. 'the database: ' . $provider_id);
|
||||
}
|
||||
|
||||
$provider = $result->row_array();
|
||||
|
||||
if ( ! isset($provider[$field_name]))
|
||||
{
|
||||
throw new Exception('The given $field_name argument does not exist in the database: ' . $field_name);
|
||||
}
|
||||
|
||||
return $provider[$field_name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all, or specific records from provider's table.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* $this->Model->get_batch('id = ' . $recordId);
|
||||
*
|
||||
* @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
|
||||
*
|
||||
* @return array Returns the rows from the database.
|
||||
*/
|
||||
public function get_batch($where = NULL, $limit = NULL, $offset = NULL, $order_by = NULL)
|
||||
{
|
||||
// CI db class may confuse two where clauses made in the same time, so get the role id first and then apply the
|
||||
// get_batch() where clause.
|
||||
$role_id = $this->get_providers_role_id();
|
||||
|
||||
if ($where !== NULL)
|
||||
{
|
||||
$this->db->where($where);
|
||||
}
|
||||
|
||||
if ($order_by !== NULL)
|
||||
{
|
||||
$this->db->order_by($order_by);
|
||||
}
|
||||
|
||||
$batch = $this->db->get_where('users', ['id_roles' => $role_id], $limit, $offset)->result_array();
|
||||
|
||||
// Include each provider services and settings.
|
||||
foreach ($batch as &$provider)
|
||||
{
|
||||
// Services
|
||||
$services = $this->db->get_where('services_providers',
|
||||
['id_users' => $provider['id']])->result_array();
|
||||
$provider['services'] = [];
|
||||
foreach ($services as $service)
|
||||
{
|
||||
$provider['services'][] = $service['id_services'];
|
||||
}
|
||||
|
||||
// Settings
|
||||
$provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider['id']])->row_array();
|
||||
unset($provider['settings']['id_users']);
|
||||
}
|
||||
|
||||
// Return provider records in an array.
|
||||
return $batch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the available system providers.
|
||||
*
|
||||
* This method returns the available providers and the services that can provide.
|
||||
*
|
||||
* @return array Returns an array with the providers data.
|
||||
*/
|
||||
public function get_available_providers()
|
||||
{
|
||||
// Get provider records from database.
|
||||
$this->db
|
||||
->select('users.*')
|
||||
->from('users')
|
||||
->join('roles', 'roles.id = users.id_roles', 'inner')
|
||||
->where('roles.slug', DB_SLUG_PROVIDER)
|
||||
->order_by('first_name ASC, last_name ASC, email ASC');
|
||||
|
||||
$providers = $this->db->get()->result_array();
|
||||
|
||||
// Include each provider services and settings.
|
||||
foreach ($providers as &$provider)
|
||||
{
|
||||
// Services
|
||||
$services = $this->db->get_where('services_providers', ['id_users' => $provider['id']])->result_array();
|
||||
|
||||
$provider['services'] = [];
|
||||
foreach ($services as $service)
|
||||
{
|
||||
$provider['services'][] = $service['id_services'];
|
||||
}
|
||||
|
||||
// Settings
|
||||
$provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider['id']])->row_array();
|
||||
unset(
|
||||
$provider['settings']['username'],
|
||||
$provider['settings']['password'],
|
||||
$provider['settings']['salt']
|
||||
);
|
||||
}
|
||||
|
||||
// Return provider records.
|
||||
return $providers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the provider working plan exception.
|
||||
*
|
||||
* @param string $date The working plan exception date (in YYYY-MM-DD format).
|
||||
* @param array $working_plan_exception Contains the working plan exception information ("start", "end" and "breaks"
|
||||
* properties).
|
||||
* @param int $provider_id The selected provider record id.
|
||||
*
|
||||
* @return bool Return if the new working plan exceptions is correctly saved to DB.
|
||||
*
|
||||
* @throws Exception If start time is after the end time.
|
||||
* @throws Exception If $provider_id argument is invalid.
|
||||
*/
|
||||
public function save_working_plan_exception($date, $working_plan_exception, $provider_id)
|
||||
{
|
||||
// Validate the working plan exception data.
|
||||
$start = date('H:i', strtotime($working_plan_exception['start']));
|
||||
$end = date('H:i', strtotime($working_plan_exception['end']));
|
||||
|
||||
if ($start > $end)
|
||||
{
|
||||
throw new Exception('Working plan exception "start" must be prior to "end".');
|
||||
}
|
||||
|
||||
// Make sure the provider record exists.
|
||||
$conditions = [
|
||||
'id' => $provider_id,
|
||||
'id_roles' => $this->db->get_where('roles', ['slug' => DB_SLUG_PROVIDER])->row()->id
|
||||
];
|
||||
|
||||
if ($this->db->get_where('users', $conditions)->num_rows() === 0)
|
||||
{
|
||||
throw new Exception('Provider record was not found in database: ' . $provider_id);
|
||||
}
|
||||
|
||||
// Add record to database.
|
||||
$working_plan_exceptions = json_decode($this->get_setting('working_plan_exceptions', $provider_id), TRUE);
|
||||
|
||||
if ( ! isset($working_plan_exception['breaks']))
|
||||
{
|
||||
$working_plan_exception['breaks'] = [];
|
||||
}
|
||||
|
||||
$working_plan_exceptions[$date] = $working_plan_exception;
|
||||
|
||||
return $this->set_setting(
|
||||
'working_plan_exceptions',
|
||||
json_encode($working_plan_exceptions),
|
||||
$provider_id
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a providers setting from the database.
|
||||
*
|
||||
* @param string $setting_name The setting name that is going to be returned.
|
||||
* @param int $provider_id The selected provider id.
|
||||
*
|
||||
* @return string Returns the value of the selected user setting.
|
||||
*/
|
||||
public function get_setting($setting_name, $provider_id)
|
||||
{
|
||||
$provider_settings = $this->db->get_where('user_settings', ['id_users' => $provider_id])->row_array();
|
||||
|
||||
return $provider_settings[$setting_name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a provider working plan exception.
|
||||
*
|
||||
* @param string $date The working plan exception date (in YYYY-MM-DD format).
|
||||
* @param int $provider_id The selected provider record id.
|
||||
*
|
||||
* @return bool Return if the new working plan exceptions is correctly deleted from DB.
|
||||
*
|
||||
* @throws Exception If $provider_id argument is invalid.
|
||||
*/
|
||||
public function delete_working_plan_exception($date, $provider_id)
|
||||
{
|
||||
$provider = $this->get_row($provider_id);
|
||||
|
||||
$working_plan_exceptions = json_decode($provider['settings']['working_plan_exceptions'], TRUE);
|
||||
|
||||
if ( ! isset($working_plan_exceptions[$date]))
|
||||
{
|
||||
return TRUE; // The selected date does not exist in provider's settings.
|
||||
}
|
||||
|
||||
unset($working_plan_exceptions[$date]);
|
||||
|
||||
return $this->set_setting(
|
||||
'working_plan_exceptions',
|
||||
json_encode(empty($working_plan_exceptions) ? new stdClass() : $working_plan_exceptions),
|
||||
$provider_id
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific provider record from the database.
|
||||
*
|
||||
|
@ -502,242 +743,4 @@ class Providers_model extends EA_Model {
|
|||
// Return provider data array.
|
||||
return $provider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific field value from the database.
|
||||
*
|
||||
* @param string $field_name The field name of the value to be returned.
|
||||
* @param int $provider_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 $provider_id is not a valid int.
|
||||
* @throws Exception When the provider record does not exist in the database.
|
||||
* @throws Exception When the selected field value is not present on database.
|
||||
*/
|
||||
public function get_value($field_name, $provider_id)
|
||||
{
|
||||
if ( ! is_numeric($provider_id))
|
||||
{
|
||||
throw new Exception('Invalid argument provided as $provider_id: ' . $provider_id);
|
||||
}
|
||||
|
||||
if ( ! is_string($field_name))
|
||||
{
|
||||
throw new Exception('$field_name argument is not a string: ' . $field_name);
|
||||
}
|
||||
|
||||
// Check whether the provider record exists in database.
|
||||
$result = $this->db->get_where('users', ['id' => $provider_id]);
|
||||
if ($result->num_rows() == 0)
|
||||
{
|
||||
throw new Exception('The record with the $provider_id argument does not exist in '
|
||||
. 'the database: ' . $provider_id);
|
||||
}
|
||||
|
||||
$provider = $result->row_array();
|
||||
if ( ! isset($provider[$field_name]))
|
||||
{
|
||||
throw new Exception('The given $field_name argument does not exist in the '
|
||||
. 'database: ' . $field_name);
|
||||
}
|
||||
|
||||
return $provider[$field_name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all, or specific records from provider's table.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* $this->Model->get_batch('id = ' . $recordId);
|
||||
*
|
||||
*
|
||||
* @param mixed|null $where (OPTIONAL) The WHERE clause of the query to be executed.
|
||||
* @param mixed|null $order_by
|
||||
* @param int|null $limit
|
||||
* @param int|null $offset
|
||||
* @return array Returns the rows from the database.
|
||||
*/
|
||||
public function get_batch($where = NULL, $order_by = NULL, $limit = NULL, $offset = NULL)
|
||||
{
|
||||
// CI db class may confuse two where clauses made in the same time, so
|
||||
// get the role id first and then apply the get_batch() where clause.
|
||||
$role_id = $this->get_providers_role_id();
|
||||
|
||||
if ($where !== NULL)
|
||||
{
|
||||
$this->db->where($where);
|
||||
}
|
||||
|
||||
if ($order_by !== NULL)
|
||||
{
|
||||
$this->db->order_by($order_by);
|
||||
}
|
||||
|
||||
$batch = $this->db->get_where('users', ['id_roles' => $role_id], $limit, $offset)->result_array();
|
||||
|
||||
// Include each provider services and settings.
|
||||
foreach ($batch as &$provider)
|
||||
{
|
||||
// Services
|
||||
$services = $this->db->get_where('services_providers',
|
||||
['id_users' => $provider['id']])->result_array();
|
||||
$provider['services'] = [];
|
||||
foreach ($services as $service)
|
||||
{
|
||||
$provider['services'][] = $service['id_services'];
|
||||
}
|
||||
|
||||
// Settings
|
||||
$provider['settings'] = $this->db->get_where('user_settings',
|
||||
['id_users' => $provider['id']])->row_array();
|
||||
unset($provider['settings']['id_users']);
|
||||
}
|
||||
|
||||
// Return provider records in an array.
|
||||
return $batch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the available system providers.
|
||||
*
|
||||
* This method returns the available providers and the services that can provide.
|
||||
*
|
||||
* @return array Returns an array with the providers data.
|
||||
*/
|
||||
public function get_available_providers()
|
||||
{
|
||||
// Get provider records from database.
|
||||
$this->db
|
||||
->select('users.*')
|
||||
->from('users')
|
||||
->join('roles', 'roles.id = users.id_roles', 'inner')
|
||||
->where('roles.slug', DB_SLUG_PROVIDER)
|
||||
->order_by('first_name ASC, last_name ASC, email ASC');
|
||||
|
||||
$providers = $this->db->get()->result_array();
|
||||
|
||||
// Include each provider services and settings.
|
||||
foreach ($providers as &$provider)
|
||||
{
|
||||
// Services
|
||||
$services = $this->db->get_where('services_providers',
|
||||
['id_users' => $provider['id']])->result_array();
|
||||
|
||||
$provider['services'] = [];
|
||||
foreach ($services as $service)
|
||||
{
|
||||
$provider['services'][] = $service['id_services'];
|
||||
}
|
||||
|
||||
// Settings
|
||||
$provider['settings'] = $this->db->get_where('user_settings',
|
||||
['id_users' => $provider['id']])->row_array();
|
||||
unset($provider['settings']['username']);
|
||||
unset($provider['settings']['password']);
|
||||
unset($provider['settings']['salt']);
|
||||
}
|
||||
|
||||
// Return provider records.
|
||||
return $providers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a providers setting from the database.
|
||||
*
|
||||
* @param string $setting_name The setting name that is going to be returned.
|
||||
* @param int $provider_id The selected provider id.
|
||||
*
|
||||
* @return string Returns the value of the selected user setting.
|
||||
*/
|
||||
public function get_setting($setting_name, $provider_id)
|
||||
{
|
||||
$provider_settings = $this->db->get_where('user_settings', ['id_users' => $provider_id])->row_array();
|
||||
|
||||
return $provider_settings[$setting_name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the provider working plan exception.
|
||||
*
|
||||
* @param string $date The working plan exception date (in YYYY-MM-DD format).
|
||||
* @param array $working_plan_exception Contains the working plan exception information ("start", "end" and "breaks"
|
||||
* properties).
|
||||
* @param int $provider_id The selected provider record id.
|
||||
*
|
||||
* @return bool Return if the new working plan exceptions is correctly saved to DB.
|
||||
*
|
||||
* @throws Exception If start time is after the end time.
|
||||
* @throws Exception If $provider_id argument is invalid.
|
||||
*/
|
||||
public function save_working_plan_exception($date, $working_plan_exception, $provider_id)
|
||||
{
|
||||
// Validate the working plan exception data.
|
||||
$start = date('H:i', strtotime($working_plan_exception['start']));
|
||||
$end = date('H:i', strtotime($working_plan_exception['end']));
|
||||
|
||||
if ($start > $end)
|
||||
{
|
||||
throw new Exception('Working plan exception "start" must be prior to "end".');
|
||||
}
|
||||
|
||||
// Make sure the provider record exists.
|
||||
$conditions = [
|
||||
'id' => $provider_id,
|
||||
'id_roles' => $this->db->get_where('roles', ['slug' => DB_SLUG_PROVIDER])->row()->id
|
||||
];
|
||||
|
||||
if ($this->db->get_where('users', $conditions)->num_rows() === 0)
|
||||
{
|
||||
throw new Exception('Provider record was not found in database: ' . $provider_id);
|
||||
}
|
||||
|
||||
// Add record to database.
|
||||
$working_plan_exceptions = json_decode($this->get_setting('working_plan_exceptions', $provider_id), TRUE);
|
||||
|
||||
if (!isset($working_plan_exception['breaks']))
|
||||
{
|
||||
$working_plan_exception['breaks'] = [];
|
||||
}
|
||||
|
||||
$working_plan_exceptions[$date] = $working_plan_exception;
|
||||
|
||||
return $this->set_setting(
|
||||
'working_plan_exceptions',
|
||||
json_encode($working_plan_exceptions),
|
||||
$provider_id
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a provider working plan exception.
|
||||
*
|
||||
* @param string $date The working plan exception date (in YYYY-MM-DD format).
|
||||
* @param int $provider_id The selected provider record id.
|
||||
*
|
||||
* @return bool Return if the new working plan exceptions is correctly deleted from DB.
|
||||
*
|
||||
* @throws Exception If $provider_id argument is invalid.
|
||||
*/
|
||||
public function delete_working_plan_exception($date, $provider_id)
|
||||
{
|
||||
$provider = $this->get_row($provider_id);
|
||||
|
||||
$working_plan_exceptions = json_decode($provider['settings']['working_plan_exceptions'], TRUE);
|
||||
|
||||
if ( ! isset($working_plan_exceptions[$date]))
|
||||
{
|
||||
return TRUE; // The selected date does not exist in provider's settings.
|
||||
}
|
||||
|
||||
unset($working_plan_exceptions[$date]);
|
||||
|
||||
return $this->set_setting(
|
||||
'working_plan_exceptions',
|
||||
json_encode(empty($working_plan_exceptions) ? new stdClass() : $working_plan_exceptions),
|
||||
$provider_id
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,8 +44,8 @@ class Roles_model extends EA_Model {
|
|||
$privileges = $this->db->get_where('roles', ['slug' => $slug])->row_array();
|
||||
unset($privileges['id'], $privileges['name'], $privileges['slug'], $privileges['is_admin']);
|
||||
|
||||
// Convert the int values to bool so that is easier to check whether a
|
||||
// user has the required privileges for a specific action.
|
||||
// Convert the int values to bool so that is easier to check whether a user has the required privileges for a
|
||||
// specific action.
|
||||
foreach ($privileges as &$value)
|
||||
{
|
||||
$privileges_number = $value;
|
||||
|
|
|
@ -25,7 +25,6 @@ class Secretaries_model extends EA_Model {
|
|||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->load->helper('general');
|
||||
$this->load->helper('data_validation');
|
||||
}
|
||||
|
@ -74,9 +73,9 @@ class Secretaries_model extends EA_Model {
|
|||
// 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('users', ['id' => $secretary['id']])
|
||||
->num_rows();
|
||||
if ($num_rows == 0)
|
||||
$num_rows = $this->db->get_where('users', ['id' => $secretary['id']])->num_rows();
|
||||
|
||||
if ($num_rows === 0)
|
||||
{
|
||||
throw new Exception('Given secretary id does not exist in database: ' . $secretary['id']);
|
||||
}
|
||||
|
@ -89,9 +88,11 @@ class Secretaries_model extends EA_Model {
|
|||
}
|
||||
|
||||
// Validate required fields integrity.
|
||||
if ( ! isset($secretary['last_name'])
|
||||
|| ! isset($secretary['email'])
|
||||
|| ! isset($secretary['phone_number']))
|
||||
if ( ! isset(
|
||||
$secretary['last_name'],
|
||||
$secretary['email'],
|
||||
$secretary['phone_number']
|
||||
))
|
||||
{
|
||||
throw new Exception('Not all required fields are provided: ' . print_r($secretary, TRUE));
|
||||
}
|
||||
|
@ -140,7 +141,7 @@ class Secretaries_model extends EA_Model {
|
|||
->join('roles', 'roles.id = users.id_roles', 'inner')
|
||||
->where('roles.slug', DB_SLUG_SECRETARY)
|
||||
->where('users.email', $secretary['email'])
|
||||
->where('users.id <>', $secretary_id)
|
||||
->where('users.id !=', $secretary_id)
|
||||
->get()
|
||||
->num_rows();
|
||||
|
||||
|
@ -164,7 +165,7 @@ class Secretaries_model extends EA_Model {
|
|||
public function validate_username($username, $user_id)
|
||||
{
|
||||
$num_rows = $this->db->get_where('user_settings',
|
||||
['username' => $username, 'id_users <> ' => $user_id])->num_rows();
|
||||
['username' => $username, 'id_users != ' => $user_id])->num_rows();
|
||||
return $num_rows > 0 ? FALSE : TRUE;
|
||||
}
|
||||
|
||||
|
@ -499,14 +500,14 @@ class Secretaries_model extends EA_Model {
|
|||
/**
|
||||
* Get all, or specific secretary records from database.
|
||||
*
|
||||
* @param mixed|null $where (OPTIONAL) The WHERE clause of the query to be executed. Use this to get
|
||||
* specific secretary records.
|
||||
* @param mixed|null $order_by
|
||||
* @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
|
||||
*
|
||||
* @return array Returns an array with secretary records.
|
||||
*/
|
||||
public function get_batch($where = NULL, $order_by = NULL, $limit = NULL, $offset = NULL)
|
||||
public function get_batch($where = NULL, $limit = NULL, $offset = NULL, $order_by = NULL)
|
||||
{
|
||||
$role_id = $this->get_secretary_role_id();
|
||||
|
||||
|
|
|
@ -23,7 +23,6 @@ class Services_model extends EA_Model {
|
|||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->load->helper('data_validation');
|
||||
}
|
||||
|
||||
|
@ -172,9 +171,11 @@ class Services_model extends EA_Model {
|
|||
*/
|
||||
public function exists($service)
|
||||
{
|
||||
if ( ! isset($service['name'])
|
||||
|| ! isset($service['duration'])
|
||||
|| ! isset($service['price']))
|
||||
if ( ! isset(
|
||||
$service['name'],
|
||||
$service['duration'],
|
||||
$service['price']
|
||||
))
|
||||
{
|
||||
throw new Exception('Not all service fields are provided in order to check whether '
|
||||
. 'a service record already exists: ' . print_r($service, TRUE));
|
||||
|
@ -316,16 +317,16 @@ class Services_model extends EA_Model {
|
|||
*
|
||||
* Example:
|
||||
*
|
||||
* $this->model->get_batch(['id' => $record_id]);
|
||||
* $this->services_model->get_batch(['id' => $record_id]);
|
||||
*
|
||||
* @param mixed $where
|
||||
* @param mixed $order_by
|
||||
* @param int|null $limit
|
||||
* @param int|null $offset
|
||||
* @param mixed $order_by
|
||||
*
|
||||
* @return array Returns the rows from the database.
|
||||
*/
|
||||
public function get_batch($where = NULL, $order_by = NULL, $limit = NULL, $offset = NULL)
|
||||
public function get_batch($where = NULL, $limit = NULL, $offset = NULL, $order_by = NULL)
|
||||
{
|
||||
if ($where !== NULL)
|
||||
{
|
||||
|
|
|
@ -32,12 +32,14 @@ class Settings_model extends EA_Model {
|
|||
public function get_setting($name)
|
||||
{
|
||||
if ( ! is_string($name))
|
||||
{ // Check argument type.
|
||||
{
|
||||
// Check argument type.
|
||||
throw new Exception('$name argument is not a string: ' . $name);
|
||||
}
|
||||
|
||||
if ($this->db->get_where('settings', ['name' => $name])->num_rows() == 0)
|
||||
{ // Check if setting exists in db.
|
||||
{
|
||||
// Check if setting exists in db.
|
||||
throw new Exception('$name setting does not exist in database: ' . $name);
|
||||
}
|
||||
|
||||
|
@ -67,6 +69,7 @@ class Settings_model extends EA_Model {
|
|||
}
|
||||
|
||||
$query = $this->db->get_where('settings', ['name' => $name]);
|
||||
|
||||
if ($query->num_rows() > 0)
|
||||
{
|
||||
// Update setting
|
||||
|
@ -83,10 +86,12 @@ class Settings_model extends EA_Model {
|
|||
'name' => $name,
|
||||
'value' => $value
|
||||
];
|
||||
|
||||
if ( ! $this->db->insert('settings', $insert_data))
|
||||
{
|
||||
throw new Exception('Could not insert database setting');
|
||||
}
|
||||
|
||||
$setting_id = (int)$this->db->insert_id();
|
||||
}
|
||||
|
||||
|
|
|
@ -25,9 +25,7 @@ class User_model extends EA_Model {
|
|||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->load->library('timezones');
|
||||
|
||||
$this->load->helper('general');
|
||||
$this->load->helper('string');
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue