<?php
/**
 * @version		$Id: history.php 143 2009-06-25 11:37:24Z eddieajau $
 * @copyright	Copyright (C) 2009 New Life in IT Pty Ltd. All rights reserved.
 * @license		GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
 * @link		http://www.theartofjoomla.com
 */

// no direct access
defined('_JEXEC') or die;

jximport('jxtended.application.component.modellist');

/**
 * About Page Model
 *
 * @package		TAOJ.Quanta
 * @subpackage	com_quanta
 */
class QuantaModelHistory extends JxModelList
{
	/**
	 * Model context string.
	 *
	 * @var		string
	 */
	public $_context = 'com_quanta.history';

	/**
	 * Overridden method to lazy load data from the request/session as necessary
	 *
	 * @access	public
	 * @param	string	$key		The key of the state item to return
	 * @param	mixed	$default	The default value to return if it does not exist
	 * @return	mixed	The requested value by key
	 * @since	1.0
	 */
	function _populateState()
	{
		$app = &JFactory::getApplication();

		$published 	= $app->getUserStateFromRequest($this->_context.'.published', 'published', 1);
		$this->setState('filter.published', ($published == '*' ? null : $published));

		// List state information
		$limit 		= $app->getUserStateFromRequest('global.list.limit', 'limit', $app->getCfg('list_limit'));
		$this->setState('list.limit', $limit);

		$limitstart = $app->getUserStateFromRequest($this->_context.'.limitstart', 'limitstart', 0);
		$this->setState('list.limitstart', $limitstart);

		$orderCol	= $app->getUserStateFromRequest($this->_context.'.ordercol', 'filter_order', 'a.record_time');
		$this->setState('list.ordering', $orderCol);

		$orderDirn	= $app->getUserStateFromRequest($this->_context.'.orderdirn', 'filter_order_Dir', 'desc');
		$this->setState('list.direction', $orderDirn);
	}

	/**
	 * Method to get a store id based on model configuration state.
	 *
	 * This is necessary because the model is used by the component and
	 * different modules that might need different sets of data or different
	 * ordering requirements.
	 *
	 * @param	string		$id	A prefix for the store id.
	 *
	 * @return	string		A store id.
	 */
	public function _getStoreId($id = '')
	{
		// Compile the store id.
		$id	.= ':'.$this->getState('list.start');
		$id	.= ':'.$this->getState('list.limit');
		$id	.= ':'.$this->getState('list.ordering');
		$id	.= ':'.$this->getState('list.direction');
		$id	.= ':'.$this->getState('filter.published');
		$id	.= ':'.$this->getState('filter.category_id');

		return md5($id);
	}

	/**
	 * @param	boolean	True to join selected foreign information
	 *
	 * @return	string
	 */
	function _getListQuery($resolveFKs = true)
	{
		$query = new JxQuery;

		$query->select($this->getState('list.select', 'a.*'));

		$query->from('#__taoj_quanta_history AS a');

		// Join on records.
		//$query->select('r.title AS set_title, r.maximum_value AS y_max');
		$query->join('LEFT', '#__taoj_quanta_records AS r ON r.id=a.record_id');

		// Join on jos_categories.
		//$query->select('c.title AS category_title');
		//$query->join('LEFT', '#__categories AS c ON c.id=r.category_id');

		// Filter by published state.
		$published = $this->getState('filter.published');
		if (is_numeric($published))
		{
			$query->where('a.published = ' . (int) $published);
			$query->where('r.published = ' . (int) $published);
		}

		// Filter by record and/or category.
		$recordId = $this->getState('filter.record_id');
		$categoryId = $this->getState('filter.category_id');
		if ($recordId && $categoryId) {
			$query->where('(r.id = '.(int) $recordId.' OR r.category_id = '.(int) $categoryId.')');
		}
		else if ($recordId) {
			$query->where('r.id = '.(int) $recordId);
		}
		else if ($categoryId) {
			$query->where('r.category_id = '.(int) $categoryId);
		}

		// Filter by age.
		if ($startTime = $this->getState('filter.start_time')) {
			$query->where('a.record_time >= '.$this->_db->quote($startTime));
		}

		// Add the list ordering clause.
		$query->order($this->_db->getEscaped($this->getState('list.ordering', 'a.record_time')).' '.$this->_db->getEscaped($this->getState('list.direction', 'ASC')));

		//echo nl2br(str_replace('#__','jos_',$query->toString()));
		return $query;
	}
}