1 package C4::External::BakerTaylor;
2 # Copyright (C) 2008 LibLime
3 # <jmf at liblime dot com>
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use HTTP::Request::Common;
30 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
31 use vars qw($user $pass $agent $image_url $link_url);
36 @EXPORT_OK = qw(&availability &content_cafe &image_url &link_url &http_jacket_link);
37 %EXPORT_TAGS = (all=>\@EXPORT_OK);
44 $user = (@_ ? shift : C4::Context->preference('BakerTaylorUsername') ) || ''; # LL17984
45 $pass = (@_ ? shift : C4::Context->preference('BakerTaylorPassword') ) || ''; # CC82349
46 $link_url = (@_ ? shift : C4::Context->preference('BakerTaylorBookstoreURL'));
47 $image_url = "http://contentcafe2.btol.com/ContentCafe/Jacket.aspx?UserID=$user&Password=$pass&Options=Y&Return=T&Type=S&Value=";
48 $agent = "Koha/$VERSION [en] (Linux)";
49 #"Mozilla/4.76 [en] (Win98; U)", # if for some reason you want to go stealth, you might prefer this
53 ($user and $pass) or return;
54 my $isbn = (@_ ? shift : '');
55 $isbn =~ s/(p|-)//g; # sanitize
56 return $image_url . $isbn;
59 my $isbn = (@_ ? shift : '');
60 $isbn =~ s/(p|-)//g; # sanitize
62 return $link_url . $isbn;
64 sub content_cafe_url {
65 ($user and $pass) or return;
66 my $isbn = (@_ ? shift : '');
67 $isbn =~ s/(p|-)//g; # sanitize
68 return "http://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=$user&Password=$pass&Options=Y&ItemKey=$isbn";
70 sub http_jacket_link {
71 my $isbn = shift or return;
72 $isbn =~ s/(p|-)//g; # sanitize
73 my $image = availability($isbn);
74 my $alt = "Buy this book";
75 $image and $image = qq(<img class="btjacket" alt="$alt" src="$image" />);
76 my $link = &link_url($isbn);
77 unless ($link) {return $image || '';}
78 return sprintf qq(<a class="btlink" href="%s">%s</a>),$link,($image||$alt);
82 my $isbn = shift or return;
83 ($user and $pass) or return;
84 $isbn =~ s/(p|-)//g; # sanitize
85 my $url = "http://contentcafe2.btol.com/ContentCafe/InventoryAvailability.asmx/CheckInventory?UserID=$user&Password=$pass&Value=$isbn";
86 $debug and warn __PACKAGE__ . " request:\n$url\n";
87 my $content = get($url);
88 $debug and print STDERR $content, "\n";
89 warn "could not retrieve $url" unless $content;
90 my $xmlsimple = XML::Simple->new();
91 my $result = $xmlsimple->XMLin($content);
92 if ($result->{Error}) {
93 warn "Error returned to " . __PACKAGE__ . " : " . $result->{Error};
95 my $avail = $result->{Availability};
96 return ($avail and $avail !~ /^false$/i) ? &image_url($isbn) : 0;
105 C4::External::BakerTaylor
109 Functions for retrieving content from Baker and Taylor, inventory availability and "Content Cafe".
111 The settings for this module are controlled by System Preferences:
113 These can be overridden for testing purposes using the initialize function.
117 =head2 availability($isbn);
119 $isbn is a isbn string
123 A request with failed authentication might see this back from Baker + Taylor:
125 <?xml version="1.0" encoding="utf-8"?>
126 <InventoryAvailability xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" DateTime="2008-03-07T22:01:25.6520429-05:00" xmlns="http://ContentCafe2.btol.com">
127 <Key Type="Undefined">string</Key>
128 <Availability>false</Availability>
129 <Error>Invalid UserID</Error>
130 </InventoryAvailability>
132 Such response will trigger a warning for each request (potentially many). Point being, do not leave this module configured with incorrect username and password in production.
141 atz AT liblime DOT com