Open Source Billing

This commit is contained in:
Deon George
2013-10-10 13:44:53 +11:00
commit b02d70adf0
2344 changed files with 392978 additions and 0 deletions

View File

@@ -0,0 +1,154 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides Admin Email management
*
* @package Email
* @category Controllers/Admin
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
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() {
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,
));
}
/**
* 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($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,
));
}
/**
* Edit Template Definition
* @todo Change this into an add_view function like payment()
*/
public function action_templateedit() {
$id = $this->request->param('id');
$eto = ORM::factory('Email_Template',$id);
if (! $eto->loaded())
HTTP::redirect(URL::link('admin','email/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($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 .= Form::close();
Editor::add();
Block::add(array(
'title'=>sprintf(_('Edit Template '),$eto->name),
'body'=>$output,
));
}
}
?>

View File

@@ -0,0 +1,14 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides email management
*
* @package Email
* @category Controllers
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Controller_Email extends Controller_TemplateDefault {
}
?>

View File

@@ -0,0 +1,14 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides email management
*
* @package Email
* @category Controllers
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Controller_EmailTemplate extends Controller_TemplateDefault {
}
?>

View File

@@ -0,0 +1,59 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides User Email View functions
*
* @package Email
* @category Controllers/User
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.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'=>URL::link('user','email/view/')),
'date_orig'=>array('label'=>'Date'),
'translate_resolve("subject")'=>array('label'=>'Subject'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>URL::link('user','email/view'),
)),
));
}
public function action_view() {
list($id,$output) = Table::page(__METHOD__);
$elo = ORM::factory('Email_Log',$id);
if (! $elo->loaded() OR ! Auth::instance()->authorised($elo->account)) {
$this->template->content = 'Unauthorised or doesnt exist?';
return FALSE;
}
$output .= View::factory($this->viewpath())
->set('elo',$elo);
Block::add(array(
'title'=>sprintf('%s: %s',_('Email'),$elo->translate_resolve('subject')),
'body'=>$output,
));
}
}
?>

View File

@@ -0,0 +1,182 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides email template functions
*
* @package Email
* @category Helpers
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Email_Template {
// We'll store the template here
private $template;
private $etto;
private $email_data = array();
// @todo Default lang should be the site setup
private $default_lang = 1;
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->etto = $this->template->email_template_translate->where('language_id','=',$language_id)->find();
if (! $this->etto->loaded())
$this->etto = $this->template->email_template_translate->where('language_id','=',$this->default_lang)->find();
// @todo Change this to log/email the admin
return;
#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 'bcc':
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 'bcc':
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() {
$result = array();
foreach ($this->components as $v)
foreach ($this->etto->variables($v) as $x => $y)
if (! in_array($y,$result))
array_push($result,$y);
return $result;
}
public function send(array $admin=array()) {
$e = Email::connect();
$sm = Swift_Message::newInstance()
->setFrom(Kohana::$config->load('config')->email_from);
foreach ($this->components as $component) {
if ($this->etto->loaded()) {
$s = $this->etto->resolve($this->email_data['variables'],$component);
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__));
}
} else {
$sm->setSubject(_('Email from').' '.Company::instance()->name());
$sm->setBody(print_r($this->email_data['variables'],TRUE),'text/plain');
}
}
if (isset($this->email_data['bcc']))
$sm->setBcc($this->bcc);
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.
$elo = ORM::factory('Email_Log');
foreach ($sa as $id) {
$elo->clear();
$elo->account_id = $id;
$elo->email = implode(',',array_keys($this->to));
$elo->email_template_translate_id = $this->etto->id;
$elo->data = $this->email_data['variables'];
$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,38 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports Email Logging
*
* @package Email
* @category Models
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Email_Log extends ORM_OSB {
// Email Log doesnt use the update column
protected $_updated_column = FALSE;
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 ! ($this->email_template_translate->variables($column)))
return $this->email_template_translate->display($column);
else
return $this->email_template_translate->resolve($this->data,$column);
}
}
?>

View File

@@ -0,0 +1,30 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
*
* @package Email
* @category Models
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Email_Template extends ORM_OSB {
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(
'status'=>array(
array('StaticList_YesNo::display',array(':value')),
),
);
}
?>

View File

@@ -0,0 +1,40 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
*
* @package Email
* @category Models
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Email_Template_Translate extends ORM_OSB {
// 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;
}
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;
}
}
?>

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('status',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,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,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('status',$template->status); ?></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,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>

View File

@@ -0,0 +1,4 @@
<tr>
<td><a href="<?php echo URL::link('admin','email/templateedit/'.$template->id,TRUE); ?>" alt=""><?php echo $template->name; ?></a></td>
<td><?php echo $template->display('status'); ?></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,20 @@
<table width="100%">
<tr>
<td>To:</td><td class="data"><?php printf('%s (%s)',$elo->account->name(),$elo->display('email')); ?></td>
</tr>
<tr>
<td>Date:</td><td class="data"><?php echo $elo->display('date_orig'); ?></td>
</tr>
<tr>
<td>Subject:</td><td class="data"><?php echo $elo->translate_resolve('subject'); ?></td>
</tr>
<tr>
<td style="width: 100%;" colspan="2">
<table class="box-full">
<tr>
<td><?php echo $elo->translate_resolve('message_html'); ?></td>
</tr>
</table>
</td>
</tr>
</table>