Work on Email and other major consistency work
This commit is contained in:
@@ -10,31 +10,32 @@
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
class Email_Template {
|
||||
// We'll store the template here
|
||||
private $template;
|
||||
private $etto;
|
||||
private $email_data = array();
|
||||
// @todo Default lang should be the site setup
|
||||
private $default_lang = 1;
|
||||
private $components = array('subject','message_text','message_html');
|
||||
// Our components that need resolving
|
||||
private $_components = array('subject','message_text','message_html');
|
||||
// Our Email Data
|
||||
private $_data = array();
|
||||
// Our Email Template Object
|
||||
private $_etto;
|
||||
|
||||
public function __construct($template,$language_id=NULL) {
|
||||
$this->template = ORM::factory('Email_Template',array('name'=>$template));
|
||||
$eto = ORM::factory('Email_Template',array('name'=>$template));
|
||||
|
||||
if (! $this->template->loaded())
|
||||
if (! $eto->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;
|
||||
$language_id = Config::language();
|
||||
|
||||
$this->etto = $this->template->email_template_translate->where('language_id','=',$language_id)->find();
|
||||
if (! $this->etto->loaded())
|
||||
$this->etto = $this->template->email_template_translate->where('language_id','=',$this->default_lang)->find();
|
||||
$this->_etto = $eto->translate
|
||||
->where_open()
|
||||
->where('language_id','=',$language_id)
|
||||
->or_where('language_id','=',Config::language())
|
||||
->where_close()
|
||||
->find();
|
||||
|
||||
// @todo Change this to log/email the admin
|
||||
return;
|
||||
#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));
|
||||
if (! $this->_etto->loaded())
|
||||
throw new Kohana_Exception('No template (:template) found for user language (:language_id) or default language (:default_lang)',
|
||||
array(':template'=>$eto->name,':language_id'=>$language_id,':default_lang'=>Config::language()));
|
||||
}
|
||||
|
||||
public function __set($key,$value) {
|
||||
@@ -44,7 +45,15 @@ class Email_Template {
|
||||
if (! is_array($value) OR ! array_intersect(array('email','account'),array_keys($value)))
|
||||
throw new Kohana_Exception('Values for to should be an array of either "mail" or "account", however :value was given',array(':value'=>serialize($value)));
|
||||
|
||||
$this->email_data[$key] = $value;
|
||||
$this->_data[$key] = $value;
|
||||
break;
|
||||
|
||||
case 'module':
|
||||
$this->_data[$key] = $value;
|
||||
break;
|
||||
|
||||
case 'module_data':
|
||||
$this->_data[$key] = $value;
|
||||
break;
|
||||
|
||||
case 'variables':
|
||||
@@ -52,7 +61,7 @@ class Email_Template {
|
||||
if (! is_array($value))
|
||||
throw new Kohana_Exception('Values for variables should be an array, however :value was given',array(':value'=>$value));
|
||||
|
||||
$this->email_data[$key] = $value;
|
||||
$this->_data[$key] = $value;
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -64,16 +73,16 @@ class Email_Template {
|
||||
switch ($key) {
|
||||
case 'bcc':
|
||||
case 'to':
|
||||
if (empty($this->email_data[$key]))
|
||||
if (empty($this->_data[$key]))
|
||||
return array();
|
||||
|
||||
elseif (isset($this->email_data[$key]['email']))
|
||||
return $this->email_data[$key]['email'];
|
||||
elseif (isset($this->_data[$key]['email']))
|
||||
return $this->_data[$key]['email'];
|
||||
|
||||
elseif (isset($this->email_data[$key]['account'])) {
|
||||
elseif (isset($this->_data[$key]['account'])) {
|
||||
$list = array();
|
||||
|
||||
foreach ($this->email_data[$key]['account'] as $id) {
|
||||
foreach ($this->_data[$key]['account'] as $id) {
|
||||
$ao = ORM::factory('Account',$id);
|
||||
if ($ao->loaded())
|
||||
$list[$ao->email] = $ao->name();
|
||||
@@ -85,7 +94,7 @@ class Email_Template {
|
||||
break;
|
||||
|
||||
case 'variables':
|
||||
return $this->email_data[$key];
|
||||
return $this->_data[$key];
|
||||
|
||||
default:
|
||||
throw new Kohana_Exception('Unknown variable :key (:value)',array(':key'=>$key,':value'=>is_string($value) ? $value : serialize($value)));
|
||||
@@ -96,25 +105,14 @@ class Email_Template {
|
||||
return new Email_Template($template);
|
||||
}
|
||||
|
||||
public function variables() {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->components as $v)
|
||||
foreach ($this->etto->variables($v) as $x => $y)
|
||||
if (! in_array($y,$result))
|
||||
array_push($result,$y);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function send(array $admin=array()) {
|
||||
$e = Email::connect();
|
||||
$sm = Swift_Message::newInstance()
|
||||
->setFrom(Kohana::$config->load('config')->email_from);
|
||||
|
||||
foreach ($this->components as $component) {
|
||||
if ($this->etto->loaded()) {
|
||||
$s = $this->etto->rresolve($this->email_data['variables'],$component);
|
||||
foreach ($this->_components as $component) {
|
||||
if ($this->_etto->loaded()) {
|
||||
$s = $this->_etto->complete($this->_data['variables'],$component);
|
||||
|
||||
switch ($component) {
|
||||
case 'message_html':
|
||||
@@ -132,14 +130,14 @@ class Email_Template {
|
||||
}
|
||||
} else {
|
||||
$sm->setSubject(_('Email from').' '.Company::instance()->name());
|
||||
$sm->setBody(print_r($this->email_data['variables'],TRUE),'text/plain');
|
||||
$sm->setBody(print_r($this->_data['variables'],TRUE),'text/plain');
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($this->email_data['bcc']))
|
||||
if (isset($this->_data['bcc']))
|
||||
$sm->setBcc($this->bcc);
|
||||
|
||||
if ($admin OR ($admin = Config::testmail($this->template->name))) {
|
||||
if ($admin OR ($admin = Config::testmail($this->_etto->template->name))) {
|
||||
$sm->setTo($admin);
|
||||
$sa = array(1);
|
||||
|
||||
@@ -153,18 +151,22 @@ class Email_Template {
|
||||
// Store our email log.
|
||||
$elo = ORM::factory('Email_Log');
|
||||
|
||||
if ($result) {
|
||||
|
||||
if ($result)
|
||||
foreach ($sa as $id) {
|
||||
$elo->clear();
|
||||
|
||||
$elo->account_id = $id;
|
||||
$elo->email = implode(',',array_keys($this->to));
|
||||
$elo->email_template_translate_id = $this->etto->id;
|
||||
$elo->data = $this->email_data['variables'];
|
||||
$elo->email_template_translate_id = $this->_etto->id;
|
||||
$elo->data = $this->_data['variables'];
|
||||
|
||||
if (isset($this->_data['module']) AND isset($this->_data['module_data'])) {
|
||||
$elo->module_id = $this->_data['module'];
|
||||
$elo->module_data = $this->_data['module_data'];
|
||||
}
|
||||
|
||||
$elo->save();
|
||||
}
|
||||
}
|
||||
|
||||
return ($result AND $elo->saved()) ? $elo->id : $result;
|
||||
}
|
||||
@@ -173,10 +175,24 @@ class Email_Template {
|
||||
// @todo Set the default account in a configuration file.
|
||||
$default = array(1);
|
||||
|
||||
if (! isset($this->email_data['to']) OR ! is_array($this->email_data['to']) OR ! array_intersect(array('email','account'),array_keys($this->email_data['to'])))
|
||||
if (! isset($this->_data['to']) OR ! is_array($this->_data['to']) OR ! array_intersect(array('email','account'),array_keys($this->_data['to'])))
|
||||
return $default;
|
||||
|
||||
return isset($this->email_data['to']['account']) ? $this->email_data['to']['account'] : $default;
|
||||
return isset($this->_data['to']['account']) ? $this->_data['to']['account'] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Work out all the required variables for this message
|
||||
*/
|
||||
public function variables() {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->_components as $v)
|
||||
foreach ($this->_etto->variables($v) as $x => $y)
|
||||
if (! in_array($y,$result))
|
||||
array_push($result,$y);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
Reference in New Issue
Block a user