From 55ed4fde7be868584818d6d9e0762646cfa30016 Mon Sep 17 00:00:00 2001
From: Graham Cole <chckens@sucs.org>
Date: Sat, 27 Feb 2010 12:23:49 +0000
Subject: [PATCH] use amazon's description for books that have none

---
 components/library.php     | 24 ++++++++++++++++++++----
 templates/library-book.tpl | 23 +++++++++++++++++++++++
 templates/library.tpl      | 19 ++++---------------
 3 files changed, 47 insertions(+), 19 deletions(-)
 create mode 100644 templates/library-book.tpl

diff --git a/components/library.php b/components/library.php
index 8cf6f04..0dc2f05 100644
--- a/components/library.php
+++ b/components/library.php
@@ -98,11 +98,27 @@ if (isset($_REQUEST['search']) && (trim($_REQUEST['search']) != "")) {
 		$book['author'] = htmlentities2($book['author']);
 		$book['onloan'] = ($book['onloan'] == 't') ? true : false;
 		
-	// Extract amazon data (maybe this should be stored in separate field in the db?)
-		$simple_xml = simplexml_load_string($book['amazon_data']);
 
-		//FIXME: figure out how to tell if there was usable amazon data after all
-		//$book['description'] = $simple_xml->Items->Item->EditorialReviews->EditorialReview->Content;
+		if (!isset($book['description'])) {
+			// no book description in the database, try using Amazon data
+
+			// Extract amazon data (maybe this should be stored in separate fields in the db?)
+			$simple_xml = simplexml_load_string($book['amazon_data']);
+
+			$book['description'] = @$simple_xml->Items->Item->EditorialReviews->EditorialReview->Content;
+			if (isset($book['description'])) {
+				// tidy description markup
+				$tidy_config['doctype'] = 'omit';
+				$tidy_config['output-xhtml'] = true;
+				$tidy_config['show-body-only'] = true;
+				$tidy_config['logical-emphasis'] = true;
+				$book['description'] = tidy_repair_string($book['description'], $tidy_config);
+
+				// update db so we don't have to do this next time
+				$DB->Execute("UPDATE books SET description=? WHERE id=?", array($book['description'],$book['id']));
+			}
+		}
+
 		$smarty->assign("book", $book);
 
 
diff --git a/templates/library-book.tpl b/templates/library-book.tpl
new file mode 100644
index 0000000..ecb16a9
--- /dev/null
+++ b/templates/library-book.tpl
@@ -0,0 +1,23 @@
+
+<div class="box">
+	<div class="boxhead"><h2>{$book.title}</h2></div>
+	<div class="boxcontent">
+
+{if $editable == true}
+	<form action="" method="POST">
+		<input type="text" name="title" value="{$book.title}" />
+		<input type="text" name="author" value="{$book.author}" />
+		<input type="text" name="publisher" value="{$book.publisher}" />
+		<textarea name="description" value="{$book.description}" /> 
+	</form> 
+{/if}
+		<strong>{if $book.onloan}On loan{else}Book Available{/if}</strong>
+		{if $book.image_url != ""}<img class="emblem" src="{$book.image_url|escape}" alt="{$book.title|escape}" />{/if}
+		<p>Author: {$book.author}</p>
+	{if isset($book.description)}
+		<div>{$book.description}</div>
+	{/if}
+	<div class="clear"></div>
+	</div>
+	<div class="hollowfoot"><div><div></div></div></div>
+</div>
diff --git a/templates/library.tpl b/templates/library.tpl
index cc736d0..1ff2ea9 100644
--- a/templates/library.tpl
+++ b/templates/library.tpl
@@ -13,20 +13,9 @@
 	{/foreach}
 
 {elseif $mode == 'display'}
-<div class="box">
-	<div class="boxhead"><h2>{$book.title}</h2></div>
-	<div class="boxcontent">
-		{if $book.image_url != ""}<img class="emblem" src="{$book.image_url|escape}" alt="{$book.title|escape}" />{/if}
-		<p>{$book.author}</p>
-		<p>{$book.description}</p>
-		{if $book.onloan}<p>On loan</p>{else}<p>Book Available</p>{/if}
-		{if $editable==true}
-			<p>Loan stuff to people maybe</p>
-		{/if}
-	<div class="clear"></div>
-	</div>
-	<div class="hollowfoot"><div><div></div></div></div>
-</div>
+
+{include file=library-book.tpl book=$book}
+
 {elseif $mode == 'search'}
 <h3>Search Results</h3>
 	{if $results|@count < 1}<p>No results found</p>
@@ -49,4 +38,4 @@
 {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>
+<p>Images and descriptions provided by <a href="http://www.amazon.co.uk">Amazon</a></p>
-- 
GitLab