Bug 13457: Suggestions.t expects sample itemtypes
[koha.git] / C4 / External / BakerTaylor.pm
blobd49aa8dc7ff941d1ea9997636ac9c0e8a7ea4a79
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use XML::Simple;
21 use LWP::Simple;
22 # use LWP::UserAgent;
23 use HTTP::Request::Common;
24 use C4::Context;
25 use C4::Debug;
27 use strict;
28 use warnings;
30 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
31 use vars qw($user $pass $agent $image_url $link_url);
33 BEGIN {
34 require Exporter;
35 $VERSION = 3.07.00.049;
36 @ISA = qw(Exporter);
37 @EXPORT_OK = qw(&availability &content_cafe &image_url &link_url &http_jacket_link);
38 %EXPORT_TAGS = (all=>\@EXPORT_OK);
40 INIT {
41 &initialize;
44 sub initialize {
45 $user = (@_ ? shift : C4::Context->preference('BakerTaylorUsername') ) || ''; # LL17984
46 $pass = (@_ ? shift : C4::Context->preference('BakerTaylorPassword') ) || ''; # CC82349
47 $link_url = (@_ ? shift : C4::Context->preference('BakerTaylorBookstoreURL'));
48 $image_url = "http://contentcafe2.btol.com/ContentCafe/Jacket.aspx?UserID=$user&Password=$pass&Options=Y&Return=T&Type=S&Value=";
49 $agent = "Koha/$VERSION [en] (Linux)";
50 #"Mozilla/4.76 [en] (Win98; U)", # if for some reason you want to go stealth, you might prefer this
53 sub image_url {
54 ($user and $pass) or return;
55 my $isbn = (@_ ? shift : '');
56 $isbn =~ s/(p|-)//g; # sanitize
57 return $image_url . $isbn;
59 sub link_url {
60 my $isbn = (@_ ? shift : '');
61 $isbn =~ s/(p|-)//g; # sanitize
62 $link_url or return;
63 return $link_url . $isbn;
65 sub content_cafe_url {
66 ($user and $pass) or return;
67 my $isbn = (@_ ? shift : '');
68 $isbn =~ s/(p|-)//g; # sanitize
69 return "http://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=$user&Password=$pass&Options=Y&ItemKey=$isbn";
71 sub http_jacket_link {
72 my $isbn = shift or return;
73 $isbn =~ s/(p|-)//g; # sanitize
74 my $image = availability($isbn);
75 my $alt = "Buy this book";
76 $image and $image = qq(<img class="btjacket" alt="$alt" src="$image" />);
77 my $link = &link_url($isbn);
78 unless ($link) {return $image || '';}
79 return sprintf qq(<a class="btlink" href="%s">%s</a>),$link,($image||$alt);
82 sub availability {
83 my $isbn = shift or return;
84 ($user and $pass) or return;
85 $isbn =~ s/(p|-)//g; # sanitize
86 my $url = "http://contentcafe2.btol.com/ContentCafe/InventoryAvailability.asmx/CheckInventory?UserID=$user&Password=$pass&Value=$isbn";
87 $debug and warn __PACKAGE__ . " request:\n$url\n";
88 my $content = get($url);
89 $debug and print STDERR $content, "\n";
90 warn "could not retrieve $url" unless $content;
91 my $xmlsimple = XML::Simple->new();
92 my $result = $xmlsimple->XMLin($content);
93 if ($result->{Error}) {
94 warn "Error returned to " . __PACKAGE__ . " : " . $result->{Error};
96 my $avail = $result->{Availability};
97 return ($avail and $avail !~ /^false$/i) ? &image_url($isbn) : 0;
102 __END__
104 =head1 NAME
106 C4::External::BakerTaylor
108 =head1 DESCRIPTION
110 Functions for retrieving content from Baker and Taylor, inventory availability and "Content Cafe".
112 The settings for this module are controlled by System Preferences:
114 These can be overridden for testing purposes using the initialize function.
116 =head1 FUNCTIONS
118 =head2 availability($isbn);
120 $isbn is a isbn string
122 =head1 NOTES
124 A request with failed authentication might see this back from Baker + Taylor:
126 <?xml version="1.0" encoding="utf-8"?>
127 <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">
128 <Key Type="Undefined">string</Key>
129 <Availability>false</Availability>
130 <Error>Invalid UserID</Error>
131 </InventoryAvailability>
133 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.
135 =head1 SEE ALSO
137 LWP::UserAgent
139 =head1 AUTHOR
141 Joe Atzberger
142 atz AT liblime DOT com
144 =cut