Move emailtemplate under email

This commit is contained in:
Deon George
2011-08-26 12:01:45 +10:00
parent 1c66acd7e4
commit 495da41e0d
16 changed files with 37 additions and 41 deletions

View File

@@ -0,0 +1,125 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides MODULE management
*
* @package lnApp
* @subpackage Page/Module
* @category Controllers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Controller_Admin_Email extends Controller_TemplateDefault_Admin {
protected $secure_actions = array(
'templateadd'=>TRUE,
'templateedit'=>TRUE,
'templatelist'=>TRUE,
);
/**
* List our defined email templates
*/
public function action_templatelist() {
$eto = ORM::factory('email_template');
$output = '';
$output .= View::factory('email/admin/template/list_head');
foreach ($eto->find_all() as $et) {
$output .= View::factory('email/admin/template/list_body')
->set('template',$et);
}
$output .= View::factory('email/admin/template/list_foot');
Block::add(array(
'title'=>_('Available Email Templates'),
'body'=>$output,
));
}
/**
* Add a template
*/
public function action_templateadd() {
$eto = ORM::factory('email_template');
$output = '';
if ($_POST AND $eto->values($_POST)->check()) {
// @todo To update the setup ID
$eto->email_setup_id=1;
// Entry updated
if ($eto->save()) {
$x = $eto->email_template_translate->values($_POST['translate']['new']);
$x->email_template_id = $eto->id;
if ($x->check())
$x->save();
}
}
$output .= Form::open();
$output .= View::factory('email/admin/template/add');
$output .= View::factory('email/admin/template/translate/add');
$output .= '<div>'.Form::submit('submit',_('Add')).'</div>';
$output .= Form::close();
Editor::add();
Block::add(array(
'title'=>_('Available Email Templates'),
'body'=>$output,
));
}
/**
* Edit Template Definition
*/
public function action_templateedit($id) {
$eto = ORM::factory('email_template',$id);
if (! $eto->loaded())
Request::current()->redirect('email/admin/template/list');
$output = '';
if ($_POST AND $eto->values($_POST)->check()) {
// Entry updated
if ($eto->save()) {
foreach ($_POST['translate'] as $id => $details) {
$x = $eto->email_template_translate->where('id','=',$id)->find();
if ($x->values($details)->check())
$x->save();
}
}
}
$output .= Form::open();
$output .= View::factory('email/admin/template/edit')
->set('template',$eto);
foreach ($eto->email_template_translate->find_all() as $to) {
$output .= View::factory('email/admin/template/translate/edit')
->set('translate',$to);
SystemMessage::add(array(
'title'=>_('Available Variables'),
'type'=>'info',
'body'=>sprintf('This template uses the following TEXT variables (%s) and HTML variables (%s)',
implode('|',array_values($to->variables('message_text'))),
implode('|',array_values($to->variables('message_html')))),
));
}
$output .= '<div>'.Form::submit('submit',_('Update')).'</div>';
$output .= Form::close();
Editor::add();
Block::add(array(
'title'=>sprintf(_('Edit Template '),$eto->name),
'body'=>$output,
));
}
}
?>

View File

@@ -0,0 +1,15 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides email management
*
* @package lnApp
* @subpackage Page/Email
* @category Controllers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Controller_EmailTemplate extends Controller_TemplateDefault {
}
?>

View File

@@ -0,0 +1,175 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides email template functions
*
* @package OSB
* @subpackage Email_Template
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Email_Template {
// 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('email_template',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->email_template_translate->where('language_id','=',$language_id)->find();
if (! $this->template_mail->loaded() AND
($this->template_mail = $this->template->email_template_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 'to':
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;
break;
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));
$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 function __get($key) {
switch ($key) {
case 'to':
if (empty($this->email_data[$key]))
return array();
elseif (isset($this->email_data[$key]['email']))
return $this->email_data[$key]['email'];
elseif (isset($this->email_data[$key]['account'])) {
$list = array();
foreach ($this->email_data[$key]['account'] as $id) {
$ao = ORM::factory('account',$id);
if ($ao->loaded())
$list[$ao->email] = $ao->name();
}
return $list;
}
break;
case 'variables':
return $this->email_data[$key];
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 Email_Template($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()
->setFrom(Kohana::config('config.email_from'));
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 ($admin = Config::testmail($this->template->name))) {
$sm->setTo($admin);
$sa = array(1);
} else {
$sm->setTo($this->to);
$sa = $this->to_accounts();
}
// @todo - Setup queue mode
$result = $e->send($sm);
if ($result) {
// Store our email log.
$data = gzcompress(serialize($this->email_data['variables']));
$elo = ORM::factory('email_log');
foreach ($sa as $id) {
$elo->clear();
$elo->account_id = $id;
$elo->email_template_id = $this->template_mail->id;
$elo->data = $data;
$elo->save();
}
}
return $result;
}
private function to_accounts() {
// @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'])))
return $default;
return isset($this->email_data['to']['account']) ? $this->email_data['to']['account'] : $default;
}
}
?>

View File

@@ -0,0 +1,31 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
*
* @package OSB
* @subpackage EmailTemplate
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Email_Template extends ORMOSB {
protected $_has_many = array(
'email_template_translate'=>array('foreign_key'=>'email_template_id','far_key'=>'id'),
);
// This module doesnt keep track of column updates automatically
protected $_created_column = FALSE;
protected $_updated_column = FALSE;
protected $_sorting = array(
'name'=>'ASC',
);
protected $_display_filters = array(
'active'=>array(
array('StaticList_YesNo::display',array(':value')),
),
);
}
?>

View File

@@ -0,0 +1,32 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
*
* @package OSB
* @subpackage EmailTemplate
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Email_Template_Translate extends ORMOSB {
// This module doesnt keep track of column updates automatically
protected $_created_column = FALSE;
protected $_updated_column = FALSE;
public function variables($field) {
$results = array();
$matches = array();
preg_match_all('/%([A-Z0-9_]+)%/U',$this->$field,$matches,PREG_OFFSET_CAPTURE);
foreach ($matches[1] as $k => $v)
$results[$v[1]] = $v[0];
$results = array_unique($results);
asort($results);
return $results;
}
}
?>

View File

@@ -0,0 +1,14 @@
<table class="box-left">
<tr>
<td class="head">Name:</td>
<td><?php echo Form::input('name','',array('size'=>30)); ?></td>
</tr>
<tr>
<td class="head">Active:</td>
<td><?php echo StaticList_YesNo::form('active',TRUE); ?></td>
</tr>
<tr>
<td class="head">Notes:</td>
<td><?php echo Form::input('notes','',array('size'=>80)); ?></td>
</tr>
</table>

View File

@@ -0,0 +1,14 @@
<table class="box-left">
<tr>
<td class="head">Name:</td>
<td><?php echo Form::input('name',$template->name,array('size'=>30)); ?></td>
</tr>
<tr>
<td class="head">Active:</td>
<td><?php echo StaticList_YesNo::form('active',$template->active); ?></td>
</tr>
<tr>
<td class="head">Notes:</td>
<td><?php echo Form::input('notes',$template->notes,array('size'=>80)); ?></td>
</tr>
</table>

View File

@@ -0,0 +1,4 @@
<tr>
<td><a href="<?php echo URL::site(sprintf('/admin/email/templateedit/%s',$template->id)); ?>" alt=""><?php echo $template->name; ?></a></td>
<td><?php echo $template->display('active'); ?></td>
</tr>

View File

@@ -0,0 +1 @@
</table>

View File

@@ -0,0 +1,6 @@
<!-- //@todo Translation required -->
<table class="box-left">
<tr class="head">
<td>Template</td>
<td>Active</td>
</tr>

View File

@@ -0,0 +1,18 @@
<table class="box-left">
<tr>
<td class="head">Language:</td>
<td><?php echo Form::input(sprintf('translate[%s][language_id]','new'),'',array('size'=>5)); ?></td>
</tr>
<tr>
<td class="head">Subject:</td>
<td><?php echo Form::input(sprintf('translate[%s][subject]','new'),'',array('size'=>80)); ?></td>
</tr>
<tr>
<td class="head">Text:</td>
<td><?php echo Form::textarea(sprintf('translate[%s][message_text]','new'),'',array('cols'=>120,'rows'=>10)); ?></td>
</tr>
<tr>
<td class="head">HTML:</td>
<td><?php echo Form::textarea(sprintf('translate[%s][message_html]','new'),'',array('cols'=>120,'rows'=>10,'class'=>'mceEditor')); ?></td>
</tr>
</table>

View File

@@ -0,0 +1,18 @@
<table class="box-left">
<tr>
<td class="head">Language:</td>
<td><?php echo Form::input(sprintf('translate[%s][language_id]',$translate->id),$translate->language_id,array('size'=>5)); ?></td>
</tr>
<tr>
<td class="head">Subject:</td>
<td><?php echo Form::input(sprintf('translate[%s][subject]',$translate->id),$translate->subject,array('size'=>80)); ?></td>
</tr>
<tr>
<td class="head">Text:</td>
<td><?php echo Form::textarea(sprintf('translate[%s][message_text]',$translate->id),$translate->message_text,array('cols'=>120,'rows'=>10)); ?></td>
</tr>
<tr>
<td class="head">HTML:</td>
<td><?php echo Form::textarea(sprintf('translate[%s][message_html]',$translate->id),$translate->message_html,array('cols'=>120,'rows'=>20,'class'=>'mceEditor')); ?></td>
</tr>
</table>