<?php
	session_name('library');
	session_start();
	
	include("functions.php");
	
	
	
	head('Add Book - Management Section');
?>

<body>
<div id="main">

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

		
	

<div id="entire_show_panel">
		<h1>Pridaj publikáciu</h1>
		<?php
			 

if ($_SESSION['admin'] == 1) {
	if(!isset($_POST['submit'])){ ?>
	<table border=0>
	<form name=register method=post action="add_book.php" enctype="multipart/form-data">
		<tr>
			<td>Názov:</td>
			<td><input type=text name=name maxlength=100></td>
		</tr>
		<tr>
			<td>Autor:</td>
			<td><input type=text name=author maxlength=100></td>
		</tr>
		<tr>
			<td>Rok vydania:</td>
			<td><input type=text name=year_published maxlength=4></td>
		</tr>
		<tr>
			<td>Popis:</td>
			<td><textarea name=description cols=30 rows=5></textarea></td>
		</tr>
		<tr>
			<td>Žáner</td>
			<td>
			<?php
					$link = connect_db();
					$sql = "SELECT * FROM book_categories";
					$result = mysql_query($sql, $link);
					if ($result) {
    					echo '<select name="genre" size="1">';
						while ($row = mysql_fetch_assoc($result)) {
							echo '<option value="' . $row['id_group'] . '"';
							if (isset($_POST["genre"]) && ($_POST["genre"] == $row['name'])) echo ' selected="selected"'; 
								echo '>' . $row['name'] . '</option>';
							}
							echo '</select> ';
							mysql_free_result($result);
					} else {
						echo '<p id="red">Chyba pri výpise, nastal probém pri získavaní údajov z databázy - ' . mysql_errno($link) . ': ' . mysql_error($link) . '</p>';
					}
					mysql_close($link); 
			?>
			</td>
		</tr>
		<tr>
			<td>Foto:</td>
			<td>
				<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
				<input type="file" accept="image/jpeg, image/gif, image/png" name="image" /> 
				<br />(podporované formáty JPEG, GIF, PNG)
			</td>
		</tr>
		<tr>
			<td colspan=2 align=right><input name="submit" type="submit" id="submit" value="Pridaj"></td>
		</tr>
	</form>
	</table>
	<?php

	}else{
	
		$link = connect_db();
	
		$name = $_POST['name'];
		$author = $_POST['author'];
		$year_published = $_POST['year_published'];
		$description = $_POST['description'];
		$genre = $_POST['genre'];
		$image = $_POST['image'];
		
		protect($name);
		protect($author);
		protect($year_published);
		protect($description);
		protect($genre);
		protect($image);
		

		if (isset($name)){

			$date = date ("Y-m-d H:m:s");
			
				
			$sql = "INSERT INTO books SET name='$name', author='$author', year_published='$year_published', description='$description', id_group='$genre', date_added='$date', in_offer=1, photo='default_book.jpg'";
			$result = mysql_query($sql) or die(mysql_error());
			
			echo '<p id="bold">The book was added successfully.</p>';
			
			if (isset($_FILES['image'])) {
				if ($_FILES['image']['error'] == UPLOAD_ERR_OK) {
					if (is_uploaded_file($_FILES['image']['tmp_name'])) {
						//$nazov = "book_photo_0".$row2[id_book];
						$nazov = $_FILES['image']['name'];
						$successful = move_uploaded_file($_FILES['image']['tmp_name'], 'images/books/' . $nazov);
						if ($successful) {
							echo '<p>Obrázok knihy bol nahratý na server.</p>';
						} else {
							echo '<p>Obrázok knihy NEbol nahratý na server.</p>';
						}
	
						$link = connect_db();
						$sql = "UPDATE books SET photo='$nazov' WHERE name='$name' LIMIT 1;";
						$result = mysql_query($sql, $link);
						if ($result) {
							echo '<p><strong>Obrázok knihy bol úspešne nahratý</strong></p>';
						} else {
							echo '<p id="red">Obrázok knihy NEbol nahratý, lebo nastal problém pri zápise do databázy - ' . mysql_errno($link) . ': ' . mysql_error($link) . '</p>';
						}
						mysql_close($link);
					}
				} else { // nastane, ak bol uploadovaný súbor väcší ako upload_max_filesize alebo ako post_max_size (chyba 2)
					echo '<p id="red">Obrázok knihy NEbol aktualizovaný, lebo nastal problém pri uploadovaní súboru ' . $_FILES['image']['name'] . ' - ' . $_FILES['image']['error'] . '</p>';
				}
			} else {
				echo '<p id="red">Problem s obrazkom</p>';
			}
		} else {
			echo '<p id="red">Nie je zadaný názov knihy.</p>';
		}
	}
} else {
	echo '<p id="red">K tejto stránke nemáte dostatočné oprávnenia</p>';
}    
		?>				
</div>

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

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

