From 1fbe17aade1c25d6d1dfcbcdb9aa3159fa5cc9d7 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sat, 11 May 2024 17:03:54 +0200 Subject: [PATCH] Define get/set methods for related model resources (provider, secretary, admin users) --- application/models/Admins_model.php | 37 ++++--- application/models/Providers_model.php | 117 ++++++++++------------- application/models/Secretaries_model.php | 112 ++++++++++------------ 3 files changed, 125 insertions(+), 141 deletions(-) diff --git a/application/models/Admins_model.php b/application/models/Admins_model.php index ac5fc5d8..507d420d 100644 --- a/application/models/Admins_model.php +++ b/application/models/Admins_model.php @@ -211,10 +211,7 @@ class Admins_model extends EA_Model foreach ($admins as &$admin) { $this->cast($admin); - - $admin['settings'] = $this->db->get_where('user_settings', ['id_users' => $admin['id']])->row_array(); - - unset($admin['settings']['id_users'], $admin['settings']['password'], $admin['settings']['salt']); + $admin['settings'] = $this->get_settings($admin['id']); } return $admins; @@ -264,7 +261,7 @@ class Admins_model extends EA_Model $settings['salt'] = generate_salt(); $settings['password'] = hash_password($settings['salt'], $settings['password']); - $this->save_settings($admin['id'], $settings); + $this->set_settings($admin['id'], $settings); return $admin['id']; } @@ -277,7 +274,7 @@ class Admins_model extends EA_Model * * @throws InvalidArgumentException */ - protected function save_settings(int $admin_id, array $settings): void + public function set_settings(int $admin_id, array $settings): void { if (empty($settings)) { throw new InvalidArgumentException('The settings argument cannot be empty.'); @@ -295,6 +292,22 @@ class Admins_model extends EA_Model } } + /** + * Get the admin settings. + * + * @param int $admin_id Admin ID. + * + * @throws InvalidArgumentException + */ + public function get_settings(int $admin_id): array + { + $settings = $this->db->get_where('user_settings', ['id_users' => $admin_id])->row_array(); + + unset($settings['id_users'], $settings['password'], $settings['salt']); + + return $settings; + } + /** * Set the value of an admin setting. * @@ -347,7 +360,7 @@ class Admins_model extends EA_Model throw new RuntimeException('Could not update admin.'); } - $this->save_settings($admin['id'], $settings); + $this->set_settings($admin['id'], $settings); return $admin['id']; } @@ -390,10 +403,7 @@ class Admins_model extends EA_Model } $this->cast($admin); - - $admin['settings'] = $this->db->get_where('user_settings', ['id_users' => $admin_id])->row_array(); - - unset($admin['settings']['id_users'], $admin['settings']['password'], $admin['settings']['salt']); + $admin['settings'] = $this->get_settings($admin['id']); return $admin; } @@ -507,10 +517,7 @@ class Admins_model extends EA_Model foreach ($admins as &$admin) { $this->cast($admin); - - $admin['settings'] = $this->db->get_where('user_settings', ['id_users' => $admin['id']])->row_array(); - - unset($admin['settings']['id_users'], $admin['settings']['password'], $admin['settings']['salt']); + $admin['settings'] = $this->get_settings($admin['id']); } return $admins; diff --git a/application/models/Providers_model.php b/application/models/Providers_model.php index 402b4d3c..a3db5df6 100755 --- a/application/models/Providers_model.php +++ b/application/models/Providers_model.php @@ -225,20 +225,8 @@ class Providers_model extends EA_Model foreach ($providers as &$provider) { $this->cast($provider); - - $provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider['id']])->row_array(); - - unset($provider['settings']['id_users'], $provider['settings']['password'], $provider['settings']['salt']); - - $provider['services'] = []; - - $service_provider_connections = $this->db - ->get_where('services_providers', ['id_users' => $provider['id']]) - ->result_array(); - - foreach ($service_provider_connections as $service_provider_connection) { - $provider['services'][] = (int) $service_provider_connection['id_services']; - } + $provider['settings'] = $this->get_settings($provider['id']); + $provider['services'] = $this->get_service_ids($provider['id']); } return $providers; @@ -289,8 +277,8 @@ class Providers_model extends EA_Model $settings['salt'] = generate_salt(); $settings['password'] = hash_password($settings['salt'], $settings['password']); - $this->save_settings($provider['id'], $settings); - $this->save_service_ids($provider['id'], $service_ids); + $this->set_settings($provider['id'], $settings); + $this->set_service_ids($provider['id'], $service_ids); return $provider['id']; } @@ -303,7 +291,7 @@ class Providers_model extends EA_Model * * @throws InvalidArgumentException */ - protected function save_settings(int $provider_id, array $settings): void + public function set_settings(int $provider_id, array $settings): void { if (empty($settings)) { throw new InvalidArgumentException('The settings argument cannot be empty.'); @@ -334,6 +322,22 @@ class Providers_model extends EA_Model } } + /** + * Get the provider settings. + * + * @param int $provider_id Provider ID. + * + * @throws InvalidArgumentException + */ + public function get_settings(int $provider_id): array + { + $settings = $this->db->get_where('user_settings', ['id_users' => $provider_id])->row_array(); + + unset($settings['id_users'], $settings['password'], $settings['salt']); + + return $settings; + } + /** * Set the value of a provider setting. * @@ -385,8 +389,8 @@ class Providers_model extends EA_Model throw new RuntimeException('Could not update provider.'); } - $this->save_settings($provider['id'], $settings); - $this->save_service_ids($provider['id'], $service_ids); + $this->set_settings($provider['id'], $settings); + $this->set_service_ids($provider['id'], $service_ids); return $provider['id']; } @@ -397,7 +401,7 @@ class Providers_model extends EA_Model * @param int $provider_id Provider ID. * @param array $service_ids Service IDs. */ - protected function save_service_ids(int $provider_id, array $service_ids): void + public function set_service_ids(int $provider_id, array $service_ids): void { // Re-insert the provider-service connections. $this->db->delete('services_providers', ['id_users' => $provider_id]); @@ -412,6 +416,26 @@ class Providers_model extends EA_Model } } + /** + * Get the provider service IDs. + * + * @param int $provider_id Provider ID. + */ + public function get_service_ids(int $provider_id): array + { + $service_provider_connections = $this->db + ->get_where('services_providers', ['id_users' => $provider_id]) + ->result_array(); + + $service_ids = []; + + foreach ($service_provider_connections as $service_provider_connection) { + $service_ids[] = (int) $service_provider_connection['id_services']; + } + + return $service_ids; + } + /** * Remove an existing provider from the database. * @@ -553,20 +577,8 @@ class Providers_model extends EA_Model } $this->cast($provider); - - $provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider_id])->row_array(); - - unset($provider['settings']['id_users'], $provider['settings']['password'], $provider['settings']['salt']); - - $service_provider_connections = $this->db - ->get_where('services_providers', ['id_users' => $provider_id]) - ->result_array(); - - $provider['services'] = []; - - foreach ($service_provider_connections as $service_provider_connection) { - $provider['services'][] = (int) $service_provider_connection['id_services']; - } + $provider['settings'] = $this->get_settings($provider['id']); + $provider['services'] = $this->get_service_ids($provider['id']); return $provider; } @@ -622,25 +634,8 @@ class Providers_model extends EA_Model foreach ($providers as &$provider) { $this->cast($provider); - - $provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider['id']])->row_array(); - - unset( - $provider['settings']['id_users'], - $provider['settings']['username'], - $provider['settings']['password'], - $provider['settings']['salt'], - ); - - $provider['services'] = []; - - $service_provider_connections = $this->db - ->get_where('services_providers', ['id_users' => $provider['id']]) - ->result_array(); - - foreach ($service_provider_connections as $service_provider_connection) { - $provider['services'][] = (int) $service_provider_connection['id_services']; - } + $provider['settings'] = $this->get_settings($provider['id']); + $provider['services'] = $this->get_service_ids($provider['id']); } return $providers; @@ -697,20 +692,8 @@ class Providers_model extends EA_Model foreach ($providers as &$provider) { $this->cast($provider); - - $provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider['id']])->row_array(); - - unset($provider['settings']['id_users'], $provider['settings']['password'], $provider['settings']['salt']); - - $provider['services'] = []; - - $service_provider_connections = $this->db - ->get_where('services_providers', ['id_users' => $provider['id']]) - ->result_array(); - - foreach ($service_provider_connections as $service_provider_connection) { - $provider['services'][] = (int) $service_provider_connection['id_services']; - } + $provider['settings'] = $this->get_settings($provider['id']); + $provider['services'] = $this->get_service_ids($provider['id']); } return $providers; diff --git a/application/models/Secretaries_model.php b/application/models/Secretaries_model.php index 904e72f3..6be14064 100644 --- a/application/models/Secretaries_model.php +++ b/application/models/Secretaries_model.php @@ -222,25 +222,9 @@ class Secretaries_model extends EA_Model $secretaries = $this->db->get_where('users', ['id_roles' => $role_id], $limit, $offset)->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']; - } + $this->cast($secretary); + $secretary['settings'] = $this->get_settings($secretary['id']); + $secretary['providers'] = $this->get_provider_ids($secretary['id']); } return $secretaries; @@ -292,21 +276,21 @@ class Secretaries_model extends EA_Model $settings['salt'] = generate_salt(); $settings['password'] = hash_password($settings['salt'], $settings['password']); - $this->save_settings($secretary['id'], $settings); - $this->save_provider_ids($secretary['id'], $provider_ids); + $this->set_settings($secretary['id'], $settings); + $this->set_provider_ids($secretary['id'], $provider_ids); return $secretary['id']; } /** - * Save the secretary settings. + * Set the secretary settings. * * @param int $secretary_id Secretary ID. * @param array $settings Associative array with the settings data. * * @throws InvalidArgumentException */ - protected function save_settings(int $secretary_id, array $settings): void + public function set_settings(int $secretary_id, array $settings): void { if (empty($settings)) { throw new InvalidArgumentException('The settings argument cannot be empty.'); @@ -324,6 +308,22 @@ class Secretaries_model extends EA_Model } } + /** + * Get the secretary settings. + * + * @param int $secretary_id Secretary ID. + * + * @throws InvalidArgumentException + */ + public function get_settings(int $secretary_id): array + { + $settings = $this->db->get_where('user_settings', ['id_users' => $secretary_id])->row_array(); + + unset($settings['id_users'], $settings['password'], $settings['salt']); + + return $settings; + } + /** * Set the value of a secretary setting. * @@ -375,19 +375,19 @@ class Secretaries_model extends EA_Model throw new RuntimeException('Could not update secretary.'); } - $this->save_settings($secretary['id'], $settings); - $this->save_provider_ids($secretary['id'], $provider_ids); + $this->set_settings($secretary['id'], $settings); + $this->set_provider_ids($secretary['id'], $provider_ids); return (int) $secretary['id']; } /** - * Save the secretary provider IDs. + * Set the secretary provider IDs. * * @param int $secretary_id Secretary ID. * @param array $provider_ids Provider IDs. */ - protected function save_provider_ids(int $secretary_id, array $provider_ids): void + public function set_provider_ids(int $secretary_id, array $provider_ids): void { // Re-insert the secretary-provider connections. $this->db->delete('secretaries_providers', ['id_users_secretary' => $secretary_id]); @@ -402,6 +402,26 @@ class Secretaries_model extends EA_Model } } + /** + * Get the secretary provider IDs. + * + * @param int $secretary_id Secretary ID. + */ + public function get_provider_ids(int $secretary_id): array + { + $secretary_provider_connections = $this->db + ->get_where('secretaries_providers', ['id_users_secretary' => $secretary_id]) + ->result_array(); + + $provider_ids = []; + + foreach ($secretary_provider_connections as $secretary_provider_connection) { + $provider_ids[] = (int) $secretary_provider_connection['id_users_provider']; + } + + return $provider_ids; + } + /** * Remove an existing secretary from the database. * @@ -522,25 +542,9 @@ class Secretaries_model extends EA_Model ->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']; - } + $this->cast($secretary); + $secretary['settings'] = $this->get_settings($secretary['id']); + $secretary['providers'] = $this->get_provider_ids($secretary['id']); } return $secretaries; @@ -731,19 +735,9 @@ class Secretaries_model extends EA_Model ); } - $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']; - } + $this->cast($secretary); + $secretary['settings'] = $this->get_settings($secretary['id']); + $secretary['providers'] = $this->get_provider_ids($secretary['id']); return $secretary; }