Added User email viewing

Improved Table::
This commit is contained in:
Deon George
2011-08-27 16:33:46 +10:00
parent 495da41e0d
commit 6d44e7d5b2
51 changed files with 701 additions and 273 deletions

View File

@@ -12,11 +12,38 @@
*/
class Controller_Admin_Email extends Controller_TemplateDefault_Admin {
protected $secure_actions = array(
'list'=>TRUE,
'templateadd'=>TRUE,
'templateedit'=>TRUE,
'templatelist'=>TRUE,
);
/**
* Show a list of emails
*/
public function action_list() {
$elo = ORM::factory('email_log');
Block::add(array(
'title'=>_('System Emails Sent'),
'body'=>Table::display(
$elo->find_all(),
25,
array(
'id'=>array('label'=>'ID','url'=>'user/email/view/'),
'date_orig'=>array('label'=>'Date'),
'translate_resolve("subject")'=>array('label'=>'Subject'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>'user/email/view',
)),
));
}
/**
* List our defined email templates
*/

View File

@@ -0,0 +1,70 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides User Email View functions
*
* @package OSB
* @subpackage Email
* @category Controllers/User
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Controller_User_Email extends Controller_TemplateDefault_User {
protected $secure_actions = array(
'list'=>TRUE,
'view'=>TRUE,
);
/**
* Show a list of emails
*/
public function action_list() {
Block::add(array(
'title'=>sprintf('%s: %s - %s',_('Email For'),$this->ao->accnum(),$this->ao->name(TRUE)),
'body'=>Table::display(
$this->ao->email_log->find_all(),
25,
array(
'id'=>array('label'=>'ID','url'=>'user/email/view/'),
'date_orig'=>array('label'=>'Date'),
'translate_resolve("subject")'=>array('label'=>'Subject'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>'user/email/view',
)),
));
}
public function action_view() {
$output = '';
if (! $id = $this->request->param('id')) {
if (isset($_POST['id']) AND is_array($_POST['id']))
Table::post('email_view','id');
list($id,$output) = Table::page('email_view');
} else {
$id = $this->request->param('id');
}
$elo = ORM::factory('email_log',$id);
if (! $elo->loaded() OR ! Auth::instance()->authorised($elo->account_id)) {
$this->template->content = 'Unauthorised or doesnt exist?';
return FALSE;
}
$output .= View::factory('email/user/view')
->set('elo',$elo);
Block::add(array(
'title'=>sprintf('%s: %s',_('Email'),$elo->translate_resolve('subject')),
'body'=>$output,
));
}
}
?>

View File

@@ -13,7 +13,7 @@
class Email_Template {
// We'll store the template here
private $template;
private $template_mail;
private $etto;
private $email_data = array();
private $default_lang = 'en';
private $components = array('subject','message_text','message_html');
@@ -27,9 +27,9 @@ class Email_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())
$this->etto = $this->template->email_template_translate->where('language_id','=',$language_id)->find();
if (! $this->etto->loaded() AND
($this->etto = $this->template->email_template_translate->where('language_id','=',$this->default_lang)->find()) AND ! $this->etto->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)',
@@ -97,23 +97,20 @@ class Email_Template {
$return = array();
foreach ($this->components as $v)
foreach ($this->template_mail->variables($v) as $x=>$y)
foreach ($this->etto->variables($v) as $x => $y)
if (! in_array($y,$return))
array_push($return,$y);
return $return;
}
public function send($admin=FALSE) {
public function send(array $admin=array()) {
$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);
$s = $this->etto->resolve($this->email_data['variables'],$component);
switch ($component) {
case 'message_html':
@@ -131,7 +128,6 @@ class Email_Template {
}
}
// @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);
@@ -153,7 +149,7 @@ class Email_Template {
$elo->clear();
$elo->account_id = $id;
$elo->email_template_id = $this->template_mail->id;
$elo->email_template_translate_id = $this->etto->id;
$elo->data = $data;
$elo->save();
}

View File

@@ -11,10 +11,26 @@
* @license http://dev.osbill.net/license.html
*/
class Model_Email_Log extends ORMOSB {
protected $_belongs_to = array(
'account'=>array('far_key'=>'id'),
'email_template_translate'=>array('far_key'=>'id'),
);
protected $_sorting = array(
'id'=>'DESC',
);
protected $_display_filters = array(
'date_orig'=>array(
array('Config::datetime',array(':value')),
),
);
public function translate_resolve($column) {
if (! $this->data OR ! ($r = $this->email_template_translate->variables($column)))
return $this->email_template_translate->display($column);
else
return $this->email_template_translate->resolve(unserialize(gzuncompress($this->data)),$column);
}
}
?>

View File

@@ -28,5 +28,14 @@ class Model_Email_Template_Translate extends ORMOSB {
return $results;
}
public function resolve($data,$column) {
$output = $this->display($column);
foreach ($this->variables($column) as $k => $v)
$output = str_replace('%'.$v.'%',$data[$v],$output);
return $output;
}
}
?>