This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/application/classes/StaticListModule.php

85 lines
2.4 KiB
PHP
Raw Normal View History

2010-11-29 22:41:08 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This is class is renders standard values based on DB table row values.
*
* @package OSB
* @category Helpers
* @author Deon George
2013-03-19 22:35:19 +00:00
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
2010-11-29 22:41:08 +00:00
*/
abstract class StaticListModule extends StaticList {
protected static $record = array();
/**
* Display a static name for a value
*/
public static function display($id) {
// Override our argument list as defined in parent
list($table,$key,$skey,$value) = func_get_args();
$db = DB::select($key)->from($table)->where($skey,'=',$value)->execute();
if ($db->count() !== 1)
return sprintf('No Value (%s)',$value);
else
return $db->get($key);
}
/**
* This function is to return the cached value of the current active record
* This is so that a follow up call to get an attribute of a value retrieved
* can reuse the active record values.
* This gets over a limitation where the query to form() to get a default
* no longer exists (or is invalid) and you want other attributes of the
* remaining active record, which may not be the default record.
*/
public static function record($table,$attribute,$skey,$value) {
if (empty(static::$record[$table]))
return static::display($table,$attribute,$skey,$value);
else
return static::$record[$table][$attribute];
}
/**
* Renders form input
*/
public static function form($name,$default='',$addblank=FALSE) {
// Override our argument list as defined in parent
2011-12-20 05:46:10 +00:00
list($name,$table,$default,$key,$value,$where,$addblank,$attributes) = func_get_args();
2010-11-29 22:41:08 +00:00
$db = DB::select()->from($table);
foreach ($where as $k=>$v) {
list ($op,$v) = explode(':',$v);
$db->where($k,$op,$v);
}
$db = $db->execute();
// If we only have one record, dont make a select list
if ($db->count() == 1) {
static::$record[$table] = $db->as_array();
static::$record[$table] = array_shift(static::$record[$table]);
return Form::hidden($name,$db->get($key)).$db->get($value);
}
// Else we return a select list
$x = array();
2011-12-20 05:46:10 +00:00
if ($addblank)
$x[] = '';
2010-11-29 22:41:08 +00:00
foreach ($db as $record) {
$x[$record[$key]] = $record[$value];
// Save our static record, in case we reference this item again.
if ($record[$key] == $default)
static::$record[$table] = $record;
}
2011-12-20 05:46:10 +00:00
return Form::select($name,$x,$default,$attributes);
2010-11-29 22:41:08 +00:00
}
}
?>