From 8f56da789e359b522b66734e8f291831e6076387 Mon Sep 17 00:00:00 2001 From: Deon George Date: Mon, 30 Jul 2012 17:47:28 +1000 Subject: [PATCH] Added Account list, DB changes for account --- application/classes/auth/osb.php | 2 +- .../classes/controller/admin/account.php | 76 +++++++++++++------ .../classes/controller/affiliate/account.php | 44 +++++++++++ .../controller/templatedefault/affiliate.php | 14 +++- application/classes/model/account.php | 46 ++++++++++- application/classes/ormosb.php | 8 ++ 6 files changed, 158 insertions(+), 32 deletions(-) create mode 100644 application/classes/controller/affiliate/account.php diff --git a/application/classes/auth/osb.php b/application/classes/auth/osb.php index e9ee4677..c8f5ca8d 100644 --- a/application/classes/auth/osb.php +++ b/application/classes/auth/osb.php @@ -226,7 +226,7 @@ class Auth_OSB extends Auth_ORM { } // If the passwords match, perform a login - if ($user->status AND $user->has_any('group',ORM::factory('group',array('name'=>'Registered Users'))->list_childgrps(TRUE)) AND $user->password === $password) + if ($user->active AND $user->has_any('group',ORM::factory('group',array('name'=>'Registered Users'))->list_childgrps(TRUE)) AND $user->password === $password) { if ($remember === TRUE) { diff --git a/application/classes/controller/admin/account.php b/application/classes/controller/admin/account.php index 81229f2b..4907645f 100644 --- a/application/classes/controller/admin/account.php +++ b/application/classes/controller/admin/account.php @@ -13,31 +13,10 @@ class Controller_Admin_Account extends Controller_TemplateDefault_Admin { protected $secure_actions = array( 'autocomplete'=>FALSE, // @todo To Change + 'list'=>TRUE, 'listlog'=>TRUE, ); - /** - * Show a list of account logins - */ - public function action_listlog() { - Block::add(array( - 'title'=>_('Account Login Log'), - 'body'=>Table::display( - ORM::factory('account_log')->order_by('id','DESC')->find_all(), - 25, - array( - 'id'=>array('label'=>'ID'), - 'date_orig'=>array('label'=>'Date'), - 'account->name()'=>array('label'=>'Account'), - 'ip'=>array('label'=>'IP Address'), - 'details'=>array('label'=>'Details'), - ), - array( - 'page'=>TRUE, - )), - )); - } - public function action_autocomplete() { $return = array(); @@ -61,7 +40,7 @@ class Controller_Admin_Account extends Controller_TemplateDefault_Admin { } // @todo The results should be limited so that users dont see what they shouldnt. - foreach ($a->find_all() as $ao) + foreach ($a->find_all() as $ao) array_push($return,array( 'id'=>$ao->id, 'label'=>sprintf('%s (%s)',$ao->name(),$ao->email), @@ -69,8 +48,55 @@ class Controller_Admin_Account extends Controller_TemplateDefault_Admin { )); $this->auto_render = FALSE; - $this->response->headers('Content-Type','application/json'); - $this->response->body(json_encode($return)); + $this->response->headers('Content-Type','application/json'); + $this->response->body(json_encode($return)); + } + + /** + * Show a list of account logins + */ + public function action_listlog() { + Block::add(array( + 'title'=>_('Account Login Log'), + 'body'=>Table::display( + ORM::factory('account_log')->order_by('id','DESC')->find_all(), + 25, + array( + 'id'=>array('label'=>'ID'), + 'date_orig'=>array('label'=>'Date'), + 'account->name()'=>array('label'=>'Account'), + 'ip'=>array('label'=>'IP Address'), + 'details'=>array('label'=>'Details'), + ), + array( + 'page'=>TRUE, + )), + )); + } + + /** + * Show a list of accounts + */ + public function action_list() { + Block::add(array( + 'title'=>_('Customer List'), + 'body'=>Table::display( + ORM::factory('account')->list_active(), + 25, + array( + 'id'=>array('label'=>'ID','url'=>'user/account/view/'), + 'accnum()'=>array('label'=>'Num'), + 'name(TRUE)'=>array('label'=>'Account'), + 'email'=>array('label'=>'Email'), + 'invoices_due_total(NULL,TRUE)'=>array('label'=>'Invoices','class'=>'right'), + 'count_services(TRUE,NULL)'=>array('label'=>'Services','class'=>'right'), + ), + array( + 'page'=>TRUE, + 'type'=>'select', + 'form'=>'user/account/view', + )), + )); } } ?> diff --git a/application/classes/controller/affiliate/account.php b/application/classes/controller/affiliate/account.php new file mode 100644 index 00000000..9e890d6e --- /dev/null +++ b/application/classes/controller/affiliate/account.php @@ -0,0 +1,44 @@ +TRUE, + ); + + /** + * Show a list of accounts + */ + public function action_list() { + + Block::add(array( + 'title'=>_('Customer List'), + 'body'=>Table::display( + $this->filter(ORM::factory('account')->list_active(),$this->ao->affiliate->id,'sortkey(TRUE)'); + 25, + array( + 'id'=>array('label'=>'ID','url'=>'user/account/view/'), + 'accnum()'=>array('label'=>'Num'), + 'name(TRUE)'=>array('label'=>'Account'), + 'email'=>array('label'=>'Email'), + 'invoices_due_total(NULL,TRUE)'=>array('label'=>'Invoices','class'=>'right'), + 'count_services(TRUE,'.$this->ao->affiliate->id.')'=>array('label'=>'Services','class'=>'right'), + ), + array( + 'page'=>TRUE, + 'type'=>'select', + 'form'=>'user/account/view', + )), + )); + } +} +?> diff --git a/application/classes/controller/templatedefault/affiliate.php b/application/classes/controller/templatedefault/affiliate.php index 14de941a..5f0b41fe 100644 --- a/application/classes/controller/templatedefault/affiliate.php +++ b/application/classes/controller/templatedefault/affiliate.php @@ -17,9 +17,17 @@ class Controller_TemplateDefault_Affiliate extends Controller_TemplateDefault_Us protected function filter($o,$af,$sort='account->name()',$afid='affiliate_id') { $result = array(); - foreach ($o as $x) - if ($x->$afid == $af) - array_push($result,$x); + foreach ($o as $x) { + if (isset($x->$afid)) { + if ($x->$afid == $af) + array_push($result,$x); + + } elseif (method_exists($x,'list_affiliates')) { + if (in_array($af,$x->list_affiliates())) + array_push($result,$x); + + } + } if ($sort) Sort::MAsort($result,$sort); diff --git a/application/classes/model/account.php b/application/classes/model/account.php index 3ebea21b..b56cd3f1 100644 --- a/application/classes/model/account.php +++ b/application/classes/model/account.php @@ -24,15 +24,15 @@ class Model_Account extends Model_Auth_UserDefault { ); protected $_display_filters = array( + 'active'=>array( + array('StaticList_YesNo::display',array(':value')), + ), 'date_orig'=>array( array('Config::date',array(':value')), ), 'date_last'=>array( array('Config::date',array(':value')), ), - 'status'=>array( - array('StaticList_YesNo::display',array(':value')), - ), ); /** @@ -49,6 +49,15 @@ class Model_Account extends Model_Auth_UserDefault { 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); } @@ -124,6 +133,28 @@ class Model_Account extends Model_Auth_UserDefault { return $alo->saved(); } + private function _active() { + return $this->where('active','=',TRUE); + } + + public function list_active() { + return $this->_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 count($this->list_services($active,$afid)); + } + /** * Search for accounts matching a term */ @@ -167,5 +198,14 @@ class Model_Account extends Model_Auth_UserDefault { return $return; } + + public function list_services($active=TRUE,$afid=NULL) { + $svs = $this->service->where('active','=',$active); + + if ($afid) + $svs->where('affiliate_id','=',$afid); + + return $svs->find_all(); + } } ?> diff --git a/application/classes/ormosb.php b/application/classes/ormosb.php index d9adf721..d21add1b 100644 --- a/application/classes/ormosb.php +++ b/application/classes/ormosb.php @@ -208,5 +208,13 @@ abstract class ORMOSB extends ORM { return empty($mc[$key]) ? '' : $mc[$key]; } + + protected function _active() { + return $this->where('active','=',TRUE); + } + + public function list_active() { + return $this->_active()->find_all(); + } } ?>