From 7c823d0b1036068894429f3d99192f1967ca1b70 Mon Sep 17 00:00:00 2001
From: Graham Cole <chckens@sucs.org>
Date: Sun, 20 Jan 2008 00:31:35 +0000
Subject: [PATCH] from the better-late-than-never department, here's a stupidly
 overcomplex yet incomplete way of adding new books. Doesn't deal with tagging
 yet, but better than nothing, don't you think? Maybe someone else should
 rewrite the whole library component in an afternoon following KISS principles
 soon ;-)

---
 components/libraryadmin.php   | 82 +++++++++++++++++++++++++++++++++++
 templates/library-addbook.tpl | 52 ++++++++++++++++++++++
 templates/library.tpl         | 11 +++--
 3 files changed, 141 insertions(+), 4 deletions(-)
 create mode 100644 components/libraryadmin.php
 create mode 100644 templates/library-addbook.tpl

diff --git a/components/libraryadmin.php b/components/libraryadmin.php
new file mode 100644
index 0000000..b184b74
--- /dev/null
+++ b/components/libraryadmin.php
@@ -0,0 +1,82 @@
+<?php
+
+// you gotta be this high to enter
+$permission="librarian";
+
+//include ISBN validation library
+require_once('../lib/Validate/ISPN.php');
+
+$error = array();
+
+function fetch_amazon_data($isbn) {
+
+	$url1 = "http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService";
+	$url1 .= "&AWSAccessKeyId=02NDPFGZ9A35HRBQ9NG2";
+	$url1 .= "&Operation=ItemLookup&IdType=ISBN&SearchIndex=Books&ItemId=";
+	$url2 = "&ResponseGroup=Medium,Offers";
+
+	return file_get_contents($url1.$isbn.$url2);
+}
+
+function valid_isbn($isbn) {
+
+        return Validate_ISPN::isbn($isbn);
+}
+
+if ($session->groups[$permission]) {
+
+	if ($_REQUEST['action'] == "search") {
+
+		$isbn = $_REQUEST['isbn'];
+		$isbn = str_replace(array('ISBN', '-', ' ', "\t", "\n"), '', $isbn);
+
+		if (valid_isbn($isbn)) {
+			$xmlresult = fetch_amazon_data($isbn);
+			$simple_xml = simplexml_load_string($xmlresult);
+			$book['isbn'] = $isbn;
+			$book['title'] = $simple_xml->Items->Item->ItemAttributes->Title;
+			$book['author'] = $simple_xml->Items->Item->ItemAttributes->Author;
+			$book['publisher'] = $simple_xml->Items->Item->ItemAttributes->Publisher;
+			$book['image_url'] = $simple_xml->Items->Item->MediumImage->URL;
+			$smarty->assign("book", $book);
+		} else {
+			// invalid isbn entered
+			trigger_error("invalid ISBN number entered", E_USER_WARNING);
+		}
+
+		
+	} elseif ($_REQUEST['action'] == "add") {
+		$book = array();
+		$book['isbn'] = $_REQUEST['isbn'];
+		$book['title'] = $_REQUEST['title'];
+		$book['author'] = $_REQUEST['author'];
+		$book['publisher'] = $_REQUEST['publisher'];
+		$book['image_url'] = $_REQUEST['image_url'];
+
+		// Validate that we have enough info to add
+		if (($book['isbn'] != "") && (!valid_isbn($book['isbn']))) {
+			trigger_error("invalid ISBN", E_USER_WARNING);
+		} elseif (trim($book['title']) == "") {
+			trigger_error("you must supply a title", E_USER_WARNING);
+		} elseif (trim($book['author']) == "") {
+			trigger_error("you must supply an author"); 
+		} else {
+			$insertdata = array($book['title'], $book['author'], $book['publisher']);
+			if (valid_isbn($book['isbn'])) {
+				$book['amazon_data'] = fetch_amazon_data($book['isbn']);
+				$newinsertdata = array($book['isbn'], $book['image_url'], $book['amazon_data']);
+				$insertdata = array_merge($insertdata, $newinsertdata);
+				$DB->Query("INSERT INTO books (title, author, publisher, isbn, image_url, amazon_data) VALUES (?,?,?,?,?,?)", $insertdata);
+			} else {
+				$DB->Query("INSERT INTO books (title, author, publisher) VALUES (?,?,?)", $insertdata);
+			}
+		}
+
+	}
+		$result = $smarty->fetch("library-addbook.tpl");
+}
+
+$smarty->assign("title", "Library Admin");
+$smarty->assign("body", $result);
+
+?>
diff --git a/templates/library-addbook.tpl b/templates/library-addbook.tpl
new file mode 100644
index 0000000..87ddcc5
--- /dev/null
+++ b/templates/library-addbook.tpl
@@ -0,0 +1,52 @@
+
+<fieldset>
+<legend>Add Book</legend>
+<form action="{$self}" method="POST">
+<input type="hidden" name="action" value="search" />
+<div class="note row">Enter an ISBN to attempt to auto-fill book details</div>
+<div class="row">
+	<label for="isbn">ISBN</label>
+	<span class="textinput"><input type="text" name="isbn" /></span>
+</div>
+<div class="row">
+	<span class="textinput"><input type="submit" value="Auto-fill" /></span>
+</div>
+</form>
+
+{if isset($book.image_url)}
+	<div class="emblem">
+		<img src="{$book.image_url}" />
+	</div>
+{/if}
+
+<div>
+<form action="{$self}" method="POST">
+	<input type="hidden" name="action" value="add" />
+	<input type="hidden" name="image_url" value="{$book.image_url}" />
+	<div class="row">
+		<label for="isbn">ISBN Number</label>
+		<span class="textinput"><input type="text" name="isbn" value="{$book.isbn}"/></span>
+	</div>
+
+	<div class="row">	
+		<label for="title">Title</label>
+		<span class="textinput"><input type="text" name="title" width="200" value="{$book.title}" /></span>
+	</div>
+	<div class="row">	
+		<label for="author">Author</label>
+		<span class="textinput"><input type="text" name="author" value="{$book.author}" /></span>
+	</div>
+	<div class="row">	
+		<label for="publisher">Publisher</label>
+		<span class="textinput"><input type="text" name="publisher" value="{$book.publisher}" /></span>
+	</div>
+	<div class="row">	
+		<label for="comments">Comments</label>
+		<span class="textinput"><textarea name="comments"></textarea></span>
+	</div>
+	<div class="row">
+		<input type="submit" name="add" value="Add Book"  />
+	</div>
+</form>
+</div>
+</fieldset>
diff --git a/templates/library.tpl b/templates/library.tpl
index 125c207..ee518cd 100644
--- a/templates/library.tpl
+++ b/templates/library.tpl
@@ -4,12 +4,12 @@
 <h3>Browse by category</h3>
 <ul>
 {foreach name=tags from=$tags item=tag}
-<li><a href="/Knowledge/Library/Tags/{$tag.name|escape:'url'}">{$tag.name}</a></li>
+<li><a href="{$baseurl}/Knowledge/Library/Tags/{$tag.name|escape:'url'}">{$tag.name}</a></li>
 {/foreach}
 </ul>
 <h3>Random Books</h3>
 	{foreach name=randoms from=$randoms item=randomitem}
-	<a href="/Knowledge/Library/{$randomitem.id}"><img src="{$randomitem.image_url}" alt="{$randomitem.title}" height="120" /></a>
+	<a href="{$baseurl}/Knowledge/Library/{$randomitem.id}"><img src="{$randomitem.image_url}" alt="{$randomitem.title}" height="120" /></a>
 	{/foreach}
 
 {elseif $mode == 'display'}
@@ -33,17 +33,20 @@
 	{else}
 	<ul>
 	{foreach name=results from=$results item=result}
-<li><a href="/Knowledge/Library/{$result.id}">{$result.title}</a>{if $result.onloan} <em>(on loan)</em>{/if} </li>
+<li><a href="{$baseurl}/Knowledge/Library/{$result.id}">{$result.title}</a>{if $result.onloan} <em>(on loan)</em>{/if} </li>
 	{/foreach}
 	</ul>
 	{/if}
 {elseif $mode == 'tagdisplay'}
 	{foreach name=results from=$results item=result}
-		<li><a href="/Knowledge/Library/{$result.id}">{$result.title}</a> </li>
+		<li><a href="{$baseurl}/Knowledge/Library/{$result.id}">{$result.title}</a> </li>
 	{/foreach}
 {elseif $mode == 'bookerror'}
 	<p>The requested book does not exist</p>
 {elseif $mode == 'tagerror'}
 	<p>The requested tag does not exist</p>
 {/if}
+{if $librarian == true}
+<p><a href="{$baseurl}/Knowledge/Library/Admin">Library Admin</a></p>
+{/if}
 <p>Images provided by <a href="http://www.amazon.co.uk">Amazon</a></p>
-- 
GitLab