<?php
/**
 * @package Joomla
 * @subpackage Abook
 * @copyright (C) 2010 Ugolotti Federica
 * @license GNU/GPL, see LICENSE.php
 * Abook is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.

 * Abook is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with Abook; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */


defined( '_JEXEC' ) or die( 'Restricted access' );

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

class AbookViewSearch extends JView
{
        function display($tpl = null)
        {
                global $mainframe, $option;
                // Get data from the model
		$params =& JComponentHelper::getParams( 'com_abook' );
		$error  = '';
		$searchkey	= $this->get( 'Key');
		if ($params->get('show_search_cat', 1)) {
			$categorieslist          =& $this->get('Categorieslist');
			$top[]    = JHTML::_('select.option', NULL , JText::_('SELECT A CATEGORY'), 'id', 'treename');
                	$categorieslist=array_merge($top, $categorieslist);
                	$lists['category'] = JHTML::_('select.genericlist', $categorieslist, 'category', 'class="inputbox" size="1"','id', 'treename', isset($searchkey['category'])?$searchkey['category']:"");
		}
		if ($params->get('show_search_editor', 1)) {
			$editorslist          =& $this->get('Editorslist');
			$topeditor[]    = JHTML::_('select.option', NULL , JText::_('SELECT A EDITOR'), 'id', 'name');
                	$editorslist=array_merge($topeditor, $editorslist);
                	$lists['ideditor'] = JHTML::_('select.genericlist', $editorslist, 'ideditor', 'class="inputbox" size="1"','id', 'name', isset($searchkey['ideditor'])?$searchkey['ideditor']:"");
		}
		if ($params->get('show_search_auth', 1)) {
	                $authorslist          =& $this->get('Authorslist');
			$topauthor[]    = JHTML::_('select.option', NULL , JText::_('SELECT AN AUTHOR'), 'id', 'name');
	                $authorslist=array_merge($topauthor, $authorslist);
        	        $lists['author'] = JHTML::_('select.genericlist', $authorslist, 'author', 'class="inputbox" size="1"','id', 'name', isset($searchkey['author'])?$searchkey['author']:"");
		}
		if ($params->get('show_search_loc', 1)) {
	                $locationslist          =& $this->get('Locationslist');
			$toplocation[]    = JHTML::_('select.option', NULL , JText::_('SELECT A LOCATION'), 'id', 'location');
                	$locationslist=array_merge($toplocation, $locationslist);
                	$lists['location'] = JHTML::_('select.genericlist', $locationslist, 'location', 'class="inputbox" size="1"','id', 'location', isset($searchkey['location'])?$searchkey['location']:"");
		}
		if ($params->get('show_search_lib', 1)) {
	                $librarieslist          =& $this->get('Librarieslist');
			$toplibraries[]    = JHTML::_('select.option', NULL, JText::_('SELECT A LIBRARY'), 'id', 'name');
	                $librarieslist=array_merge($toplibraries, $librarieslist);
        	        $lists['library'] = JHTML::_('select.genericlist', $librarieslist, 'library', 'class="inputbox" size="1"','id', 'name', isset($searchkey['library'])?$searchkey['library']:"");
		}
		if ($params->get('show_search_year', 1)) {
                        $yearlist          =& $this->get('Yearlist');
			$lists['year'] = isset($searchkey['year'])?$searchkey['year']:'';
			$topyear[]    = JHTML::_('select.option', NULL, JText::_('SELECT YEAR'), 'id', 'name');
        	        $yearlist=array_merge($topyear, $yearlist);
	                $lists['year'] = JHTML::_('select.genericlist', $yearlist, 'year', 'class="inputbox" size="1"','id', 'name', isset($searchkey['year'])?$searchkey['year']:"");
                }
		if ($searchkey > 0) {
			$items          = & $this->get( 'Data');
			$total      = $this->get( 'Total');
			if (count($items) > 0) {
				$pagination = & $this->get( 'Pagination' );
			}else{
				$error=JText::_( 'NO RESULT' );
			}
		}
		$pathway   =& $mainframe->getPathway();
		$document       =& JFactory::getDocument();
		$document->addStyleSheet('components/'.$option.'/css/style.css');

		$lists['keyword'] = isset($searchkey['keyword'])? $searchkey['keyword']:'';

		$lists['n_items']=JText::_( 'TOTALRESULTSFOUND').' '. $total;

		//$params = &$mainframe->getParams();

		JHTML::addIncludePath(JPATH_COMPONENT.DS.'helpers');
		$lists['credit']=JHTML::_('credit.credit');

		//*-------------
                $menus           = & JSite::getMenu();
                $menu           = $menus->getActive();
                if (is_object( $menu )) {
                        //$menu_params = new JParameter( $menu->params );
                        if (!$params->get( 'page_title')) {
                                $params->set('page_title', $params->get('comp_name'));
                        }
                } else {
                        $params->set('page_title',     $params->get('comp_name'));
                }
//------------*/


                // push data into the template
                $this->assignRef('lists', $lists);
		$this->assignRef('items', $items);
		$this->assignRef('pagination', $pagination);
		$this->assignRef('searchkey', $searchkey);
		$this->assignRef('params', $params);
		$this->assign('error', $error);
	
		$pathway->addItem(JText::_('Search'), '');	
                parent::display($tpl);
        }
}
