2013-05-04 00:26:04 +03:00
|
|
|
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
|
2013-05-17 16:09:10 +03:00
|
|
|
require_once dirname(__FILE__) . '/external/class.phpmailer.php';
|
2013-05-08 17:31:17 +03:00
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
/**
|
|
|
|
* This library handles all the notification email deliveries
|
|
|
|
* on the system.
|
|
|
|
*
|
|
|
|
* Custom system settings for the notification section are loaded
|
|
|
|
* during the execution of the class methods.
|
|
|
|
*/
|
|
|
|
class Notifications {
|
2013-05-17 16:09:10 +03:00
|
|
|
private $CI;
|
2013-05-08 17:31:17 +03:00
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
/**
|
|
|
|
* Class Constructor
|
|
|
|
*/
|
2013-05-17 16:09:10 +03:00
|
|
|
public function __construct() {
|
|
|
|
$this->CI =& get_instance();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the email template variables.
|
|
|
|
*
|
|
|
|
* This method finds and replaces the html variables of an email
|
|
|
|
* template. It is used to generate dynamic HTML emails that are
|
|
|
|
* send as notifications to the system users.
|
|
|
|
*
|
|
|
|
* @param array $replace_array Array that contains the variables
|
|
|
|
* to be replaced.
|
|
|
|
* @param string $email_html The email template hmtl.
|
|
|
|
* @return string Returns the new email html that contain the variables
|
|
|
|
* of the $replace_array.
|
|
|
|
*/
|
|
|
|
private function replace_template_variables($replace_array, $email_html) {
|
|
|
|
foreach($replace_array as $var=>$value) {
|
|
|
|
$email_html = str_replace($var, $value, $email_html);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $email_html;
|
2013-05-04 00:26:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a success email to the customer that booked
|
|
|
|
* a new appointment.
|
|
|
|
*
|
2013-05-17 16:09:10 +03:00
|
|
|
* @expectedException NotificationException Raises when an unexpected
|
|
|
|
* error has occured when the email is send.
|
|
|
|
*
|
2013-05-04 00:26:04 +03:00
|
|
|
* @param array $customer_data Associative array with the customer's
|
|
|
|
* data. Each key has the same name as the corresponding field in db.
|
|
|
|
* @param array $appointment_data Associative array with the appointment's
|
|
|
|
* data. Each key has the same name as the corresponding field in db.
|
|
|
|
* @return bool Returns the operation result.
|
|
|
|
*/
|
|
|
|
public function send_book_success($customer_data, $appointment_data) {
|
2013-05-17 16:09:10 +03:00
|
|
|
$this->CI->load->model('Providers_Model');
|
|
|
|
$this->CI->load->model('Services_Model');
|
|
|
|
$this->CI->load->model('Settings_Model');
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-05-17 16:09:10 +03:00
|
|
|
$provider_data = $this->CI->Providers_Model->get_row($appointment_data['id_users_provider']);
|
|
|
|
$service_data = $this->CI->Services_Model->get_row($appointment_data['id_services']);
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-05-17 16:09:10 +03:00
|
|
|
$replace_array = array(
|
|
|
|
'$appointment_service' => $service_data['name'],
|
|
|
|
'$appointment_provider' => $provider_data['first_name'] . ' ' . $provider_data['last_name'],
|
|
|
|
'$appointment_date' => date('d/m/Y H:i', strtotime($appointment_data['start_datetime'])),
|
|
|
|
'$appointment_duration' => $service_data['duration'] . ' minutes',
|
|
|
|
'$company_link' => $this->CI->Settings_Model->get_setting('company_link'),
|
|
|
|
'$company_name' => $this->CI->Settings_Model->get_setting('company_name'),
|
|
|
|
'$customer_name' => $customer_data['first_name'] . ' ' . $customer_data['last_name']
|
|
|
|
);
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-05-17 16:09:10 +03:00
|
|
|
$email_html = file_get_contents(dirname(dirname(__FILE__)) . '/views/emails/book_success.php');
|
|
|
|
$email_html = $this->replace_template_variables($replace_array, $email_html);
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-05-17 16:09:10 +03:00
|
|
|
$mail = new PHPMailer();
|
|
|
|
$mail->From = $this->CI->Settings_Model->get_setting('company_email');
|
|
|
|
$mail->FromName = $this->CI->Settings_Model->get_setting('company_name');
|
|
|
|
$mail->AddAddress($customer_data['email']); // Do not use the name argument, phpmailer crushes.
|
|
|
|
$mail->IsHTML(true);
|
|
|
|
$mail->CharSet = 'UTF-8';
|
|
|
|
$mail->Subject = 'Appointment Book Success!';
|
|
|
|
$mail->Body = $email_html;
|
|
|
|
|
|
|
|
if(!$mail->Send()) {
|
|
|
|
throw new NotificationException('Email could not been sent. '
|
|
|
|
. 'Mailer Error (' . __LINE__ . '): ' . $mail->ErrorInfo);
|
|
|
|
}
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-05-17 16:09:10 +03:00
|
|
|
return TRUE;
|
2013-05-08 17:31:17 +03:00
|
|
|
}
|
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
/**
|
|
|
|
* Send an email notification to a provider that
|
|
|
|
* a new appointment has been added to his plan.
|
|
|
|
*
|
2013-05-17 16:09:10 +03:00
|
|
|
* @expectedException NotificationException Raises when an unexpected
|
|
|
|
* error has occured when the email is send.
|
|
|
|
*
|
2013-05-04 00:26:04 +03:00
|
|
|
* @param array $customer_data Associative array with the customer's
|
|
|
|
* data. Each key has the same name as the corresponding field in db.
|
|
|
|
* @param array $appointment_data Associative array with the appointment's
|
|
|
|
* data. Each key has the same name as the corresponding field in db.
|
|
|
|
* @return bool Returns the operation result.
|
|
|
|
*/
|
|
|
|
public function send_new_appointment($customer_data, $appointment_data) {
|
2013-05-17 16:09:10 +03:00
|
|
|
$this->CI =& get_instance();
|
|
|
|
$this->CI->load->model('Providers_Model');
|
|
|
|
$this->CI->load->model('Services_Model');
|
|
|
|
$this->CI->load->model('Settings_Model');
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-05-17 16:09:10 +03:00
|
|
|
$provider_data = $this->CI->Providers_Model->get_row($appointment_data['id_users_provider']);
|
|
|
|
$service_data = $this->CI->Services_Model->get_row($appointment_data['id_services']);
|
|
|
|
|
|
|
|
$replace_array = array(
|
|
|
|
'$appointment_service' => $service_data['name'],
|
|
|
|
'$appointment_provider' => $provider_data['first_name'] . ' ' . $provider_data['last_name'],
|
|
|
|
'$appointment_date' => date('d/m/Y H:i', strtotime($appointment_data['start_datetime'])),
|
|
|
|
'$appointment_duration' => $service_data['duration'] . ' minutes',
|
|
|
|
'$company_link' => $this->CI->Settings_Model->get_setting('company_link'),
|
|
|
|
'$company_name' => $this->CI->Settings_Model->get_setting('company_name'),
|
|
|
|
'$customer_name' => $customer_data['first_name'] . ' ' . $customer_data['last_name'],
|
|
|
|
'$customer_email' => $customer_data['email'],
|
|
|
|
'$customer_phone' => $customer_data['phone_number'],
|
|
|
|
'$customer_address' => $customer_data['address']
|
|
|
|
);
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-05-17 16:09:10 +03:00
|
|
|
$email_html = file_get_contents(dirname(dirname(__FILE__)) . '/views/emails/new_appointment.php');
|
|
|
|
$email_html = $this->replace_template_variables($replace_array, $email_html);
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-05-17 16:09:10 +03:00
|
|
|
$mail = new PHPMailer();
|
|
|
|
$mail->From = $this->CI->Settings_Model->get_setting('company_email');
|
|
|
|
$mail->FromName = $this->CI->Settings_Model->get_setting('company_name');;
|
|
|
|
$mail->AddAddress($provider_data['email']); // Do not use the name argument, phpmailer crushes.
|
|
|
|
$mail->IsHTML(true);
|
|
|
|
$mail->CharSet = 'UTF-8';
|
|
|
|
$mail->Subject = 'New Appointment';
|
|
|
|
$mail->Body = $email_html;
|
|
|
|
|
|
|
|
if(!$mail->Send()) {
|
|
|
|
throw new NotificationException('Email could not been sent. '
|
|
|
|
. 'Mailer Error (Line ' . __LINE__ . '): ' . $mail->ErrorInfo);
|
|
|
|
}
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-05-17 16:09:10 +03:00
|
|
|
return TRUE;
|
2013-05-04 00:26:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* End of file notifications.php */
|
|
|
|
/* Location: ./application/libraries/notifications.php */
|