Merge remote-tracking branch 'origin/new/bug_7143'
[koha.git] / C4 / External / BakerTaylor.pm
1 package C4::External::BakerTaylor;
2 # Copyright (C) 2008 LibLime
3 # <jmf at liblime dot com>
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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.
19
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;
26
27 use strict;
28 use warnings;
29
30 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
31 use vars qw($user $pass $agent $image_url $link_url);
32
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);
39 }
40 INIT {
41         &initialize;
42 }
43
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
51 }
52
53 sub image_url (;$) {
54         ($user and $pass) or return undef;
55         my $isbn = (@_ ? shift : '');
56         $isbn =~ s/(p|-)//g;    # sanitize
57         return $image_url . $isbn;
58 }
59 sub link_url (;$) {
60         my $isbn = (@_ ? shift : '');
61         $isbn =~ s/(p|-)//g;    # sanitize
62         $link_url or return undef;
63         return $link_url . $isbn;
64 }
65 sub content_cafe_url ($) {
66         ($user and $pass) or return undef;
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";
70 }
71 sub http_jacket_link ($) {
72         my $isbn = shift or return undef;
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);
80 }
81
82 sub availability ($) {
83         my $isbn = shift or return undef;
84         ($user and $pass) or return undef;
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};
95         }
96         my $avail = $result->{Availability};
97         return ($avail and $avail !~ /^false$/i) ? &image_url($isbn) : 0;
98 }
99
100 1;
101
102 __END__
103
104 =head1 NAME
105
106 C4::External::BakerTaylor
107
108 =head1 DESCRIPTION
109
110 Functions for retrieving content from Baker and Taylor, inventory availability and "Content Cafe".
111
112 The settings for this module are controlled by System Preferences:
113
114 These can be overridden for testing purposes using the initialize function.
115
116 =head1 FUNCTIONS
117
118 =head2 availability($isbn);
119
120 $isbn is a isbn string
121
122 =head1 NOTES
123
124 A request with failed authentication might see this back from Baker + Taylor: 
125
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>
132
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.
134
135 =head1 SEE ALSO
136
137 C4::External::Amazon
138 LWP::UserAgent
139
140 =head1 AUTHOR
141
142 Joe Atzberger
143 atz AT liblime DOT com
144
145 =cut