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)
->where($c,'=',$oldsess);
// @todo There must be a way that ORM can update multiple records with 1 SQL
foreach ($orm->find_all() as $o)
$o->set('session_id',session_id())
->update();

View File

@@ -45,7 +45,7 @@ class Config extends lnApp_Config {
static $return = array();
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 $return;

View File

@@ -12,44 +12,20 @@
*/
class Controller_Admin_Account extends Controller_TemplateDefault_Admin {
protected $secure_actions = array(
'autocomplete'=>FALSE, // @todo To Change
'ajaxlist'=>FALSE, // @todo To Change
'list'=>TRUE,
'listlog'=>TRUE,
);
public function action_autocomplete() {
public function action_ajaxlist() {
$return = array();
$a = ORM::factory('account')->where('status','=',1);
if (isset($_REQUEST['term']) AND trim($_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,
));
if (isset($_REQUEST['term']) AND trim($_REQUEST['term']))
$return += ORM::factory('account')->list_autocomplete($_REQUEST['term']);
$this->auto_render = FALSE;
$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.
* This is because Kohana::$file is cached with the enabled modules
* 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() {
// If caching isnt enabled, we can skip this anyway

View File

@@ -82,6 +82,11 @@ abstract class lnApp_Controller_TemplateDefault extends Controller_Template {
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();
// Check user auth and role

View File

@@ -117,7 +117,7 @@ class Model_Account extends Model_Auth_UserDefault {
// @todo This shouldnt really be required
if ($result < 0)
$result = 0;
throw new Kohana_Exception($result);
return $format ? Currency::display($result) : $result;
}
@@ -133,10 +133,6 @@ class Model_Account extends Model_Auth_UserDefault {
return $alo->saved();
}
private function _where_active() {
return $this->where('status','=',TRUE);
}
public function list_active() {
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
*/
public function list_autocomplete($term,$index='id') {
public function list_autocomplete($term,$index='id',array $limit=array()) {
$return = array();
$this->clear();
$this->where_active();
$value = 'name(TRUE)';
// Build our where clause
@@ -183,13 +180,20 @@ class Model_Account extends Model_Auth_UserDefault {
$value = 'email';
} else {
$this->where('company','like','%'.$term.'%')
$this->where_open()
->where('company','like','%'.$term.'%')
->or_where('first_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)
$return[$o->$index] = array(
'value'=>$o->$index,

View File

@@ -47,7 +47,7 @@ class Model_Group extends Model_Auth_RoleDefault {
if (! $this->loaded())
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);
$return = array_merge($return,$go->list_childgrps());
@@ -69,7 +69,7 @@ class Model_Group extends Model_Auth_RoleDefault {
if (! $this->loaded())
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);
$return = array_merge($return,$go->list_parentgrps());

View File

@@ -127,5 +127,14 @@ class ORM extends Kohana_ORM {
->where('site_id', '=', Config::siteid())
->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.
protected $_form = array();
// Rules to assist with site ID and getting next record ID for inserts.
public function rules() {
return array(
'id'=>array(
array('ORMOSB::get_next_id',array(':validation',':model',':field')),
array('ORMOSB::get_next_id',array(':model',':field')),
),
'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 string Primary Key
*/
// @todo Do we need the $array?
public static function get_next_id(Validation $array,$model,$field) {
public static function get_next_id($model,$field) {
if (! is_null($model->$field))
return TRUE;
@@ -92,8 +92,10 @@ abstract class ORMOSB extends ORM {
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))
return TRUE;
@@ -209,14 +211,6 @@ abstract class ORMOSB extends ORM {
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() {
return $this->_where_active()->find_all();
}

View File

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

View File

@@ -49,7 +49,6 @@ abstract class StaticListModule extends StaticList {
// Override our argument list as defined in parent
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);
foreach ($where as $k=>$v) {