<?php
/**
 * @version		$Id$
 * @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 QuantaModelHistories extends JxModelList
{
	/**
	 * Model context string.
	 *
	 * @var		string
	 */
	public $_context = 'com_quanta.histories';

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

		$search = $app->getUserStateFromRequest($this->_context.'.search', 'filter_search');
		$this->setState('filter.search', $search);

		$published = $app->getUserStateFromRequest($this->_context.'.published', 'filter_published', '');
		$this->setState('filter.published', $published);

		$recordId = $app->getUserStateFromRequest($this->_context.'.record_id', 'filter_record_id');
		$this->setState('filter.record_id', $recordId);

		$categoryId = $app->getUserStateFromRequest($this->_context.'.category_id', 'filter_category_id');
		$this->setState('filter.category_id', $categoryId);

		// 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.search');
		$id	.= ':'.$this->getState('filter.published');
		$id	.= ':'.$this->getState('filter.record_id');
		$id	.= ':'.$this->getState('filter.category_id');

		return md5($id);
	}

	/**
	 * Builds an SQL query to load the list data.
	 *
	 * @return	JQuery	A query object.
	 */
	function _getListQuery()
	{
		$query = new JxQuery;

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

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

		// Join on the records.
		$query->select('r.title AS record_title');
		$query->join('INNER', '#__taoj_quanta_records AS r on r.id = a.record_id');

		// Join on the 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);
		}
		else if ($published === '') {
			$query->where('(a.published = 0 OR a.published = 1)');
		}

		// Filter by record id.
		if ($recordId = (int) $this->getState('filter.record_id')) {
			$query->where('a.record_id = '.$recordId);
		}

		// Filter by category id.
		if ($categoryId = (int) $this->getState('filter.category_id')) {
			$query->where('r.category_id = '.$categoryId);
		}

		// Filter by search in title
		$search = $this->getState('filter.search');
		if (!empty($search)) {
			$search = $this->_db->Quote('%'.$this->_db->getEscaped($search, true).'%', false);
			$query->where('(r.title LIKE '.$search.')');
		}

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

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