
<?php
session_name('library');
session_start();

$searching = "no";

include("functions.php");

head('Advanced Search');

?>


<body>
<div id="main">

	<?php
		include("header.php");
		include("menu.php");
	?>
	
<div id="entire_panel">
	<h1>Pokročilé vyhľadávanie</h1>
		
	<?php
	if(!isset($_POST['search'])){
	?> 
		<form name="search" method="post" action="advanced_search.php">
			Vyhľadaj:<br /><br /> 
			<input type="text" name="searchterm" /> v  
			<Select name="field">
				<Option value="name">Názov knihy</option>
				<Option value="author">Meno autora</option>
				<Option value="year_published">Rok vydania</option>
			</Select><br /><br />
			<Select name="logic">
				<Option value="AND">a zároveň</option>
				<Option value="OR">alebo</option>
				<Option value="NAND">a zároveň nie</option>
			</Select><br /><br /> 
			<input type="text" name="searchterm2" /> v 
			<Select name="field2">
				<Option value="name">Názov knihy</option>
				<Option value="author">Meno autora</option>
				<Option value="year_published">Rok vydania</option>
			</Select><br /><br />
			<input name="search" type="submit" id="search" value="Hľadaj" />
			<br /><br />
		</form>
			
			<?php
			   
			//This is only displayed if they have submitted the form 
 
	} else { 
		echo "<h4>Výsledok hľadania</h4>";
				
		$term = $_POST['searchterm'];
		$field = $_POST['field'];
		$term2 = $_POST['searchterm2'];
		$field2 = $_POST['field2'];
		
		$logic = $_POST['logic'];
		
		
			
		//If they did not enter a search term we give them an error 
		if ($term != "" && $term2 != "") {  
		
			// We preform a bit of filtering 
			$term = strtoupper($term); 
			$term = strip_tags($term); 
			$term = trim ($term);
			
			$term2 = strtoupper($term2); 
			$term2 = strip_tags($term2); 
			$term2 = trim ($term2);
			
			// Otherwise we connect to our Database 
			$link = connect_db(); 
		
			//Now we search for our search term, in the field the user specified
			if ($logic == "NAND") {
				$sql = "SELECT * FROM books WHERE " .$field. " LIKE '%" .$term. "%' AND !(".$field2. " LIKE '%" .$term2. "%')";
			} else {
				$sql = "SELECT * FROM books WHERE " .$field. " LIKE '%" .$term. "%'" .$logic. " ".$field2. " LIKE '%" .$term2. "%'";
			}	
			$result = mysql_query($sql, $link) or die(mysql_error());
			
			echo '<table border="1">';
			echo '<tr><th>FOTO</th>';
			echo '<th>NÁZOV, '; 
			echo 'AUTOR, ';
			echo 'ROK, ';
			echo 'KATEGÓRIA</th>';
			echo '<th>POPIS</th><th>AKCIA</th></tr>';
			
			//And we display the results 
			while($row = mysql_fetch_array($result)) 
			{ 
				echo '<tr><td rowspan="4">';
				if (($row['photo'] != '') && (file_exists('images/books/'.$row['photo']))){ 
					echo '<img src="images/books/' . $row['photo'] . '" height="75" width="50" alt="book cover" />';
				} else { 
					echo '<img src="images/books/default_book.jpg" height="75" width="50" alt="no cover" />';  
				}
				echo '</td>';
				echo '<td id="bold">' . $row['name'] . '</td>';
				echo '<td rowspan="4">' . $row['description'] . '</td>';
				if (isset($_SESSION['user'])) {
					if ($row['in_offer']==1) {
						if ($_SESSION['admin']==1) {
							echo '<td rowspan="4"><br />';
							echo '<a href="change_book_info.php?id=' . $row['id_book'] . '" title="Uprav"><img src="images/edit.png" height="32" width="32"></a><br />';
							echo '<a href="remove_book.php?id=' . $row['id_book'] . '" title="Vymaž"><img src="images/delete.png" height="32" width="32"></a></td></tr>';
						} else {
							echo '<td rowspan="4">'; 
							echo '<a href="order.php?id=' . $row['id_book'] . '" title="Objednaj"><img src="images/ok.png" height="32" width="32"></a><br />';
							echo '</td>';
						} 
					} else {
						echo '<td rowspan="4">';
						echo '<img src="images/alert.png" height="32" width="32" title="Požičané">';
						echo '</td>';
					}
				} else {
					echo '<td rowspan="4"><img src="images/question.png" height="32" width="32"></td></tr>';
				}
				echo '<tr><td>' . $row['author'] . '</td></tr>';
				echo '<tr><td>' . $row['year_published'] . '</td></tr>';
							
				$sql2 = "SELECT * FROM book_categories WHERE id_group='" .$row['id_group']. "'";
				$result2 = mysql_query($sql2, $link);
				$row2 = mysql_fetch_assoc($result2);
				
				echo '<tr><td>' . $row2['name'] . '</td></tr>';
					 
			} 
			
			echo '</table>';
		
			//This counts the number or results - and if there wasn't any it gives them a little message explaining that  
			if (mysql_num_rows($result) == 0) 
			{ 
				echo "Sorry, but we can not find an entry to match your query<br><br>"; 
			} else {
				echo 'Celkový počet vyhľadaných položiek: ' . mysql_num_rows($result) . '<br /><br />';
			}
			
			mysql_free_result($result); 
		
			//And we remind them what they searched for 
			echo '<b>Vyhľadávanie pre:</b> ' .$term. ' a ' .$term2. '<br /><br />'; 
		} else { 
			echo '<p id="red">Nezadali ste oba termíny na vyhľadanie</p>';  
		}
	}
	
	?>
			
    </div>


	<?php include("footer.php"); ?>

</div>
</body>
</html>