<?php
/**
 * @version		$Id: record.php 152 2009-06-29 06:53:00Z 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;

require_once dirname(__FILE__).DS.'history.php';

/**
 * About Page Model
 *
 * @package		TAOJ.Quanta
 * @subpackage	com_quanta
 */
class QuantaModelRecord extends QuantaModelHistory
{
	/**
	 * Overridden method to lazy load data from the request/session as necessary
	 */
	public function _populateState()
	{
		parent::_populateState();

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

		// Configure the select for the history
		$select	= 'a.record_id AS set_id, a.record_value AS y, a.record_value2 AS y2, a.record_time AS x';
		$this->setState('list.select', $select);

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

		//$model->setState('list.limit',	0);
		$this->setState('filter.published', 1);

		$select = 'a.id, a.title, a.alias, a.label, a.maximum_value AS y_max, a.params, a.description, a.published, a.access';
		$this->setState('item.select', $select);

		// Load the parameters.
		$params = JComponentHelper::getParams('com_quanta');
		$this->setState('params', $params);

		// Limit for this module is time based.
		if ($limit = (int) $params->get('limit'))
		{
			$now = JFactory::getDate();
			$unix = $now->toUnix() - ($limit * 86400);
			$startDate = new JDate($unix);

			$this->setState('filter.start_time', $startDate->toMySQL());
		}


	}

	/**
	 * Method to get an ojbect.
	 *
	 * @param	integer	The id of the object to get.
	 * @return	mixed	Object on success, false on failure.
	 */
	public function &getItem($pk = null)
	{
		// Initialize variables.
		$pk		= (!empty($pk)) ? $pk : (int) $this->getState('record.id');
		$result	= false;

		if ($pk)
		{
			$query = new JxQuery;

			$query->select($this->getState('item.select', 'a.*'));
			$query->from('#__taoj_quanta_records AS a');
			$query->where('id = '.(int) $pk);

			$this->_db->setQuery($query->toString());

			$data	= $this->_db->loadObject();

			if ($error = $this->_db->getErrorMsg())
			{
				$this->setError($error);
				return $result;
			}

			// Check for published state.
			if ($data->published < 1)
			{
				$this->setError('Quanta_Error_Record_not_found');
				return $result;
			}

			// Check access control
			/*jximport('jxtended.acl.acl');
			$allowed = JxAcl::getAllowedAssetGroups('core', 'view', null, false);
			if (!in_array($data->access, $allowed))
			{
				$this->setError('Quanta_Error_Record_not_auth');
				return $result;
			}*/
			$user = &JFactory::getUser();
			if ($data->access > $user->get('aid'))
			{
				$this->setError('Quanta_Error_Chart_not_auth');
				return $result;
			}

			// Convert the params field to a JObject.
			if (isset($data->params))
			{
				$temp = json_decode($data->params, true);
				$data->params = new JObject;
				$data->params->setProperties((array) $temp);
			}

			$result = $data;
		}
		else {
			$this->setError('Quanta_Error_Record_not_found');
		}

		return $result;
	}

}