2020-10-21 21:37:47 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2020-10-21 21:37:47 +03:00
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2020-10-21 21:37:47 +03:00
|
|
|
* @since v1.4.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
2021-11-06 18:21:27 +03:00
|
|
|
* Notifications library.
|
2020-10-21 21:37:47 +03:00
|
|
|
*
|
2021-10-28 15:00:33 +03:00
|
|
|
* Handles the notifications related functionality.
|
2021-11-06 18:11:55 +03:00
|
|
|
*
|
2021-10-28 15:00:33 +03:00
|
|
|
* @package Libraries
|
2020-10-21 21:37:47 +03:00
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Notifications
|
|
|
|
{
|
2020-10-21 21:37:47 +03:00
|
|
|
/**
|
2023-03-13 11:06:18 +03:00
|
|
|
* @var EA_Controller|CI_Controller
|
2020-10-21 21:37:47 +03:00
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
protected EA_Controller|CI_Controller $CI;
|
2020-10-21 21:37:47 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifications constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
$this->CI = &get_instance();
|
2020-11-16 12:47:10 +03:00
|
|
|
|
2021-10-28 15:00:33 +03:00
|
|
|
$this->CI->load->model('admins_model');
|
|
|
|
$this->CI->load->model('appointments_model');
|
2020-10-21 21:37:47 +03:00
|
|
|
$this->CI->load->model('providers_model');
|
|
|
|
$this->CI->load->model('secretaries_model');
|
|
|
|
$this->CI->load->model('secretaries_model');
|
|
|
|
$this->CI->load->model('settings_model');
|
2020-11-16 12:47:10 +03:00
|
|
|
|
2021-11-06 18:11:55 +03:00
|
|
|
$this->CI->load->library('email_messages');
|
2020-10-21 21:37:47 +03:00
|
|
|
$this->CI->load->library('ics_file');
|
2020-11-16 12:47:10 +03:00
|
|
|
$this->CI->load->library('timezones');
|
2020-10-21 21:37:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the required notifications, related to an appointment creation/modification.
|
|
|
|
*
|
2021-10-28 15:00:33 +03:00
|
|
|
* @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.
|
2020-10-21 21:37:47 +03:00
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public function notify_appointment_saved(
|
|
|
|
array $appointment,
|
|
|
|
array $service,
|
|
|
|
array $provider,
|
|
|
|
array $customer,
|
|
|
|
array $settings,
|
2023-12-22 13:35:41 +03:00
|
|
|
bool $manage_mode = false,
|
2023-11-29 12:24:09 +03:00
|
|
|
): void {
|
|
|
|
try {
|
2023-12-12 14:14:01 +03:00
|
|
|
$current_language = config('english');
|
2020-10-21 21:37:47 +03:00
|
|
|
|
2021-12-17 11:19:31 +03:00
|
|
|
$customer_link = site_url('booking/reschedule/' . $appointment['hash']);
|
2021-11-06 18:11:55 +03:00
|
|
|
|
2021-12-17 11:19:31 +03:00
|
|
|
$provider_link = site_url('calendar/reschedule/' . $appointment['hash']);
|
2020-10-21 21:37:47 +03:00
|
|
|
|
2020-11-16 12:47:10 +03:00
|
|
|
$ics_stream = $this->CI->ics_file->get_stream($appointment, $service, $provider, $customer);
|
|
|
|
|
2021-10-28 15:00:33 +03:00
|
|
|
// Notify customer.
|
2023-11-29 12:24:09 +03:00
|
|
|
$send_customer =
|
|
|
|
!empty($customer['email']) && filter_var(setting('customer_notifications'), FILTER_VALIDATE_BOOLEAN);
|
2020-10-21 21:37:47 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($send_customer === true) {
|
2023-12-12 14:14:01 +03:00
|
|
|
config(['language' => $customer['language']]);
|
|
|
|
$this->CI->lang->load('translations');
|
|
|
|
$subject = $manage_mode ? lang('appointment_details_changed') : lang('appointment_booked');
|
|
|
|
$message = $manage_mode ? '' : lang('thank_you_for_appointment');
|
|
|
|
|
2023-05-04 19:02:40 +03:00
|
|
|
$this->CI->email_messages->send_appointment_saved(
|
2021-10-28 15:00:33 +03:00
|
|
|
$appointment,
|
|
|
|
$provider,
|
|
|
|
$service,
|
|
|
|
$customer,
|
|
|
|
$settings,
|
2023-12-12 14:14:01 +03:00
|
|
|
$subject,
|
|
|
|
$message,
|
2021-10-28 15:00:33 +03:00
|
|
|
$customer_link,
|
2021-11-06 18:11:55 +03:00
|
|
|
$customer['email'],
|
|
|
|
$ics_stream,
|
2023-12-22 13:35:41 +03:00
|
|
|
$customer['timezone'],
|
2021-10-28 15:00:33 +03:00
|
|
|
);
|
2020-10-21 21:37:47 +03:00
|
|
|
}
|
|
|
|
|
2021-10-28 15:00:33 +03:00
|
|
|
// Notify provider.
|
2020-10-21 21:37:47 +03:00
|
|
|
$send_provider = filter_var(
|
2021-10-28 15:00:33 +03:00
|
|
|
$this->CI->providers_model->get_setting($provider['id'], 'notifications'),
|
2023-12-22 13:35:41 +03:00
|
|
|
FILTER_VALIDATE_BOOLEAN,
|
2021-10-28 15:00:33 +03:00
|
|
|
);
|
2020-10-21 21:37:47 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($send_provider === true) {
|
2023-12-12 14:14:01 +03:00
|
|
|
config(['language' => $provider['language']]);
|
|
|
|
$this->CI->lang->load('translations');
|
|
|
|
$subject = $manage_mode ? lang('appointment_details_changed') : lang('appointment_added_to_your_plan');
|
|
|
|
$message = $manage_mode ? '' : lang('appointment_link_description');
|
|
|
|
|
2023-05-04 19:02:40 +03:00
|
|
|
$this->CI->email_messages->send_appointment_saved(
|
2021-10-28 15:00:33 +03:00
|
|
|
$appointment,
|
|
|
|
$provider,
|
|
|
|
$service,
|
|
|
|
$customer,
|
|
|
|
$settings,
|
2023-12-12 14:14:01 +03:00
|
|
|
$subject,
|
|
|
|
$message,
|
2021-10-28 15:00:33 +03:00
|
|
|
$provider_link,
|
2021-11-06 18:11:55 +03:00
|
|
|
$provider['email'],
|
|
|
|
$ics_stream,
|
2023-12-22 13:35:41 +03:00
|
|
|
$provider['timezone'],
|
2021-10-28 15:00:33 +03:00
|
|
|
);
|
2020-10-21 21:37:47 +03:00
|
|
|
}
|
|
|
|
|
2021-10-28 15:00:33 +03:00
|
|
|
// Notify admins.
|
|
|
|
$admins = $this->CI->admins_model->get();
|
2020-10-21 21:37:47 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($admins as $admin) {
|
|
|
|
if ($admin['settings']['notifications'] === '0') {
|
2020-10-21 21:37:47 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-12-12 14:14:01 +03:00
|
|
|
config(['language' => $admin['language']]);
|
|
|
|
$this->CI->lang->load('translations');
|
|
|
|
$subject = $manage_mode ? lang('appointment_details_changed') : lang('appointment_added_to_your_plan');
|
|
|
|
$message = $manage_mode ? '' : lang('appointment_link_description');
|
|
|
|
|
2023-05-04 19:02:40 +03:00
|
|
|
$this->CI->email_messages->send_appointment_saved(
|
2021-10-28 15:00:33 +03:00
|
|
|
$appointment,
|
|
|
|
$provider,
|
|
|
|
$service,
|
|
|
|
$customer,
|
|
|
|
$settings,
|
2023-12-12 14:14:01 +03:00
|
|
|
$subject,
|
|
|
|
$message,
|
2021-10-28 15:00:33 +03:00
|
|
|
$provider_link,
|
2021-11-06 18:11:55 +03:00
|
|
|
$admin['email'],
|
|
|
|
$ics_stream,
|
2023-12-22 13:35:41 +03:00
|
|
|
$admin['timezone'],
|
2021-10-28 15:00:33 +03:00
|
|
|
);
|
2020-10-21 21:37:47 +03:00
|
|
|
}
|
|
|
|
|
2021-10-28 15:00:33 +03:00
|
|
|
// Notify secretaries.
|
|
|
|
$secretaries = $this->CI->secretaries_model->get();
|
2020-10-21 21:37:47 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($secretaries as $secretary) {
|
|
|
|
if ($secretary['settings']['notifications'] === '0') {
|
2020-10-21 21:37:47 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!in_array($provider['id'], $secretary['providers'])) {
|
2020-10-21 21:37:47 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-12-12 14:14:01 +03:00
|
|
|
config(['language' => $secretary['language']]);
|
|
|
|
$this->CI->lang->load('translations');
|
|
|
|
$subject = $manage_mode ? lang('appointment_details_changed') : lang('appointment_added_to_your_plan');
|
|
|
|
$message = $manage_mode ? '' : lang('appointment_link_description');
|
|
|
|
|
2023-05-04 19:02:40 +03:00
|
|
|
$this->CI->email_messages->send_appointment_saved(
|
2021-10-28 15:00:33 +03:00
|
|
|
$appointment,
|
|
|
|
$provider,
|
|
|
|
$service,
|
|
|
|
$customer,
|
|
|
|
$settings,
|
2023-12-12 14:14:01 +03:00
|
|
|
$subject,
|
|
|
|
$message,
|
2021-10-28 15:00:33 +03:00
|
|
|
$provider_link,
|
2021-11-06 18:11:55 +03:00
|
|
|
$secretary['email'],
|
|
|
|
$ics_stream,
|
2023-12-22 13:35:41 +03:00
|
|
|
$secretary['timezone'],
|
2021-10-28 15:00:33 +03:00
|
|
|
);
|
2020-10-21 21:37:47 +03:00
|
|
|
}
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
log_message(
|
|
|
|
'error',
|
|
|
|
'Notifications - Could not email confirmation details of appointment (' .
|
|
|
|
($appointment['id'] ?? '-') .
|
|
|
|
') : ' .
|
2023-12-22 13:35:41 +03:00
|
|
|
$e->getMessage(),
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2021-10-28 15:00:33 +03:00
|
|
|
log_message('error', $e->getTraceAsString());
|
2023-12-12 14:14:01 +03:00
|
|
|
} finally {
|
|
|
|
config(['language' => $current_language ?? 'english']);
|
|
|
|
$this->CI->lang->load('translations');
|
2020-10-21 21:37:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the required notifications, related to an appointment removal.
|
|
|
|
*
|
2021-10-28 15:00:33 +03:00
|
|
|
* @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.
|
2020-10-21 21:37:47 +03:00
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public function notify_appointment_deleted(
|
|
|
|
array $appointment,
|
|
|
|
array $service,
|
|
|
|
array $provider,
|
|
|
|
array $customer,
|
|
|
|
array $settings,
|
2023-12-22 13:35:41 +03:00
|
|
|
string $cancellation_reason = '',
|
2023-11-29 12:24:09 +03:00
|
|
|
): void {
|
|
|
|
try {
|
2023-12-12 14:14:01 +03:00
|
|
|
$current_language = config('language');
|
|
|
|
|
2021-10-28 15:00:33 +03:00
|
|
|
// Notify provider.
|
2021-11-06 18:11:55 +03:00
|
|
|
$send_provider = filter_var(
|
|
|
|
$this->CI->providers_model->get_setting($provider['id'], 'notifications'),
|
2023-12-22 13:35:41 +03:00
|
|
|
FILTER_VALIDATE_BOOLEAN,
|
2021-11-06 18:11:55 +03:00
|
|
|
);
|
2020-10-21 21:37:47 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($send_provider === true) {
|
2023-12-12 14:14:01 +03:00
|
|
|
config(['language' => $provider['language']]);
|
|
|
|
$this->CI->lang->load('translations');
|
|
|
|
|
2023-05-04 19:02:40 +03:00
|
|
|
$this->CI->email_messages->send_appointment_deleted(
|
2021-10-28 15:00:33 +03:00
|
|
|
$appointment,
|
|
|
|
$provider,
|
|
|
|
$service,
|
|
|
|
$customer,
|
|
|
|
$settings,
|
2021-11-06 18:11:55 +03:00
|
|
|
$provider['email'],
|
2023-06-01 15:25:02 +03:00
|
|
|
$cancellation_reason,
|
2023-12-22 13:35:41 +03:00
|
|
|
$provider['timezone'],
|
2021-10-28 15:00:33 +03:00
|
|
|
);
|
2020-10-21 21:37:47 +03:00
|
|
|
}
|
|
|
|
|
2021-10-28 15:00:33 +03:00
|
|
|
// Notify customer.
|
2023-11-29 12:24:09 +03:00
|
|
|
$send_customer =
|
|
|
|
!empty($customer['email']) && filter_var(setting('customer_notifications'), FILTER_VALIDATE_BOOLEAN);
|
2020-10-21 21:37:47 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($send_customer === true) {
|
2023-12-12 14:14:01 +03:00
|
|
|
config(['language' => $customer['language']]);
|
|
|
|
$this->CI->lang->load('translations');
|
|
|
|
|
2023-05-04 19:02:40 +03:00
|
|
|
$this->CI->email_messages->send_appointment_deleted(
|
2021-10-28 15:00:33 +03:00
|
|
|
$appointment,
|
|
|
|
$provider,
|
|
|
|
$service,
|
|
|
|
$customer,
|
|
|
|
$settings,
|
2021-11-06 18:11:55 +03:00
|
|
|
$customer['email'],
|
2023-06-01 15:25:02 +03:00
|
|
|
$cancellation_reason,
|
2023-12-22 13:35:41 +03:00
|
|
|
$customer['timezone'],
|
2021-10-28 15:00:33 +03:00
|
|
|
);
|
2020-10-21 21:37:47 +03:00
|
|
|
}
|
|
|
|
|
2021-10-28 15:00:33 +03:00
|
|
|
// Notify admins.
|
|
|
|
$admins = $this->CI->admins_model->get();
|
2020-10-21 21:37:47 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($admins as $admin) {
|
|
|
|
if ($admin['settings']['notifications'] === '0') {
|
2020-10-21 21:37:47 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-12-12 14:14:01 +03:00
|
|
|
config(['language' => $admin['language']]);
|
|
|
|
$this->CI->lang->load('translations');
|
|
|
|
|
2023-05-04 19:02:40 +03:00
|
|
|
$this->CI->email_messages->send_appointment_deleted(
|
2021-10-28 15:00:33 +03:00
|
|
|
$appointment,
|
|
|
|
$provider,
|
|
|
|
$service,
|
|
|
|
$customer,
|
|
|
|
$settings,
|
2021-11-06 18:11:55 +03:00
|
|
|
$admin['email'],
|
2023-06-01 15:25:02 +03:00
|
|
|
$cancellation_reason,
|
2023-12-22 13:35:41 +03:00
|
|
|
$admin['timezone'],
|
2021-10-28 15:00:33 +03:00
|
|
|
);
|
2020-10-21 21:37:47 +03:00
|
|
|
}
|
|
|
|
|
2021-10-28 15:00:33 +03:00
|
|
|
// Notify secretaries.
|
|
|
|
$secretaries = $this->CI->secretaries_model->get();
|
2020-10-21 21:37:47 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($secretaries as $secretary) {
|
|
|
|
if ($secretary['settings']['notifications'] === '0') {
|
2020-10-21 21:37:47 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!in_array($provider['id'], $secretary['providers'])) {
|
2020-10-21 21:37:47 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-12-12 14:14:01 +03:00
|
|
|
config(['language' => $secretary['language']]);
|
|
|
|
$this->CI->lang->load('translations');
|
|
|
|
|
2023-05-04 19:02:40 +03:00
|
|
|
$this->CI->email_messages->send_appointment_deleted(
|
2021-10-28 15:00:33 +03:00
|
|
|
$appointment,
|
|
|
|
$provider,
|
|
|
|
$service,
|
|
|
|
$customer,
|
|
|
|
$settings,
|
2021-11-06 18:11:55 +03:00
|
|
|
$secretary['email'],
|
2023-06-01 15:25:02 +03:00
|
|
|
$cancellation_reason,
|
2023-12-22 13:35:41 +03:00
|
|
|
$secretary['timezone'],
|
2021-10-28 15:00:33 +03:00
|
|
|
);
|
2020-10-21 21:37:47 +03:00
|
|
|
}
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
log_message(
|
|
|
|
'error',
|
|
|
|
'Notifications - Could not email cancellation details of appointment (' .
|
|
|
|
($appointment['id'] ?? '-') .
|
|
|
|
') : ' .
|
2023-12-22 13:35:41 +03:00
|
|
|
$e->getMessage(),
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2021-10-28 15:00:33 +03:00
|
|
|
log_message('error', $e->getTraceAsString());
|
2023-12-12 14:14:01 +03:00
|
|
|
} finally {
|
|
|
|
config(['language' => $current_language ?? 'english']);
|
|
|
|
$this->CI->lang->load('translations');
|
2020-10-21 21:37:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|