OSB enhancements to date
This commit is contained in:
131
modules/emailtemplate/classes/controller/admin/emailtemplate.php
Normal file
131
modules/emailtemplate/classes/controller/admin/emailtemplate.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?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_EmailTemplate extends Controller_TemplateDefault {
|
||||
public $secure_actions = array(
|
||||
);
|
||||
|
||||
public function action_menu() {
|
||||
}
|
||||
|
||||
/**
|
||||
* List our defined email templates
|
||||
*/
|
||||
public function action_list() {
|
||||
$eto = ORM::factory('emailtemplate');
|
||||
$output = '';
|
||||
|
||||
$output .= View::factory('admin/emailtemplate/list_header');
|
||||
foreach ($eto->find_all() as $et) {
|
||||
$output .= View::factory('admin/emailtemplate/list_body')
|
||||
->set('template',$et);
|
||||
}
|
||||
$output .= View::factory('admin/emailtemplate/list_footer');
|
||||
|
||||
Block::add(array(
|
||||
'title'=>_('Available Email Templates'),
|
||||
'body'=>$output,
|
||||
));
|
||||
|
||||
$this->template->content = Block::factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a template
|
||||
*/
|
||||
public function action_add() {
|
||||
$eto = ORM::factory('emailtemplate');
|
||||
$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->emailtemplate_translate->values($_POST['translate']['new']);
|
||||
|
||||
$x->email_template_id = $eto->id;
|
||||
if ($x->check())
|
||||
$x->save();
|
||||
}
|
||||
}
|
||||
|
||||
$output .= Form::open();
|
||||
$output .= View::factory('admin/emailtemplate/add');
|
||||
$output .= View::factory('admin/emailtemplate/add_translate');
|
||||
$output .= '<div>'.Form::submit('submit',_('Add')).'</div>';
|
||||
$output .= Form::close();
|
||||
|
||||
Editor::add();
|
||||
Block::add(array(
|
||||
'title'=>_('Available Email Templates'),
|
||||
'body'=>$output,
|
||||
));
|
||||
|
||||
$this->template->content = Block::factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit Template Definition
|
||||
*/
|
||||
public function action_edit($id) {
|
||||
$eto = ORM::factory('emailtemplate',$id);
|
||||
|
||||
if (! $eto->loaded())
|
||||
Request::instance()->redirect('admin/emailtemplate/list');
|
||||
|
||||
$output = '';
|
||||
|
||||
if ($_POST AND $eto->values($_POST)->check()) {
|
||||
// Entry updated
|
||||
if ($eto->save()) {
|
||||
foreach ($_POST['translate'] as $id => $details) {
|
||||
$x = $eto->emailtemplate_translate->where('id','=',$id)->find();
|
||||
|
||||
if ($x->values($details)->check())
|
||||
$x->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$output .= Form::open();
|
||||
|
||||
$output .= View::factory('admin/emailtemplate/edit')
|
||||
->set('template',$eto);
|
||||
|
||||
foreach ($eto->emailtemplate_translate->find_all() as $to) {
|
||||
$output .= View::factory('admin/emailtemplate/edit_translate')
|
||||
->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,
|
||||
));
|
||||
|
||||
$this->template->content = Block::factory();
|
||||
}
|
||||
}
|
||||
?>
|
106
modules/emailtemplate/classes/emailtemplate.php
Normal file
106
modules/emailtemplate/classes/emailtemplate.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
?>
|
31
modules/emailtemplate/classes/model/emailtemplate.php
Normal file
31
modules/emailtemplate/classes/model/emailtemplate.php
Normal 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_EmailTemplate extends ORMOSB {
|
||||
protected $_table_name = 'email_template';
|
||||
|
||||
protected $_has_many = array(
|
||||
'emailtemplate_translate'=>array('foreign_key'=>'email_template_id'),
|
||||
);
|
||||
|
||||
// This module doesnt keep track of column updates automatically
|
||||
protected $_created_column = FALSE;
|
||||
protected $_updated_column = FALSE;
|
||||
|
||||
protected $_formats = array(
|
||||
'active'=>array('StaticList_YesNo::display'=>array()),
|
||||
);
|
||||
|
||||
protected $_sorting = array(
|
||||
'name'=>'ASC',
|
||||
);
|
||||
}
|
||||
?>
|
@@ -0,0 +1,34 @@
|
||||
<?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_EmailTemplate_Translate extends ORMOSB {
|
||||
protected $_table_name = 'email_template_translate';
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
?>
|
14
modules/emailtemplate/views/admin/emailtemplate/add.php
Normal file
14
modules/emailtemplate/views/admin/emailtemplate/add.php
Normal 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>
|
@@ -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>
|
14
modules/emailtemplate/views/admin/emailtemplate/edit.php
Normal file
14
modules/emailtemplate/views/admin/emailtemplate/edit.php
Normal 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>
|
@@ -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>
|
@@ -0,0 +1,4 @@
|
||||
<tr>
|
||||
<td><a href="<?php echo URL::site(sprintf('/admin/emailtemplate/edit/%s',$template->id)); ?>" alt=""><?php echo $template->name; ?></a></td>
|
||||
<td><?php echo $template->display('active'); ?></td>
|
||||
</tr>
|
@@ -0,0 +1 @@
|
||||
</table>
|
@@ -0,0 +1,6 @@
|
||||
<!-- //@todo Translation required -->
|
||||
<table class="box-left">
|
||||
<tr class="head">
|
||||
<td>Template</td>
|
||||
<td>Active</td>
|
||||
</tr>
|
Reference in New Issue
Block a user