diff --git a/application/libraries/Notifications.php b/application/libraries/Notifications.php index a691fca1..5933519a 100644 --- a/application/libraries/Notifications.php +++ b/application/libraries/Notifications.php @@ -17,9 +17,11 @@ use EA\Engine\Types\Text; use EA\Engine\Types\Url; /** - * Class Notifications + * Notifications library * - * Handles the system notifications (mostly related to scheduling changes). + * Handles the notifications related functionality. + * + * @package Libraries */ class Notifications { /** @@ -34,30 +36,28 @@ class Notifications { { $this->CI =& get_instance(); + $this->CI->load->model('admins_model'); + $this->CI->load->model('appointments_model'); $this->CI->load->model('providers_model'); $this->CI->load->model('secretaries_model'); $this->CI->load->model('secretaries_model'); - $this->CI->load->model('admins_model'); - $this->CI->load->model('appointments_model'); $this->CI->load->model('settings_model'); $this->CI->load->library('ics_file'); $this->CI->load->library('timezones'); - - $this->CI->config->load('email'); } /** * Send the required notifications, related to an appointment creation/modification. * - * @param array $appointment Appointment record. - * @param array $service Service record. - * @param array $provider Provider record. - * @param array $customer Customer record. - * @param array $settings Required settings for the notification content. - * @param bool|false $manage_mode + * @param array $appointment Appointment data. + * @param array $service Service data. + * @param array $provider Provider data. + * @param array $customer Customer data. + * @param array $settings Required settings. + * @param bool|false $manage_mode Manage mode. */ - public function notify_appointment_saved($appointment, $service, $provider, $customer, $settings, $manage_mode = FALSE) + public function notify_appointment_saved(array $appointment, array $service, array $provider, array $customer, array $settings, bool $manage_mode = FALSE) { try { @@ -83,30 +83,54 @@ class Notifications { $ics_stream = $this->CI->ics_file->get_stream($appointment, $service, $provider, $customer); + // Notify customer. $send_customer = filter_var( - $this->CI->settings_model->get_setting('customer_notifications'), - FILTER_VALIDATE_BOOLEAN); + setting('customer_notifications'), + FILTER_VALIDATE_BOOLEAN + ); if ($send_customer === TRUE) { - $email->send_appointment_details($appointment, $provider, - $service, $customer, $settings, $customer_title, - $customer_message, $customer_link, new Email($customer['email']), new Text($ics_stream), $customer['timezone']); + $email->send_appointment_details( + $appointment, + $provider, + $service, + $customer, + $settings, + $customer_title, + $customer_message, + $customer_link, + new Email($customer['email']), + new Text($ics_stream), + $customer['timezone'] + ); } + // Notify provider. $send_provider = filter_var( - $this->CI->providers_model->get_setting('notifications', $provider['id']), - FILTER_VALIDATE_BOOLEAN); + $this->CI->providers_model->get_setting($provider['id'], 'notifications'), + FILTER_VALIDATE_BOOLEAN + ); if ($send_provider === TRUE) { - $email->send_appointment_details($appointment, $provider, - $service, $customer, $settings, $provider_title, - $provider_message, $provider_link, new Email($provider['email']), new Text($ics_stream), $provider['timezone']); + $email->send_appointment_details( + $appointment, + $provider, + $service, + $customer, + $settings, + $provider_title, + $provider_message, + $provider_link, + new Email($provider['email']), + new Text($ics_stream), + $provider['timezone'] + ); } - // Notify admins - $admins = $this->CI->admins_model->get_batch(); + // Notify admins. + $admins = $this->CI->admins_model->get(); foreach ($admins as $admin) { @@ -115,13 +139,23 @@ class Notifications { continue; } - $email->send_appointment_details($appointment, $provider, - $service, $customer, $settings, $provider_title, - $provider_message, $provider_link, new Email($admin['email']), new Text($ics_stream), $admin['timezone']); + $email->send_appointment_details( + $appointment, + $provider, + $service, + $customer, + $settings, + $provider_title, + $provider_message, + $provider_link, + new Email($admin['email']), + new Text($ics_stream), + $admin['timezone'] + ); } - // Notify secretaries - $secretaries = $this->CI->secretaries_model->get_batch(); + // Notify secretaries. + $secretaries = $this->CI->secretaries_model->get(); foreach ($secretaries as $secretary) { @@ -130,69 +164,93 @@ class Notifications { continue; } - if (!in_array($provider['id'], $secretary['providers'])) + if ( ! in_array($provider['id'], $secretary['providers'])) { continue; } - $email->send_appointment_details($appointment, $provider, - $service, $customer, $settings, $provider_title, - $provider_message, $provider_link, new Email($secretary['email']), new Text($ics_stream), $secretary['timezone']); + $email->send_appointment_details( + $appointment, + $provider, + $service, + $customer, + $settings, + $provider_title, + $provider_message, + $provider_link, + new Email($secretary['email']), + new Text($ics_stream), + $secretary['timezone'] + ); } } - catch (Exception $exception) + catch (Throwable $e) { - log_message('error', $exception->getMessage()); - log_message('error', $exception->getTraceAsString()); + log_message('error', $e->getMessage()); + log_message('error', $e->getTraceAsString()); } } /** * Send the required notifications, related to an appointment removal. * - * @param array $appointment Appointment record. - * @param array $service Service record. - * @param array $provider Provider record. - * @param array $customer Customer record. - * @param array $settings Required settings for the notification content. + * @param array $appointment Appointment data. + * @param array $service Service data. + * @param array $provider Provider data. + * @param array $customer Customer data. + * @param array $settings Required settings. */ - public function notify_appointment_deleted($appointment, $service, $provider, $customer, $settings) + public function notify_appointment_deleted(array $appointment, array $service, array $provider, array $customer, array $settings) { - // Send email notification to customer and provider. try { $email = new EmailClient($this->CI, $this->CI->config->config); - $delete_reason = (string)$this->CI->input->post('delete_reason'); + $delete_reason = (string)request('delete_reason'); - if ( ! $delete_reason) + if (empty($delete_reason)) { - $delete_reason = (string)$this->CI->input->post('cancel_reason'); + $delete_reason = (string)request('cancel_reason'); } + // Notify provider. $send_provider = filter_var($this->CI->providers_model->get_setting('notifications', $provider['id']), FILTER_VALIDATE_BOOLEAN); if ($send_provider === TRUE) { - $email->send_delete_appointment($appointment, $provider, - $service, $customer, $settings, new Email($provider['email']), - new Text($delete_reason)); + $email->send_delete_appointment( + $appointment, + $provider, + $service, + $customer, + $settings, + new Email($provider['email']), + new Text($delete_reason) + ); } + // Notify customer. $send_customer = filter_var( - $this->CI->settings_model->get_setting('customer_notifications'), - FILTER_VALIDATE_BOOLEAN); + setting('customer_notifications'), + FILTER_VALIDATE_BOOLEAN + ); if ($send_customer === TRUE) { - $email->send_delete_appointment($appointment, $provider, - $service, $customer, $settings, new Email($customer['email']), - new Text($delete_reason)); + $email->send_delete_appointment( + $appointment, + $provider, + $service, + $customer, + $settings, + new Email($customer['email']), + new Text($delete_reason) + ); } - // Notify admins - $admins = $this->CI->admins_model->get_batch(); + // Notify admins. + $admins = $this->CI->admins_model->get(); foreach ($admins as $admin) { @@ -201,13 +259,19 @@ class Notifications { continue; } - $email->send_delete_appointment($appointment, $provider, - $service, $customer, $settings, new Email($admin['email']), - new Text($this->CI->input->post('cancel_reason'))); + $email->send_delete_appointment( + $appointment, + $provider, + $service, + $customer, + $settings, + new Email($admin['email']), + new Text($delete_reason) + ); } - // Notify secretaries - $secretaries = $this->CI->secretaries_model->get_batch(); + // Notify secretaries. + $secretaries = $this->CI->secretaries_model->get(); foreach ($secretaries as $secretary) { @@ -216,19 +280,26 @@ class Notifications { continue; } - if (!in_array($provider['id'], $secretary['providers'])) + if ( ! in_array($provider['id'], $secretary['providers'])) { continue; } - $email->send_delete_appointment($appointment, $provider, - $service, $customer, $settings, new Email($secretary['email']), - new Text($this->CI->input->post('cancel_reason'))); + $email->send_delete_appointment( + $appointment, + $provider, + $service, + $customer, + $settings, + new Email($secretary['email']), + new Text($delete_reason) + ); } } - catch (Exception $exception) + catch (Throwable $e) { - $exceptions[] = $exception; + log_message('error', $e->getMessage()); + log_message('error', $e->getTraceAsString()); } } }