2013-10-10 02:44:53 +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
|
|
|
|
* @category Helpers
|
|
|
|
* @author Deon George
|
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
|
|
|
* @license http://dev.osbill.net/license.html
|
|
|
|
*/
|
|
|
|
abstract class ORM_OSB 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);
|
|
|
|
|
2013-05-10 10:48:10 +00:00
|
|
|
// Our attributes used in forms.
|
|
|
|
protected $_form = array();
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-04-26 01:42:09 +00:00
|
|
|
// Our attributes that should be converted to NULL when empty
|
|
|
|
protected $_nullifempty = array();
|
|
|
|
|
2013-05-10 10:48:10 +00:00
|
|
|
// Our attribute values that need to be stored as serialized
|
|
|
|
protected $_serialize_column = array();
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-09-06 05:39:56 +00:00
|
|
|
// If we need to load any sub items on loading this model
|
|
|
|
protected $_sub_items = array();
|
|
|
|
protected $_sub_items_load = array();
|
|
|
|
protected $_sub_items_sorted = FALSE;
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
// Rules to assist with site ID and getting next record ID for inserts.
|
|
|
|
public function rules() {
|
|
|
|
return array(
|
|
|
|
'id'=>array(
|
|
|
|
array('ORM_OSB::get_next_id',array(':model',':field')),
|
|
|
|
),
|
|
|
|
'site_id'=>array(
|
|
|
|
array('ORM_OSB::set_site_id',array(':model',':field')),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-04-26 01:42:09 +00:00
|
|
|
/**
|
|
|
|
* Retrieve and Store DB BLOB data.
|
|
|
|
*/
|
|
|
|
private function _blob($data,$set=FALSE) {
|
|
|
|
try {
|
|
|
|
return $set ? gzcompress($this->_serialize($data,$set)) : $this->_serialize(gzuncompress($data));
|
|
|
|
|
|
|
|
// Maybe the data isnt compressed?
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return $this->_serialize($data,$set);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
/**
|
|
|
|
* Auto process some data as it comes from the database
|
|
|
|
* @see parent::__get()
|
|
|
|
*/
|
|
|
|
public function __get($column) {
|
|
|
|
if (array_key_exists($column,$this->_table_columns)) {
|
|
|
|
// If the column is a blob, we'll decode it automatically
|
|
|
|
if (
|
|
|
|
$this->_table_columns[$column]['data_type'] == 'blob'
|
|
|
|
AND ! is_null($this->_object[$column])
|
|
|
|
AND ! isset($this->_changed[$column])
|
|
|
|
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 {
|
2013-04-26 01:42:09 +00:00
|
|
|
$this->_object[$column] = $this->_blob($this->_object[$column]);
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
catch(Exception $e) {
|
2013-05-10 10:48:10 +00:00
|
|
|
HTTP_Exception::factory(501,Kohana_Exception::text($e));
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_table_columns[$column]['auto_convert'] = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the column is a serialized object, we'll unserialize it.
|
|
|
|
if (
|
|
|
|
in_array($column,$this->_serialize_column)
|
|
|
|
AND is_string($this->_object[$column])
|
|
|
|
AND ! is_null($this->_object[$column])
|
|
|
|
AND ! isset($this->_changed[$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) {
|
2013-05-10 10:48:10 +00:00
|
|
|
HTTP_Exception::factory(501,Kohana_Exception::text($e));
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_table_columns[$column]['unserialized'] = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::__get($column);
|
|
|
|
}
|
|
|
|
|
2013-09-06 05:39:56 +00:00
|
|
|
/**
|
|
|
|
* Intercept our object load, so that we can load our subitems
|
|
|
|
*/
|
|
|
|
protected function _load_values(array $values) {
|
|
|
|
parent::_load_values($values);
|
|
|
|
|
|
|
|
$sort = FALSE;
|
|
|
|
if ($this->_loaded AND $this->_sub_items_load AND count($this->_sub_items_load) == 1)
|
|
|
|
foreach ($this->_sub_items_load as $item => $sort)
|
|
|
|
$this->_sub_items = $this->$item->find_all()->as_array();
|
|
|
|
|
|
|
|
if ($sort) {
|
|
|
|
Sort::MAsort($this->_sub_items,$sort);
|
|
|
|
$this->_sub_items_sorted = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
/**
|
2013-05-10 10:48:10 +00:00
|
|
|
* If a column is marked to be nullified if it is empty, this is where it is done.
|
2013-10-10 02:44:53 +00:00
|
|
|
*/
|
2013-05-10 10:48:10 +00:00
|
|
|
private function _nullifempty(array $array) {
|
|
|
|
foreach ($array as $k=>$v) {
|
|
|
|
if (is_array($v)) {
|
|
|
|
if (is_null($x=$this->_nullifempty($v)))
|
|
|
|
unset($array[$k]);
|
|
|
|
else
|
|
|
|
$array[$k] = $x;
|
|
|
|
|
2013-11-14 11:50:35 +00:00
|
|
|
} elseif (! $v AND $v !== 0 AND $v !== '0')
|
|
|
|
unset($array[$k]);
|
2013-05-10 10:48:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return count($array) ? $array : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try and (un)serialize our data, and if it fails, just return it.
|
|
|
|
*/
|
|
|
|
private function _serialize($data,$set=FALSE) {
|
|
|
|
try {
|
|
|
|
return $set ? serialize($data) : unserialize($data);
|
|
|
|
|
|
|
|
// Maybe the data serialized?
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return $data;
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function config($key) {
|
|
|
|
$mc = Config::instance()->module_config($this->_object_name);
|
|
|
|
|
|
|
|
return empty($mc[$key]) ? '' : $mc[$key];
|
|
|
|
}
|
|
|
|
|
2013-12-05 05:22:23 +00:00
|
|
|
public function dump() {
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
$result['this'] = $this->object();
|
|
|
|
|
|
|
|
foreach ($this->_sub_items as $o)
|
|
|
|
$result['sub'][] = $o->dump();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
/**
|
|
|
|
* Get Next record id
|
|
|
|
*
|
|
|
|
* @param array Validate object
|
|
|
|
* @param string Primary Key
|
|
|
|
*/
|
|
|
|
final public static function get_next_id($model,$field) {
|
|
|
|
if (! is_null($model->$field))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
$model->_changed[$field] = $field;
|
|
|
|
|
|
|
|
$ido = ORM::factory('Module')
|
|
|
|
->where('name','=',$model->_table_name)
|
|
|
|
->find();
|
|
|
|
|
|
|
|
if (! $ido->loaded())
|
|
|
|
throw new Kohana_Exception('Problem getting record_id for :table',array(':table'=>$model->_table_name));
|
|
|
|
|
|
|
|
$model->$field = $ido->record_id->next_id($ido->id);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-05-14 13:53:04 +00:00
|
|
|
final public function module() {
|
|
|
|
return ORM::factory(Kohana::classname($this->name));
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
final public function mid() {
|
|
|
|
return ORM::factory('Module',array('name'=>$this->_table_name));
|
|
|
|
}
|
|
|
|
|
2013-11-22 04:36:50 +00:00
|
|
|
public function save(Validation $validation=NULL) {
|
2013-10-10 02:44:53 +00:00
|
|
|
// Find any fields that have changed, and process them.
|
|
|
|
if ($this->_changed)
|
2013-04-26 01:42:09 +00:00
|
|
|
foreach ($this->_changed as $c) {
|
2013-10-10 02:44:53 +00:00
|
|
|
// Any fields that are blobs, and encode them.
|
2013-04-26 01:42:09 +00:00
|
|
|
if (! is_null($this->_object[$c]) AND $this->_table_columns[$c]['data_type'] == 'blob') {
|
|
|
|
$this->_object[$c] = $this->_blob($this->_object[$c],TRUE);
|
2013-10-10 02:44:53 +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;
|
|
|
|
|
|
|
|
// Any fields that should be seriailzed, we'll do that.
|
|
|
|
} elseif (is_array($this->_object[$c]) AND in_array($c,$this->_serialize_column)) {
|
|
|
|
$this->_object[$c] = serialize($this->_object[$c]);
|
|
|
|
}
|
2013-11-22 04:36:50 +00:00
|
|
|
|
|
|
|
// Test if the value has still changed
|
|
|
|
if ($this->_original_values AND $this->_object[$c] == $this->_original_values[$c])
|
|
|
|
unset($this->_changed[$c]);
|
2013-04-26 01:42:09 +00:00
|
|
|
}
|
2013-10-10 02:44:53 +00:00
|
|
|
|
|
|
|
return parent::save($validation);
|
|
|
|
}
|
|
|
|
|
2013-05-10 10:48:10 +00:00
|
|
|
/**
|
|
|
|
* Set the site ID attribute for each row update
|
|
|
|
*/
|
|
|
|
final public static function set_site_id($model,$field) {
|
|
|
|
if (! is_null($model->$field))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
$model->_changed[$field] = $field;
|
|
|
|
$model->$field = Company::instance()->site();
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-12-05 05:22:23 +00:00
|
|
|
public function subitems() {
|
|
|
|
return $this->_sub_items;
|
|
|
|
}
|
|
|
|
|
2013-11-14 11:50:35 +00:00
|
|
|
/**
|
|
|
|
* Override the Kohana processing so we can null values if required.
|
|
|
|
*/
|
|
|
|
public function values(array $values,array $expected=NULL) {
|
|
|
|
foreach ($values as $k=>$v) {
|
|
|
|
// Convert to NULL
|
|
|
|
if (in_array($k,$this->_nullifempty)) {
|
|
|
|
if (is_array($v))
|
|
|
|
$values[$k] = $this->_nullifempty($v);
|
|
|
|
|
|
|
|
elseif (! $v AND $v !== 0 AND $v !== '0')
|
|
|
|
$values[$k] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::values($values,$expected);
|
|
|
|
}
|
|
|
|
|
2013-04-26 01:42:09 +00:00
|
|
|
/**
|
|
|
|
* Function help to find records that are active
|
|
|
|
*/
|
2013-11-28 06:41:34 +00:00
|
|
|
public function list_active($active=TRUE) {
|
|
|
|
$x=($active ? $this->_where_active() : $this);
|
|
|
|
|
|
|
|
return $x->find_all();
|
2013-04-26 01:42:09 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
public function list_count($active=TRUE) {
|
|
|
|
$x=($active ? $this->_where_active() : $this);
|
|
|
|
|
|
|
|
return $x->find_all()->count();
|
|
|
|
}
|
2013-04-26 01:42:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public function list_select($blank=FALSE) {
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
if ($blank)
|
|
|
|
$result[] = '';
|
|
|
|
|
|
|
|
if ($this->_form AND array_intersect(array('id','value'),$this->_form))
|
|
|
|
foreach ($this->find_all() as $o)
|
2013-09-06 05:39:56 +00:00
|
|
|
$result[$o->{$this->_form['id']}] = $o->resolve($this->_form['value']);
|
2013-04-26 01:42:09 +00:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
?>
|