Work on Email and other major consistency work

This commit is contained in:
Deon George
2013-11-22 15:36:50 +11:00
parent 89deb9c97b
commit c18d5a3881
55 changed files with 550 additions and 575 deletions

View File

@@ -11,90 +11,87 @@
*/
class Controller_Admin_Email extends Controller_Email {
protected $secure_actions = array(
'ajaxtemplatetranslate'=>TRUE,
'list'=>TRUE,
'templateadd'=>TRUE,
'templateedit'=>TRUE,
'templatelist'=>TRUE,
);
public function action_ajaxtemplatetranslate() {
$eto = ORM::factory('Email_Template',$this->request->param('id'));
if (! $eto->loaded() OR ! isset($_REQUEST['key'])) {
$output = _('Unable to find translate data');
} else {
$eto = $eto->translate->where('language_id','=',$_REQUEST['key'])->find();
$output = View::factory('email/admin/ajaxtemplatetranslate')
->set('o',$eto);
}
$this->template->content = $output;
}
/**
* Show a list of emails
*/
public function action_list() {
Block::add(array(
'title'=>_('System Emails Sent'),
'body'=>Table::display(
ORM::factory('Email_Log')->find_all(),
25,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','email/view/')),
'date_orig'=>array('label'=>'Date'),
'email'=>array('label'=>'To'),
'translate_resolve("subject")'=>array('label'=>'Subject'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>URL::link('user','email/view'),
)),
));
}
/**
* List our defined email templates
*/
public function action_templatelist() {
$eto = ORM::factory('Email_Template');
$output = '';
// @todo Change this to use Table::
$output .= View::factory($this->viewpath().'/head');
foreach ($eto->find_all() as $et) {
$output .= View::factory($this->viewpath().'/body')
->set('template',$et);
}
$output .= View::factory($this->viewpath().'/foot');
Block::add(array(
'title'=>_('Available Email Templates'),
'body'=>$output,
));
Block::factory()
->title(_('System Emails Sent'))
->title_icon('icon-th')
->body(Table::factory()
->page_items(25)
->data(ORM::factory('Email_Log')->find_all())
->columns(array(
'id'=>'ID',
'date_orig'=>'Date',
'email'=>'To',
'resolve("subject")'=>'Subject',
'account->accnum()'=>'Cust ID',
'account->name()'=>'Customer',
))
->prepend(array(
'id'=>array('url'=>URL::link('user','email/view/')),
))
->postproc(array(
'resolve("subject")'=>array('trim'=>60),
))
);
}
/**
* Add a template
*/
public function action_templateadd() {
$eto = ORM::factory('Email_Template');
$output = '';
Block::factory()
->type('form-horizontal')
->title('Add Email Template')
->title_icon('icon-wrench')
->body($this->add_edit_template());
}
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($this->viewpath());
$output .= View::factory($this->viewpath().'/translate');
$output .= '<div>'.Form::submit('submit',_('Add'),array('class'=>'form_button')).'</div>';
$output .= Form::close();
Editor::add();
Block::add(array(
'title'=>_('Available Email Templates'),
'body'=>$output,
));
/**
* List our defined email templates
*/
public function action_templatelist() {
Block::factory()
->title(_('System Emails Templates Available'))
->title_icon('icon-th')
->body(Table::factory()
->page_items(25)
->data(ORM::factory('Email_Template')->find_all())
->columns(array(
'id'=>'ID',
'name'=>'Name',
'status'=>'Active',
'description'=>'Descrption',
))
->prepend(array(
'id'=>array('url'=>URL::link('admin','email/templateedit/')),
))
);
}
/**
@@ -102,51 +99,64 @@ class Controller_Admin_Email extends Controller_Email {
* @todo Change this into an add_view function like payment()
*/
public function action_templateedit() {
$id = $this->request->param('id');
list($id,$output) = Table::page(__METHOD__);
Block::factory()
->type('form-horizontal')
->title('Update Email Template')
->title_icon('icon-wrench')
->body($this->add_edit_template($id,$output));
}
private function add_edit_template($id=NULL,$output='') {
$eto = ORM::factory('Email_Template',$id);
if (! $eto->loaded())
HTTP::redirect(URL::link('admin','email/template/list'));
if ($_POST) {
// @todo To update the setup ID
$eto->email_setup_id = '1';
$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();
}
}
if (! $this->save($eto))
$eto->reload();
}
Script::factory()
->type('stdin')
->data('
$(document).ready(function() {
$("select[name=language_id]").change(function() {
// If we select a blank, then dont continue
if (this.value == 0)
return false;
// Send the request and update sub category dropdown
$.ajax({
type: "GET",
data: "key="+$(this).val(),
dataType: "html",
cache: false,
url: "'.URL::link('admin','email/ajaxtemplatetranslate/'.$eto->id,TRUE).'",
timeout: 2000,
error: function(x) {
alert("Failed to submit");
},
success: function(data) {
$("div[id=translate]").replaceWith(data);
}
});
});
});
');
return View::factory('email/admin/add_edit_template')
->set('o',$eto);
$output .= Form::open();
$output .= View::factory($this->viewpath())
->set('template',$eto);
foreach ($eto->email_template_translate->find_all() as $to) {
$output .= View::factory($this->viewpath().'/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'),array('class'=>'form_button')).'</div>';
$output .= View::factory($this->viewpath());
$output .= View::factory($this->viewpath().'/translate');
$output .= '<div>'.Form::submit('submit',_('Add'),array('class'=>'form_button')).'</div>';
$output .= Form::close();
Editor::add();
Block::add(array(
'title'=>sprintf(_('Edit Template '),$eto->name),
'title'=>_('Available Email Templates'),
'body'=>$output,
));
}