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);
|
|
|
|
|
2012-03-30 04:13:01 +00:00
|
|
|
// Our attribute values that need to be stored as serialized
|
|
|
|
protected $_serialize_column = array();
|
|
|
|
|
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
|
|
|
|
*/
|
2011-10-14 05:44:12 +00:00
|
|
|
// @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;
|
|
|
|
}
|
|
|
|
|
2011-10-14 05:44:12 +00:00
|
|
|
// @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
|
|
|
|
2012-03-30 04:13:01 +00:00
|
|
|
// @todo Change this to be called by array_blob functions
|
2011-10-14 05:44:12 +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);
|
|
|
|
}
|
|
|
|
|
2012-01-01 09:41:42 +00:00
|
|
|
public function __get($column) {
|
2012-03-30 04:13:01 +00:00
|
|
|
if (array_key_exists($column,$this->_table_columns)) {
|
2012-01-02 01:35:47 +00:00
|
|
|
|
2012-03-30 04:13:01 +00:00
|
|
|
// If the column is a blob, we'll decode it automatically
|
|
|
|
if ($this->_table_columns[$column]['data_type'] == 'blob' AND (! isset($this->_table_columns[$column]['auto_convert']) OR ! $this->_table_columns[$column]['auto_convert'])) {
|
|
|
|
|
|
|
|
// In case our blob hasnt been saved as one.
|
|
|
|
try {
|
|
|
|
$this->_object[$column] = $this->blob($this->_object[$column]);
|
|
|
|
}
|
|
|
|
catch(Exception $e) {
|
|
|
|
// @todo Log this exception
|
|
|
|
echo Kohana_Exception::text($e), "\n";
|
|
|
|
echo debug_print_backtrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_table_columns[$column]['auto_convert'] = TRUE;
|
2012-01-02 01:35:47 +00:00
|
|
|
}
|
2012-01-01 09:41:42 +00:00
|
|
|
|
2012-03-30 04:13:01 +00:00
|
|
|
// If the column is a serialized object, we'll unserialize it.
|
|
|
|
if (in_array($column,$this->_serialize_column) AND (! isset($this->_table_columns[$column]['unserialized']) OR ! $this->_table_columns[$column]['unserialized'])) {
|
|
|
|
|
|
|
|
// In case our object hasnt been saved as serialized.
|
|
|
|
try {
|
|
|
|
$this->_object[$column] = unserialize($this->_object[$column]);
|
|
|
|
}
|
|
|
|
catch(Exception $e) {
|
|
|
|
// @todo Log this exception
|
|
|
|
echo Kohana_Exception::text($e), "\n";
|
|
|
|
echo debug_print_backtrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_table_columns[$column]['unserialized'] = TRUE;
|
|
|
|
}
|
2012-01-01 09:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return parent::__get($column);
|
|
|
|
}
|
|
|
|
|
2012-03-30 04:13:01 +00:00
|
|
|
public function keyget($column,$key=NULL) {
|
|
|
|
if (is_null($key) OR ! is_array($this->$column))
|
|
|
|
return $this->$column;
|
|
|
|
else
|
|
|
|
return array_key_exists($key,$this->$column) ? $this->{$column}[$key] : NULL;
|
|
|
|
}
|
|
|
|
|
2011-12-30 07:10:02 +00:00
|
|
|
public function save(Validation $validation = NULL) {
|
|
|
|
// Find any fields that have changed, and that are blobs, and encode them.
|
|
|
|
if ($this->_changed)
|
|
|
|
foreach ($this->_changed as $c)
|
2012-01-02 01:35:47 +00:00
|
|
|
if ($this->_table_columns[$c]['data_type'] == 'blob') {
|
2012-01-09 01:35:24 +00:00
|
|
|
$this->_object[$c] = $this->blob($this->_object[$c],TRUE);
|
2011-12-30 07:10:02 +00:00
|
|
|
|
2012-01-02 01:35:47 +00:00
|
|
|
// We need to reset our auto_convert flag
|
|
|
|
if (isset($this->_table_columns[$c]['auto_convert']))
|
|
|
|
$this->_table_columns[$c]['auto_convert'] = FALSE;
|
|
|
|
}
|
|
|
|
|
2011-12-30 07:10:02 +00:00
|
|
|
return parent::save($validation);
|
|
|
|
}
|
|
|
|
|
2011-12-20 05:46:10 +00:00
|
|
|
public function changed() {
|
|
|
|
return $this->_changed;
|
|
|
|
}
|
2011-12-30 07:10:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve and Store DB BLOB data.
|
|
|
|
*/
|
2012-01-01 09:41:42 +00:00
|
|
|
private function blob($data,$set=FALSE) {
|
2011-12-30 07:10:02 +00:00
|
|
|
return $set ? gzcompress(serialize($data)) : unserialize(gzuncompress($data));
|
|
|
|
}
|
2012-01-02 01:35:47 +00:00
|
|
|
|
|
|
|
public function config($key) {
|
|
|
|
$mc = Config::instance()->so->module_config($this->_object_name);
|
|
|
|
|
|
|
|
return empty($mc[$key]) ? '' : $mc[$key];
|
|
|
|
}
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
?>
|