<?php
/**
 * @version		$Id: view.php 135 2009-06-25 04:32:04Z 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;

/**
 * Module View
 *
 * @package		TAOJ.Quanta
 * @subpackage	mod_quanta_pie
 */
class QuantaModuleViewPie extends JObject
{
	/**
	 * @var object
	 */
	protected $baseurl = null;

	/**
	 * @var object
	 */
	protected $params = null;

	/**
	 * @var object
	 */
	protected $module = null;

	/**
	 * @var object
	 */
	protected $items = null;

	/**
	 * @var object
	 */
	protected $chart = null;

	/**
	 * @var boolean
	 */
	protected $debug = true;

	/**
	 * Constructor
	 *
	 * @param	object	$module
	 * @param	obejct	$params
	 */
	function __construct(&$module, &$params)
	{
		// Include dependancies.
		jximport('joomla.application.component.model');
		jximport('jxtended.database.query');
		JModel::addIncludePath(JPATH_SITE.DS.'components'.DS.'com_quanta'.DS.'models');

		$this->baseurl = JURI::base(true);
		$this->module	= &$module;
		$this->params	= &$params;
		$this->items	= &$this->getItems();
		$this->chart	= &$this->getChart();

		// Check for errors.
		if (count($errors = $this->getErrors())) {
			JError::raiseError(500, implode("\n", $errors));
			return false;
		}

		$this->debug	= $this->params->get('debug');
	}

	/**
	 * Display the module layout.
	 */
	function display()
	{
		require JModuleHelper::getLayoutPath($this->module->module, $this->params->get('layout', 'default'));
	}

	/**
	 * Get a list of the records to display in a category
	 */
	function &getItems()
	{
		// Initialise variables.
		$app	= &JFactory::getApplication();
		$offset	= $app->getCfg('offset');

		$model	= &JModel::getInstance('Records', 'QuantaModel', array('ignore_request' => true));
		$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',			$this->params->get('limit',	5));
		$model->setState('filter.published',	1);
		$model->setState('filter.category_id',	$this->params->get('category_id'));
		//$model->setState('access',		$config->get('show_registered') ? null : $user->get('gid'));

		switch ($this->params->get('order'))
		{
			case 'title_asc':
				$model->setState('list.ordering',	'a.title');
				$model->setState('list.direction',	'ASC');
				break;
		}

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

		return $result;
	}

	/**
	 * Get the chart
	 *
	 * @return	object
	 */
	function &getChart()
	{
		// Load the chart class.
		JLoader::import('chart.pie', JPATH_QUANTA.DS.'libraries');

		// Inital the chart, data and parameters.
		$chart = new JChartPie($this->items, $this->params);

		// Set the chart sub-type.
		$chart->setSubType($this->params->get('chart-subtype'));

		return $chart;
	}
}
