107 lines
3.2 KiB
PHP
107 lines
3.2 KiB
PHP
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||
|
|
||
|
/**
|
||
|
* This class provides email template functions
|
||
|
*
|
||
|
* @package OSB
|
||
|
* @subpackage EmailTemplate
|
||
|
* @category Helpers
|
||
|
* @author Deon George
|
||
|
* @copyright (c) 2010 Deon George
|
||
|
* @license http://dev.leenooks.net/license.html
|
||
|
*/
|
||
|
class EmailTemplate {
|
||
|
// We'll store the template here
|
||
|
private $template;
|
||
|
private $template_mail;
|
||
|
private $email_data = array();
|
||
|
private $default_lang = 'en';
|
||
|
private $components = array('subject','message_text','message_html');
|
||
|
|
||
|
public function __construct($template,$language_id=NULL) {
|
||
|
|
||
|
$this->template = ORM::factory('emailtemplate',array('name'=>$template));
|
||
|
|
||
|
if (! $this->template->loaded())
|
||
|
throw new Kohana_Exception('Email template :template not defined in DB',array(':template'=>$template));
|
||
|
|
||
|
if (is_null($language_id))
|
||
|
$language_id=$this->default_lang;
|
||
|
|
||
|
$this->template_mail = $this->template->emailtemplate_translate->where('language_id','=',$language_id)->find();
|
||
|
if (! $this->template_mail->loaded() AND
|
||
|
($this->template_mail = $this->template->emailtemplate_translate->where('language_id','=',$this->default_lang)->find()) AND ! $this->template_mail->loaded())
|
||
|
|
||
|
// @todo Change this to log/email the admin
|
||
|
throw new Kohana_Exception('No template (:template) found for user language (:language_id) or default language (:default_lang)',
|
||
|
array(':template'=>$this->template->name,':language_id'=>$language_id,':default_lang'=>$this->default_lang));
|
||
|
}
|
||
|
|
||
|
public function __set($key,$value) {
|
||
|
switch ($key) {
|
||
|
case 'variables':
|
||
|
// Our variables should be an array
|
||
|
if (! is_array($value))
|
||
|
throw new Kohana_Exception('Values for variables should be an array, however :value was given',array(':value'=>$value));
|
||
|
|
||
|
case 'to':
|
||
|
$this->email_data[$key] = $value;
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
throw new Kohana_Exception('Unknown variable :key (:value)',array(':key'=>$key,':value'=>is_string($value) ? $value : serialize($value)));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static function instance($template) {
|
||
|
return new EmailTemplate($template);
|
||
|
}
|
||
|
|
||
|
public function variables() {
|
||
|
$return = array();
|
||
|
foreach ($this->components as $v)
|
||
|
foreach ($this->template_mail->variables($v) as $x=>$y)
|
||
|
if (! in_array($y,$return))
|
||
|
array_push($return,$y);
|
||
|
|
||
|
return $return;
|
||
|
}
|
||
|
|
||
|
public function send($admin=FALSE) {
|
||
|
$e = Email::connect();
|
||
|
$sm = Swift_Message::newInstance();
|
||
|
|
||
|
foreach ($this->components as $component) {
|
||
|
$s = $this->template_mail->$component;
|
||
|
|
||
|
foreach ($this->template_mail->variables($component) as $k => $v)
|
||
|
$s = str_replace('%'.$v.'%',$this->email_data['variables'][$v],$s);
|
||
|
|
||
|
switch ($component) {
|
||
|
case 'message_html':
|
||
|
$sm->setBody($s,'text/html');
|
||
|
break;
|
||
|
case 'message_text':
|
||
|
$sm->setBody($s,'text/plain');
|
||
|
break;
|
||
|
case 'subject':
|
||
|
$sm->setSubject($s);
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
throw new Kohana_Exception('Component :component has not been configured in :method',array(':component'=>$component,':method'=>__METHOD__));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// @todo This should go to the admin defined in email_setup
|
||
|
if ($admin OR ($mail = Config::testmail($this->template->name)))
|
||
|
$sm->setTo($mail);
|
||
|
else
|
||
|
$sm->setTo($this->email_data['to']);
|
||
|
|
||
|
// @todo - Setup queue mode
|
||
|
$e->send($sm);
|
||
|
}
|
||
|
}
|
||
|
?>
|