Moved some more common code into lnapp

This commit is contained in:
Deon George
2014-02-17 11:29:11 +11:00
parent 7a78a9a7d6
commit b2912e4007
11 changed files with 190 additions and 6 deletions

View File

@@ -76,6 +76,56 @@ abstract class lnApp_ORM extends Kohana_ORM {
return $result;
}
/**
* This function is our AJAX helper, used by module list_autocomplete()
*/
public function list_autocomplete($term,$index,$value,array $label,array $limit=array(),array $options=NULL) {
$result = array();
$query = empty($options['object']) ? $this : $options['object'];
foreach ($limit as $w) {
list($k,$s,$v) = $w;
$query->and_where($k,$s,$v);
}
$c = 0;
foreach ((empty($options['object']) ? $query->find_all() : $query->execute()) as $o) {
// If we got here via a DB query, we need to reload our ORM object from the result.
if (! is_object($o)) {
if (empty($options['key']))
throw new Kohana_Exception('Missing key for non object');
$o = $this->clear()->where($options['key'],'=',$o[$options['key']])->find();
}
switch ($index) {
case 'url':
if (empty($options['urlprefix']))
throw new Kohana_Exception('Missing URL Prefix');
$v = $options['urlprefix'].$o->resolve($value);
break;
default: $v = $o->resolve($value);
}
$k = '';
foreach ($label as $k => $details)
foreach ($details as $lvalue)
$k = preg_replace('/%s/',$o->resolve($lvalue),$k,1);
$result[$c++] = array(
'value'=>$v,
'label'=>$k,
);
}
return $result;
}
/**
* Return an array of data that can be used in a SELECT statement.
* The ID and VALUE is defined in the model for the select.
@@ -84,7 +134,7 @@ abstract class lnApp_ORM extends Kohana_ORM {
$result = array();
if ($blank)
$result[] = '';
$result[NULL] = '';
if ($this->_form AND array_intersect(array('id','value'),$this->_form))
foreach ($this->find_all() as $o)