OSB enhancements to date

This commit is contained in:
Deon George
2010-11-30 09:41:08 +11:00
parent 8715a2059b
commit ec6a542bc3
478 changed files with 23423 additions and 9309 deletions

View 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();
}
}
?>

View 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);
}
}
?>

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_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',
);
}
?>

View File

@@ -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;
}
}
?>