From 39c8ec9068ad134f3c3cf830586620ef43a26d45 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Thu, 12 Nov 2020 14:51:10 +0200 Subject: [PATCH] Groupped load commands in model constructor methods --- application/models/Admins_model.php | 14 ++++++++------ application/models/Appointments_model.php | 20 ++++++++++++-------- application/models/Customers_model.php | 15 +++++++++++---- application/models/Providers_model.php | 15 +++++++++++---- application/models/Secretaries_model.php | 17 +++++++++++------ application/models/Services_model.php | 19 +++++++++++++------ application/models/User_model.php | 20 +++++++++++++------- 7 files changed, 79 insertions(+), 41 deletions(-) diff --git a/application/models/Admins_model.php b/application/models/Admins_model.php index 37b5a540..83a644d6 100644 --- a/application/models/Admins_model.php +++ b/application/models/Admins_model.php @@ -22,6 +22,14 @@ * @package Models */ class Admins_Model extends CI_Model { + public function __construct() + { + parent::__construct(); + + $this->load->helper('general'); + $this->load->helper('data_validation'); + } + /** * Add (insert or update) an admin user record into database. * @@ -63,8 +71,6 @@ class Admins_Model extends CI_Model { */ public function validate($admin) { - $this->load->helper('data_validation'); - // If a record id is provided then check whether the record exists in the database. if (isset($admin['id'])) { @@ -227,8 +233,6 @@ class Admins_Model extends CI_Model { */ protected function insert($admin) { - $this->load->helper('general'); - $admin['id_roles'] = $this->get_admin_role_id(); $settings = $admin['settings']; unset($admin['settings']); @@ -278,8 +282,6 @@ class Admins_Model extends CI_Model { */ protected function update($admin) { - $this->load->helper('general'); - $settings = $admin['settings']; unset($admin['settings']); $settings['id_users'] = $admin['id']; diff --git a/application/models/Appointments_model.php b/application/models/Appointments_model.php index 5f86f8c5..8b49cde5 100644 --- a/application/models/Appointments_model.php +++ b/application/models/Appointments_model.php @@ -20,6 +20,18 @@ * @package Models */ class Appointments_Model extends CI_Model { + /** + * Appointments_Model constructor. + */ + public function __construct() + { + parent::__construct(); + + $this->load->helper('data_validation'); + + $this->load->library('timezones'); + } + /** * Add an appointment record to the database. * @@ -61,8 +73,6 @@ class Appointments_Model extends CI_Model { */ public function validate($appointment) { - $this->load->helper('data_validation'); - // If a appointment id is given, check whether the record exists in the database. if (isset($appointment['id'])) { @@ -310,8 +320,6 @@ class Appointments_Model extends CI_Model { $appointment = $this->db->get_where('appointments', ['id' => $appointment_id])->row_array(); - $this->load->library('timezones'); - $appointment = $this->timezones->convert_event_timezone($appointment); return $appointment; @@ -356,8 +364,6 @@ class Appointments_Model extends CI_Model { throw new Exception('The given field name does not exist in the database: ' . $field_name); } - $this->load->library('timezones'); - $row_data = $this->timezones->convert_event_timezone($row_data); return $row_data[$field_name]; @@ -393,8 +399,6 @@ class Appointments_Model extends CI_Model { $appointments = $this->db->get('appointments', $limit, $offset)->result_array(); - $this->load->library('timezones'); - foreach ($appointments as &$appointment) { $appointment = $this->timezones->convert_event_timezone($appointment); diff --git a/application/models/Customers_model.php b/application/models/Customers_model.php index d11fa165..94409bed 100644 --- a/application/models/Customers_model.php +++ b/application/models/Customers_model.php @@ -20,6 +20,16 @@ * @package Models */ class Customers_Model extends CI_Model { + /** + * Customers_Model constructor. + */ + public function __construct() + { + parent::__construct(); + + $this->load->helper('data_validation'); + } + /** * Add a customer record to the database. * @@ -68,10 +78,7 @@ class Customers_Model extends CI_Model { */ public function validate($customer) { - $this->load->helper('data_validation'); - - // 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'])) { $num_rows = $this->db->get_where('users', diff --git a/application/models/Providers_model.php b/application/models/Providers_model.php index cf354cd8..bac0b4b5 100755 --- a/application/models/Providers_model.php +++ b/application/models/Providers_model.php @@ -22,6 +22,17 @@ * @package Models */ class Providers_Model extends CI_Model { + /** + * Providers_Model constructor. + */ + public function __construct() + { + parent::__construct(); + + $this->load->helper('data_validation'); + $this->load->helper('general'); + } + /** * Add (insert - update) a service provider record. * @@ -66,7 +77,6 @@ class Providers_Model extends CI_Model { */ public function validate($provider) { - $this->load->helper('data_validation'); // If a provider id is present, check whether the record exist in the database. if (isset($provider['id'])) @@ -254,7 +264,6 @@ class Providers_Model extends CI_Model { */ protected function insert($provider) { - $this->load->helper('general'); // Get provider role id. $provider['id_roles'] = $this->get_providers_role_id(); @@ -394,8 +403,6 @@ class Providers_Model extends CI_Model { */ protected function update($provider) { - $this->load->helper('general'); - // Store service and settings (must not be present on the $provider array). $services = $provider['services']; unset($provider['services']); diff --git a/application/models/Secretaries_model.php b/application/models/Secretaries_model.php index 861a20a0..d08f7891 100644 --- a/application/models/Secretaries_model.php +++ b/application/models/Secretaries_model.php @@ -22,6 +22,17 @@ * @package Models */ class Secretaries_Model extends CI_Model { + /** + * Secretaries_Model constructor. + */ + public function __construct() + { + parent::__construct(); + + $this->load->helper('general'); + $this->load->helper('data_validation'); + } + /** * Add (insert or update) a secretary user record into database. * @@ -63,8 +74,6 @@ class Secretaries_Model extends CI_Model { */ public function validate($secretary) { - $this->load->helper('data_validation'); - // If a record id is provided then check whether the record exists in the database. if (isset($secretary['id'])) { @@ -233,8 +242,6 @@ class Secretaries_Model extends CI_Model { */ protected function insert($secretary) { - $this->load->helper('general'); - $providers = $secretary['providers']; unset($secretary['providers']); $settings = $secretary['settings']; @@ -358,8 +365,6 @@ class Secretaries_Model extends CI_Model { */ protected function update($secretary) { - $this->load->helper('general'); - $providers = $secretary['providers']; unset($secretary['providers']); $settings = $secretary['settings']; diff --git a/application/models/Services_model.php b/application/models/Services_model.php index df6d101a..b6767493 100644 --- a/application/models/Services_model.php +++ b/application/models/Services_model.php @@ -20,6 +20,16 @@ * @package Models */ class Services_Model extends CI_Model { + /** + * Services_Model constructor. + */ + public function __construct() + { + parent::__construct(); + + $this->load->helper('data_validation'); + } + /** * Add (insert or update) a service record on the database * @@ -55,14 +65,11 @@ class Services_Model extends CI_Model { */ public function validate($service) { - $this->load->helper('data_validation'); - - // If record id is provided we need to check whether the record exists - // in the database. + // If record id is provided we need to check whether the record exists in the database. if (isset($service['id'])) { - $num_rows = $this->db->get_where('services', ['id' => $service['id']]) - ->num_rows(); + $num_rows = $this->db->get_where('services', ['id' => $service['id']])->num_rows(); + if ($num_rows == 0) { throw new Exception('Provided service id does not exist in the database.'); diff --git a/application/models/User_model.php b/application/models/User_model.php index 417734aa..64db2564 100644 --- a/application/models/User_model.php +++ b/application/models/User_model.php @@ -22,6 +22,19 @@ * @package Models */ class User_Model extends CI_Model { + /** + * User_Model constructor. + */ + public function __construct() + { + parent::__construct(); + + $this->load->library('timezones'); + + $this->load->helper('general'); + $this->load->helper('string'); + } + /** * Returns the user from the database for the "settings" page. * @@ -53,7 +66,6 @@ class User_Model extends CI_Model { // Prepare user password (hash). if (isset($user_settings['password'])) { - $this->load->helper('general'); $salt = $this->db->get_where('user_settings', ['id_users' => $user['id']])->row()->salt; $user_settings['password'] = hash_password($salt, $user_settings['password']); } @@ -81,7 +93,6 @@ class User_Model extends CI_Model { */ public function check_login($username, $password) { - $this->load->helper('general'); $salt = $this->get_salt($username); $password = hash_password($salt, $password); @@ -109,8 +120,6 @@ class User_Model extends CI_Model { return NULL; } - $this->load->library('timezones'); - $default_timezone = $this->timezones->get_default_timezone(); return [ @@ -167,9 +176,6 @@ class User_Model extends CI_Model { */ public function regenerate_password($username, $email) { - $this->load->helper('general'); - $this->load->helper('string'); - $result = $this->db ->select('users.id') ->from('users')