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.
khosb/application/classes/ORM.php

212 lines
5.7 KiB
PHP
Raw Permalink Normal View History

2013-10-10 02:44:53 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
2016-05-01 11:36:37 +00:00
* This class extends Kohana's [ORM] class to create defaults
2013-10-10 02:44:53 +00:00
*
* 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 OSB
2016-05-01 11:36:37 +00:00
* @category Helpers
2013-10-10 02:44:53 +00:00
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
2014-02-23 03:54:35 +00:00
abstract class ORM extends lnApp_ORM {
2016-05-01 11:36:37 +00:00
/**
* @var string Database to connect to
*/
protected $_db = 'default';
2013-05-10 10:48:10 +00:00
// Tables that do not have a site_id column
public static $no_site_id_tables = array('setup','country','currency','language','tax');
2016-05-01 11:36:37 +00:00
// Rules to assist with site ID and getting next record ID for inserts.
public function rules() {
return array(
'id'=>array(
array('ORM::get_next_id',array(':model',':field')),
),
'site_id'=>array(
array('ORM::set_site_id',array(':model',':field')),
),
);
}
2013-05-10 10:48:10 +00:00
/**
* Add our OSB site_id to each SELECT query
* @see parent::__build()
*/
final protected function _build($type) {
// Exclude tables without site ID's
if (! in_array($this->_table_name,ORM::$no_site_id_tables))
$this->where($this->_object_name.'.site_id','=',Company::instance()->site());
return parent::_build($type);
}
2013-10-10 02:44:53 +00:00
2013-11-08 11:02:32 +00:00
/**
* Determine if the account is authoised by the user
*/
public function authorised(Model $o=NULL,Model_Account $ao=NULL,$aid='account_id') {
if (is_null($o))
$o = $this;
2016-05-01 11:36:37 +00:00
2013-11-08 11:02:32 +00:00
if (is_null($ao))
$ao = Auth::instance()->get_user();
return in_array($o->{$aid},$ao->RTM->customers($ao->RTM));
}
2016-05-01 11:36:37 +00:00
public function config($key) {
$mc = Config::instance()->module_config($this->_object_name);
return empty($mc[$key]) ? '' : $mc[$key];
}
2013-10-10 02:44:53 +00:00
/**
* Override KH's ORM count_relations() function, to include our site_id in the query.
*
* This is a copy of KH's ORM count_relations() function, with the addition of a where
* clause to include the site id.
*/
public function count_relations($alias, $far_keys = NULL)
{
if ($far_keys === NULL)
{
return (int) DB::select(array(DB::expr('COUNT(*)'), 'records_found'))
->from($this->_has_many[$alias]['through'])
->where($this->_has_many[$alias]['foreign_key'], '=', $this->pk())
->where('site_id', '=', Company::instance()->site())
->execute($this->_db)->get('records_found');
}
$far_keys = ($far_keys instanceof ORM) ? $far_keys->pk() : $far_keys;
// We need an array to simplify the logic
$far_keys = (array) $far_keys;
// Nothing to check if the model isn't loaded or we don't have any far_keys
if ( ! $far_keys OR ! $this->_loaded)
return 0;
$count = (int) DB::select(array(DB::expr('COUNT(*)'), 'records_found'))
->from($this->_has_many[$alias]['through'])
->where($this->_has_many[$alias]['foreign_key'], '=', $this->pk())
->where($this->_has_many[$alias]['far_key'], 'IN', $far_keys)
->where('site_id', '=', Company::instance()->site())
->execute($this->_db)->get('records_found');
// Rows found need to match the rows searched
return (int) $count;
}
2016-05-01 11:36:37 +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;
}
/**
* Return the module record for this ORM object
*/
final public function mid() {
return ORM::factory('Module',array('name'=>$this->_table_name));
}
/**
* Name value return for the record
2016-08-03 04:00:51 +00:00
*
* @param $variable to enable further processing to determine name, eg: language
*/
2016-08-03 04:00:51 +00:00
public function name($variable=NULL) {
return sprintf('Unknown [%s]',$this->id);
}
/**
* Sub-Name value return for the record
*
* @param $variable to enable further processing to determine name, eg: language
*/
public function namesub($variable=NULL) {
return sprintf('Unknown [%s]',$this->id);
}
2016-08-03 04:00:51 +00:00
/**
* A reference number relating to the object
*/
public function refnum($short=FALSE) {
return ($short ? '' : 'x').sprintf('%06s',$this->id);
}
2016-05-01 11:36:37 +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-10-10 02:44:53 +00:00
/**
* Function help to find records that are active
*/
2016-05-01 11:36:37 +00:00
final public function list_active($active=TRUE) {
2016-08-31 05:03:09 +00:00
$x=($active ? $this->where_active() : $this);
2016-05-01 11:36:37 +00:00
return $x->find_all();
2013-10-10 02:44:53 +00:00
}
2016-08-31 05:03:09 +00:00
/**
* Function help to find records that are active
*/
2016-05-01 11:36:37 +00:00
final public function where_active() {
2016-08-31 05:03:09 +00:00
return $this->where($this->_table_name.'.active','=',TRUE);
}
/**
* Function help to find records that are inactive
*/
final public function where_inactive() {
return $this->where_open()->where($this->_table_name.'.active','=',FALSE)->or_where($this->_table_name.'.active','IS',NULL)->where_close();
2013-10-10 02:44:53 +00:00
}
2016-05-01 11:36:37 +00:00
// @todo This function shouldnt be here.
public function where_authorised(Model_Account $ao=NULL,$aid='account_id') {
if (is_null($ao))
$ao = Auth::instance()->get_user();
return $this->where($aid,'IN',$ao->RTM->customers($ao->RTM));
}
2013-10-10 02:44:53 +00:00
}
?>