<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.view');
class ARTIOFusionChartsViewARTIOFusionChart extends JView
{
    function display ($tpl = null)
    {
        $chart = $this->get('data');
        $config = JComponentHelper::getParams(ARTIOFUSIONCHARTS_OPTION);
        if ($chart->id == 0 && JRequest::getCmd('task') == 'add') { //new
            $chart->category = JRequest::getString('type');
            $chart->type = JRequest::getString('chart');
            $chart->width = (int) $config->get('new_chart_width', 500);
            $chart->height = (int) $config->get('new_chart_height', 300);
            $chart->created = null;
            $chart->created_by = null;
            $chart->modified = null;
            $chart->modified_by = null;
            $chart->categories = array();
            $chart->datasets = array();
        }
        $chart = ARTIOFusionChartsHelper::loadChart($chart->type, $chart->category, $chart);
        $model = &$this->getModel();
        $model->getTrendLines($chart);
        if ($chart->chartType == 1) {
            $model->getDataSingle($chart);
            $chart->xml = ARTIOFusionChartsHelper::getXMLSingle($chart);
        }
        if ($chart->chartType == 2) {
            $model->getDataMulti($chart);
            $chart->xml = ARTIOFusionChartsHelper::getXMLMulti($chart);
        }
        $categories = ARTIOFusionChartsHelper::loadCategories();
        $optCategories = array();
        $optCategories[] = JHTML::_('select.option', '0', '- ' . JText::_('Select category') . ' -', 'id', 'type');
        $lists_charts = array();
        foreach ($categories as $item) {
            $optCategories[] = JHTML::_('select.option', $item->base, $item->name, 'id', 'type');
            $charts = array();
            $charts[] = JHTML::_('select.option', '0', '- ' . JText::_('Select Chart') . ' -', 'id', 'type');
            $names = ARTIOFusionChartsHelper::getChartsNamesArray($item);
            foreach ($names as $i => $name) {
                $charts[] = JHTML::_('select.option', $i, $name, 'id', 'type');
            }
            $style = $item->base != $chart->category ? 'style="display: none"' : '';
            $name = $item->base != $chart->category ? '' : 'type';
            $lists_charts[$item->base] = JHTML::_('select.genericlist', $charts, $name, 'class="inputbox" size="1" ' . $style, 'id', 'type', strval($chart->type), $item->base);
        }
        $lists['categories'] = JHTML::_('select.genericlist', $optCategories, 'category', 'class="inputbox" size="1" onchange="Chart.changeCharts()"', 'id', 'type', strval($chart->category));
        if ($chart->id != 0) {
            $user = &JFactory::getUser($chart->created_by);
            $chart->created_by = $user;
            $chart->created_by->link = ARTIOFusionChartsHelperRoute::getUserRoute($user->id);
            if ($chart->modified_by != 0) {
                $user = &JFactory::getUser($chart->modified_by);
                $chart->modified_by = $user;
                $chart->modified_by->link = ARTIOFusionChartsHelperRoute::getUserRoute($user->id);
            } else {
                $chart->modified_by = null;
            }
            $chart->created = JHTML::_('date', $chart->created, JText::_('DATE_FORMAT_LC2'));
            $db = &JFactory::getDBO();
            if ($chart->modified != $db->getNullDate()) {
                $chart->modified = JHTML::_('date', $chart->modified, JText::_('DATE_FORMAT_LC2'));
            } else {
                $chart->modified = null;
            }
        }
        ARTIOFusionChartsHelper::informAboutCacheDir();
        $chart->title = htmlspecialchars( $chart->title, ENT_QUOTES, 'UTF-8' );
        $this->assignRef('lists_charts', $lists_charts);
        $this->assignRef('lists', $lists);
        $this->assignRef('chart', $chart);
        $this->assignRef('config', $config);
        parent::display($tpl);
    }
}
?>