<?php
defined ( '_JEXEC' ) or die ( 'Restricted access' );

jimport ( 'joomla.application.component.view' );

class ARTIOFusionChartsViewPlugin extends JView {
	function display($tpl = null) {
		$mainframe = &JFactory::getApplication ();
		$option = JRequest::getString ( 'option' );
		$search = $mainframe->getUserStateFromRequest ( $option . 'search', 'search', '', 'string' );
		$search = JString::strtolower ( $search );
		$category = $mainframe->getUserStateFromRequest ( $option . 'category', 'category', '', 'string' );
		$type = $mainframe->getUserStateFromRequest ( $option . 'type', 'type', '', 'string' );
		$items = & $this->get ( 'Data' );
		$pagination = & $this->get ( 'Pagination' );
		$lists = array ();
		$categories = ARTIOFusionChartsHelper::loadCategories ();
		$javascript = 'onchange="document.adminForm.submit();"';
		$categoriesOptions = array ();
		$categoriesOptions [] = JHTML::_ ( 'select.option', '0', '- ' . JText::_ ( 'Select Category' ) . ' -', 'id', 'name' );
		$categoriesOptions = array_merge ( $categoriesOptions, $categories );
		$lists ['filter_categories'] = JHTML::_ ( 'select.genericlist', $categoriesOptions, 'filter_categories', 'class="inputbox" size="1" ' . $javascript, 'id', 'name', intval ( $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' );
			}
		}
		$lists ['filter_types'] = JHTML::_ ( 'select.genericlist', $typesOptions, 'filter_types', 'class="inputbox" size="1" ' . $javascript, 'id', 'name', intval ( $type ) );
		$categoryNames = ARTIOFusionChartsHelper::getCategoryNamesArray ( $categories );
		$typesNames = ARTIOFusionChartsHelper::getTypesNamesArray ( $categories );
		foreach ( $items as $i => $item ) {
			$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' );
			}
		}
		$this->assignRef ( 'items', $items );
		$this->assignRef ( 'pagination', $pagination );
		$this->assignRef ( 'lists', $lists );
		$this->assignRef ( 'categoryNames', $categoryNames );
		$this->assignRef ( 'typesNames', $typesNames );
		parent::display ( $tpl );
	}
}
?>