Code enhancements in the model classes

This commit is contained in:
Alex Tselegidis 2020-12-05 11:38:57 +02:00
parent 14017a49f5
commit 9c3d253456
13 changed files with 363 additions and 341 deletions

View file

@ -49,9 +49,9 @@ class Admins extends API_V1_Controller {
{ {
try 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) if ($id !== NULL && count($admins) === 0)
{ {

View file

@ -55,16 +55,16 @@ class Appointments extends API_V1_Controller {
{ {
try try
{ {
$conditions = [ $where = [
'is_unavailable' => FALSE 'is_unavailable' => FALSE
]; ];
if ($id !== NULL) 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) if ($id !== NULL && count($appointments) === 0)
{ {

View file

@ -49,9 +49,9 @@ class Unavailabilities extends API_V1_Controller {
{ {
try 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) if ($id !== NULL && count($unavailabilities) === 0)
{ {

View file

@ -108,7 +108,7 @@ class Availability {
// every reserved appointment is considered to be a taken space in the plan. // every reserved appointment is considered to be a taken space in the plan.
$working_day = strtolower(date('l', strtotime($date))); $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. // Search if the $date is an custom availability period added outside the normal working plan.
if (isset($working_plan_exceptions[$date])) if (isset($working_plan_exceptions[$date]))
@ -349,7 +349,7 @@ class Availability {
$working_day = strtolower(date('l', strtotime($date))); $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. // Search if the $date is an custom availability period added outside the normal working plan.
if (isset($working_plan_exceptions[$date])) if (isset($working_plan_exceptions[$date]))

View file

@ -80,9 +80,11 @@ class Admins_model extends EA_Model {
} }
// Validate required fields integrity. // Validate required fields integrity.
if ( ! isset($admin['last_name']) if ( ! isset(
|| ! isset($admin['email']) $admin['last_name'],
|| ! isset($admin['phone_number'])) $admin['email'],
$admin['phone_number']
))
{ {
throw new Exception('Not all required fields are provided: ' . print_r($admin, TRUE)); 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. // 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 $num_rows = $this->db
->select('*') ->select('*')
@ -131,7 +133,7 @@ class Admins_model extends EA_Model {
->join('roles', 'roles.id = users.id_roles', 'inner') ->join('roles', 'roles.id = users.id_roles', 'inner')
->where('roles.slug', DB_SLUG_ADMIN) ->where('roles.slug', DB_SLUG_ADMIN)
->where('users.email', $admin['email']) ->where('users.email', $admin['email'])
->where('users.id <>', $admin_id) ->where('users.id !=', $admin_id)
->get() ->get()
->num_rows(); ->num_rows();
@ -154,8 +156,11 @@ class Admins_model extends EA_Model {
*/ */
public function validate_username($username, $user_id) public function validate_username($username, $user_id)
{ {
$num_rows = $this->db->get_where('user_settings', $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; return $num_rows > 0 ? FALSE : TRUE;
} }
@ -419,14 +424,14 @@ class Admins_model extends EA_Model {
/** /**
* Get all, or specific admin records from database. * 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 * @param mixed|null $where (OPTIONAL) The WHERE clause of the query to be executed.
* specific admin records.
* @param mixed|null $order_by
* @param int|null $limit * @param int|null $limit
* @param int|null $offset * @param int|null $offset
* @param mixed|null $order_by
*
* @return array Returns an array with admin records. * @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(); $role_id = $this->get_admin_role_id();

View file

@ -23,9 +23,7 @@ class Appointments_model extends EA_Model {
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
$this->load->helper('data_validation'); $this->load->helper('data_validation');
$this->load->library('timezones'); $this->load->library('timezones');
} }
@ -109,7 +107,8 @@ class Appointments_model extends EA_Model {
->where('users.id', $appointment['id_users_provider']) ->where('users.id', $appointment['id_users_provider'])
->where('roles.slug', DB_SLUG_PROVIDER) ->where('roles.slug', DB_SLUG_PROVIDER)
->get()->num_rows(); ->get()->num_rows();
if ($num_rows == 0)
if ($num_rows === 0)
{ {
throw new Exception('Appointment provider id is invalid.'); 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('users.id', $appointment['id_users_customer'])
->where('roles.slug', DB_SLUG_CUSTOMER) ->where('roles.slug', DB_SLUG_CUSTOMER)
->get()->num_rows(); ->get()->num_rows();
if ($num_rows == 0)
if ($num_rows === 0)
{ {
throw new Exception('Appointment customer id is invalid.'); throw new Exception('Appointment customer id is invalid.');
} }
// Check if the service id is valid. // Check if the service id is valid.
$num_rows = $this->db->get_where('services', $num_rows = $this->db->get_where('services', ['id' => $appointment['id_services']])->num_rows();
['id' => $appointment['id_services']])->num_rows();
if ($num_rows == 0) if ($num_rows === 0)
{ {
throw new Exception('Appointment service id is invalid.'); throw new Exception('Appointment service id is invalid.');
} }
@ -213,11 +213,13 @@ class Appointments_model extends EA_Model {
*/ */
public function exists($appointment) public function exists($appointment)
{ {
if ( ! isset($appointment['start_datetime']) if ( ! isset(
|| ! isset($appointment['end_datetime']) $appointment['start_datetime'],
|| ! isset($appointment['id_users_provider']) $appointment['end_datetime'],
|| ! isset($appointment['id_users_customer']) $appointment['id_users_provider'],
|| ! isset($appointment['id_services'])) $appointment['id_users_customer'],
$appointment['id_services']
))
{ {
throw new Exception('Not all appointment field values are provided: ' throw new Exception('Not all appointment field values are provided: '
. print_r($appointment, TRUE)); . print_r($appointment, TRUE));
@ -371,19 +373,19 @@ class Appointments_model extends EA_Model {
* *
* Example: * 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' * @param mixed|null $where (OPTIONAL) The WHERE clause of the query to be executed.
* KEYWORD.
* @param mixed|null $order_by
* @param int|null $limit * @param int|null $limit
* @param int|null $offset * @param int|null $offset
* @param mixed|null $order_by
* @param bool $aggregates (OPTIONAL) Defines whether to add aggregations or not. * @param bool $aggregates (OPTIONAL) Defines whether to add aggregations or not.
* *
* @return array Returns the rows from the database. * @return array Returns the rows from the database.
*
* @throws Exception * @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) if ($where !== NULL)
{ {
@ -419,12 +421,18 @@ class Appointments_model extends EA_Model {
*/ */
private function get_aggregates(array $appointment) private function get_aggregates(array $appointment)
{ {
$appointment['service'] = $this->db->get_where('services', $appointment['service'] = $this->db->get_where('services', [
['id' => $appointment['id_services']])->row_array(); 'id' => $appointment['id_services']
$appointment['provider'] = $this->db->get_where('users', ])->row_array();
['id' => $appointment['id_users_provider']])->row_array();
$appointment['customer'] = $this->db->get_where('users', $appointment['provider'] = $this->db->get_where('users', [
['id' => $appointment['id_users_customer']])->row_array(); 'id' => $appointment['id_users_provider']
])->row_array();
$appointment['customer'] = $this->db->get_where('users', [
'id' => $appointment['id_users_customer']
])->row_array();
return $appointment; return $appointment;
} }
@ -443,6 +451,7 @@ class Appointments_model extends EA_Model {
// Validate period // Validate period
$start = strtotime($unavailable['start_datetime']); $start = strtotime($unavailable['start_datetime']);
$end = strtotime($unavailable['end_datetime']); $end = strtotime($unavailable['end_datetime']);
if ($start > $end) if ($start > $end)
{ {
throw new Exception('Unavailable period start must be prior to 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(); $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. 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); throw new Exception('Invalid argument type $provider_id: ' . $provider_id);
} }
$this->db->update('appointments', ['id_google_calendar' => NULL], $this->db->update('appointments', ['id_google_calendar' => NULL], [
['id_users_provider' => $provider_id]); 'id_users_provider' => $provider_id
]);
} }
/** /**

View file

@ -23,7 +23,6 @@ class Customers_model extends EA_Model {
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
$this->load->helper('data_validation'); $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 a customer id is provided, check whether the record exist in the database.
if (isset($customer['id'])) if (isset($customer['id']))
{ {
$num_rows = $this->db->get_where('users', $num_rows = $this->db->get_where('users', ['id' => $customer['id']])->num_rows();
['id' => $customer['id']])->num_rows();
if ($num_rows == 0) if ($num_rows === 0)
{ {
throw new Exception('Provided customer id does not ' throw new Exception('Provided customer id does not '
. 'exist in the database.'); . 'exist in the database.');
} }
} }
$query = $this->db->get_where('settings', ['name' => 'require_phone_number']); $phone_number_required = $this->db->get_where('settings', ['name' => 'require_phone_number'])->row()->value === '1';
$phone_number_required = $query->num_rows() > 0 ? $query->row() === '1' : FALSE;
// Validate required fields // Validate required fields
if (empty($customer['first_name']) if ( ! isset(
|| empty($customer['last_name']) $customer['first_name'],
|| empty($customer['email']) $customer['last_name'],
|| (empty($customer['phone_number']) && $phone_number_required)) $customer['email']
)
|| ( ! isset($customer['phone_number']) && $phone_number_required))
{ {
throw new Exception('Not all required fields are provided: ' throw new Exception('Not all required fields are provided: ' . print_r($customer, TRUE));
. print_r($customer, TRUE));
} }
// Validate email address // Validate email address
if ( ! filter_var($customer['email'], FILTER_VALIDATE_EMAIL)) if ( ! filter_var($customer['email'], FILTER_VALIDATE_EMAIL))
{ {
throw new Exception('Invalid email address provided: ' throw new Exception('Invalid email address provided: ' . $customer['email']);
. $customer['email']);
} }
// When inserting a record the email address must be unique. // 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 $num_rows = $this->db
->select('*') ->select('*')
@ -116,7 +114,7 @@ class Customers_model extends EA_Model {
->join('roles', 'roles.id = users.id_roles', 'inner') ->join('roles', 'roles.id = users.id_roles', 'inner')
->where('roles.slug', DB_SLUG_CUSTOMER) ->where('roles.slug', DB_SLUG_CUSTOMER)
->where('users.email', $customer['email']) ->where('users.email', $customer['email'])
->where('users.id <>', $customer_id) ->where('users.id !=', $customer_id)
->get() ->get()
->num_rows(); ->num_rows();
@ -331,8 +329,8 @@ class Customers_model extends EA_Model {
. 'does not exist in the database: ' . $customer_id); . 'does not exist in the database: ' . $customer_id);
} }
$row_data = $this->db->get_where('users', ['id' => $customer_id] $row_data = $this->db->get_where('users', ['id' => $customer_id])->row_array();
)->row_array();
if ( ! isset($row_data[$field_name])) if ( ! isset($row_data[$field_name]))
{ {
throw new Exception('The given $field_name argument does not' throw new Exception('The given $field_name argument does not'
@ -349,15 +347,16 @@ class Customers_model extends EA_Model {
* *
* Example: * Example:
* *
* $this->Model->getBatch('id = ' . $recordId); * $this->appointments_model->get_batch([$id => $record_id]);
* *
* @param mixed|null $where * @param mixed|null $where
* @param mixed|null $order_by
* @param int|null $limit * @param int|null $limit
* @param int|null $offset * @param int|null $offset
* @param mixed|null $order_by
*
* @return array Returns the rows from the database. * @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(); $role_id = $this->get_customers_role_id();

View file

@ -25,7 +25,6 @@ class Providers_model extends EA_Model {
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
$this->load->helper('data_validation'); $this->load->helper('data_validation');
$this->load->helper('general'); $this->load->helper('general');
} }
@ -87,9 +86,11 @@ class Providers_model extends EA_Model {
} }
// Validate required fields. // Validate required fields.
if ( ! isset($provider['last_name']) if ( ! isset(
|| ! isset($provider['email']) $provider['last_name'],
|| ! isset($provider['phone_number'])) $provider['email'],
$provider['phone_number']
))
{ {
throw new Exception('Not all required fields are provided: ' . print_r($provider, TRUE)); 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)); throw new Exception('Invalid provider services given: ' . print_r($provider, TRUE));
} }
else else
{ // Check if services are valid int values. {
// Check if services are valid int values.
foreach ($provider['services'] as $service_id) foreach ($provider['services'] as $service_id)
{ {
if ( ! is_numeric($service_id)) if ( ! is_numeric($service_id))
@ -118,7 +120,7 @@ class Providers_model extends EA_Model {
} }
// Validate provider settings. // Validate provider settings.
if ( ! isset($provider['settings']) || count($provider['settings']) == 0 if ( ! isset($provider['settings']) || count($provider['settings']) === 0
|| ! is_array($provider['settings'])) || ! is_array($provider['settings']))
{ {
throw new Exception('Invalid provider settings given: ' . print_r($provider, TRUE)); 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') ->join('roles', 'roles.id = users.id_roles', 'inner')
->where('roles.slug', DB_SLUG_PROVIDER) ->where('roles.slug', DB_SLUG_PROVIDER)
->where('users.email', $provider['email']) ->where('users.email', $provider['email'])
->where('users.id <>', $provider_id) ->where('users.id !=', $provider_id)
->get() ->get()
->num_rows(); ->num_rows();
@ -186,7 +188,7 @@ class Providers_model extends EA_Model {
public function validate_username($username, $user_id) public function validate_username($username, $user_id)
{ {
$num_rows = $this->db->get_where('user_settings', $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; return $num_rows > 0 ? FALSE : TRUE;
} }
@ -388,6 +390,7 @@ class Providers_model extends EA_Model {
// Save provider services in the database (delete old records and add new). // Save provider services in the database (delete old records and add new).
$this->db->delete('services_providers', ['id_users' => $provider_id]); $this->db->delete('services_providers', ['id_users' => $provider_id]);
foreach ($services as $service_id) foreach ($services as $service_id)
{ {
$service_provider = [ $service_provider = [
@ -452,7 +455,7 @@ class Providers_model extends EA_Model {
} }
$num_rows = $this->db->get_where('users', ['id' => $provider_id])->num_rows(); $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. return FALSE; // Record does not exist in database.
} }
@ -460,49 +463,6 @@ class Providers_model extends EA_Model {
return $this->db->delete('users', ['id' => $provider_id]); return $this->db->delete('users', ['id' => $provider_id]);
} }
/**
* Get a specific provider record from the database.
*
* @param int $provider_id The id of the record to be returned.
*
* @return array Returns an associative array with the selected record's data. Each key has the same name as the
* database field names.
*
* @throws Exception When the selected record does not exist in database.
*/
public function get_row($provider_id)
{
if ( ! is_numeric($provider_id))
{
throw new Exception('$provider_id argument is not a valid numeric value: ' . $provider_id);
}
// Check if selected record exists on database.
if ($this->db->get_where('users', ['id' => $provider_id])->num_rows() == 0)
{
throw new Exception('Selected record does not exist in the database.');
}
// Get provider data.
$provider = $this->db->get_where('users', ['id' => $provider_id])->row_array();
// Include 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'];
}
// Include provider settings.
$provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider_id])->row_array();
unset($provider['settings']['id_users']);
// Return provider data array.
return $provider;
}
/** /**
* Get a specific field value from the database. * Get a specific field value from the database.
* *
@ -530,6 +490,7 @@ class Providers_model extends EA_Model {
// Check whether the provider record exists in database. // Check whether the provider record exists in database.
$result = $this->db->get_where('users', ['id' => $provider_id]); $result = $this->db->get_where('users', ['id' => $provider_id]);
if ($result->num_rows() == 0) if ($result->num_rows() == 0)
{ {
throw new Exception('The record with the $provider_id argument does not exist in ' throw new Exception('The record with the $provider_id argument does not exist in '
@ -537,10 +498,10 @@ class Providers_model extends EA_Model {
} }
$provider = $result->row_array(); $provider = $result->row_array();
if ( ! isset($provider[$field_name])) if ( ! isset($provider[$field_name]))
{ {
throw new Exception('The given $field_name argument does not exist in the ' throw new Exception('The given $field_name argument does not exist in the database: ' . $field_name);
. 'database: ' . $field_name);
} }
return $provider[$field_name]; return $provider[$field_name];
@ -553,17 +514,17 @@ class Providers_model extends EA_Model {
* *
* $this->Model->get_batch('id = ' . $recordId); * $this->Model->get_batch('id = ' . $recordId);
* *
*
* @param mixed|null $where (OPTIONAL) The WHERE clause of the query to be executed. * @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 $limit
* @param int|null $offset * @param int|null $offset
* @param mixed|null $order_by
*
* @return array Returns the rows from the database. * @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)
{ {
// CI db class may confuse two where clauses made in the same time, so // CI db class may confuse two where clauses made in the same time, so get the role id first and then apply the
// get the role id first and then apply the get_batch() where clause. // get_batch() where clause.
$role_id = $this->get_providers_role_id(); $role_id = $this->get_providers_role_id();
if ($where !== NULL) if ($where !== NULL)
@ -591,8 +552,7 @@ class Providers_model extends EA_Model {
} }
// Settings // Settings
$provider['settings'] = $this->db->get_where('user_settings', $provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider['id']])->row_array();
['id_users' => $provider['id']])->row_array();
unset($provider['settings']['id_users']); unset($provider['settings']['id_users']);
} }
@ -623,8 +583,7 @@ class Providers_model extends EA_Model {
foreach ($providers as &$provider) foreach ($providers as &$provider)
{ {
// Services // Services
$services = $this->db->get_where('services_providers', $services = $this->db->get_where('services_providers', ['id_users' => $provider['id']])->result_array();
['id_users' => $provider['id']])->result_array();
$provider['services'] = []; $provider['services'] = [];
foreach ($services as $service) foreach ($services as $service)
@ -633,32 +592,18 @@ class Providers_model extends EA_Model {
} }
// Settings // Settings
$provider['settings'] = $this->db->get_where('user_settings', $provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider['id']])->row_array();
['id_users' => $provider['id']])->row_array(); unset(
unset($provider['settings']['username']); $provider['settings']['username'],
unset($provider['settings']['password']); $provider['settings']['password'],
unset($provider['settings']['salt']); $provider['settings']['salt']
);
} }
// Return provider records. // Return provider records.
return $providers; 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. * Save the provider working plan exception.
* *
@ -711,6 +656,21 @@ class Providers_model extends EA_Model {
); );
} }
/**
* 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. * Delete a provider working plan exception.
* *
@ -740,4 +700,47 @@ class Providers_model extends EA_Model {
$provider_id $provider_id
); );
} }
/**
* Get a specific provider record from the database.
*
* @param int $provider_id The id of the record to be returned.
*
* @return array Returns an associative array with the selected record's data. Each key has the same name as the
* database field names.
*
* @throws Exception When the selected record does not exist in database.
*/
public function get_row($provider_id)
{
if ( ! is_numeric($provider_id))
{
throw new Exception('$provider_id argument is not a valid numeric value: ' . $provider_id);
}
// Check if selected record exists on database.
if ($this->db->get_where('users', ['id' => $provider_id])->num_rows() == 0)
{
throw new Exception('Selected record does not exist in the database.');
}
// Get provider data.
$provider = $this->db->get_where('users', ['id' => $provider_id])->row_array();
// Include 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'];
}
// Include provider settings.
$provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider_id])->row_array();
unset($provider['settings']['id_users']);
// Return provider data array.
return $provider;
}
} }

View file

@ -44,8 +44,8 @@ class Roles_model extends EA_Model {
$privileges = $this->db->get_where('roles', ['slug' => $slug])->row_array(); $privileges = $this->db->get_where('roles', ['slug' => $slug])->row_array();
unset($privileges['id'], $privileges['name'], $privileges['slug'], $privileges['is_admin']); unset($privileges['id'], $privileges['name'], $privileges['slug'], $privileges['is_admin']);
// Convert the int values to bool so that is easier to check whether a // Convert the int values to bool so that is easier to check whether a user has the required privileges for a
// user has the required privileges for a specific action. // specific action.
foreach ($privileges as &$value) foreach ($privileges as &$value)
{ {
$privileges_number = $value; $privileges_number = $value;

View file

@ -25,7 +25,6 @@ class Secretaries_model extends EA_Model {
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
$this->load->helper('general'); $this->load->helper('general');
$this->load->helper('data_validation'); $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 a record id is provided then check whether the record exists in the database.
if (isset($secretary['id'])) if (isset($secretary['id']))
{ {
$num_rows = $this->db->get_where('users', ['id' => $secretary['id']]) $num_rows = $this->db->get_where('users', ['id' => $secretary['id']])->num_rows();
->num_rows();
if ($num_rows == 0) if ($num_rows === 0)
{ {
throw new Exception('Given secretary id does not exist in database: ' . $secretary['id']); 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. // Validate required fields integrity.
if ( ! isset($secretary['last_name']) if ( ! isset(
|| ! isset($secretary['email']) $secretary['last_name'],
|| ! isset($secretary['phone_number'])) $secretary['email'],
$secretary['phone_number']
))
{ {
throw new Exception('Not all required fields are provided: ' . print_r($secretary, TRUE)); 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') ->join('roles', 'roles.id = users.id_roles', 'inner')
->where('roles.slug', DB_SLUG_SECRETARY) ->where('roles.slug', DB_SLUG_SECRETARY)
->where('users.email', $secretary['email']) ->where('users.email', $secretary['email'])
->where('users.id <>', $secretary_id) ->where('users.id !=', $secretary_id)
->get() ->get()
->num_rows(); ->num_rows();
@ -164,7 +165,7 @@ class Secretaries_model extends EA_Model {
public function validate_username($username, $user_id) public function validate_username($username, $user_id)
{ {
$num_rows = $this->db->get_where('user_settings', $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; return $num_rows > 0 ? FALSE : TRUE;
} }
@ -499,14 +500,14 @@ class Secretaries_model extends EA_Model {
/** /**
* Get all, or specific secretary records from database. * 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 * @param mixed|null $where (OPTIONAL) The WHERE clause of the query to be executed.
* specific secretary records.
* @param mixed|null $order_by
* @param int|null $limit * @param int|null $limit
* @param int|null $offset * @param int|null $offset
* @param mixed|null $order_by
*
* @return array Returns an array with secretary records. * @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(); $role_id = $this->get_secretary_role_id();

View file

@ -23,7 +23,6 @@ class Services_model extends EA_Model {
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
$this->load->helper('data_validation'); $this->load->helper('data_validation');
} }
@ -172,9 +171,11 @@ class Services_model extends EA_Model {
*/ */
public function exists($service) public function exists($service)
{ {
if ( ! isset($service['name']) if ( ! isset(
|| ! isset($service['duration']) $service['name'],
|| ! isset($service['price'])) $service['duration'],
$service['price']
))
{ {
throw new Exception('Not all service fields are provided in order to check whether ' throw new Exception('Not all service fields are provided in order to check whether '
. 'a service record already exists: ' . print_r($service, TRUE)); . 'a service record already exists: ' . print_r($service, TRUE));
@ -316,16 +317,16 @@ class Services_model extends EA_Model {
* *
* Example: * Example:
* *
* $this->model->get_batch(['id' => $record_id]); * $this->services_model->get_batch(['id' => $record_id]);
* *
* @param mixed $where * @param mixed $where
* @param mixed $order_by
* @param int|null $limit * @param int|null $limit
* @param int|null $offset * @param int|null $offset
* @param mixed $order_by
* *
* @return array Returns the rows from the database. * @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) if ($where !== NULL)
{ {

View file

@ -32,12 +32,14 @@ class Settings_model extends EA_Model {
public function get_setting($name) public function get_setting($name)
{ {
if ( ! is_string($name)) if ( ! is_string($name))
{ // Check argument type. {
// Check argument type.
throw new Exception('$name argument is not a string: ' . $name); throw new Exception('$name argument is not a string: ' . $name);
} }
if ($this->db->get_where('settings', ['name' => $name])->num_rows() == 0) 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); 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]); $query = $this->db->get_where('settings', ['name' => $name]);
if ($query->num_rows() > 0) if ($query->num_rows() > 0)
{ {
// Update setting // Update setting
@ -83,10 +86,12 @@ class Settings_model extends EA_Model {
'name' => $name, 'name' => $name,
'value' => $value 'value' => $value
]; ];
if ( ! $this->db->insert('settings', $insert_data)) if ( ! $this->db->insert('settings', $insert_data))
{ {
throw new Exception('Could not insert database setting'); throw new Exception('Could not insert database setting');
} }
$setting_id = (int)$this->db->insert_id(); $setting_id = (int)$this->db->insert_id();
} }

View file

@ -25,9 +25,7 @@ class User_model extends EA_Model {
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
$this->load->library('timezones'); $this->load->library('timezones');
$this->load->helper('general'); $this->load->helper('general');
$this->load->helper('string'); $this->load->helper('string');
} }