Minor refactoring for the appointments page

This commit is contained in:
alextselegidis 2021-11-12 17:09:31 +01:00
parent eb64b6c7ef
commit d2be2327e8
1 changed files with 112 additions and 126 deletions

View File

@ -53,149 +53,135 @@ class Appointments extends EA_Controller {
*/ */
public function index(string $appointment_hash = '') public function index(string $appointment_hash = '')
{ {
try if ( ! is_app_installed())
{ {
if ( ! is_app_installed()) redirect('installation');
return;
}
$available_services = $this->services_model->get_available_services();
$available_providers = $this->providers_model->get_available_providers();
$company_name = setting('company_name');
$book_advance_timeout = setting('book_advance_timeout');
$date_format = setting('date_format');
$time_format = setting('time_format');
$first_weekday = setting('first_weekday');
$require_phone_number = setting('require_phone_number');
$show_field['phone-number'] = setting('show_phone_number');
$show_field['address'] = setting('show_address');
$show_field['city'] = setting('show_city');
$show_field['zip-code'] = setting('show_zip_code');
$show_field['notes'] = setting('show_notes');
$display_cookie_notice = setting('display_cookie_notice');
$cookie_notice_content = setting('cookie_notice_content');
$display_terms_and_conditions = setting('display_terms_and_conditions');
$terms_and_conditions_content = setting('terms_and_conditions_content');
$display_privacy_policy = setting('display_privacy_policy');
$privacy_policy_content = setting('privacy_policy_content');
$display_any_provider = setting('display_any_provider');
$timezones = $this->timezones->to_array();
// Remove the data that are not needed inside the $available_providers array.
foreach ($available_providers as &$available_provider)
{
$this->providers_model->only($available_provider, [
'id',
'first_name',
'last_name',
'services',
'timezone'
]);
}
// If an appointment hash is provided then it means that the customer is trying to edit a registered appointment
// record.
if ($appointment_hash !== '')
{
// Load the appointments data and enable the manage mode of the page.
$manage_mode = TRUE;
$results = $this->appointments_model->get(['hash' => $appointment_hash]);
if (empty($results))
{ {
redirect('installation'); // The requested appointment was not found in the database.
$this->load->layout('layouts/message/message_layout', 'pages/booking/booking_message_page', [
'message_title' => lang('appointment_not_found'),
'message_text' => lang('appointment_does_not_exist_in_db'),
'message_icon' => base_url('assets/img/error.png')
]);
return; return;
} }
$available_services = $this->services_model->get_available_services(); // If the requested appointment begin date is lower than book_advance_timeout. Display a message to the
$available_providers = $this->providers_model->get_available_providers(); // customer.
$company_name = setting('company_name'); $start_datetime = strtotime($results[0]['start_datetime']);
$book_advance_timeout = setting('book_advance_timeout');
$date_format = setting('date_format');
$time_format = setting('time_format');
$first_weekday = setting('first_weekday');
$require_phone_number = setting('require_phone_number');
$show_field['phone-number'] = setting('show_phone_number');
$show_field['address'] = setting('show_address');
$show_field['city'] = setting('show_city');
$show_field['zip-code'] = setting('show_zip_code');
$show_field['notes'] = setting('show_notes');
$display_cookie_notice = setting('display_cookie_notice');
$cookie_notice_content = setting('cookie_notice_content');
$display_terms_and_conditions = setting('display_terms_and_conditions');
$terms_and_conditions_content = setting('terms_and_conditions_content');
$display_privacy_policy = setting('display_privacy_policy');
$privacy_policy_content = setting('privacy_policy_content');
$display_any_provider = setting('display_any_provider');
$timezones = $this->timezones->to_array();
// Remove the data that are not needed inside the $available_providers array. $limit = strtotime('+' . $book_advance_timeout . ' minutes', strtotime('now'));
foreach ($available_providers as $index => $provider)
if ($start_datetime < $limit)
{ {
$stripped_data = [ $hours = floor($book_advance_timeout / 60);
'id' => $provider['id'], $minutes = ($book_advance_timeout % 60);
'first_name' => $provider['first_name'],
'last_name' => $provider['last_name'], $this->load->layout('layouts/message/message_layout', 'pages/booking/booking_message_page', [
'services' => $provider['services'], 'message_title' => lang('appointment_locked'),
'timezone' => $provider['timezone'] 'message_text' => strtr(lang('appointment_locked_message'), [
]; '{$limit}' => sprintf('%02d:%02d', $hours, $minutes)
$available_providers[$index] = $stripped_data; ]),
'message_icon' => base_url('assets/img/error.png')
]);
return;
} }
// If an appointment hash is provided then it means that the customer is trying to edit a registered $appointment = $results[0];
// appointment record.
if ($appointment_hash !== '')
{
// Load the appointments data and enable the manage mode of the page.
$manage_mode = TRUE;
$results = $this->appointments_model->get(['hash' => $appointment_hash]); $provider = $this->providers_model->find($appointment['id_users_provider']);
if (empty($results)) $customer = $this->customers_model->find($appointment['id_users_customer']);
{
// The requested appointment was not found in the database.
$view = [
'message_title' => lang('appointment_not_found'),
'message_text' => lang('appointment_does_not_exist_in_db'),
'message_icon' => base_url('assets/img/error.png')
];
$this->load->layout('layouts/message/message_layout', 'pages/booking/booking_message_page', $view); $customer_token = md5(uniqid(mt_rand(), TRUE));
return; // Cache the token for 10 minutes.
} $this->cache->save('customer-token-' . $customer_token, $customer['id'], 600);
// If the requested appointment begin date is lower than book_advance_timeout. Display a message to the
// customer.
$start_datetime = strtotime($results[0]['start_datetime']);
$limit = strtotime('+' . $book_advance_timeout . ' minutes', strtotime('now'));
if ($start_datetime < $limit)
{
$hours = floor($book_advance_timeout / 60);
$minutes = ($book_advance_timeout % 60);
$view = [
'message_title' => lang('appointment_locked'),
'message_text' => strtr(lang('appointment_locked_message'), [
'{$limit}' => sprintf('%02d:%02d', $hours, $minutes)
]),
'message_icon' => base_url('assets/img/error.png')
];
$this->load->layout('layouts/message/message_layout', 'pages/booking/booking_message_page', $view);
return;
}
$appointment = $results[0];
$provider = $this->providers_model->find($appointment['id_users_provider']);
$customer = $this->customers_model->find($appointment['id_users_customer']);
$customer_token = md5(uniqid(mt_rand(), TRUE));
// Save the token for 10 minutes.
$this->cache->save('customer-token-' . $customer_token, $customer['id'], 600);
}
else
{
// The customer is going to book a new appointment so there is no need for the manage functionality to
// be initialized.
$manage_mode = FALSE;
$customer_token = FALSE;
$appointment = [];
$provider = [];
$customer = [];
}
// Load the book appointment view.
$view = [
'available_services' => $available_services,
'available_providers' => $available_providers,
'company_name' => $company_name,
'manage_mode' => $manage_mode,
'customer_token' => $customer_token,
'date_format' => $date_format,
'time_format' => $time_format,
'first_weekday' => $first_weekday,
'require_phone_number' => $require_phone_number,
'show_field' => $show_field,
'appointment_data' => $appointment,
'provider_data' => $provider,
'customer_data' => $customer,
'display_cookie_notice' => $display_cookie_notice,
'cookie_notice_content' => $cookie_notice_content,
'display_terms_and_conditions' => $display_terms_and_conditions,
'terms_and_conditions_content' => $terms_and_conditions_content,
'display_privacy_policy' => $display_privacy_policy,
'privacy_policy_content' => $privacy_policy_content,
'timezones' => $timezones,
'display_any_provider' => $display_any_provider,
];
} }
catch (Throwable $e) else
{ {
$view['exceptions'][] = $e; // The customer is going to book a new appointment so there is no need for the manage functionality to
// be initialized.
$manage_mode = FALSE;
$customer_token = FALSE;
$appointment = [];
$provider = [];
$customer = [];
} }
$this->load->layout('layouts/booking/booking_layout', 'pages/booking/booking_page', $view); $this->load->layout('layouts/booking/booking_layout', 'pages/booking/booking_page', [
'available_services' => $available_services,
'available_providers' => $available_providers,
'company_name' => $company_name,
'manage_mode' => $manage_mode,
'customer_token' => $customer_token,
'date_format' => $date_format,
'time_format' => $time_format,
'first_weekday' => $first_weekday,
'require_phone_number' => $require_phone_number,
'show_field' => $show_field,
'appointment_data' => $appointment,
'provider_data' => $provider,
'customer_data' => $customer,
'display_cookie_notice' => $display_cookie_notice,
'cookie_notice_content' => $cookie_notice_content,
'display_terms_and_conditions' => $display_terms_and_conditions,
'terms_and_conditions_content' => $terms_and_conditions_content,
'display_privacy_policy' => $display_privacy_policy,
'privacy_policy_content' => $privacy_policy_content,
'timezones' => $timezones,
'display_any_provider' => $display_any_provider,
]);
} }
/** /**