More use of list_active(), setup ajax actions that are check to be ajax

This commit is contained in:
Deon George 2012-10-07 15:15:34 +11:00
parent 4220ade8ac
commit 878c159e3a
19 changed files with 137 additions and 130 deletions

View File

@ -258,7 +258,6 @@ class Auth_OSB extends Auth_ORM {
$orm = ORM::factory($t) $orm = ORM::factory($t)
->where($c,'=',$oldsess); ->where($c,'=',$oldsess);
// @todo There must be a way that ORM can update multiple records with 1 SQL
foreach ($orm->find_all() as $o) foreach ($orm->find_all() as $o)
$o->set('session_id',session_id()) $o->set('session_id',session_id())
->update(); ->update();

View File

@ -45,7 +45,7 @@ class Config extends lnApp_Config {
static $return = array(); static $return = array();
if (! count($return)) if (! count($return))
foreach (ORM::factory('module')->where('status','=',1)->find_all() as $mo) foreach (ORM::factory('module')->list_active() as $mo)
$return[$mo->name] = MODPATH.$mo->name; $return[$mo->name] = MODPATH.$mo->name;
return $return; return $return;

View File

@ -12,44 +12,20 @@
*/ */
class Controller_Admin_Account extends Controller_TemplateDefault_Admin { class Controller_Admin_Account extends Controller_TemplateDefault_Admin {
protected $secure_actions = array( protected $secure_actions = array(
'autocomplete'=>FALSE, // @todo To Change 'ajaxlist'=>FALSE, // @todo To Change
'list'=>TRUE, 'list'=>TRUE,
'listlog'=>TRUE, 'listlog'=>TRUE,
); );
public function action_autocomplete() { public function action_ajaxlist() {
$return = array(); $return = array();
$a = ORM::factory('account')->where('status','=',1); if (isset($_REQUEST['term']) AND trim($_REQUEST['term']))
if (isset($_REQUEST['term']) AND trim($_REQUEST['term'])) { $return += ORM::factory('account')->list_autocomplete($_REQUEST['term']);
$t = $_REQUEST['term'];
// @todo - Implement different search criteria, eg: @ by email, space for first/last, etc
if (FALSE) {
// All search
} else {
$a = $a
->where_open()
->where('first_name','like','%'.$t.'%')
->or_where('last_name','like','%'.$t.'%')
->or_where('company','like','%'.$t.'%')
->or_where('email','like','%'.$t.'%')
->where_close();
}
}
// @todo The results should be limited so that users dont see what they shouldnt.
foreach ($a->find_all() as $ao)
array_push($return,array(
'id'=>$ao->id,
'label'=>sprintf('%s (%s)',$ao->name(),$ao->email),
'value'=>$ao->id,
));
$this->auto_render = FALSE; $this->auto_render = FALSE;
$this->response->headers('Content-Type','application/json'); $this->response->headers('Content-Type','application/json');
$this->response->body(json_encode($return)); $this->response->body(json_encode(array_values($return)));
} }
/** /**

View File

@ -18,8 +18,6 @@ class Kohana extends Kohana_Core {
* unless each site has exactly the same modules enabled. * unless each site has exactly the same modules enabled.
* This is because Kohana::$file is cached with the enabled modules * This is because Kohana::$file is cached with the enabled modules
* and is not OSB multi-site aware. * and is not OSB multi-site aware.
*
* @todo It might be nice to cache the Kohana::$file site-aware for improved performance
*/ */
public static function shutdown_handler() { public static function shutdown_handler() {
// If caching isnt enabled, we can skip this anyway // If caching isnt enabled, we can skip this anyway

View File

@ -82,6 +82,11 @@ abstract class lnApp_Controller_TemplateDefault extends Controller_Template {
return; return;
} }
$TEST = FALSE;
// Actions that start with ajax, should only be ajax
if (preg_match('/^ajax/',Request::current()->action()) AND ! Request::current()->is_ajax() AND ! $TEST)
die();
parent::before(); parent::before();
// Check user auth and role // Check user auth and role

View File

@ -117,7 +117,7 @@ class Model_Account extends Model_Auth_UserDefault {
// @todo This shouldnt really be required // @todo This shouldnt really be required
if ($result < 0) if ($result < 0)
$result = 0; throw new Kohana_Exception($result);
return $format ? Currency::display($result) : $result; return $format ? Currency::display($result) : $result;
} }
@ -133,10 +133,6 @@ class Model_Account extends Model_Auth_UserDefault {
return $alo->saved(); return $alo->saved();
} }
private function _where_active() {
return $this->where('status','=',TRUE);
}
public function list_active() { public function list_active() {
return $this->_where_active()->order_by('company,last_name,first_name')->find_all(); return $this->_where_active()->order_by('company,last_name,first_name')->find_all();
} }
@ -158,10 +154,11 @@ class Model_Account extends Model_Auth_UserDefault {
/** /**
* Search for accounts matching a term * Search for accounts matching a term
*/ */
public function list_autocomplete($term,$index='id') { public function list_autocomplete($term,$index='id',array $limit=array()) {
$return = array(); $return = array();
$this->clear(); $this->clear();
$this->where_active();
$value = 'name(TRUE)'; $value = 'name(TRUE)';
// Build our where clause // Build our where clause
@ -183,13 +180,20 @@ class Model_Account extends Model_Auth_UserDefault {
$value = 'email'; $value = 'email';
} else { } else {
$this->where('company','like','%'.$term.'%') $this->where_open()
->where('company','like','%'.$term.'%')
->or_where('first_name','like','%'.$term.'%') ->or_where('first_name','like','%'.$term.'%')
->or_where('last_name','like','%'.$term.'%') ->or_where('last_name','like','%'.$term.'%')
->or_where('email','like','%'.$term.'%'); ->or_where('email','like','%'.$term.'%')
->where_close();
}
foreach ($limit as $w) {
list($k,$s,$v) = $w;
$this->and_where($k,$s,$v);
} }
// @todo This should limit the results so that users dont see other users services.
foreach ($this->find_all() as $o) foreach ($this->find_all() as $o)
$return[$o->$index] = array( $return[$o->$index] = array(
'value'=>$o->$index, 'value'=>$o->$index,

View File

@ -47,7 +47,7 @@ class Model_Group extends Model_Auth_RoleDefault {
if (! $this->loaded()) if (! $this->loaded())
return $return; return $return;
foreach (ORM::factory('group')->where('status','=',1)->and_where('parent_id','=',$this)->find_all() as $go) { foreach (ORM::factory('group')->where_active()->and_where('parent_id','=',$this)->find_all() as $go) {
array_push($return,$go); array_push($return,$go);
$return = array_merge($return,$go->list_childgrps()); $return = array_merge($return,$go->list_childgrps());
@ -69,7 +69,7 @@ class Model_Group extends Model_Auth_RoleDefault {
if (! $this->loaded()) if (! $this->loaded())
return $return; return $return;
foreach (ORM::factory('group')->where('status','=',1)->and_where('id','=',$this->parent_id)->find_all() as $go) { foreach (ORM::factory('group')->where_active()->and_where('id','=',$this->parent_id)->find_all() as $go) {
array_push($return,$go); array_push($return,$go);
$return = array_merge($return,$go->list_parentgrps()); $return = array_merge($return,$go->list_parentgrps());

View File

@ -127,5 +127,14 @@ class ORM extends Kohana_ORM {
->where('site_id', '=', Config::siteid()) ->where('site_id', '=', Config::siteid())
->execute($this->_db)->get('records_found'); ->execute($this->_db)->get('records_found');
} }
protected function _where_active() {
return $this->where('status','=',TRUE);
}
public function where_active() {
return $this->_where_active();
}
} }
?> ?>

View File

@ -25,13 +25,14 @@ abstract class ORMOSB extends ORM {
// Our attributes used in forms. // Our attributes used in forms.
protected $_form = array(); protected $_form = array();
// Rules to assist with site ID and getting next record ID for inserts.
public function rules() { public function rules() {
return array( return array(
'id'=>array( 'id'=>array(
array('ORMOSB::get_next_id',array(':validation',':model',':field')), array('ORMOSB::get_next_id',array(':model',':field')),
), ),
'site_id'=>array( 'site_id'=>array(
array('ORMOSB::set_site_id',array(':validation',':model',':field')), array('ORMOSB::set_site_id',array(':model',':field')),
), ),
); );
} }
@ -73,8 +74,7 @@ abstract class ORMOSB extends ORM {
* @param array Validate object * @param array Validate object
* @param string Primary Key * @param string Primary Key
*/ */
// @todo Do we need the $array? public static function get_next_id($model,$field) {
public static function get_next_id(Validation $array,$model,$field) {
if (! is_null($model->$field)) if (! is_null($model->$field))
return TRUE; return TRUE;
@ -92,8 +92,10 @@ abstract class ORMOSB extends ORM {
return TRUE; return TRUE;
} }
// @todo Do we need the $array? /**
public static function set_site_id(Validation $array,$model,$field) { * Set the site ID attribute for each row update
*/
public static function set_site_id($model,$field) {
if (! is_null($model->$field)) if (! is_null($model->$field))
return TRUE; return TRUE;
@ -209,14 +211,6 @@ abstract class ORMOSB extends ORM {
return empty($mc[$key]) ? '' : $mc[$key]; return empty($mc[$key]) ? '' : $mc[$key];
} }
protected function _where_active() {
return $this->where('status','=',TRUE);
}
public function where_active() {
return $this->_where_active();
}
public function list_active() { public function list_active() {
return $this->_where_active()->find_all(); return $this->_where_active()->find_all();
} }

View File

@ -23,56 +23,57 @@ class Period {
* @param boolean Show dates in 'Y-m-d' format * @param boolean Show dates in 'Y-m-d' format
* @return array * @return array
*/ */
public static function details($type,$weekday=NULL,$start=NULL,$df=FALSE) { public static function details($type,$weekday=NULL,$start=NULL,$df=FALSE,$strict=FALSE) {
// Our precision for the pro-rata percentage
$precision = 4;
// Make the period consistent, eg: Quarterly = Jan-Mar,Apr-Jun; HalfYearly = Jan-Jun,Jul-Dec
$strict = FALSE;
// Round the time integer to a whole day. // Round the time integer to a whole day.
if (is_null($start)) if (is_null($start))
$start = strtotime('today'); $start = strtotime('today');
else else
$start = strtotime(date('Y-m-d',$start)); $start = strtotime(date('Y-m-d',$start));
$inc_months = $used_months = 0;
switch ($type) { switch ($type) {
// Weekly // Weekly
// @todo Make Weekly pro-rata to a day of the week
case 0: case 0:
$period_end = $start+(86400*(7-1)); if ($strict)
return array('start'=>$start,'date'=>$start,'end'=>$period_end,'prorate'=>1); throw new Kohana_Exception('Strict doesnt work here');
# Monthly $period_start = is_null($weekday) ? $start : $start+86400*($weekday-date('w',$start));
$period_end = $period_start+(86400*7);
break;
// Monthly
case 1: case 1:
// NOTE: Strict doesnt do anything here.
$inc_months = 1; $inc_months = 1;
break; break;
# Quarterly // Quarterly
case 2: case 2:
# @todo Make this configurable.
$strict = TRUE;
$inc_months = 3; $inc_months = 3;
break; break;
# Half Yearly // Half Yearly
case 3: case 3:
# @todo Make this configurable.
$strict = TRUE;
$inc_months = 6; $inc_months = 6;
break; break;
# Yearly // Yearly
case 4: case 4:
$inc_months = 12; $inc_months = 12;
break; break;
# Biennial // Biennial
case 5: case 5:
$inc_months = 24; $inc_months = 24;
break; break;
# Triennial // Triennial
case 6: case 6:
if ($strict)
throw new Kohana_Exception('Strict not implemented here');
$inc_months = 36; $inc_months = 36;
break; break;
@ -80,21 +81,24 @@ class Period {
return FALSE; return FALSE;
} }
// If workout a day of week we calculate to. // Workout the period start day
if (is_null($weekday)) if (is_null($weekday))
$weekday = date('d',$start); $weekday = $strict ? 1 : date('d',$start);
$used_months = 0; // We only work our used_months for periods monthly or more.
if ($strict && $type > 0 && $type < 5) if ($inc_months) {
$used_months = $inc_months-(($inc_months-(date('n',$start)%$inc_months))%$inc_months+1); if ($strict)
$used_months = $inc_months-(($inc_months-(date('n',$start)%$inc_months))%$inc_months+1);
$d = mktime(0,0,0,date('m',$start)-$used_months,$weekday,date('y',$start)); $d = mktime(0,0,0,date('m',$start)-$used_months,$weekday,date('y',$start));
if ($d <= $start) if ($d <= $start)
$period_start = $d; $period_start = $d;
else else
$period_start = mktime(0,0,0,date('m',$d)-1-$used_months,$weekday,date('y',$d)); $period_start = mktime(0,0,0,date('m',$d)-1-$used_months,$weekday,date('y',$d));
$period_end = mktime(0,0,0,date('m',$period_start)+$inc_months,$weekday,date('y',$period_start)); // Workout the period end
$period_end = mktime(0,0,0,date('m',$period_start)+$inc_months,$weekday,date('y',$period_start));
}
$total_time = $period_end-$period_start; $total_time = $period_end-$period_start;
$remain_time = $period_end-$start; $remain_time = $period_end-$start;
@ -104,21 +108,21 @@ class Period {
$period_end -= 86400; $period_end -= 86400;
$return = array( $return = array(
'start'=>$period_start, 'start'=>$period_start,
'start_time'=>$start, 'start_time'=>$start,
'date'=>$start, 'date'=>$start,
'end'=>$period_end, 'end'=>$period_end,
'end_time'=>$period_end, 'end_time'=>$period_end,
'weekday'=>$weekday, 'weekday'=>$weekday,
'prorata'=>round($remain_time/$total_time,$precision), 'prorata'=>round($remain_time/$total_time,4),
'total_time'=>sprintf('%3.1f',$total_time/86400), 'total_time'=>sprintf('%3.1f',$total_time/86400),
'remain_time'=>sprintf('%3.1f',$remain_time/86400), 'remain_time'=>sprintf('%3.1f',$remain_time/86400),
'used_time'=>sprintf('%3.1f',$used_time/86400)); 'used_time'=>sprintf('%3.1f',$used_time/86400),
);
// @todo Use the configured data format
if ($df) if ($df)
foreach (array('start','date','end') as $key) foreach (array('start','date','end') as $key)
$return[$key] = date('Y-m-d',$return[$key]); $return[$key] = Config::date($return[$key]);
return $return; return $return;
} }

View File

@ -49,7 +49,6 @@ abstract class StaticListModule extends StaticList {
// Override our argument list as defined in parent // Override our argument list as defined in parent
list($name,$table,$default,$key,$value,$where,$addblank,$attributes) = func_get_args(); list($name,$table,$default,$key,$value,$where,$addblank,$attributes) = func_get_args();
// @todo - our query type should come from our configuration?
$db = DB::select()->from($table); $db = DB::select()->from($table);
foreach ($where as $k=>$v) { foreach ($where as $k=>$v) {

View File

@ -91,7 +91,7 @@ class Controller_Admin_Charge extends Controller_TemplateDefault_Admin {
Script::add(array('type'=>'stdin','data'=>' Script::add(array('type'=>'stdin','data'=>'
$(document).ready(function() { $(document).ready(function() {
$("input[name=account_id]").autocomplete({ $("input[name=account_id]").autocomplete({
source: "'.URL::site('admin/account/autocomplete').'", source: "'.URL::site('admin/account/ajaxlist').'",
minLength: 2, minLength: 2,
change: function(event,ui) { change: function(event,ui) {
// Send the request and update sub category dropdown // Send the request and update sub category dropdown
@ -100,7 +100,7 @@ class Controller_Admin_Charge extends Controller_TemplateDefault_Admin {
data: "aid="+$(this).val(), data: "aid="+$(this).val(),
dataType: "json", dataType: "json",
cache: false, cache: false,
url: "'.URL::site('admin/service/autolist').'", url: "'.URL::site('admin/service/ajaxlist').'",
timeout: 2000, timeout: 2000,
error: function() { error: function() {
alert("Failed to submit"); alert("Failed to submit");
@ -115,7 +115,7 @@ class Controller_Admin_Charge extends Controller_TemplateDefault_Admin {
// Fill sub category select // Fill sub category select
$.each(data, function(i, j){ $.each(data, function(i, j){
var row = "<option value=\"" + j.value + "\">" + j.text + "</option>"; var row = "<option value=\"" + j.value + "\">" + j.label + "</option>";
$(row).appendTo("select[name=service_id]"); $(row).appendTo("select[name=service_id]");
}); });
} }

View File

@ -577,7 +577,7 @@ class Model_Invoice extends ORMOSB {
* @todo This should be optimised a little to return only invoices to send, instead of looking for them. * @todo This should be optimised a little to return only invoices to send, instead of looking for them.
*/ */
public function list_tosend() { public function list_tosend() {
return ORM::factory('invoice')->where('status','=',1)->where_open()->where('print_status','is',NULL)->or_where('print_status','!=',1)->where_close(); return ORM::factory('invoice')->where_active()->where_open()->where('print_status','is',NULL)->or_where('print_status','!=',1)->where_close();
} }
public function html() { public function html() {

View File

@ -16,15 +16,11 @@ class Controller_Admin_Payment extends Controller_TemplateDefault_Admin {
'addbulk'=>TRUE, 'addbulk'=>TRUE,
'list'=>TRUE, 'list'=>TRUE,
'view'=>TRUE, 'view'=>TRUE,
'autocomplete'=>FALSE, 'ajaxlist'=>FALSE,
'autoitemlist'=>FALSE, 'autoitemlist'=>FALSE,
); );
public function action_autocomplete() { public function action_ajaxlist() {
// We are only available via an ajax call.
if (! Request::current()->is_ajax())
die();
$return = array(); $return = array();
if (isset($_REQUEST['term']) AND trim($_REQUEST['term'])) { if (isset($_REQUEST['term']) AND trim($_REQUEST['term'])) {
@ -155,7 +151,7 @@ class Controller_Admin_Payment extends Controller_TemplateDefault_Admin {
} }
}); });
$("input[name=account_id]").autocomplete({ $("input[name=account_id]").autocomplete({
source: "'.URL::site('admin/payment/autocomplete').'", source: "'.URL::site('admin/payment/ajaxlist').'",
minLength: 2, minLength: 2,
change: function(event,ui) { change: function(event,ui) {
// Send the request and update sub category dropdown // Send the request and update sub category dropdown

View File

@ -34,8 +34,7 @@ class Controller_Product_Category extends Controller_TemplateDefault {
*/ */
private function _get_categories() { private function _get_categories() {
return ORM::factory('product_category') return ORM::factory('product_category')
->where('status','=',TRUE) ->list_active();
->find_all();
} }
} }
?> ?>

View File

@ -13,7 +13,7 @@
class Controller_Admin_Service extends Controller_TemplateDefault_Admin { class Controller_Admin_Service extends Controller_TemplateDefault_Admin {
// @todo This "module" menu items should belong in the module dir. // @todo This "module" menu items should belong in the module dir.
protected $secure_actions = array( protected $secure_actions = array(
'autolist'=>FALSE, // @todo To Change 'ajaxlist'=>FALSE, // @todo To Change
'adslstat'=>TRUE, 'adslstat'=>TRUE,
'list'=>TRUE, 'list'=>TRUE,
'listbycheckout'=>TRUE, 'listbycheckout'=>TRUE,
@ -31,23 +31,17 @@ class Controller_Admin_Service extends Controller_TemplateDefault_Admin {
'view'=>TRUE, 'view'=>TRUE,
); );
public function action_autolist() { public function action_ajaxlist() {
$return = array(); $return = array();
$s = ORM::factory('service')->where_active(); $return += ORM::factory('service')->list_autocomplete(
if (isset($_REQUEST['aid'])) isset($_REQUEST['term']) ? $_REQUEST['term'] : '',
$s = $s->where('account_id','=',$_REQUEST['aid']); 'id',
isset($_REQUEST['aid']) ? array(array('account_id','=',$_REQUEST['aid'])) : array());
// @todo This should limit the results so that users dont see other users services.
foreach ($s->find_all() as $so)
array_push($return,array(
'value'=>$so->id,
'text'=>sprintf('%s: %s',$so->id,$so->service_name()),
));
$this->auto_render = FALSE; $this->auto_render = FALSE;
$this->response->headers('Content-Type','application/json'); $this->response->headers('Content-Type','application/json');
$this->response->body(json_encode($return)); $this->response->body(json_encode(array_values($return)));
} }
/** /**
@ -84,7 +78,7 @@ class Controller_Admin_Service extends Controller_TemplateDefault_Admin {
// @todo This needs to be configurable // @todo This needs to be configurable
$go = ORM::factory('group',array('name'=>'Personal')); $go = ORM::factory('group',array('name'=>'Personal'));
foreach (ORM::factory('account')->where('status','=',1)->find_all() as $ao) foreach (ORM::factory('account')->list_active() as $ao)
if ($ao->has_any('group',array($go))) if ($ao->has_any('group',array($go)))
foreach ($ao->service->list_active() as $so) foreach ($ao->service->list_active() as $so)
if (! $so->service_billing->checkout_plugin_id) if (! $so->service_billing->checkout_plugin_id)

View File

@ -54,7 +54,7 @@ class Controller_Affiliate_Service extends Controller_TemplateDefault_Affiliate
// @todo This needs to be configurable // @todo This needs to be configurable
$go = ORM::factory('group',array('name'=>'Personal')); $go = ORM::factory('group',array('name'=>'Personal'));
foreach (ORM::factory('account')->where('status','=',1)->find_all() as $ao) foreach (ORM::factory('account')->list_active() as $ao)
if ($ao->has_any('group',array($go))) if ($ao->has_any('group',array($go)))
foreach ($this->filter($ao->service->list_active(),$this->ao->affiliate->id,'name()') as $so) foreach ($this->filter($ao->service->list_active(),$this->ao->affiliate->id,'name()') as $so)
if (! $so->service_billing->checkout_plugin_id) if (! $so->service_billing->checkout_plugin_id)

View File

@ -143,6 +143,36 @@ class Model_Service extends ORMOSB {
/** LIST FUNCTIONS **/ /** LIST FUNCTIONS **/
/**
* Search for services matching a term
*/
public function list_autocomplete($term,$index='id',array $limit=array()) {
$return = array();
$this->clear();
$this->where_active();
$value = 'service_name()';
// Build our where clause
$this->where_open()
->where('id','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('SVC %s: %s',$o->id,Table::resolve($o,$value)),
);
return $return;
}
public function list_bylistgroup($cat) { public function list_bylistgroup($cat) {
$result = array(); $result = array();

View File

@ -27,7 +27,7 @@ class Controller_User_Statement extends Controller_TemplateDefault_User {
$ta[$i]['payment'] = $o; $ta[$i]['payment'] = $o;
} }
foreach ($this->ao->invoice->where('status','!=',0)->find_all() as $o) { foreach ($this->ao->invoice->list_active() as $o) {
$i = count($ta); $i = count($ta);
$ta[$i]['time'] = $o->date_orig; $ta[$i]['time'] = $o->date_orig;
$ta[$i]['invoice'] = $o; $ta[$i]['invoice'] = $o;