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/ormosb.php
2011-05-03 09:49:04 +10:00

154 lines
3.9 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class extends Kohana's [ORM] class to create defaults for OSB.
*
* @package OSB
* @subpackage Core
* @category ORM
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
abstract class ORMOSB extends ORM {
/**
* @var string Database to connect to
*/
protected $_db = 'default';
protected $_object_formated = array();
protected $_formated = FALSE;
protected $_formats = array();
/**
* @var boolean Database names plural configuration
*/
protected $_table_names_plural = false;
protected $_created_column = array('column'=>'date_orig','format'=>TRUE);
protected $_updated_column = array('column'=>'date_last','format'=>TRUE);
protected $_callbacks = array(
'id'=>array('get_next_id'),
'site_id'=>array('set_site_id'),
);
/**
* Format fields for display purposes
*
* @param string column name
* @return mixed
*/
protected function _format() {
$format = Validate::factory($this->_object);
foreach ($this->_formats as $column => $formats)
$format->filters($column,$formats);
if ($format->check())
foreach ($format as $column => $value)
$this->_object_formated[$column] = $value;
$this->_formated = TRUE;
}
/**
* Return a formated columns, as per the model definition
*/
public function display($column) {
// Trigger a load of the record.
$value = $this->__get($column);
// If some of our fields need to be formated for display purposes.
if ($this->_loaded AND ! $this->_formated AND $this->_formats)
$this->_format();
if (isset($this->_object_formated[$column]))
return $this->_object_formated[$column];
else
return $value;
}
/**
* Our child models should provide an invoice display, this is shown
* on printed invoices.
*/
public function invoice_display() {
throw new Kohana_Exception(':module has not configured an :method, but has made the call',array(':module'=>get_class($this),'method'=>__METHOD__));
}
/**
* Override the _load_result() function so that our site ID is automatically
* added to the SQL query
* @todo This is not picked up by all queries. Need to investigate why
* @todo This is not being done by inserts
*/
protected function _load_result($multiple = FALSE)
{
$this->_db_builder->where($this->_table_name.'.site_id','=',Config::siteid());
return parent::_load_result($multiple);
}
/**
* This function will enhance the [Validate::filter], since it always passes
* the value as the first argument and sometimes functions need that to not
* be the first argument.
*
* Currently this implements:
* [date()][date-ref]
*
* [date-ref]: http://www.php.net/date
*
* This function will throw an exception if called without a function
* defined.
*
* @param mixed $val Value to be processed
* @param string $func Name of function to call
* @param string $arg Other arguments for the function
*/
final public static function _filters($val,$func,$arg) {
switch ($func) {
case 'date':
return date($arg,$val);
default:
throw new Exception(sprintf(_('Unknown function: %s (%s,%s)'),$func,$arg,$val));
}
}
/**
* Get Next record id
*
* @param array Validate object
* @param string Primary Key
*/
public function get_next_id(Validate $array,$field) {
if (! is_null($array[$field]))
return TRUE;
$this->_changed[$field] = $field;
$ido = ORM::factory('module')
->where('name','=',$this->_table_name)
->find();
if (! $ido->loaded())
throw new Kohana_Exception('Problem getting record_id for :table',array(':table'=>$this->_table_name));
$array[$field] = $ido->record_id->next_id($ido->id);
return TRUE;
}
public function set_site_id(Validate $array,$field) {
if (! is_null($array[$field]))
return TRUE;
// @todo This should be a config item
$this->_changed[$field] = $field;
$array[$field] = Config::siteid();
return TRUE;
}
}
?>