Always display events in the provider's timezone in the backend calendar pages

This commit is contained in:
Alex Tselegidis 2020-12-14 20:41:21 +02:00
parent 5512731c41
commit cf7bad9f19
3 changed files with 11 additions and 70 deletions

View file

@ -82,11 +82,11 @@ class Backend_api extends EA_Controller {
]) ])
]; ];
foreach ($response['appointments'] as $index => $appointment) foreach ($response['appointments'] as &$appointment)
{ {
$response['appointments'][$index]['provider'] = $this->providers_model->get_row($appointment['id_users_provider']); $appointment['provider'] = $this->providers_model->get_row($appointment['id_users_provider']);
$response['appointments'][$index]['service'] = $this->services_model->get_row($appointment['id_services']); $appointment['service'] = $this->services_model->get_row($appointment['id_services']);
$response['appointments'][$index]['customer'] = $this->customers_model->get_row($appointment['id_users_customer']); $appointment['customer'] = $this->customers_model->get_row($appointment['id_users_customer']);
} }
$user_id = $this->session->userdata('user_id'); $user_id = $this->session->userdata('user_id');
@ -218,6 +218,11 @@ class Backend_api extends EA_Controller {
$response['unavailables'] = $this->appointments_model->get_batch($where_clause); $response['unavailables'] = $this->appointments_model->get_batch($where_clause);
} }
foreach ($response['unavailables'] as &$unavailable)
{
$unavailable['provider'] = $this->providers_model->get_row($unavailable['id_users_provider']);
}
$this->output $this->output
->set_content_type('application/json') ->set_content_type('application/json')
->set_output(json_encode($response)); ->set_output(json_encode($response));
@ -282,16 +287,6 @@ class Backend_api extends EA_Controller {
$appointment['id_users_customer'] = $customer['id']; $appointment['id_users_customer'] = $customer['id'];
} }
$provider_timezone = $this->user_model->get_user_timezone($appointment['id_users_provider']);
$session_timezone = $this->timezones->get_session_timezone();
$appointment['start_datetime'] = $this->timezones->convert($appointment['start_datetime'],
$session_timezone, $provider_timezone);
$appointment['end_datetime'] = $this->timezones->convert($appointment['end_datetime'],
$session_timezone, $provider_timezone);
$appointment['id'] = $this->appointments_model->add($appointment); $appointment['id'] = $this->appointments_model->add($appointment);
} }
@ -565,8 +560,7 @@ class Backend_api extends EA_Controller {
foreach ($customers as &$customer) foreach ($customers as &$customer)
{ {
$appointments = $this->appointments_model $appointments = $this->appointments_model->get_batch(['id_users_customer' => $customer['id']]);
->get_batch(['id_users_customer' => $customer['id']]);
foreach ($appointments as &$appointment) foreach ($appointments as &$appointment)
{ {
@ -615,16 +609,6 @@ class Backend_api extends EA_Controller {
$provider = $this->providers_model->get_row($unavailable['id_users_provider']); $provider = $this->providers_model->get_row($unavailable['id_users_provider']);
// Add appointment // Add appointment
$provider_timezone = $this->user_model->get_user_timezone($unavailable['id_users_provider']);
$session_timezone = $this->timezones->get_session_timezone();
$unavailable['start_datetime'] = $this->timezones->convert($unavailable['start_datetime'],
$session_timezone, $provider_timezone);
$unavailable['end_datetime'] = $this->timezones->convert($unavailable['end_datetime'],
$session_timezone, $provider_timezone);
$unavailable['id'] = $this->appointments_model->add_unavailable($unavailable); $unavailable['id'] = $this->appointments_model->add_unavailable($unavailable);
$unavailable = $this->appointments_model->get_row($unavailable['id']); // fetch all inserted data $unavailable = $this->appointments_model->get_row($unavailable['id']); // fetch all inserted data

View file

@ -13,10 +13,6 @@
/** /**
* Timezones * Timezones
*
* @property CI_Loader $load
*
* @package Models
*/ */
class Timezones { class Timezones {
/** /**
@ -484,36 +480,6 @@ class Timezones {
return $this->timezones; return $this->timezones;
} }
/**
* 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)
{
if ( ! isset($event['start_datetime'], $event['end_datetime'], $event['id_users_provider']))
{
throw new Exception('The provided event does not have the required properties: ' . print_r($event));
}
$user_timezone = $this->CI->user_model->get_user_timezone($event['id_users_provider']);
$session_timezone = $this->get_session_timezone();
$event['start_datetime'] = $this->convert($event['start_datetime'],
$user_timezone, $session_timezone);
$event['end_datetime'] = $this->convert($event['end_datetime'],
$user_timezone, $session_timezone);
return $event;
}
/** /**
* Returns the session timezone or the default timezone as a fallback. * Returns the session timezone or the default timezone as a fallback.
* *

View file

@ -24,7 +24,6 @@ class Appointments_model extends EA_Model {
{ {
parent::__construct(); parent::__construct();
$this->load->helper('data_validation'); $this->load->helper('data_validation');
$this->load->library('timezones');
} }
/** /**
@ -317,11 +316,7 @@ class Appointments_model extends EA_Model {
. $appointment_id); . $appointment_id);
} }
$appointment = $this->db->get_where('appointments', ['id' => $appointment_id])->row_array(); return $this->db->get_where('appointments', ['id' => $appointment_id])->row_array();
$appointment = $this->timezones->convert_event_timezone($appointment);
return $appointment;
} }
/** /**
@ -363,8 +358,6 @@ class Appointments_model extends EA_Model {
throw new Exception('The given field name does not exist in the database: ' . $field_name); throw new Exception('The given field name does not exist in the database: ' . $field_name);
} }
$row_data = $this->timezones->convert_event_timezone($row_data);
return $row_data[$field_name]; return $row_data[$field_name];
} }
@ -401,8 +394,6 @@ class Appointments_model extends EA_Model {
foreach ($appointments as &$appointment) foreach ($appointments as &$appointment)
{ {
$appointment = $this->timezones->convert_event_timezone($appointment);
if ($aggregates) if ($aggregates)
{ {
$appointment = $this->get_aggregates($appointment); $appointment = $this->get_aggregates($appointment);