2012-12-10 21:48:30 +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
|
2013-03-19 22:35:19 +00:00
|
|
|
* @category Helpers
|
2012-12-10 21:48:30 +00:00
|
|
|
* @author Deon George
|
2013-03-19 22:35:19 +00:00
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
2012-12-10 21:48:30 +00:00
|
|
|
* @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();
|
2012-12-10 21:48:30 +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();
|
2012-12-10 21:48:30 +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-02-08 11:41:29 +00:00
|
|
|
/**
|
|
|
|
* Auto process some data as it comes from the database
|
|
|
|
* @see parent::__get()
|
|
|
|
*/
|
2013-01-15 06:07:54 +00:00
|
|
|
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-01-15 06:07:54 +00:00
|
|
|
}
|
|
|
|
catch(Exception $e) {
|
2013-05-10 10:48:10 +00:00
|
|
|
HTTP_Exception::factory(501,Kohana_Exception::text($e));
|
2013-01-15 06:07:54 +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-01-15 06:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_table_columns[$column]['unserialized'] = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::__get($column);
|
|
|
|
}
|
|
|
|
|
2012-12-10 21:48:30 +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.
|
2012-12-10 21:48:30 +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;
|
|
|
|
|
|
|
|
} else
|
|
|
|
if (! $v)
|
|
|
|
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;
|
2012-12-10 21:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
public function config($key) {
|
2013-02-12 11:14:59 +00:00
|
|
|
$mc = Config::instance()->module_config($this->_object_name);
|
2013-01-15 06:07:54 +00:00
|
|
|
|
|
|
|
return empty($mc[$key]) ? '' : $mc[$key];
|
|
|
|
}
|
|
|
|
|
2012-12-10 21:48:30 +00:00
|
|
|
/**
|
|
|
|
* Get Next record id
|
|
|
|
*
|
|
|
|
* @param array Validate object
|
|
|
|
* @param string Primary Key
|
|
|
|
*/
|
2013-01-15 06:07:54 +00:00
|
|
|
final public static function get_next_id($model,$field) {
|
2012-12-10 21:48:30 +00:00
|
|
|
if (! is_null($model->$field))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
$model->_changed[$field] = $field;
|
|
|
|
|
2012-11-09 23:13:57 +00:00
|
|
|
$ido = ORM::factory('Module')
|
2012-12-10 21:48:30 +00:00
|
|
|
->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-01-15 06:07:54 +00:00
|
|
|
final public function mid() {
|
|
|
|
return ORM::factory('Module',array('name'=>$this->_table_name));
|
|
|
|
}
|
|
|
|
|
2012-12-10 21:48:30 +00:00
|
|
|
public function save(Validation $validation = NULL) {
|
|
|
|
// 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) {
|
|
|
|
// Convert to NULL
|
|
|
|
if (in_array($c,$this->_nullifempty)) {
|
|
|
|
if (is_array($this->_object[$c]))
|
|
|
|
$this->_object[$c] = $this->_nullifempty($this->_object[$c]);
|
|
|
|
|
|
|
|
elseif (! $this->_object[$c])
|
|
|
|
$this->_object[$c] = NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-10 21:48:30 +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);
|
2012-12-10 21:48:30 +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-04-26 01:42:09 +00:00
|
|
|
}
|
2012-12-10 21:48:30 +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-04-26 01:42:09 +00:00
|
|
|
public function status($render=FALSE) {
|
|
|
|
if (! isset($this->_table_columns['status']))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (! $render)
|
|
|
|
return $this->display('status');
|
|
|
|
|
|
|
|
return View::factory(Config::theme().'/status')
|
|
|
|
->set('label',$this->status ? 'label-success' : '')
|
|
|
|
->set('status',$this->display('status'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function help to find records that are active
|
|
|
|
*/
|
|
|
|
public function list_active() {
|
|
|
|
return $this->_where_active()->find_all();
|
|
|
|
}
|
|
|
|
|
2012-12-10 21:48:30 +00:00
|
|
|
public function list_count($active=TRUE) {
|
2013-02-08 11:41:29 +00:00
|
|
|
$x=($active ? $this->_where_active() : $this);
|
2012-12-10 21:48:30 +00:00
|
|
|
|
2013-02-08 11:41:29 +00:00
|
|
|
return $x->find_all()->count();
|
2012-12-10 21:48:30 +00:00
|
|
|
}
|
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)
|
|
|
|
$result[$o->{$this->_form['id']}] = $o->{$this->_form['value']};
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2012-12-10 21:48:30 +00:00
|
|
|
}
|
|
|
|
?>
|