<?php
/**
 * @version		$Id: chart.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;

jximport('jxtended.application.component.modelitem');
jximport('jxtended.database.query');

/**
 * About Page Model
 *
 * @package		TAOJ.Quanta
 * @subpackage	com_quanta
 */
class QuantaModelChart extends JxModelItem
{
	/**
	 * Overridden method to lazy load data from the request/session as necessary
	 */
	public function _populateState()
	{
		$pk = JRequest::getInt('chart_id');
		$this->setState('chart.id', $pk);

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

	/**
	 * 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('chart.id');
		$result	= false;

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

			$query->select($this->getState('list.select', 'a.*'));
			$query->from('#__taoj_quanta_charts 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_Chart_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_Chart_not_auth');
				return $result;
			}
			*/
			$user = &JFactory::getUser();
			if ($data->access > $user->get('aid'))
			{
				$this->setError('Quanta_Error_Chart_not_auth');
				return $result;
			}

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

		return $result;
	}
}