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
|
|
|
*/
|
|
|
|
class Notifications {
|
|
|
|
/**
|
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()
|
|
|
|
{
|
|
|
|
$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-02-21 10:17:22 +03:00
|
|
|
public function notify_appointment_saved(array $appointment, array $service, array $provider, array $customer, array $settings, bool $manage_mode = FALSE): void
|
2020-10-21 21:37:47 +03:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-11-16 12:47:10 +03:00
|
|
|
if ($manage_mode)
|
2020-10-21 21:37:47 +03:00
|
|
|
{
|
2021-11-06 18:11:55 +03:00
|
|
|
$customer_subject = lang('appointment_changes_saved');
|
|
|
|
|
|
|
|
$customer_message = '';
|
|
|
|
|
|
|
|
$provider_subject = lang('appointment_details_changed');
|
|
|
|
|
|
|
|
$provider_message = '';
|
2020-10-21 21:37:47 +03:00
|
|
|
}
|
2020-11-16 12:47:10 +03:00
|
|
|
else
|
|
|
|
{
|
2021-11-06 18:11:55 +03:00
|
|
|
$customer_subject = lang('appointment_booked');
|
|
|
|
|
|
|
|
$customer_message = lang('thank_you_for_appointment');
|
|
|
|
|
|
|
|
$provider_subject = lang('appointment_added_to_your_plan');
|
|
|
|
|
|
|
|
$provider_message = lang('appointment_link_description');
|
2020-11-16 12:47:10 +03:00
|
|
|
}
|
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.
|
2021-12-20 11:39:01 +03:00
|
|
|
$send_customer = ! empty($customer['email']) && filter_var(
|
|
|
|
setting('customer_notifications'),
|
|
|
|
FILTER_VALIDATE_BOOLEAN
|
|
|
|
);
|
2020-10-21 21:37:47 +03:00
|
|
|
|
|
|
|
if ($send_customer === TRUE)
|
|
|
|
{
|
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,
|
2021-11-06 18:11:55 +03:00
|
|
|
$customer_subject,
|
2021-10-28 15:00:33 +03:00
|
|
|
$customer_message,
|
|
|
|
$customer_link,
|
2021-11-06 18:11:55 +03:00
|
|
|
$customer['email'],
|
|
|
|
$ics_stream,
|
2021-10-28 15:00:33 +03:00
|
|
|
$customer['timezone']
|
|
|
|
);
|
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'),
|
|
|
|
FILTER_VALIDATE_BOOLEAN
|
|
|
|
);
|
2020-10-21 21:37:47 +03:00
|
|
|
|
|
|
|
if ($send_provider === TRUE)
|
|
|
|
{
|
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,
|
2021-11-06 18:11:55 +03:00
|
|
|
$provider_subject,
|
2021-10-28 15:00:33 +03:00
|
|
|
$provider_message,
|
|
|
|
$provider_link,
|
2021-11-06 18:11:55 +03:00
|
|
|
$provider['email'],
|
|
|
|
$ics_stream,
|
2021-10-28 15:00:33 +03:00
|
|
|
$provider['timezone']
|
|
|
|
);
|
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
|
|
|
|
|
|
|
foreach ($admins as $admin)
|
|
|
|
{
|
2020-11-16 12:47:10 +03:00
|
|
|
if ($admin['settings']['notifications'] === '0')
|
2020-10-21 21:37:47 +03:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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,
|
2021-11-06 18:11:55 +03:00
|
|
|
$provider_subject,
|
2021-10-28 15:00:33 +03:00
|
|
|
$provider_message,
|
|
|
|
$provider_link,
|
2021-11-06 18:11:55 +03:00
|
|
|
$admin['email'],
|
|
|
|
$ics_stream,
|
2021-10-28 15:00:33 +03:00
|
|
|
$admin['timezone']
|
|
|
|
);
|
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
|
|
|
|
|
|
|
foreach ($secretaries as $secretary)
|
|
|
|
{
|
2020-11-16 12:47:10 +03:00
|
|
|
if ($secretary['settings']['notifications'] === '0')
|
2020-10-21 21:37:47 +03:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-10-28 15:00:33 +03:00
|
|
|
if ( ! in_array($provider['id'], $secretary['providers']))
|
2020-10-21 21:37:47 +03:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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,
|
2021-11-06 18:11:55 +03:00
|
|
|
$provider_subject,
|
2021-10-28 15:00:33 +03:00
|
|
|
$provider_message,
|
|
|
|
$provider_link,
|
2021-11-06 18:11:55 +03:00
|
|
|
$secretary['email'],
|
|
|
|
$ics_stream,
|
2021-10-28 15:00:33 +03:00
|
|
|
$secretary['timezone']
|
|
|
|
);
|
2020-10-21 21:37:47 +03:00
|
|
|
}
|
|
|
|
}
|
2021-10-28 15:00:33 +03:00
|
|
|
catch (Throwable $e)
|
2020-10-21 21:37:47 +03:00
|
|
|
{
|
2022-06-20 12:09:56 +03:00
|
|
|
log_message('error', 'Notifications - Could not email confirmation details of appointment (' . ($appointment['id'] ?? '-') . ') : ' . $e->getMessage());
|
2021-10-28 15:00:33 +03:00
|
|
|
log_message('error', $e->getTraceAsString());
|
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-02-21 10:17:22 +03:00
|
|
|
public function notify_appointment_deleted(array $appointment, array $service, array $provider, array $customer, array $settings): void
|
2020-10-21 21:37:47 +03:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-10-28 15:00:33 +03:00
|
|
|
$delete_reason = (string)request('delete_reason');
|
2021-10-04 10:26:12 +03:00
|
|
|
|
2021-10-28 15:00:33 +03:00
|
|
|
if (empty($delete_reason))
|
2021-10-04 10:26:12 +03:00
|
|
|
{
|
2022-01-18 20:03:55 +03:00
|
|
|
$delete_reason = (string)request('cancellation_reason');
|
2021-10-04 10:26:12 +03:00
|
|
|
}
|
|
|
|
|
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'),
|
|
|
|
FILTER_VALIDATE_BOOLEAN
|
|
|
|
);
|
2020-10-21 21:37:47 +03:00
|
|
|
|
|
|
|
if ($send_provider === TRUE)
|
|
|
|
{
|
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-02-21 10:17:22 +03:00
|
|
|
$delete_reason,
|
|
|
|
$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.
|
2021-12-20 11:39:01 +03:00
|
|
|
$send_customer = ! empty($customer['email']) && filter_var(
|
|
|
|
setting('customer_notifications'),
|
|
|
|
FILTER_VALIDATE_BOOLEAN
|
|
|
|
);
|
2020-10-21 21:37:47 +03:00
|
|
|
|
|
|
|
if ($send_customer === TRUE)
|
|
|
|
{
|
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-02-21 10:17:22 +03:00
|
|
|
$delete_reason,
|
|
|
|
$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
|
|
|
|
|
|
|
foreach ($admins as $admin)
|
|
|
|
{
|
2021-05-17 13:57:06 +03:00
|
|
|
if ($admin['settings']['notifications'] === '0')
|
2020-10-21 21:37:47 +03:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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-02-21 10:17:22 +03:00
|
|
|
$delete_reason,
|
|
|
|
$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
|
|
|
|
|
|
|
foreach ($secretaries as $secretary)
|
|
|
|
{
|
2021-03-12 21:48:19 +03:00
|
|
|
if ($secretary['settings']['notifications'] === '0')
|
2020-10-21 21:37:47 +03:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-10-28 15:00:33 +03:00
|
|
|
if ( ! in_array($provider['id'], $secretary['providers']))
|
2020-10-21 21:37:47 +03:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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-02-21 10:17:22 +03:00
|
|
|
$delete_reason,
|
|
|
|
$secretary['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
|
|
|
catch (Throwable $e)
|
2020-10-21 21:37:47 +03:00
|
|
|
{
|
2022-06-20 12:09:56 +03:00
|
|
|
log_message('error', 'Notifications - Could not email cancellation details of appointment (' . ($appointment['id'] ?? '-') . ') : ' . $e->getMessage());
|
2021-10-28 15:00:33 +03:00
|
|
|
log_message('error', $e->getTraceAsString());
|
2020-10-21 21:37:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|