diff --git a/application/controllers/Appointments.php b/application/controllers/Appointments.php index 20c1b109..09c8b61d 100755 --- a/application/controllers/Appointments.php +++ b/application/controllers/Appointments.php @@ -35,7 +35,7 @@ use EA\Engine\Types\Url; * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model @@ -59,7 +59,7 @@ class Appointments extends CI_Controller { $this->load->model('services_model'); $this->load->model('customers_model'); $this->load->model('settings_model'); - $this->load->model('timezones_model'); + $this->load->library('timezones'); if ($this->session->userdata('language')) { @@ -106,7 +106,7 @@ class Appointments extends CI_Controller { $display_privacy_policy = $this->settings_model->get_setting('display_privacy_policy'); $privacy_policy_content = $this->settings_model->get_setting('privacy_policy_content'); $display_any_provider = $this->settings_model->get_setting('display_any_provider'); - $timezones = $this->timezones_model->to_array(); + $timezones = $this->timezones->to_array(); // Remove the data that are not needed inside the $available_providers array. foreach ($available_providers as $index => $provider) diff --git a/application/controllers/Backend.php b/application/controllers/Backend.php index f4959994..f6c4e589 100755 --- a/application/controllers/Backend.php +++ b/application/controllers/Backend.php @@ -30,7 +30,7 @@ * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model @@ -88,7 +88,7 @@ class Backend extends CI_Controller { $this->load->model('roles_model'); $this->load->model('user_model'); $this->load->model('secretaries_model'); - $this->load->model('timezones_model'); + $this->load->library('timezones'); $calendar_view_query_param = $this->input->get('view'); @@ -108,7 +108,7 @@ class Backend extends CI_Controller { $view['available_services'] = $this->services_model->get_available_services(); $view['customers'] = $this->customers_model->get_batch(); $view['calendar_view'] = !empty($calendar_view_query_param) ? $calendar_view_query_param : $user['settings']['calendar_view']; - $view['timezones'] = $this->timezones_model->to_array(); + $view['timezones'] = $this->timezones->to_array(); $this->set_user_data($view); if ($this->session->userdata('role_slug') === DB_SLUG_SECRETARY) @@ -225,7 +225,7 @@ class Backend extends CI_Controller { $this->load->model('services_model'); $this->load->model('settings_model'); $this->load->model('user_model'); - $this->load->model('timezones_model'); + $this->load->library('timezones'); $view['base_url'] = $this->config->item('base_url'); $view['user_display_name'] = $this->user_model->get_user_display_name($this->session->userdata('user_id')); @@ -238,7 +238,7 @@ class Backend extends CI_Controller { $view['customers'] = $this->customers_model->get_batch(); $view['available_providers'] = $this->providers_model->get_available_providers(); $view['available_services'] = $this->services_model->get_available_services(); - $view['timezones'] = $this->timezones_model->to_array(); + $view['timezones'] = $this->timezones->to_array(); if ($this->session->userdata('role_slug') === DB_SLUG_SECRETARY) { @@ -278,7 +278,7 @@ class Backend extends CI_Controller { $this->load->model('services_model'); $this->load->model('settings_model'); $this->load->model('user_model'); - $this->load->model('timezones_model'); + $this->load->library('timezones'); $view['base_url'] = $this->config->item('base_url'); $view['user_display_name'] = $this->user_model->get_user_display_name($this->session->userdata('user_id')); @@ -289,7 +289,7 @@ class Backend extends CI_Controller { $view['first_weekday'] = $this->settings_model->get_setting('first_weekday'); $view['services'] = $this->services_model->get_batch(); $view['categories'] = $this->services_model->get_all_categories(); - $view['timezones'] = $this->timezones_model->to_array(); + $view['timezones'] = $this->timezones->to_array(); $this->set_user_data($view); $this->load->view('backend/header', $view); @@ -318,7 +318,7 @@ class Backend extends CI_Controller { $this->load->model('services_model'); $this->load->model('settings_model'); $this->load->model('user_model'); - $this->load->model('timezones_model'); + $this->load->library('timezones'); $view['base_url'] = $this->config->item('base_url'); $view['user_display_name'] = $this->user_model->get_user_display_name($this->session->userdata('user_id')); @@ -332,7 +332,7 @@ class Backend extends CI_Controller { $view['secretaries'] = $this->secretaries_model->get_batch(); $view['services'] = $this->services_model->get_batch(); $view['working_plan'] = $this->settings_model->get_setting('company_working_plan'); - $view['timezones'] = $this->timezones_model->to_array(); + $view['timezones'] = $this->timezones->to_array(); $view['extra_working_plan'] = '{}'; $this->set_user_data($view); @@ -359,7 +359,7 @@ class Backend extends CI_Controller { $this->load->model('settings_model'); $this->load->model('user_model'); - $this->load->model('timezones_model'); + $this->load->library('timezones'); $this->load->library('session'); $user_id = $this->session->userdata('user_id'); @@ -374,7 +374,7 @@ class Backend extends CI_Controller { $view['role_slug'] = $this->session->userdata('role_slug'); $view['system_settings'] = $this->settings_model->get_settings(); $view['user_settings'] = $this->user_model->get_user($user_id); - $view['timezones'] = $this->timezones_model->to_array(); + $view['timezones'] = $this->timezones->to_array(); $this->set_user_data($view); $this->load->view('backend/header', $view); diff --git a/application/controllers/Backend_api.php b/application/controllers/Backend_api.php index 0345c6ac..da14e19c 100755 --- a/application/controllers/Backend_api.php +++ b/application/controllers/Backend_api.php @@ -37,7 +37,7 @@ use EA\Engine\Types\Url; * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model @@ -280,7 +280,8 @@ class Backend_api extends CI_Controller { $this->load->model('services_model'); $this->load->model('customers_model'); $this->load->model('settings_model'); - $this->load->model('timezones_model'); + $this->load->library('timezones'); + $this->load->model('user_model'); // Save customer changes to the database. if ($this->input->post('customer_data')) @@ -319,14 +320,14 @@ class Backend_api extends CI_Controller { $appointment['id_users_customer'] = $customer['id']; } - $provider_timezone = $this->timezones_model->get_user_timezone($appointment['id_users_provider']); + $provider_timezone = $this->user_model->get_user_timezone($appointment['id_users_provider']); - $session_timezone = $this->timezones_model->get_session_timezone(); + $session_timezone = $this->timezones->get_session_timezone(); - $appointment['start_datetime'] = $this->timezones_model->convert($appointment['start_datetime'], + $appointment['start_datetime'] = $this->timezones->convert($appointment['start_datetime'], $session_timezone, $provider_timezone); - $appointment['end_datetime'] = $this->timezones_model->convert($appointment['end_datetime'], + $appointment['end_datetime'] = $this->timezones->convert($appointment['end_datetime'], $session_timezone, $provider_timezone); $appointment['id'] = $this->appointments_model->add($appointment); diff --git a/application/controllers/Captcha.php b/application/controllers/Captcha.php index 634f6d95..86bd4b90 100644 --- a/application/controllers/Captcha.php +++ b/application/controllers/Captcha.php @@ -30,7 +30,7 @@ * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/Consents.php b/application/controllers/Consents.php index 30c5dd69..60494828 100644 --- a/application/controllers/Consents.php +++ b/application/controllers/Consents.php @@ -33,7 +33,7 @@ * @property Customers_Model customers_model * @property Consents_Model consents_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/Errors.php b/application/controllers/Errors.php index a831a993..49c01f45 100644 --- a/application/controllers/Errors.php +++ b/application/controllers/Errors.php @@ -30,7 +30,7 @@ * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/Google.php b/application/controllers/Google.php index 7f589f40..cde455b2 100644 --- a/application/controllers/Google.php +++ b/application/controllers/Google.php @@ -32,7 +32,7 @@ * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/Installation.php b/application/controllers/Installation.php index 023b62eb..4c1aaa1e 100644 --- a/application/controllers/Installation.php +++ b/application/controllers/Installation.php @@ -33,7 +33,7 @@ * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/Privacy.php b/application/controllers/Privacy.php index 8d54b72d..77186cba 100644 --- a/application/controllers/Privacy.php +++ b/application/controllers/Privacy.php @@ -30,7 +30,7 @@ * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/User.php b/application/controllers/User.php index e2d07c88..37e0e6fc 100644 --- a/application/controllers/User.php +++ b/application/controllers/User.php @@ -34,7 +34,7 @@ use EA\Engine\Types\NonEmptyText; * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/api/v1/Admins.php b/application/controllers/api/v1/Admins.php index d33985b9..f67ec3c6 100644 --- a/application/controllers/api/v1/Admins.php +++ b/application/controllers/api/v1/Admins.php @@ -36,7 +36,7 @@ use EA\Engine\Types\NonEmptyText; * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/api/v1/Appointments.php b/application/controllers/api/v1/Appointments.php index c549d355..b2b46890 100644 --- a/application/controllers/api/v1/Appointments.php +++ b/application/controllers/api/v1/Appointments.php @@ -36,7 +36,7 @@ use EA\Engine\Types\NonEmptyText; * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/api/v1/Availabilities.php b/application/controllers/api/v1/Availabilities.php index 66e0e1c8..945f848b 100644 --- a/application/controllers/api/v1/Availabilities.php +++ b/application/controllers/api/v1/Availabilities.php @@ -35,7 +35,7 @@ use EA\Engine\Types\UnsignedInteger; * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/api/v1/Categories.php b/application/controllers/api/v1/Categories.php index d3899a0e..6fb0cbf0 100644 --- a/application/controllers/api/v1/Categories.php +++ b/application/controllers/api/v1/Categories.php @@ -36,7 +36,7 @@ use EA\Engine\Types\NonEmptyText; * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/api/v1/Customers.php b/application/controllers/api/v1/Customers.php index 6e5f26d8..4fb1f25c 100644 --- a/application/controllers/api/v1/Customers.php +++ b/application/controllers/api/v1/Customers.php @@ -36,7 +36,7 @@ use EA\Engine\Types\NonEmptyText; * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/api/v1/Providers.php b/application/controllers/api/v1/Providers.php index e641472a..695666eb 100644 --- a/application/controllers/api/v1/Providers.php +++ b/application/controllers/api/v1/Providers.php @@ -36,7 +36,7 @@ use EA\Engine\Types\NonEmptyText; * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/api/v1/Secretaries.php b/application/controllers/api/v1/Secretaries.php index ff5bfb80..5aa8fb42 100644 --- a/application/controllers/api/v1/Secretaries.php +++ b/application/controllers/api/v1/Secretaries.php @@ -36,7 +36,7 @@ use EA\Engine\Types\NonEmptyText; * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/api/v1/Services.php b/application/controllers/api/v1/Services.php index 2330519a..cd3427e4 100644 --- a/application/controllers/api/v1/Services.php +++ b/application/controllers/api/v1/Services.php @@ -36,7 +36,7 @@ use EA\Engine\Types\NonEmptyText; * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/api/v1/Settings.php b/application/controllers/api/v1/Settings.php index 3acfb500..07a694ce 100644 --- a/application/controllers/api/v1/Settings.php +++ b/application/controllers/api/v1/Settings.php @@ -35,7 +35,7 @@ use EA\Engine\Api\V1\Response; * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/controllers/api/v1/Unavailabilities.php b/application/controllers/api/v1/Unavailabilities.php index 9b34a37f..f3534b60 100644 --- a/application/controllers/api/v1/Unavailabilities.php +++ b/application/controllers/api/v1/Unavailabilities.php @@ -36,7 +36,7 @@ use EA\Engine\Types\NonEmptyText; * @property Services_Model services_model * @property Customers_Model customers_model * @property Settings_Model settings_model - * @property Timezones_Model timezones_model + * @property Timezones timezones * @property Roles_Model roles_model * @property Secretaries_Model secretaries_model * @property Admins_Model admins_model diff --git a/application/helpers/render_helper.php b/application/helpers/render_helper.php index c59f6d54..84e6a595 100644 --- a/application/helpers/render_helper.php +++ b/application/helpers/render_helper.php @@ -21,9 +21,9 @@ function render_timezone_dropdown($attributes = '') { $framework = get_instance(); - $framework->load->model('timezones_model'); + $framework->load->library('timezones'); - $timezones = $framework->timezones_model->to_grouped_array(); + $timezones = $framework->timezones->to_grouped_array(); ob_start(); diff --git a/application/models/Timezones_model.php b/application/libraries/Timezones.php similarity index 97% rename from application/models/Timezones_model.php rename to application/libraries/Timezones.php index 48a9fd9f..befa61d5 100644 --- a/application/models/Timezones_model.php +++ b/application/libraries/Timezones.php @@ -12,14 +12,18 @@ * ---------------------------------------------------------------------------- */ /** - * Timezones Model + * Timezones * - * @property CI_DB_query_builder db * @property CI_Loader load * * @package Models */ -class Timezones_Model extends CI_Model { +class Timezones { + /** + * @var CI_Controller + */ + protected $framework; + /** * @var string */ @@ -460,6 +464,15 @@ class Timezones_Model extends CI_Model { ], ]; + public function __construct() + { + $this->framework = & get_instance(); + + $this->framework->load->model('user_model'); + + $this->framework->load->library('session'); + } + /** * Get all timezones to a grouped array (by continent). * @@ -474,7 +487,9 @@ class Timezones_Model extends CI_Model { * Convert the dates of an event to the timezone of the user. * * @param array $event Must contain the "start_datetime", "end_datetime" and "id_users_provider" properties. + * * @return array + * * @throws Exception */ public function convert_event_timezone($event) @@ -484,7 +499,7 @@ class Timezones_Model extends CI_Model { throw new Exception('The provided event does not have the required properties: ' . print_r($event)); } - $user_timezone = $this->get_user_timezone($event['id_users_provider']); + $user_timezone = $this->framework->user_model->get_user_timezone($event['id_users_provider']); $session_timezone = $this->get_session_timezone(); @@ -497,18 +512,6 @@ class Timezones_Model extends CI_Model { return $event; } - /** - * Get the timezone of a user. - * - * @param int $id Database ID of the user. - * @return string|null - */ - public function get_user_timezone($id) - { - $row = $this->db->get_where('users', ['id' => $id])->row_array(); - - return $row ? $row['timezone'] : NULL; - } /** * Returns the session timezone or the default timezone as a fallback. @@ -517,11 +520,11 @@ class Timezones_Model extends CI_Model { */ public function get_session_timezone() { - $this->load->library('session'); - $default_timezone = $this->get_default_timezone(); - return isset($this->session->timezone) ? $this->session->timezone : $default_timezone; + return $this->framework->session->has_userdata('timezone') + ? $this->framework->session->userdata('timezone') + : $default_timezone; } /** @@ -540,7 +543,9 @@ class Timezones_Model extends CI_Model { * @param string $value Provide a date time value as a string (format Y-m-d H:i:s). * @param string $from_timezone From timezone value. * @param string $to_timezone To timezone value. + * * @return string + * * @throws Exception */ public function convert($value, $from_timezone, $to_timezone) @@ -565,6 +570,7 @@ class Timezones_Model extends CI_Model { * Get the timezone name for the provided value. * * @param string $value + * * @return string|null */ public function get_timezone_name($value) diff --git a/application/models/Appointments_model.php b/application/models/Appointments_model.php index a1b7127b..539a6e18 100644 --- a/application/models/Appointments_model.php +++ b/application/models/Appointments_model.php @@ -302,9 +302,9 @@ class Appointments_Model extends CI_Model { $appointment = $this->db->get_where('appointments', ['id' => $appointment_id])->row_array(); - $this->load->model('timezones_model'); + $this->load->library('timezones'); - $appointment = $this->timezones_model->convert_event_timezone($appointment); + $appointment = $this->timezones->convert_event_timezone($appointment); return $appointment; } @@ -348,9 +348,9 @@ class Appointments_Model extends CI_Model { throw new Exception('The given field name does not exist in the database: ' . $field_name); } - $this->load->model('timezones_model'); + $this->load->library('timezones'); - $row_data = $this->timezones_model->convert_event_timezone($row_data); + $row_data = $this->timezones->convert_event_timezone($row_data); return $row_data[$field_name]; } @@ -385,11 +385,11 @@ class Appointments_Model extends CI_Model { $appointments = $this->db->get('appointments', $limit, $offset)->result_array(); - $this->load->model('timezones_model'); + $this->load->library('timezones'); foreach ($appointments as &$appointment) { - $appointment = $this->timezones_model->convert_event_timezone($appointment); + $appointment = $this->timezones->convert_event_timezone($appointment); if ($aggregates) { diff --git a/application/models/User_model.php b/application/models/User_model.php index 6ed2b763..904544ad 100644 --- a/application/models/User_model.php +++ b/application/models/User_model.php @@ -109,9 +109,9 @@ class User_Model extends CI_Model { return NULL; } - $this->load->model('timezones_model'); + $this->load->library('timezones'); - $default_timezone = $this->timezones_model->get_default_timezone(); + $default_timezone = $this->timezones->get_default_timezone(); return [ 'user_id' => $user['id'], @@ -192,4 +192,18 @@ class User_Model extends CI_Model { return $new_password; } + + /** + * Get the timezone of a user. + * + * @param int $id Database ID of the user. + * + * @return string|null + */ + public function get_user_timezone($id) + { + $row = $this->db->get_where('users', ['id' => $id])->row_array(); + + return $row ? $row['timezone'] : NULL; + } }