2017-09-15 14:36:37 +03:00
|
|
|
<?php
|
2016-07-16 17:33:34 +03:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2018-03-27 10:23:09 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2018, Alex Tselegidis
|
2016-07-16 17:33:34 +03:00
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @since v1.2.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
namespace EA\Engine\Notifications;
|
2016-07-16 17:33:34 +03:00
|
|
|
|
2016-11-09 21:56:24 +03:00
|
|
|
use \EA\Engine\Types\Text;
|
|
|
|
use \EA\Engine\Types\NonEmptyText;
|
2016-07-16 17:33:34 +03:00
|
|
|
use \EA\Engine\Types\Url;
|
|
|
|
use \EA\Engine\Types\Email as EmailAddress;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Email Notifications Class
|
|
|
|
*
|
2017-09-15 14:36:37 +03:00
|
|
|
* This library handles all the notification email deliveries on the system.
|
2016-07-16 17:33:34 +03:00
|
|
|
*
|
|
|
|
* Important: The email configuration settings are located at: /application/config/email.php
|
|
|
|
*/
|
|
|
|
class Email {
|
|
|
|
/**
|
|
|
|
* Framework Instance
|
|
|
|
*
|
|
|
|
* @var CI_Controller
|
|
|
|
*/
|
|
|
|
protected $framework;
|
|
|
|
|
|
|
|
/**
|
2016-10-23 23:32:12 +03:00
|
|
|
* Contains email configuration.
|
2016-07-16 17:33:34 +03:00
|
|
|
*
|
2016-10-23 23:32:12 +03:00
|
|
|
* @var array
|
2016-07-16 17:33:34 +03:00
|
|
|
*/
|
2016-10-23 23:32:12 +03:00
|
|
|
protected $config;
|
2016-07-16 17:33:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Constructor
|
|
|
|
*
|
2016-11-09 22:10:27 +03:00
|
|
|
* @param \CI_Controller $framework
|
2016-07-16 17:33:34 +03:00
|
|
|
* @param array $config Contains the email configuration to be used.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function __construct(\CI_Controller $framework, array $config)
|
|
|
|
{
|
2016-10-23 23:32:12 +03:00
|
|
|
$this->framework = $framework;
|
|
|
|
$this->config = $config;
|
2016-07-16 17:33:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the email template variables.
|
|
|
|
*
|
2017-09-15 14:36:37 +03:00
|
|
|
* This method finds and replaces the html variables of an email template. It is used to
|
2016-07-16 17:33:34 +03:00
|
|
|
* generate dynamic HTML emails that are send as notifications to the system users.
|
|
|
|
*
|
|
|
|
* @param array $replaceArray Array that contains the variables to be replaced.
|
|
|
|
* @param string $templateHtml The email template HTML.
|
|
|
|
*
|
|
|
|
* @return string Returns the new email html that contain the variables of the $replaceArray.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
protected function _replaceTemplateVariables(array $replaceArray, $templateHtml)
|
|
|
|
{
|
|
|
|
foreach ($replaceArray as $name => $value)
|
|
|
|
{
|
2016-07-16 17:33:34 +03:00
|
|
|
$templateHtml = str_replace($name, $value, $templateHtml);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $templateHtml;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an email with the appointment details.
|
|
|
|
*
|
|
|
|
* This email template also needs an email title and an email text in order to complete
|
|
|
|
* the appointment details.
|
|
|
|
*
|
|
|
|
* @param array $appointment Contains the appointment data.
|
|
|
|
* @param array $provider Contains the provider data.
|
|
|
|
* @param array $service Contains the service data.
|
|
|
|
* @param array $customer Contains the customer data.
|
|
|
|
* @param array $company Contains settings of the company. By the time the
|
|
|
|
* "company_name", "company_link" and "company_email" values are required in the array.
|
2016-11-09 21:56:24 +03:00
|
|
|
* @param \EA\Engine\Types\Text $title The email title may vary depending the receiver.
|
|
|
|
* @param \EA\Engine\Types\Text $message The email message may vary depending the receiver.
|
2016-07-16 17:33:34 +03:00
|
|
|
* @param \EA\Engine\Types\Url $appointmentLink This link is going to enable the receiver to make changes
|
|
|
|
* to the appointment record.
|
|
|
|
* @param \EA\Engine\Types\Email $recipientEmail The recipient email address.
|
2017-11-02 16:09:15 +03:00
|
|
|
* @param \EA\Engine\Types\Text $icsStream Stream contents of the ICS file.
|
2016-07-16 17:33:34 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function sendAppointmentDetails(
|
|
|
|
array $appointment,
|
|
|
|
array $provider,
|
|
|
|
array $service,
|
|
|
|
array $customer,
|
|
|
|
array $company,
|
|
|
|
Text $title,
|
|
|
|
Text $message,
|
|
|
|
Url $appointmentLink,
|
2017-11-02 16:09:15 +03:00
|
|
|
EmailAddress $recipientEmail,
|
|
|
|
Text $icsStream
|
2017-09-15 14:36:37 +03:00
|
|
|
) {
|
|
|
|
switch ($company['date_format'])
|
|
|
|
{
|
2017-07-04 09:46:54 +03:00
|
|
|
case 'DMY':
|
|
|
|
$date_format = 'd/m/Y';
|
|
|
|
break;
|
|
|
|
case 'MDY':
|
|
|
|
$date_format = 'm/d/Y';
|
|
|
|
break;
|
|
|
|
case 'YMD':
|
|
|
|
$date_format = 'Y/m/d';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \Exception('Invalid date_format value: ' . $company['date_format']);
|
|
|
|
}
|
2016-07-16 17:33:34 +03:00
|
|
|
|
2018-03-06 14:20:47 +03:00
|
|
|
switch ($company['time_format'])
|
|
|
|
{
|
2018-03-08 16:56:31 +03:00
|
|
|
case 'military':
|
2018-03-06 14:20:47 +03:00
|
|
|
$timeFormat = 'H:i';
|
|
|
|
break;
|
2018-03-08 16:56:31 +03:00
|
|
|
case 'regular':
|
2018-03-06 14:20:47 +03:00
|
|
|
$timeFormat = 'g:i A';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \Exception('Invalid time_format value: ' . $company['time_format']);
|
|
|
|
}
|
|
|
|
|
2016-07-16 17:33:34 +03:00
|
|
|
// Prepare template replace array.
|
2017-09-15 14:36:37 +03:00
|
|
|
$replaceArray = [
|
2016-07-16 17:33:34 +03:00
|
|
|
'$email_title' => $title->get(),
|
|
|
|
'$email_message' => $message->get(),
|
|
|
|
'$appointment_service' => $service['name'],
|
|
|
|
'$appointment_provider' => $provider['first_name'] . ' ' . $provider['last_name'],
|
2018-03-06 14:20:47 +03:00
|
|
|
'$appointment_start_date' => date($date_format . ' ' . $timeFormat, strtotime($appointment['start_datetime'])),
|
|
|
|
'$appointment_end_date' => date($date_format . ' ' . $timeFormat, strtotime($appointment['end_datetime'])),
|
2016-07-16 17:33:34 +03:00
|
|
|
'$appointment_link' => $appointmentLink->get(),
|
|
|
|
'$company_link' => $company['company_link'],
|
|
|
|
'$company_name' => $company['company_name'],
|
|
|
|
'$customer_name' => $customer['first_name'] . ' ' . $customer['last_name'],
|
|
|
|
'$customer_email' => $customer['email'],
|
|
|
|
'$customer_phone' => $customer['phone_number'],
|
|
|
|
'$customer_address' => $customer['address'],
|
|
|
|
|
|
|
|
// Translations
|
|
|
|
'Appointment Details' => $this->framework->lang->line('appointment_details_title'),
|
|
|
|
'Service' => $this->framework->lang->line('service'),
|
|
|
|
'Provider' => $this->framework->lang->line('provider'),
|
|
|
|
'Start' => $this->framework->lang->line('start'),
|
|
|
|
'End' => $this->framework->lang->line('end'),
|
|
|
|
'Customer Details' => $this->framework->lang->line('customer_details_title'),
|
|
|
|
'Name' => $this->framework->lang->line('name'),
|
|
|
|
'Email' => $this->framework->lang->line('email'),
|
|
|
|
'Phone' => $this->framework->lang->line('phone'),
|
|
|
|
'Address' => $this->framework->lang->line('address'),
|
|
|
|
'Appointment Link' => $this->framework->lang->line('appointment_link_title')
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2016-07-16 17:33:34 +03:00
|
|
|
|
|
|
|
$html = file_get_contents(__DIR__ . '/../../application/views/emails/appointment_details.php');
|
|
|
|
$html = $this->_replaceTemplateVariables($replaceArray, $html);
|
|
|
|
|
2016-10-23 23:32:12 +03:00
|
|
|
$mailer = $this->_createMailer();
|
|
|
|
|
|
|
|
$mailer->From = $company['company_email'];
|
|
|
|
$mailer->FromName = $company['company_name'];
|
|
|
|
$mailer->AddAddress($recipientEmail->get());
|
|
|
|
$mailer->Subject = $title->get();
|
2017-09-15 14:36:37 +03:00
|
|
|
$mailer->Body = $html;
|
2016-10-23 23:32:12 +03:00
|
|
|
|
2017-11-02 16:09:15 +03:00
|
|
|
$mailer->addStringAttachment($icsStream->get(), 'invitation.ics');
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ( ! $mailer->Send())
|
|
|
|
{
|
|
|
|
throw new \RuntimeException('Email could not been sent. Mailer Error (Line ' . __LINE__ . '): '
|
|
|
|
. $mailer->ErrorInfo);
|
2016-07-16 17:33:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an email notification to both provider and customer on appointment removal.
|
|
|
|
*
|
|
|
|
* Whenever an appointment is cancelled or removed, both the provider and customer
|
|
|
|
* need to be informed. This method sends the same email twice.
|
|
|
|
*
|
|
|
|
* <strong>IMPORTANT!</strong> This method's arguments should be taken
|
|
|
|
* from database before the appointment record is deleted.
|
|
|
|
*
|
|
|
|
* @param array $appointment The record data of the removed appointment.
|
|
|
|
* @param array $provider The record data of the appointment provider.
|
|
|
|
* @param array $service The record data of the appointment service.
|
|
|
|
* @param array $customer The record data of the appointment customer.
|
2017-09-15 14:36:37 +03:00
|
|
|
* @param array $company Some settings that are required for this function. By now this array must contain
|
2016-07-16 17:33:34 +03:00
|
|
|
* the following values: "company_link", "company_name", "company_email".
|
|
|
|
* @param \EA\Engine\Types\Email $recipientEmail The email address of the email recipient.
|
2016-11-09 22:10:27 +03:00
|
|
|
* @param \EA\Engine\Types\Text $reason The reason why the appointment is deleted.
|
2016-07-16 17:33:34 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function sendDeleteAppointment(
|
|
|
|
array $appointment,
|
|
|
|
array $provider,
|
|
|
|
array $service,
|
|
|
|
array $customer,
|
|
|
|
array $company,
|
|
|
|
EmailAddress $recipientEmail,
|
|
|
|
Text $reason
|
|
|
|
) {
|
|
|
|
switch ($company['date_format'])
|
|
|
|
{
|
2017-07-04 09:46:54 +03:00
|
|
|
case 'DMY':
|
|
|
|
$date_format = 'd/m/Y';
|
|
|
|
break;
|
|
|
|
case 'MDY':
|
|
|
|
$date_format = 'm/d/Y';
|
|
|
|
break;
|
|
|
|
case 'YMD':
|
|
|
|
$date_format = 'Y/m/d';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \Exception('Invalid date_format value: ' . $company['date_format']);
|
|
|
|
}
|
|
|
|
|
2018-03-06 14:20:47 +03:00
|
|
|
switch ($company['time_format'])
|
|
|
|
{
|
2018-03-08 16:56:31 +03:00
|
|
|
case 'military':
|
2018-03-06 14:20:47 +03:00
|
|
|
$timeFormat = 'H:i';
|
|
|
|
break;
|
2018-03-08 16:56:31 +03:00
|
|
|
case 'regular':
|
2018-03-06 14:20:47 +03:00
|
|
|
$timeFormat = 'g:i A';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \Exception('Invalid time_format value: ' . $company['time_format']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare email template data.
|
2017-09-15 14:36:37 +03:00
|
|
|
$replaceArray = [
|
2016-07-16 17:33:34 +03:00
|
|
|
'$email_title' => $this->framework->lang->line('appointment_cancelled_title'),
|
|
|
|
'$email_message' => $this->framework->lang->line('appointment_removed_from_schedule'),
|
|
|
|
'$appointment_service' => $service['name'],
|
|
|
|
'$appointment_provider' => $provider['first_name'] . ' ' . $provider['last_name'],
|
2018-03-06 14:20:47 +03:00
|
|
|
'$appointment_date' => date($date_format . ' ' . $timeFormat, strtotime($appointment['start_datetime'])),
|
2018-06-03 16:05:18 +03:00
|
|
|
'$appointment_duration' => $service['duration'] . ' ' . $this->framework->lang->line('minutes'),
|
2016-07-16 17:33:34 +03:00
|
|
|
'$company_link' => $company['company_link'],
|
|
|
|
'$company_name' => $company['company_name'],
|
|
|
|
'$customer_name' => $customer['first_name'] . ' ' . $customer['last_name'],
|
|
|
|
'$customer_email' => $customer['email'],
|
|
|
|
'$customer_phone' => $customer['phone_number'],
|
|
|
|
'$customer_address' => $customer['address'],
|
|
|
|
'$reason' => $reason->get(),
|
|
|
|
|
|
|
|
// Translations
|
|
|
|
'Appointment Details' => $this->framework->lang->line('appointment_details_title'),
|
|
|
|
'Service' => $this->framework->lang->line('service'),
|
|
|
|
'Provider' => $this->framework->lang->line('provider'),
|
|
|
|
'Date' => $this->framework->lang->line('start'),
|
|
|
|
'Duration' => $this->framework->lang->line('duration'),
|
|
|
|
'Customer Details' => $this->framework->lang->line('customer_details_title'),
|
|
|
|
'Name' => $this->framework->lang->line('name'),
|
|
|
|
'Email' => $this->framework->lang->line('email'),
|
|
|
|
'Phone' => $this->framework->lang->line('phone'),
|
|
|
|
'Address' => $this->framework->lang->line('address'),
|
|
|
|
'Reason' => $this->framework->lang->line('reason')
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2016-07-16 17:33:34 +03:00
|
|
|
|
|
|
|
$html = file_get_contents(__DIR__ . '/../../application/views/emails/delete_appointment.php');
|
|
|
|
$html = $this->_replaceTemplateVariables($replaceArray, $html);
|
|
|
|
|
2016-10-24 23:56:09 +03:00
|
|
|
$mailer = $this->_createMailer();
|
|
|
|
|
2016-07-16 17:33:34 +03:00
|
|
|
// Send email to recipient.
|
2016-10-24 23:56:09 +03:00
|
|
|
$mailer->From = $company['company_email'];
|
|
|
|
$mailer->FromName = $company['company_name'];
|
|
|
|
$mailer->AddAddress($recipientEmail->get()); // "Name" argument crushes the phpmailer class.
|
|
|
|
$mailer->Subject = $this->framework->lang->line('appointment_cancelled_title');
|
|
|
|
$mailer->Body = $html;
|
2016-07-16 17:33:34 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ( ! $mailer->Send())
|
|
|
|
{
|
|
|
|
throw new \RuntimeException('Email could not been sent. Mailer Error (Line ' . __LINE__ . '): '
|
|
|
|
. $mailer->ErrorInfo);
|
2016-07-16 17:33:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method sends an email with the new password of a user.
|
|
|
|
*
|
2016-11-09 21:56:24 +03:00
|
|
|
* @param \EA\Engine\Types\NonEmptyText $password Contains the new password.
|
2016-10-23 23:32:12 +03:00
|
|
|
* @param \EA\Engine\Types\Email $recipientEmail The receiver's email address.
|
2016-07-16 17:33:34 +03:00
|
|
|
* @param array $company The company settings to be included in the email.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function sendPassword(NonEmptyText $password, EmailAddress $recipientEmail, array $company)
|
|
|
|
{
|
|
|
|
$replaceArray = [
|
2016-07-16 17:33:34 +03:00
|
|
|
'$email_title' => $this->framework->lang->line('new_account_password'),
|
|
|
|
'$email_message' => $this->framework->lang->line('new_password_is'),
|
|
|
|
'$company_name' => $company['company_name'],
|
|
|
|
'$company_email' => $company['company_email'],
|
|
|
|
'$company_link' => $company['company_link'],
|
|
|
|
'$password' => '<strong>' . $password->get() . '</strong>'
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2016-07-16 17:33:34 +03:00
|
|
|
|
|
|
|
$html = file_get_contents(__DIR__ . '/../../application/views/emails/new_password.php');
|
|
|
|
$html = $this->_replaceTemplateVariables($replaceArray, $html);
|
|
|
|
|
2016-10-23 23:32:12 +03:00
|
|
|
$mailer = $this->_createMailer();
|
2016-07-16 17:33:34 +03:00
|
|
|
|
2016-10-23 23:32:12 +03:00
|
|
|
$mailer->From = $company['company_email'];
|
|
|
|
$mailer->FromName = $company['company_name'];
|
|
|
|
$mailer->AddAddress($recipientEmail->get()); // "Name" argument crushes the phpmailer class.
|
|
|
|
$mailer->Subject = $this->framework->lang->line('new_account_password');
|
|
|
|
$mailer->Body = $html;
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ( ! $mailer->Send())
|
|
|
|
{
|
|
|
|
throw new \RuntimeException('Email could not been sent. Mailer Error (Line ' . __LINE__ . '): '
|
2016-10-24 23:56:09 +03:00
|
|
|
. $mailer->ErrorInfo);
|
2016-07-16 17:33:34 +03:00
|
|
|
}
|
|
|
|
}
|
2016-10-23 23:32:12 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create PHP Mailer Instance
|
|
|
|
*
|
|
|
|
* @return \PHPMailer
|
|
|
|
*/
|
|
|
|
protected function _createMailer()
|
|
|
|
{
|
|
|
|
$mailer = new \PHPMailer;
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($this->config['protocol'] === 'smtp')
|
|
|
|
{
|
2016-10-23 23:32:12 +03:00
|
|
|
$mailer->isSMTP();
|
|
|
|
$mailer->Host = $this->config['smtp_host'];
|
2017-09-15 14:36:37 +03:00
|
|
|
$mailer->SMTPAuth = TRUE;
|
2016-10-23 23:32:12 +03:00
|
|
|
$mailer->Username = $this->config['smtp_user'];
|
|
|
|
$mailer->Password = $this->config['smtp_pass'];
|
|
|
|
$mailer->SMTPSecure = $this->config['smtp_crypto'];
|
|
|
|
$mailer->Port = $this->config['smtp_port'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$mailer->IsHTML($this->config['mailtype'] === 'html');
|
|
|
|
$mailer->CharSet = $this->config['charset'];
|
|
|
|
|
|
|
|
return $mailer;
|
|
|
|
}
|
2016-07-16 17:33:34 +03:00
|
|
|
}
|