<?php
	session_name('library');
	session_start();
	
	include("functions.php");
	
	head("List of books");
?>

<body>
<div id="main">

	<?php
		include("header.php");
		include("menu.php");
	?>
	

<div id="entire_panel">	
		<h1>Zoznam poskytovanej literatúty</h1>
		
<?php

	$book_sort = $_GET['sort'];
	protect($book_sort);
	$_SESSION['book_sort'] = $book_sort;

	$link = connect_db();
	
	switch ($book_sort) {
    case "name":
        $sql = "SELECT * FROM books ORDER BY name";
        break;
    case "author":
        $sql = "SELECT * FROM books ORDER BY author";
        break;
    case "year":
        $sql = "SELECT * FROM books ORDER BY year_published";
        break;
	    case "category":
        $sql = "SELECT * FROM books ORDER BY id_group";
        break;
	default:
       	$sql = "SELECT * FROM books";
	}
	
	$result = mysql_query($sql, $link);
	if ($result) {
		echo '<table border="1">';
		echo '<tr><th>FOTO</th>';
		echo '<th><a href="book_list.php?sort=name" title="Zoraď podľa mena">NÁZOV</a>, '; 
		echo '<a href="book_list.php?sort=author" title="Zoraď podľa autora">AUTOR</a>, ';
		echo '<a href="book_list.php?sort=year" title="Zoraď podľa roku vydania">ROK</a>, ';
		echo '<a href="book_list.php?sort=category" title="Zoskup podľa kategórii">KATEGÓRIA</th>';
		echo '<th>POPIS</th><th>AKCIA</th></tr>';
		while ($row = mysql_fetch_assoc($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>';
		echo 'Celkový počet publikácií: ' . mysql_num_rows($result) . '<br />';
		mysql_free_result($result);
	} else {
		echo '<p id="red">Nastal problém pri získavaní údajov z databázy - ' . mysql_errno($link) . ': ' . mysql_error($link) . '</p>';
	}
	mysql_close($link);

?>

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

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