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

123 lines
3.2 KiB
PHP
Raw Normal View History

2010-11-29 22:41:08 +00:00
<?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 $_created_column = array('column'=>'date_orig','format'=>TRUE);
protected $_updated_column = array('column'=>'date_last','format'=>TRUE);
2011-05-14 07:35:33 +00:00
public function rules() {
return array(
'id'=>array(
array('ORMOSB::get_next_id',array(':validation',':model',':field')),
),
'site_id'=>array(
array('ORMOSB::set_site_id',array(':validation',':model',':field')),
),
);
2010-11-29 22:41:08 +00:00
}
/**
* 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
2011-05-14 07:35:33 +00:00
* @todo This has probably changed in KH 3.1
2010-11-29 22:41:08 +00:00
*/
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
*/
// @todo Do we need the $array?
2011-05-14 07:35:33 +00:00
public static function get_next_id(Validation $array,$model,$field) {
if (! is_null($model->$field))
2010-11-29 22:41:08 +00:00
return TRUE;
2011-05-14 07:35:33 +00:00
$model->_changed[$field] = $field;
2010-11-29 22:41:08 +00:00
$ido = ORM::factory('module')
2011-05-14 07:35:33 +00:00
->where('name','=',$model->_table_name)
2010-11-29 22:41:08 +00:00
->find();
if (! $ido->loaded())
2011-05-14 07:35:33 +00:00
throw new Kohana_Exception('Problem getting record_id for :table',array(':table'=>$model->_table_name));
2010-11-29 22:41:08 +00:00
2011-05-14 07:35:33 +00:00
$model->$field = $ido->record_id->next_id($ido->id);
2010-11-29 22:41:08 +00:00
return TRUE;
}
// @todo Do we need the $array?
2011-05-14 07:35:33 +00:00
public static function set_site_id(Validation $array,$model,$field) {
if (! is_null($model->$field))
2010-11-29 22:41:08 +00:00
return TRUE;
2011-05-14 07:35:33 +00:00
$model->_changed[$field] = $field;
$model->$field = Config::siteid();
2010-11-29 22:41:08 +00:00
return TRUE;
}
2011-07-14 09:09:03 +00:00
public static function serialize_array(ORM $model,$field,$value) {
if (is_null($value))
return TRUE;
if (! is_array($value))
return FALSE;
$model->_changed[$field] = $field;
$model->$field = serialize($value);
}
2011-07-14 09:09:03 +00:00
/**
* Generate a view path to help View::factory() calls
*
* The purpose of this method is to ensure that we have a consistant
* layout for our view files, including those that are needed by
* plugins
*
* @param string Plugin Name (optional)
*/
public function viewpath($plugin='') {
$request = Request::current();
return $plugin ? sprintf('%s/%s/%s/%s',$request->controller(),$request->directory(),$plugin,$request->action()) : sprintf('%s/%s/%s',$request->controller(),$request->directory(),$request->action());
}
2010-11-29 22:41:08 +00:00
}
?>