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.
lnapp/classes/lnApp/ORM.php

130 lines
3.4 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class overrides Kohana's ORM
*
* 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
* @category Modifications
* @author Deon George
* @copyright (c) 2014 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class lnApp_ORM extends Kohana_ORM {
protected $_table_names_plural = FALSE;
protected $_model_names_plural = FALSE;
private $_object_formated = array();
private $_formated = FALSE;
// Our filters used to display values in a friendly format
protected $_display_filters = array();
// Whether to show a SystemMessage when a record is saved.
protected $_save_message = FALSE;
/**
* Format fields for display purposes
*
* @param string column name
* @return mixed
*/
private function _format() {
foreach ($this->_display_filters as $column => $formats)
$this->_object_formated[$column] = $this->run_filter($column,$this->__get($column),array($column=>$formats));
$this->_formated = TRUE;
}
/**
* Overrides Kohana cache so that it can be globally disabled.
*/
public function cached($lifetime=NULL) {
return $this->_db->caching($this->_table_name) ? parent::cached($lifetime) : $this;
}
/**
* Return a formated columns, as per the model definition
*/
public function display($column) {
// Trigger a load of the record.
$value = $this->__get($column);
// If some of our fields need to be formated for display purposes.
if (! $this->_formated AND $this->_display_filters)
$this->_format();
if (isset($this->_object_formated[$column]))
return $this->_object_formated[$column];
else
return is_array($value) ? join(', ',$value) : $value;
}
public function display_filters(array $filters) {
$this->_display_filters = Arr::merge($this->_display_filters,$filters);
}
public function dump() {
$result = array();
$result['this'] = $this->object();
foreach ($this->_sub_items as $o)
$result['sub'][] = $o->dump();
return $result;
}
/**
* 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->resolve($this->_form['value']);
return $result;
}
/**
* This function is used so that methods can be called via variables
*/
public function resolve($key) {
eval("\$x = \$this->$key;");
return $x;
}
public function save(Validation $validation=NULL) {
parent::save();
if ($this->saved() AND $this->_save_message AND (PHP_SAPI !== 'cli'))
SystemMessage::factory()
->title('Record Updated')
->type('success')
->body(sprintf('Record %s:%s Updated',$this->_table_name,$this->id));
return $this;
}
/**
* We override this function, because we do set our own primary key value
*/
public function values(array $values, array $expected = NULL) {
parent::values($values,$expected);
if (isset($values[$this->_primary_key]))
$this->{$this->_primary_key} = $values[$this->_primary_key];
return $this;
}
}
?>