Updated the Notifications.php library to work with the updated code.

This commit is contained in:
Alex Tselegidis 2021-10-28 14:00:33 +02:00
parent 23d80259d9
commit 44aea4febb

View file

@ -17,9 +17,11 @@ use EA\Engine\Types\Text;
use EA\Engine\Types\Url; 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 { class Notifications {
/** /**
@ -34,30 +36,28 @@ class Notifications {
{ {
$this->CI =& get_instance(); $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('providers_model');
$this->CI->load->model('secretaries_model'); $this->CI->load->model('secretaries_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->model('settings_model');
$this->CI->load->library('ics_file'); $this->CI->load->library('ics_file');
$this->CI->load->library('timezones'); $this->CI->load->library('timezones');
$this->CI->config->load('email');
} }
/** /**
* Send the required notifications, related to an appointment creation/modification. * Send the required notifications, related to an appointment creation/modification.
* *
* @param array $appointment Appointment record. * @param array $appointment Appointment data.
* @param array $service Service record. * @param array $service Service data.
* @param array $provider Provider record. * @param array $provider Provider data.
* @param array $customer Customer record. * @param array $customer Customer data.
* @param array $settings Required settings for the notification content. * @param array $settings Required settings.
* @param bool|false $manage_mode * @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 try
{ {
@ -83,30 +83,54 @@ class Notifications {
$ics_stream = $this->CI->ics_file->get_stream($appointment, $service, $provider, $customer); $ics_stream = $this->CI->ics_file->get_stream($appointment, $service, $provider, $customer);
// Notify customer.
$send_customer = filter_var( $send_customer = filter_var(
$this->CI->settings_model->get_setting('customer_notifications'), setting('customer_notifications'),
FILTER_VALIDATE_BOOLEAN); FILTER_VALIDATE_BOOLEAN
);
if ($send_customer === TRUE) if ($send_customer === TRUE)
{ {
$email->send_appointment_details($appointment, $provider, $email->send_appointment_details(
$service, $customer, $settings, $customer_title, $appointment,
$customer_message, $customer_link, new Email($customer['email']), new Text($ics_stream), $customer['timezone']); $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( $send_provider = filter_var(
$this->CI->providers_model->get_setting('notifications', $provider['id']), $this->CI->providers_model->get_setting($provider['id'], 'notifications'),
FILTER_VALIDATE_BOOLEAN); FILTER_VALIDATE_BOOLEAN
);
if ($send_provider === TRUE) if ($send_provider === TRUE)
{ {
$email->send_appointment_details($appointment, $provider, $email->send_appointment_details(
$service, $customer, $settings, $provider_title, $appointment,
$provider_message, $provider_link, new Email($provider['email']), new Text($ics_stream), $provider['timezone']); $provider,
$service,
$customer,
$settings,
$provider_title,
$provider_message,
$provider_link,
new Email($provider['email']),
new Text($ics_stream),
$provider['timezone']
);
} }
// Notify admins // Notify admins.
$admins = $this->CI->admins_model->get_batch(); $admins = $this->CI->admins_model->get();
foreach ($admins as $admin) foreach ($admins as $admin)
{ {
@ -115,13 +139,23 @@ class Notifications {
continue; continue;
} }
$email->send_appointment_details($appointment, $provider, $email->send_appointment_details(
$service, $customer, $settings, $provider_title, $appointment,
$provider_message, $provider_link, new Email($admin['email']), new Text($ics_stream), $admin['timezone']); $provider,
$service,
$customer,
$settings,
$provider_title,
$provider_message,
$provider_link,
new Email($admin['email']),
new Text($ics_stream),
$admin['timezone']
);
} }
// Notify secretaries // Notify secretaries.
$secretaries = $this->CI->secretaries_model->get_batch(); $secretaries = $this->CI->secretaries_model->get();
foreach ($secretaries as $secretary) foreach ($secretaries as $secretary)
{ {
@ -135,64 +169,88 @@ class Notifications {
continue; continue;
} }
$email->send_appointment_details($appointment, $provider, $email->send_appointment_details(
$service, $customer, $settings, $provider_title, $appointment,
$provider_message, $provider_link, new Email($secretary['email']), new Text($ics_stream), $secretary['timezone']); $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', $e->getMessage());
log_message('error', $exception->getTraceAsString()); log_message('error', $e->getTraceAsString());
} }
} }
/** /**
* Send the required notifications, related to an appointment removal. * Send the required notifications, related to an appointment removal.
* *
* @param array $appointment Appointment record. * @param array $appointment Appointment data.
* @param array $service Service record. * @param array $service Service data.
* @param array $provider Provider record. * @param array $provider Provider data.
* @param array $customer Customer record. * @param array $customer Customer data.
* @param array $settings Required settings for the notification content. * @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 try
{ {
$email = new EmailClient($this->CI, $this->CI->config->config); $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']), $send_provider = filter_var($this->CI->providers_model->get_setting('notifications', $provider['id']),
FILTER_VALIDATE_BOOLEAN); FILTER_VALIDATE_BOOLEAN);
if ($send_provider === TRUE) if ($send_provider === TRUE)
{ {
$email->send_delete_appointment($appointment, $provider, $email->send_delete_appointment(
$service, $customer, $settings, new Email($provider['email']), $appointment,
new Text($delete_reason)); $provider,
$service,
$customer,
$settings,
new Email($provider['email']),
new Text($delete_reason)
);
} }
// Notify customer.
$send_customer = filter_var( $send_customer = filter_var(
$this->CI->settings_model->get_setting('customer_notifications'), setting('customer_notifications'),
FILTER_VALIDATE_BOOLEAN); FILTER_VALIDATE_BOOLEAN
);
if ($send_customer === TRUE) if ($send_customer === TRUE)
{ {
$email->send_delete_appointment($appointment, $provider, $email->send_delete_appointment(
$service, $customer, $settings, new Email($customer['email']), $appointment,
new Text($delete_reason)); $provider,
$service,
$customer,
$settings,
new Email($customer['email']),
new Text($delete_reason)
);
} }
// Notify admins // Notify admins.
$admins = $this->CI->admins_model->get_batch(); $admins = $this->CI->admins_model->get();
foreach ($admins as $admin) foreach ($admins as $admin)
{ {
@ -201,13 +259,19 @@ class Notifications {
continue; continue;
} }
$email->send_delete_appointment($appointment, $provider, $email->send_delete_appointment(
$service, $customer, $settings, new Email($admin['email']), $appointment,
new Text($this->CI->input->post('cancel_reason'))); $provider,
$service,
$customer,
$settings,
new Email($admin['email']),
new Text($delete_reason)
);
} }
// Notify secretaries // Notify secretaries.
$secretaries = $this->CI->secretaries_model->get_batch(); $secretaries = $this->CI->secretaries_model->get();
foreach ($secretaries as $secretary) foreach ($secretaries as $secretary)
{ {
@ -221,14 +285,21 @@ class Notifications {
continue; continue;
} }
$email->send_delete_appointment($appointment, $provider, $email->send_delete_appointment(
$service, $customer, $settings, new Email($secretary['email']), $appointment,
new Text($this->CI->input->post('cancel_reason'))); $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());
} }
} }
} }