<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class extends Kohana's [ORM] class to create defaults for Method and Site aware table.
 *
 * @package    lnAuth
 * @category   Helpers
 * @author     Deon George
 * @copyright  (c) 2014 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
abstract class lnAuth_ORM extends lnApp_ORM {
	// Tables that do not have a site_id column
	public static $no_site_id_tables = array('setup','country','currency','language');

	// 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')),
			),
		);
	}

	/**
	 * 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);
	}

	/**
	 * Determine if the account is authoised by the user
	 */
	public function authorised(Model $o=NULL,Model_Account $ao=NULL,$aid='account_id') {
		return FALSE;

		if (is_null($o))
			$o = $this;
		if (is_null($ao))
			$ao = Auth::instance()->get_user();

		// @todo To implement
		return in_array($o->{$aid},$ao->RTM->customers($ao->RTM));
	}

	/**
	 * 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;
	}

	/**
	 * 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;
	}

	final public function mid() {
		return ORM::factory('Module',array('name'=>$this->_table_name));
	}

	/**
	 * 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;
	}

	public function where_authorised(Model_Account $ao=NULL,$aid='account_id') {
		return $this;

		if (is_null($ao))
			$ao = Auth::instance()->get_user();

		// @todo To implement.
		return $this->where($aid,'IN',$ao->RTM->customers($ao->RTM));
	}
}
?>