Hello all!

A while back (a few years, I think!) I wrote and posted to this mailing list a set of php scripts that provided a new books display. When we recently upgraded our koha instance (shifting to Plack in the process!), I decided to rewrite this new books display in perl.

And I thought I'd put it up here in case anyone was interested in it.

Perl's not my language of choice - so if there are any improvements to be made, please let me know.

To use it, simply install the newbooksdisplay.pl in the opac cgi-bin directory, and add the following snippet to the OpacMain systempreferences:

<h2 style="align="center">Newest books in the library</h2>
<div id="newbooks"></div>
<script type="text/javascript">
$("#newbooks").load("/cgi-bin/koha/newbooksdisplay.pl", function() {

KOHA.Google.GetCoverFromIsbn();

});

</script>


I've also modified the google-jackets.js to produce a nice artificial cover if none is available from google books (currently only if the correct HTML5 data tags are supplied, as in the newbooksdisplay)


if (typeof KOHA == "undefined" || !KOHA) {
var KOHA = {};
}

/**
* A namespace for Google related functions.
*/
KOHA.Google = {


/**
* Search all:
* <div title="biblionumber" id="isbn" class="gbs-thumbnail"></div>
* or
* <div title="biblionumber" id="isbn" class="gbs-thumbnail-preview"></div>
* and run a search with all collected isbns to Google Book Search.
* The result is asynchronously returned by Google and catched by
* gbsCallBack().
*/
GetCoverFromIsbn: function(newWindow) {
var bibkeys = [];
$("div [id^=gbs-thumbnail]").each(function(i) {
bibkeys.push($(this).attr("class").split(' ')[0]); // id=isbn
});
bibkeys = bibkeys.join(',');
var scriptElement = document.createElement("script");
this.openInNewWindow=newWindow;
scriptElement.setAttribute("id", "jsonScript");
scriptElement.setAttribute("src",
"http://books.google.com/books?bibkeys=" + escape(bibkeys) +
"&jscmd=viewapi&callback=KOHA.Google.gbsCallBack");
scriptElement.setAttribute("type", "text/javascript");
document.documentElement.firstChild.appendChild(scriptElement);

},

/**
* Add cover pages <div
* and link to preview if div id is gbs-thumbnail-preview
*/
gbsCallBack: function(booksInfo) {
var target = '';
if (this.openInNewWindow) {
target = 'target="_blank" ';
}
for (id in booksInfo) {
var book = booksInfo[id];
$("."+book.bib_key).each(function() {
var gbsre = /^gbs-thumbnail/;
if ( gbsre.exec($(this).attr("id")) ) {
var a = document.createElement("a");
a.href = book.info_url;
if (typeof(book.thumbnail_url) != "undefined") {
var img = document.createElement("img");
img.setAttribute("class","thumbnail");
img.src = book.thumbnail_url+"&zoom=1";
$(this).append(img);
var re = /^gbs-thumbnail-preview/;
if ( re.exec($(this).attr("id")) ) {
$(this).append(
'<div style="margin-bottom:5px; margin-top:-5px;font-size:9px">' +
'<a '+target+'href="' +
book.info_url +
'"><img src="' +
'http://books.google.com/intl/en/googlebooks/images/gbs_preview_sticker1.gif' +
'"></a></div>'
);
}
} else if ($(this).attr('data-title')) {
var fakeCoverDiv = document.createElement("div");
$(fakeCoverDiv).attr("class", "fakeCover");
$(this).append(fakeCoverDiv);
var auSpan = document.createElement("span");
$(auSpan).attr("class", "author");
$(auSpan).html($(this).attr("data-author"));
$(fakeCoverDiv).append(auSpan);
var tiSpan = document.createElement("span");
$(tiSpan).attr("class", "title");
$(tiSpan).html($(this).attr("data-title"));
$(fakeCoverDiv).append(tiSpan);
} else {
var message = document.createElement("span");
$(message).attr("class","no-image");
$(message).html(NO_GOOGLE_JACKET);
$(this).append(message);
}
}
});
}
}
};