2013-10-10 02:44:53 +00:00
|
|
|
<?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 OSB
|
|
|
|
* @category Modifications
|
|
|
|
* @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 {
|
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');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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());
|
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
// Ensure we Cache our queries
|
2015-09-29 01:54:45 +00:00
|
|
|
// @todo: Disabled as it is not working as expected
|
|
|
|
/*
|
2013-06-19 09:26:07 +00:00
|
|
|
$caching = FALSE;
|
|
|
|
foreach ($this->_db_pending as $method)
|
|
|
|
if ($method['name'] == 'cached') {
|
|
|
|
$caching = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $caching)
|
|
|
|
$this->cached(Kohana::$config->load('cache.orm.'.$this->_table_name));
|
2015-09-29 01:54:45 +00:00
|
|
|
*/
|
2013-06-19 09:26:07 +00:00
|
|
|
|
2013-05-10 10:48:10 +00:00
|
|
|
return parent::_build($type);
|
|
|
|
}
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-05-10 10:48:10 +00:00
|
|
|
/**
|
|
|
|
* Function help to find records that are active
|
|
|
|
*/
|
|
|
|
protected function _where_active() {
|
|
|
|
return $this->where('status','=',TRUE);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
if (is_null($ao))
|
|
|
|
$ao = Auth::instance()->get_user();
|
|
|
|
|
|
|
|
return in_array($o->{$aid},$ao->RTM->customers($ao->RTM));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function help to find records that are active
|
|
|
|
*/
|
|
|
|
public function list_active() {
|
|
|
|
return $this->_where_active()->find_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function where_active() {
|
|
|
|
return $this->_where_active();
|
|
|
|
}
|
2013-10-09 05:43:41 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
?>
|