<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

/**
*
* @package Booklibrary
* @copyright 2011 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru);
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* Homepage: http://www.ordasoft.com
* @version: 2.2 FREE
**/

$showInterior_extras  = $params->get ('showInterior_extras', 0);
$showSafety_options = $params->get ('showSafety_options', 0);
$showWarranty_options = $params->get ('showWarranty_options', 0);
$showInterior_colors  = $params->get ('showInterior_colors', 0);
$showWheeltype  = $params->get ('showWheeltype', 0);

?>

<link rel="stylesheet" type="text/css" href="administrator/components/com_booklibrary/includes/booklibrary.css">
<?php
if( !function_exists( 'categoryArray')) {
/*
* function categoryArray ()
* Gets the Category list depending of user access level.
* for 1.6
*/
    function categoryArray() 
    { 
        global $database, $my;
	
		$usergroups = getGroupsByUser ($my->id,'');
		$usergroups_sh = $usergroups; 

		$usergroups = '-2'.$usergroups; 
	

		$usergroups_sh[] = -2; 
		
		$s = '';
		for ($i=0; $i<count($usergroups_sh); $i++) {  
			$g = $usergroups_sh[$i];
			$s .= " c.params like '%,{$g}' or c.params = '{$g}' or c.params like '{$g},%' or c.params like '%,{$g},%' ";
			if ( ($i+1)<count($usergroups_sh) )
				$s .= ' or ';
		} 
		
		$query = "select c.id,c.parent_id, COUNT(bc.bookid) AS books, c.title, c.image, '1' AS display, c.parent_id AS parent, c.name" .
        " FROM  #__booklibrary_main_categories as c
        LEFT JOIN #__booklibrary_categories AS bc ON c.id=bc.catid \n
        LEFT JOIN #__booklibrary AS b ON b.id=bc.bookid AND b.published=1 AND b.approved=1
        WHERE  c.section='com_booklibrary'
          AND c.published=1 AND ({$s})
        GROUP BY c.id \n
        ORDER BY parent_id DESC, c.ordering ";
		
		$database->setQuery($query);
		$items = $database->loadObjectList();

		
       // establish the hierarchy of the menu
        $children = array();
        // first pass - collect children
        foreach ($items as $v) {
            $pt = $v->parent;
            $list = @$children[$pt] ? $children[$pt] : array();
            array_push($list, $v);
            $children[$pt] = $list;
        } 
        // second pass - get an indent list of the items
/// $array = mosTreeRecurse(0, '', array(), $children);
		$array =	bookLibraryTreeRecurse (0, '', array(), $children); 
        return $array;
    }

}

if ( !function_exists( 'bookLibraryTreeRecurse') ) {
	/*
* function bookLibraryTreeRecurse ()
* Redefines a standard function to not display &nbsp;
*/
function bookLibraryTreeRecurse( $id, $indent, $list, $children, $maxlevel=9999, $level=0, $type=1 ) {

	if (@$children[$id] && $level <= $maxlevel) {
	    $parent_id = $id;
		foreach ($children[$id] as $v) {
			$id = $v->id;

			if ( $type ) {
				$pre 	= " "; //'<sup>|_</sup>_';
				$spacer = '. -- ';
			} else {
				$pre 	= "- ";
				$spacer = ' . -';
			}

			if ( $v->parent == 0 ) {
				$txt 	= $v->name; 
			} else {
				$txt 	= $pre . $v->name; 
			}
			$pt = $v->parent;
			$list[$id] = $v;
			$list[$id]->treename = "$indent$txt";
			$list[$id]->children = count( @$children[$id] );
    		$list[$id]->all_fields_in_list = count(@$children[$parent_id]);

			$list = bookLibraryTreeRecurse( $id, $indent . $spacer, $list, $children, $maxlevel, $level+1, $type );
		}
	}
	return $list;
}
}


if( !function_exists( 'mod_categoryTreeList')) {
  function mod_categoryTreeList($id, $action,$is_new, $options = array())
  {
    global  $database, $mosConfig_absolute_path;
    $list = categoryArray();
    ///$cat = new mosCategory($database);
	require_once($mosConfig_absolute_path."/components/com_booklibrary/booklibrary.main.categories.class.php");//for 1.6
	
	$cat = new mainBooklibraryCategories($database);//for 1.6
    $cat->load($id);
  
    $this_treename = '';
    $childs_ids =  Array();
    foreach ($list as $item) {
      if ($item->id == $cat->id || array_key_exists($item->parent_id,$childs_ids) )
      $childs_ids[$item->id] = $item->id;
    }
  
    foreach ($list as $item) {
      if ($this_treename) {
        if ($item->id != $cat->id
          && strpos($item->treename, $this_treename) === false
          && array_key_exists($item->id,$childs_ids) === false) {
          $options[] = mosHTML::makeOption($item->id, $item->treename);
        }
      } else {
        if ($item->id != $cat->id) {
          $options[] = mosHTML::makeOption($item->id, $item->treename);
        } else {
          $this_treename = "$item->treename/";
        }
      }
    }
  
    $parent= null;
    $parent = mosHTML::selectList($options, 'catid',
  'class="inputbox" size="1" style="max-width:170px;"', 'value', 'text', $cat->parent_id);
    return $parent;
  }
}
	
if( !function_exists( 'sefreltoabs')) {
  function sefRelToAbs( $value ) {
    //Need check!!!

    // Replace all &amp; with & as the router doesn't understand &amp;
    $url = str_replace('&amp;', '&', $value);
    if(substr(strtolower($url),0,9) != "index.php") return $url;
    $uri    = JURI::getInstance();
    $prefix = $uri->toString(array('scheme', 'host', 'port'));
    return $prefix.JRoute::_($url);
  }
}

  
  global $mosConfig_absolute_path, $mosConfig_allowUserRegistration,$mosConfig_lang;
$database = JFactory::getDBO();
$my = JFactory::getUser();
$GLOBALS['database'] = $database;
$GLOBALS['my']= $my;
$acl = JFactory::getACL();
$GLOBALS['acl'] = $acl;
$GLOBALS['mosConfig_absolute_path'] = $mosConfig_absolute_path = JPATH_SITE;
  
// load language
$lang_def_en=0;
$lang = JFactory::getLanguage();
foreach ($lang->getLocale() as $locale) {
  $mosConfig_lang=$locale;
  if (file_exists($mosConfig_absolute_path."/components/com_booklibrary/language/{$mosConfig_lang}.php" ))
  {  
    include_once($mosConfig_absolute_path."/components/com_booklibrary/language/{$mosConfig_lang}.php" );
    $lang_def_en=1;
    break;
  } 
}
if($lang_def_en!=1)
 {
  $mosConfig_lang = "english";
  include_once($mosConfig_absolute_path."/components/com_booklibrary/language/english.php" );
 }
 
   include_once($mosConfig_absolute_path."/administrator/components/com_booklibrary/compat.joomla1.5.php" );
 
$showAuthor = $params->get ('showAuthor', 0);
$showTitle  = $params->get ('showTitle', 0);
$showIsbn = $params->get ('showIsbn', 0);
$showBookId = $params->get ('showBookId', 0);
$showDescription  = $params->get ('showDescription', 0);
$showPublisher  = $params->get ('showPublisher', 0);
$showAdvanceSearch = $params->get ('showAdvanceSearch', 0);
$showPrice = $params->get ('showPrice', 0);


	$categories[] = mosHTML :: makeOption('0', _BOOKLIBRARY_SEARCH_CATEGORY );
  
$ItemId_tmp_from_params=$params->get('ItemId');  
 $database->setQuery("SELECT id  FROM #__menu WHERE menutype like '%menu%' AND link LIKE '%index.php?option=com_booklibrary%' ");
  $ItemId_tmp_from_db = $database->loadResult();
if($ItemId_tmp_from_params!=''){
      $Itemid=$ItemId_tmp_from_params;
    }
    else{
      $Itemid=$ItemId_tmp_from_db;
    }


	$clist = mod_categoryTreeList(0,'',true,$categories);
 $moduleclass_sfx = $params->get('moduleclass_sfx');
?>

<?php if($moduleclass_sfx!='') {?>
   <div  class="<?php echo $moduleclass_sfx;?>"> <?php }?>
<div>
	<form action="<?php echo sefRelToAbs("index.php?option=com_booklibrary&amp;task=search&amp;catid=0&amp;ItemId=".$Itemid); ?>" method="get" name="mod_booklibsearchForm">
 <table  border="0" cellpadding="4" cellspacing="0" class="basictable" width="100%">
   <tr>
				 <td align="left">
				   <?php echo _BOOKLIBRARY_LABEL_SEARCH_BUTTON ?>:&nbsp;
				 </td>
	</tr>
	<tr>
		 <td align="left">
				<input class="inputbox" type="text" name="searchtext" size="15" maxlength="20"/>
		 </td>
	</tr>

  <?php if($showAuthor==0){?>
  <tr>
    <td  align="left" nowrap>
    <?php echo _BOOKLIBRARY_SHOW_SEARCH_FOR_AUTORS ?>:&nbsp;<input type="checkbox" name="author" checked="checked">
    </td>
  </tr>
  <?php }elseif($showAuthor==1){ ?>
    <input type="hidden" name="author" value="on">
  <?php }?>  
  
  <?php if($showTitle==0){?>
  <tr>
    <td  align="left" nowrap>
    <?php echo _BOOKLIBRARY_SHOW_SEARCH_FOR_TITLE ?>:&nbsp;<input type="checkbox" name="title" checked="checked">
    </td>
  </tr>
  <?php }elseif($showTitle==1){ ?>
    <input type="hidden" name="title" value="on">
  <?php }?>  
  
  <?php if($showIsbn==0){?>
  <tr>
    <td  align="left" nowrap>
    <?php echo _BOOKLIBRARY_SHOW_SEARCH_FOR_ISBN ?>:&nbsp;<input type="checkbox" name="isbn" checked="checked">
    </td>
  </tr>
  <?php }elseif($showIsbn==1){ ?>
    <input type="hidden" name="isbn" value="on">
  <?php }?>
  <?php if($showBookId==0){?>
  <tr>
    <td  align="left" nowrap>
    <?php echo _BOOKLIBRARY_SHOW_SEARCH_FOR_BOOK_ID ?>:&nbsp;<input type="checkbox" name="bookid" checked="checked">
    </td>
  </tr>
  <?php }elseif($showBookid==1){ ?>
    <input type="hidden" name="bookid" value="on">
  <?php }?>
  <?php if($showDescription==0){?>
  <tr>
    <td  align="left" nowrap>
    <?php echo _BOOKLIBRARY_SHOW_SEARCH_FOR_DESCRIPTION ?>:&nbsp;<input type="checkbox" name="description" checked="checked">
    </td>
  </tr>
  <?php }elseif($showDescription==1){ ?>
    <input type="hidden" name="description" value="on">
  <?php }?>  
        
  <?php if($showPublisher==0){?>
  <tr>
    <td  align="left" nowrap>
    <?php echo _BOOKLIBRARY_SHOW_SEARCH_FOR_PUBLISHER ?>:&nbsp;<input type="checkbox" name="publisher" checked="checked">
    </td>
  </tr>
  <?php }elseif($showPublisher==1){ ?>
    <input type="hidden" name="publisher" value="on">
  <?php }?>  
  <?php if($showPrice==0):?>
  <tr>
    <td colspan="1" >
        <?php echo _BOOKLIBRARY_SHOW_SEARCH_PRICE_FROM; ?>:&nbsp;
        <input type="text" name="pricefrom" size="6"/>&nbsp;
    </td>
  </tr>
  <tr>
    <td>
        <?php echo _BOOKLIBRARY_SHOW_SEARCH_PRICE_TO; ?>:&nbsp;
        <input type="text" name="priceto" size="6"/>
    </td>
  </tr>
  <?php endif;?>
	<tr>
		<td align="left">
			<?php echo _BOOKLIBRARY_HEADER_CATEGORY ?>:&nbsp;
		</td>
	</tr>
	<tr>
	  <td align="left">
			  <?php echo $clist; ?>
	  </td>
  </tr>
	<tr>
		<td align="left" colspan="2" style="margin: 5px;padding: 5px;">
		<div style="float:left;margin-right: 5px"><input type="submit" value="<?php echo _BOOKLIBRARY_LABEL_SEARCH_BUTTON; ?>" class="button" /></div>
		<noscript>Javascript is required to use Book Library <a href="http://ordasoft.com/Book-Library/booklibrary-versions-feature-comparison.html">Book Library - create book library, ebook, book collection  </a>, 

<a href="http://ordasoft.com/location-map.html">Book library book sowftware for Joomla</a></noscript>
  <?php if($showAdvanceSearch==0){
     $link1 ="index.php?option=com_booklibrary&amp;task=show_search&amp;catid=0&amp;Itemid=".$Itemid;?>
  <?php } ?>
			<input type="hidden" name="option" value="com_booklibrary">
			<input type="hidden" name="task" value="search">
			<input type="hidden" name="Itemid" value="<?php echo $Itemid ?>">
		</td>
	</tr>
		    </table>
		</form>
</div>
<?php if($moduleclass_sfx!='') {?>
   </div> <?php }?>
<div style="text-align: center;"></div>