<?php
/**
 * @version		$Id: records.php 144 2009-06-27 02:19:12Z 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 QuantaModelRecords extends JxModelList
{
	/**
	 * Model context string.
	 *
	 * @var		string
	 */
	public $_context = 'com_quanta.records';

	/**
	 * 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 	= 1;
		$this->setState('filter.published', $published);

		$recordId = JRequest::getInt('record_id');
		$this->setState('filter.record_id', $recordId);

		$categoryId = JRequest::getInt('category_id');
		$this->setState('filter.category_id', $categoryId);

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

		$limitstart = JRequest::getInt('limitstart', 0);
		$this->setState('list.limitstart', $limitstart);

		$orderCol	= JRequest::getVar('filter_order', 'a.title');
		$this->setState('list.ordering', $orderCol);

		$orderDirn	= JRequest::getVar('filter_order_Dir', 'ASC');
		$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.record_id');
		$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_records AS a');

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

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

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

		// 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_',$query->toString()));
		return $query;
	}

	/**
	 * Override getItems to parse the params field.
	 *
	 * @return	mixed	An array of objects on success, false on failure.
	 */
	function &getItems()
	{
		if ($items = &parent::getItems())
		{
			foreach ($items as &$item)
			{
				// Convert the params field to a JObject.
				if (isset($item->params))
				{
					$temp = json_decode($item->params, true);
					$item->params = new JObject;
					$item->params->setProperties((array) $temp);
				}
			}
		}

		return $items;
	}
}