<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.view');
/**
 * Charts browse page.
 *
 * @version    1.0.0
 * @author 	   ARTIO s.r.o., http://www.artio.net
 * @package    Charts
 * @subpackage Views
 * @copyright  Copyright (C) 2009 ARTIO s.r.o.. All rights reserved.
 * @since      Version 1.0.0
 */
class ARTIOFusionChartsViewARTIOFusionCharts extends JView
{
    function display ($tpl = null)
    {
        $mainframe = &JFactory::getApplication();
        $lists = array();
        $option = JRequest::getString('option');
        $filter_order = $mainframe->getUserStateFromRequest($option . 'filter_order', 'filter_order', 'id', 'cmd');
        $filter_order_Dir = $mainframe->getUserStateFromRequest($option . 'filter_order_Dir', 'filter_order_Dir', 'desc', 'word');
        $lists['order_Dir'] = $filter_order_Dir;
        $lists['order'] = $filter_order;
        //search filter
        $search = $mainframe->getUserStateFromRequest($option . 'search', 'search', '', 'string');
        $search = JString::strtolower($search);
        //category filter
        $category = $mainframe->getUserStateFromRequest($option . 'filter_categories', 'filter_categories', '', 'string');
        //type filter
        $type = $mainframe->getUserStateFromRequest($option . 'filter_types', 'filter_types', '', 'string');
        $items = & $this->get('Data');
        $pagination = & $this->get('Pagination');
        $lists['search'] = $search;
        $categories = ARTIOFusionChartsHelper::loadCategories();
        $categoriesOptions = array();
        $categoriesOptions[] = JHTML::_('select.option', '0', '- ' . JText::_('Select Category') . ' -', 'base', 'name');
        $categoriesOptions = array_merge($categoriesOptions, $categories);
        $lists['filter_categories'] = JHTML::_('select.genericlist', $categoriesOptions, 'filter_categories', 'class="inputbox" size="1" ' . 'onchange="ChartsList.submit();"', 'base', 'name', strval($category));
        $typesOptions = array();
        $typesOptions[] = JHTML::_('select.option', '0', '- ' . JText::_('Select Type') . ' -', 'id', 'name');
        foreach ($categories as $item) {
            if ($category && $category != $item->base) {
                continue;
            }
            foreach ($item->charts as $chart) {
                $typesOptions[] = JHTML::_('select.option', $chart->base, $chart->name, 'id', 'name');
            }
        }
        $params = 'onchange="document.adminForm.submit()"';
        if (! $category) {
            $params .= ' disabled="disabled"';
        }
        $lists['filter_types'] = JHTML::_('select.genericlist', $typesOptions, 'filter_types', 'class="inputbox" size="1" ' . $params, 'id', 'name', strval($type));
        $categoryNames = ARTIOFusionChartsHelper::getCategoryNamesArray($categories);
        $typesNames = ARTIOFusionChartsHelper::getTypesNamesArray($categories);
        $user = &JFactory::getUser();
        $config = &JFactory::getConfig();
        $offset = $config->getValue('config.offset');
        $db = &JFactory::getDBO();
        foreach ($items as $i => $item) {
            $item->checked = JHTML::_('grid.checkedout', $item, $i);
            $item->link = ARTIOFusionChartsHelperRoute::getEditRoute($item->id);
            $item->offset = $pagination->getRowOffset($i);
            $item->isChecked = JTable::isCheckedOut($user->get('id'), $item->checked_out);
            $item->title = $this->escape($item->title);
            if (isset($categoryNames[$item->category])) {
                $item->category = $categoryNames[$item->category];
            } else {
                $item->category = JText::_('Undefined');
            }
            if (isset($typesNames[$item->type])) {
                $item->type = $typesNames[$item->type];
            } else {
                $item->type = JText::_('Undefined');
            }
            $item->created = &JFactory::getDate($item->created);
            $item->created->setOffset($offset);
            $item->created = $item->created->toFormat();
            if ($item->modified != $db->getNullDate()) {
                $item->modified = &JFactory::getDate($item->modified);
                $item->modified->setOffset($offset);
                $item->modified = $item->modified->toFormat();
            } else {
                $item->modified = null;
            }
            $item->created_by = &JFactory::getUser($item->created_by);
            $item->created_by->link = ARTIOFusionChartsHelperRoute::getUserRoute($item->created_by->id);
            if ($item->modified_by) {
                $item->modified_by = &JFactory::getUser($item->modified_by);
                $item->modified_by->link = ARTIOFusionChartsHelperRoute::getUserRoute($item->modified_by->id);
            } else {
                $item->modified_by = '-';
            }
        }
        ARTIOFusionChartsHelper::informAboutCacheDir();
        ARTIOFusionChartsHelper::importListJS();
        
		JSubMenuHelper::addEntry(JText::_('Create New Chart'), ARTIOFusionChartsHelperRoute::getCreateListRoute());
		JSubMenuHelper::addEntry(JText::_('Charts Manager'), ARTIOFusionChartsHelperRoute::getListChartsRoute(), true);
        
        $this->assignRef('items', $items);
        $this->assignRef('pagination', $pagination);
        $this->assignRef('lists', $lists);
        $this->assignRef('categoryNames', $categoryNames);
        $this->assignRef('typesNames', $typesNames);
        parent::display($tpl);
    }
}
?>