<?php
/**
 * @version		$Id: pie.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.modelitem');

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

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

		// Load the parameters.
		$params = &$app->getParams();
		$this->setState('params', $params);
	}

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

		return md5($id);
	}

	/**
	 *
	 */
	public function getRecords()
	{
		$state	= &$this->getState();

		$model	= &JModel::getInstance('Records', 'QuantaModel');
		$select	= 'a.id, a.title, a.alias, a.label, a.maximum_value AS y_max, a.latest_value AS y, a.latest_time AS x, a.params';
		$model->setState('list.select',			$select);
		$model->setState('list.limit',			$state->params->get('limit',	5));
		//$model->setState('filter.published',	1);
		//$model->setState('filter.category_id',	$this->getState('filter.category_id'));
		//$model->setState('access',		$config->get('show_registered') ? null : $user->get('gid'));

		$result	= &$model->getItems();
		if ($result === false) {
			$this->setError($model->getError());
		}

		return $result;
	}
}