Fixes to OSB to work with KH 3.3

This commit is contained in:
Deon George
2012-11-10 10:13:57 +11:00
parent ea36639638
commit 6db02ae77d
238 changed files with 813 additions and 938 deletions

View File

@@ -0,0 +1,215 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* @package lnApp
* @subpackage Auth
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Account extends Model_Auth_UserDefault {
// Relationships
protected $_has_many = array(
'user_tokens' => array('model' => 'user_token'),
'email_log' => array('far_key'=>'id'),
'group' => array('through' => 'account_group'),
'invoice' => array('far_key'=>'id'),
'payment'=>array('far_key'=>'id'),
'service' => array('far_key'=>'id'),
);
protected $_has_one = array(
'affiliate' => array('far_key'=>'id'),
'language'=>array('foreign_key'=>'id','far_key'=>'language_id'),
);
protected $_display_filters = array(
'date_orig'=>array(
array('Config::date',array(':value')),
),
'date_last'=>array(
array('Config::date',array(':value')),
),
'status'=>array(
array('StaticList_YesNo::display',array(':value')),
),
);
/**
* Return an account name
*/
public function name($withcompany=FALSE) {
if ($withcompany)
return sprintf('%s %s%s',$this->first_name,$this->last_name,$this->company ? sprintf(' (%s)',$this->company) : '');
else
return sprintf('%s %s',$this->first_name,$this->last_name);
}
public function accnum() {
return sprintf('%s-%04s',Config::siteid(TRUE),$this->id);
}
public function sortkey($withcompany=FALSE) {
$sk = '';
if ($withcompany AND $this->company)
$sk .= $this->company.' ';
return $sk.sprintf('%s %s',$this->last_name,$this->first_name);
}
public function title($name) {
return StaticList_Title::form($name,$this->title);
}
public function currency($name) {
return StaticList_Module::form($name,'currency',$this->currency_id,'id','name',array('status'=>'=:1'),FALSE,array('class'=>'form_button'));
}
public function country($name) {
return StaticList_Module::form($name,'country',$this->country_id,'id','name',array('status'=>'=:1'),FALSE,array('class'=>'form_button'));
}
/**
* Get the groups that an account belongs to
*/
public function groups() {
return $this->group->find_all();
}
public function isAdmin() {
// @todo Define admins in the config file or DB
$admins = array(ORM::factory('Group',array('name'=>'Root')));
return $this->has('group',$admins);
}
/**
* Get a list of all invoices for this account
*/
public function invoices() {
return $this->invoice->distinct('id')->find_all();
}
/**
* Get a list of due invoices for this account
*
* @param int Date (in secs) to only retrieve invoices prior to this date
*/
public function invoices_due($date=NULL) {
$return = array();
foreach ($this->invoices() as $io)
if ((is_null($date) OR $io->date_orig < $date) AND $io->due())
$return[$io->id] = $io;
return $return;
}
/**
* Calculate the total of invoices due for this account
*/
public function invoices_due_total($date=NULL,$format=FALSE) {
$result = 0;
foreach ($this->invoices_due($date) as $io)
$result += $io->due();
// @todo This shouldnt really be required
if ($result < 0)
throw new Kohana_Exception($result);
return $format ? Currency::display($result) : $result;
}
public function log($message) {
// Log the logout
$alo = ORM::factory('Account_Log');
$alo->account_id = $this->id;
$alo->ip = Request::$client_ip;
$alo->details = $message;
$alo->save();
return $alo->saved();
}
public function list_active() {
return $this->_where_active()->order_by('company,last_name,first_name')->find_all();
}
public function list_affiliates() {
$return = array();
foreach ($this->list_services() as $so)
if (! isset($return[$so->affiliate_id]))
$return[$so->affiliate_id] = $so->affiliate;
return $return;
}
public function count_services($active=TRUE,$afid=NULL) {
return $this->list_services($active,$afid)->count();
}
/**
* Search for accounts matching a term
*/
public function list_autocomplete($term,$index='id',array $limit=array()) {
$return = array();
$this->clear();
$this->where_active();
$value = 'name(TRUE)';
// Build our where clause
// First Name, Last name
if (preg_match('/\ /',$term)) {
list($fn,$ln) = explode(' ',$term,2);
$this->where_open()
->where('first_name','like','%'.$fn.'%')
->and_where('last_name','like','%'.$ln.'%')
->where_close()
->or_where('company','like','%'.$term.'%');
} elseif (is_numeric($term)) {
$this->where('id','like','%'.$term.'%');
} elseif (preg_match('/\@/',$term)) {
$this->where('email','like','%'.$term.'%');
$value = 'email';
} else {
$this->where_open()
->where('company','like','%'.$term.'%')
->or_where('first_name','like','%'.$term.'%')
->or_where('last_name','like','%'.$term.'%')
->or_where('email','like','%'.$term.'%')
->where_close();
}
foreach ($limit as $w) {
list($k,$s,$v) = $w;
$this->and_where($k,$s,$v);
}
foreach ($this->find_all() as $o)
$return[$o->$index] = array(
'value'=>$o->$index,
'label'=>sprintf('ACC %s: %s',$o->id,Table::resolve($o,$value)),
);
return $return;
}
public function list_services($active=TRUE,$afid=NULL) {
$svs = $this->service->where_active();
if ($afid)
$svs->where('affiliate_id','=',$afid);
return $svs->find_all();
}
}
?>

View File

@@ -0,0 +1,24 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports Account Login Logging
*
* @package OSB
* @subpackage Account
* @category Models
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Account_Log extends ORM_OSB {
protected $_belongs_to = array(
'account'=>array(),
);
protected $_display_filters = array(
'date_orig'=>array(
array('Config::datetime',array(':value')),
),
);
}
?>

View File

@@ -0,0 +1,15 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* OSB Affiliate
*
* @package OSB
* @subpackage Modules
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Affiliate extends ORM_OSB {
}
?>

View File

@@ -0,0 +1,13 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* @package lnApp
* @subpackage Auth
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Auth_RoleDefault extends Model_Auth_Role {
}
?>

View File

@@ -0,0 +1,76 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* @package OSB
* @subpackage Auth
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Auth_UserDefault extends Model_Auth_User {
// Validation rules
public function rules() {
return array(
'username' => array(
array('not_empty'),
array('min_length', array(':value', 4)),
array('max_length', array(':value', 32)),
),
'password' => array(
array('not_empty'),
array('min_length', array(':value', 5)),
array('max_length', array(':value', 32)),
),
'email' => array(
array('not_empty'),
array('min_length', array(':value', 4)),
array('max_length', array(':value', 127)),
array('email'),
),
// @todo To test
'password_confirm' => array(
array('matches_ifset', array(':validation', 'password', 'password_confirm')),
),
);
}
// Validation callbacks
// @todo _callbacks no longer used
protected $_callbacks = array(
'username' => array('username_available'),
'email' => array('email_available'),
);
// Columns to ignore
protected $_ignored_columns = array('password_confirm');
/*
* Complete our login
*
* For some database logins, we may not want to record the user last login
* details in the repository, so we just override that parent function
* here.
*
* We can also do some other post-login actions here.
* @todo Maybe we can do our session update here.
*/
public function complete_login() {
return $this->log('Logged In');
}
/**
* Debug function to see that has() finds
* @todo This function could be removed
*/
public function has_list($alias, $model) {
// Return list of matches
return DB::select()
->from($this->_has_many[$alias]['through'])
->where($this->_has_many[$alias]['foreign_key'], '=', $this->pk())
->where($this->_has_many[$alias]['far_key'], '=', $model->pk())
->execute($this->_db)
->as_array();
}
}
?>

View File

@@ -0,0 +1,18 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* OSB Country Model
*
* @package OSB
* @subpackage Modules
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Country extends ORM_OSB {
public function currency() {
return ORM::factory('Currency')->where('country_id','=',$this->id)->find();
}
}
?>

View File

@@ -0,0 +1,15 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* OSB Currency Model
*
* @package OSB
* @subpackage Modules
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Currency extends ORM_OSB {
}
?>

View File

@@ -0,0 +1,84 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* @package lnApp
* @subpackage Auth
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Group extends Model_Auth_RoleDefault {
// Relationships
protected $_has_many = array(
'account'=>array('through'=>'account_group'),
'module_method'=>array('through'=>'group_method','far_key'=>'method_id'),
);
protected $_sorting = array(
'name'=>'ASC',
);
// Validation rules
protected $_rules = array(
'name' => array(
'not_empty' => NULL,
'min_length' => array(4),
'max_length' => array(32),
),
'description' => array(
'max_length' => array(255),
),
);
protected $_display_filters = array(
'status'=>array(
array('StaticList_YesNo::display',array(':value')),
),
);
/**
* This function will, given a group, list all of the children that
* are also related to this group, in the group heirarchy.
*/
public function list_childgrps($incParent=FALSE) {
$return = array();
if (! $this->loaded())
return $return;
foreach (ORM::factory('Group')->where_active()->and_where('parent_id','=',$this)->find_all() as $go) {
array_push($return,$go);
$return = array_merge($return,$go->list_childgrps());
}
if ($incParent)
array_push($return,$this);
return $return;
}
/**
* This function will, given a group, list all of the parent that
* are also related to this group, in the group heirarchy.
*/
public function list_parentgrps($incParent=FALSE) {
$return = array();
if (! $this->loaded())
return $return;
foreach (ORM::factory('Group')->where_active()->and_where('id','=',$this->parent_id)->find_all() as $go) {
array_push($return,$go);
$return = array_merge($return,$go->list_parentgrps());
}
if ($incParent)
array_push($return,$this);
return $return;
}
}
?>

View File

@@ -0,0 +1,26 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* OSB Application Module Method Model
*
* @package OSB
* @subpackage Modules
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Group_Method extends ORM_OSB {
// Relationships
protected $_has_one = array(
'record_id'=>array(),
);
protected $_belongs_to = array(
'group'=>array(),
);
// This module doesnt keep track of column updates automatically
protected $_created_column = FALSE;
protected $_updated_column = FALSE;
}
?>

View File

@@ -0,0 +1,16 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* OSB Language Model
*
* @package OSB
* @subpackage Modules
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Language extends ORM_OSB {
protected $_form = array('id'=>'id','value'=>'name');
}
?>

View File

@@ -0,0 +1,43 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* OSB Application Module Model
*
* This module must remain in applications/ as it is used very early in the
* OSB initialisation.
*
* @package OSB
* @subpackage Modules
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Module extends ORM_OSB {
// Relationships
protected $_has_many = array(
'module_method'=>array('far_key'=>'id'),
);
protected $_has_one = array(
'record_id'=>array('far_key'=>'id'),
);
protected $_sorting = array(
'status'=>'DESC',
'name'=>'ASC',
);
protected $_display_filters = array(
'name'=>array(
array('strtoupper',array(':value')),
),
'status'=>array(
array('StaticList_YesNo::display',array(':value')),
),
);
public function list_external() {
return $this->_where_active()->where('external','=',TRUE)->find_all();
}
}
?>

View File

@@ -0,0 +1,44 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* OSB Application Module Method Model
*
* @package OSB
* @subpackage Modules
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Module_Method extends ORM_OSB {
// Relationships
protected $_belongs_to = array(
'module'=>array(),
);
protected $_has_one = array(
'record_id'=>array(),
);
protected $_has_many = array(
'group'=>array('through'=>'group_method','foreign_key'=>'method_id')
);
protected $_sorting = array(
'name'=>'ASC',
);
protected $_display_filters = array(
'menu_display'=>array(
array('StaticList_YesNo::display',array(':value')),
),
);
// This module doesnt keep track of column updates automatically
protected $_created_column = FALSE;
protected $_updated_column = FALSE;
// Return the method name.
public function name() {
return sprintf('%s::%s',$this->module->name,$this->name);
}
}
?>

View File

@@ -0,0 +1,109 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* OSB Application Module Method Token Model
*
* @package OSB
* @subpackage Modules
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Module_Method_Token extends ORM_OSB {
// Relationships
protected $_belongs_to = array(
'account'=>array(),
'module_method'=>array('foreign_key'=>'method_id'),
);
protected $_has_one = array(
'record_id'=>array(),
);
// This module doesnt keep track of column updates automatically
protected $_update_column = FALSE;
public function method(array $modmeth) {
list($module,$method) = $modmeth;
if (! $method instanceof Model_Module_Method) {
if (is_numeric($module))
$mo = ORM::factory('Module',$module);
elseif (is_string($module))
$mo = ORM::factory('Module',array('name'=>$module));
elseif (! $module instanceof Model_Module)
throw new Kohana_Exception('Unknown module :module',array(':module'=>serialize($module)));
else
$mo = $module;
if (! $mo->loaded())
throw new Kohana_Exception('Unknown module :module - not loaded?',array(':module'=>$mo->id));
if (is_numeric($method))
$mmo = ORM::factory('Module_Method',$method);
elseif (is_string($method))
$mmo = ORM::factory('Module_Method',array('name'=>$method,'module_id'=>$mo->id));
else
throw new Kohana_Exception('Unknown method :method',array(':method'=>serialize($method)));
} else
$mmo = $method;
if (! $mmo->loaded())
throw new Kohana_Exception('Unknown method :method - not loaded?',array(':method'=>$mmo->id));
$this->method_id = $mmo->id;
return $this;
}
public function account($account) {
if (! $account instanceof Model_Account) {
if (is_numeric($account))
$ao = ORM::factory('Account',$account);
else
throw new Kohana_Exception('Unknown account :account',array(':account'=>serialize($account)));
} else
$ao = $account;
$this->account_id = $ao->id;
return $this;
}
public function uses($uses) {
$this->uses = $uses;
return $this;
}
public function expire($expire) {
$this->date_expire = $expire;
return $this;
}
// @todo Login Reset: When called within a timelimit (so existing token already exists), is returning true but password reset emails have blanks where the tokens are
public function generate() {
if (! $this->account_id OR ! $this->method_id OR ! ($this->date_expire OR $this->uses))
return NULL;
// Check we dont already have a valid token
$mmto = ORM::factory('Module_Method_Token')
->where('account_id','=',$this->account_id)
->where('method_id','=',$this->method_id)
->find();
if ($mmto->loaded()) {
if ((is_null($mmto->date_expire) OR $mmto->date_expire > time()) AND (is_null($mmto->uses) OR $mmto->uses > 0))
return $mmto->token;
else
$mmto->delete();
}
$this->token = md5(sprintf('%s:%s:%s',$this->account_id,$this->method_id,time()));
$this->save();
return $this->saved() ? $this->token : NULL;
}
}
?>

View File

@@ -0,0 +1,41 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* @package OSB
* @subpackage Modules
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Record_Id extends ORM_OSB {
protected $_primary_key = 'module_id';
// This module doesnt keep track of column updates automatically
protected $_created_column = FALSE;
protected $_updated_column = FALSE;
// @todo we need $mid here, since if there is no record, we cant figure out the module that called us.
public function next_id($mid) {
if (is_null($this->id)) {
$this->module_id = $mid;
// We'll get the next ID as the MAX(id) of the table
$mo = ORM::factory('Module',$mid);
$max = DB::select(array('MAX(id)','id'))
->from($mo->name)
->where('site_id','=',Config::siteid());
$this->id = $max->execute()->get('id');
}
$this->id++;
if (! $this->save())
throw new Koahan_Exception(_('Unable to increase ID for :table'),array(':table'=>$mid));
return $this->id;
}
}
?>

View File

@@ -0,0 +1,83 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* OSB Setup Model
*
* This module must remain in applications/ as it is used very early in the
* OSB initialisation.
*
* @package OSB
* @subpackage Modules
* @category Models
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Setup extends ORM_OSB {
// Setup doesnt use the update column
protected $_updated_column = FALSE;
protected $_has_one = array(
'country'=>array('foreign_key'=>'id','far_key'=>'country_id'),
'language'=>array('foreign_key'=>'id','far_key'=>'language_id'),
);
public function rules() {
$r = parent::rules();
// This module doesnt use site_id.
unset($r['site_id']);
return $r;
}
/**
* Get/Set Module Configuration
*
* @param $key Module name.
* @param $value Values to store. If NULL, retrieves the value stored, otherwise stores value.
*/
public function module_config($key,array $value=NULL) {
// If we are not loaded, we dont have any config.
if (! $this->loaded() OR (is_null($value) AND ! $this->module_config))
return array();
$mo = ORM::factory('Module')->where('name','=',$key)->find();
if (! $mo->loaded())
throw new Kohana_Exception('Unknown module :name',array(':name'=>$key));
$mc = $this->module_config ? $this->module_config : array();
// If $value is NULL, we are a getter
if ($value === NULL)
return empty($mc[$mo->id]) ? array() : $mc[$mo->id];
// Store new value
$mc[$mo->id] = $value;
$this->module_config = $mc;
return $this;
}
/**
* Get/Set our Site Configuration from the DB
*
* @param $key Key
* @param $value Values to store. If NULL, retrieves the value stored, otherwise stores value.
*/
public function site_details($key,array $value=NULL) {
if (! in_array($key,array('name','address1','address2','city','state','pcode','phone','fax','email')))
throw new Kohana_Exception('Unknown Site Configuration Key :key',array(':key'=>$key));
// If $value is NULL, we are a getter
if ($value === NULL)
return empty($this->site_details[$key]) ? '' : $this->site_details[$key];
// Store new value
$sc[$key] = $value;
return $this;
}
}
?>