Major updates from other projects
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
* This file contains enhancements for Kohana, that should be considered upstream and maybe havent been yet.
|
||||
* It also contains some functionality for OSB, which cannot be covered in ORM_OSB.
|
||||
*
|
||||
* @package WWZ
|
||||
* @package lnApp
|
||||
* @category Modifications
|
||||
* @author Deon George
|
||||
* @copyright (c) 2014 Deon George
|
||||
@@ -18,12 +18,95 @@ abstract class lnApp_ORM extends Kohana_ORM {
|
||||
private $_object_formated = array();
|
||||
private $_formated = FALSE;
|
||||
|
||||
protected $_created_column = array('column'=>'date_orig','format'=>TRUE);
|
||||
protected $_updated_column = array('column'=>'date_last','format'=>TRUE);
|
||||
|
||||
// Our filters used to display values in a friendly format
|
||||
protected $_display_filters = array();
|
||||
|
||||
// Our attributes used in forms.
|
||||
protected $_form = array();
|
||||
|
||||
// Our attribute blobs that should be compressed
|
||||
protected $_compress_column = array();
|
||||
|
||||
// Our attributes that should be converted to NULL when empty
|
||||
protected $_nullifempty = array();
|
||||
|
||||
// Our attribute values that need to be stored as serialized
|
||||
protected $_serialize_column = array();
|
||||
|
||||
// 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;
|
||||
|
||||
// Whether to show a SystemMessage when a record is saved.
|
||||
protected $_save_message = FALSE;
|
||||
|
||||
/**
|
||||
* 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 in_array($column,$this->_compress_column)
|
||||
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 {
|
||||
$this->_object[$column] = $this->_blob($this->_object[$column]);
|
||||
}
|
||||
catch(Exception $e) {
|
||||
HTTP_Exception::factory(501,Kohana_Exception::text($e));
|
||||
}
|
||||
|
||||
$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) {
|
||||
HTTP_Exception::factory(501,Kohana_Exception::text($e));
|
||||
}
|
||||
|
||||
$this->_table_columns[$column]['unserialized'] = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
return parent::__get($column);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve and Store DB BLOB data in compressed format.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Format fields for display purposes
|
||||
*
|
||||
@@ -37,6 +120,57 @@ abstract class lnApp_ORM extends Kohana_ORM {
|
||||
$this->_formated = TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* If a column is marked to be nullified if it is empty, this is where it is done.
|
||||
*/
|
||||
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;
|
||||
|
||||
} elseif (! $v AND $v !== 0 AND $v !== '0')
|
||||
unset($array[$k]);
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides Kohana cache so that it can be globally disabled.
|
||||
*/
|
||||
@@ -164,6 +298,27 @@ abstract class lnApp_ORM extends Kohana_ORM {
|
||||
}
|
||||
|
||||
public function save(Validation $validation=NULL) {
|
||||
// Find any fields that have changed, and process them.
|
||||
if ($this->_changed)
|
||||
foreach ($this->_changed as $c) {
|
||||
// Any fields that are blobs, and encode them.
|
||||
if (! is_null($this->_object[$c]) AND $this->_table_columns[$c]['data_type'] == 'blob' AND in_array($c,$this->_compress_column)) {
|
||||
$this->_object[$c] = $this->_blob($this->_object[$c],TRUE);
|
||||
|
||||
// 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]);
|
||||
}
|
||||
|
||||
// Test if the value has still changed
|
||||
if ($this->_original_values AND $this->_object[$c] == $this->_original_values[$c])
|
||||
unset($this->_changed[$c]);
|
||||
}
|
||||
|
||||
parent::save();
|
||||
|
||||
if ($this->saved() AND $this->_save_message AND (PHP_SAPI !== 'cli'))
|
||||
@@ -175,10 +330,26 @@ abstract class lnApp_ORM extends Kohana_ORM {
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function subitems() {
|
||||
return $this->_sub_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the Kohana processing so we can null values if required.
|
||||
* We override this function, because we do set our own primary key value
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
parent::values($values,$expected);
|
||||
|
||||
if (isset($values[$this->_primary_key]))
|
||||
@@ -186,5 +357,16 @@ abstract class lnApp_ORM extends Kohana_ORM {
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function what_changed() {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->changed() as $k) {
|
||||
$result[$k]['old'] = ($x=Arr::get($this->_original_values,$k)) ? $x : serialize($x);
|
||||
$result[$k]['new'] = ($x=Arr::get($this->_object,$k)) ? $x : serialize($x);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
Reference in New Issue
Block a user