<?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_scatters
 */
class QuantaModuleViewScatters 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 = false;

	/**
	 * 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->records	= &$this->getRecords();
		$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 &getRecords()
	{
		$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.params';
		$model->setState('list.select',			$select);
		$model->setState('list.limit',			0);
		$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'));

		$order	= $this->params->get('order');

		switch ($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());
		}

		// Create the reverse lookup on record id.
		$records = array();
		foreach ($result as &$record) {
			$records[$record->id] = &$record;
		}

		return $records;
	}

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

		$model	= &JModel::getInstance('History', 'QuantaModel', array('ignore_request' => true));
		$select	= 'a.record_id AS set_id, a.record_value AS y, a.record_value2 AS y2, a.record_time AS x';
		$model->setState('list.select',			$select);
		//$model->setState('list.limit',			0);
		$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'));

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

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

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

		$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.scatter', JPATH_QUANTA.DS.'libraries');

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

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

		return $chart;
	}
}
