Bug 16636: t/00-load.t warning from C4/External/BakerTaylor.pm
[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
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.
11 #
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.
16 #
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>.
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(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
31 use vars qw($user $pass $agent $image_url $link_url);
32 &initialize;
33
34 BEGIN {
35         require Exporter;
36         @ISA = qw(Exporter);
37     $VERSION = 3.07.00.049;
38         @EXPORT_OK = qw(&availability &content_cafe &image_url &link_url &http_jacket_link);
39         %EXPORT_TAGS = (all=>\@EXPORT_OK);
40 }
41
42 sub initialize {
43         $user     = (@_ ? shift : C4::Context->preference('BakerTaylorUsername')    ) || ''; # LL17984
44         $pass     = (@_ ? shift : C4::Context->preference('BakerTaylorPassword')    ) || ''; # CC82349
45         $link_url = (@_ ? shift : C4::Context->preference('BakerTaylorBookstoreURL'));
46         $image_url = "http://contentcafe2.btol.com/ContentCafe/Jacket.aspx?UserID=$user&Password=$pass&Options=Y&Return=T&Type=S&Value=";
47         $agent = "Koha/$VERSION [en] (Linux)";
48                         #"Mozilla/4.76 [en] (Win98; U)",        #  if for some reason you want to go stealth, you might prefer this
49 }
50
51 sub image_url {
52         ($user and $pass) or return;
53         my $isbn = (@_ ? shift : '');
54         $isbn =~ s/(p|-)//g;    # sanitize
55         return $image_url . $isbn;
56 }
57 sub link_url {
58         my $isbn = (@_ ? shift : '');
59         $isbn =~ s/(p|-)//g;    # sanitize
60         $link_url or return;
61         return $link_url . $isbn;
62 }
63 sub content_cafe_url {
64         ($user and $pass) or return;
65         my $isbn = (@_ ? shift : '');
66         $isbn =~ s/(p|-)//g;    # sanitize
67         return "http://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=$user&Password=$pass&Options=Y&ItemKey=$isbn";
68 }
69 sub http_jacket_link {
70         my $isbn = shift or return;
71         $isbn =~ s/(p|-)//g;    # sanitize
72         my $image = availability($isbn);
73         my $alt = "Buy this book";
74         $image and $image = qq(<img class="btjacket" alt="$alt" src="$image" />);
75         my $link = &link_url($isbn);
76         unless ($link) {return $image || '';}
77         return sprintf qq(<a class="btlink" href="%s">%s</a>),$link,($image||$alt);
78 }
79
80 sub availability {
81         my $isbn = shift or return;
82         ($user and $pass) or return;
83         $isbn =~ s/(p|-)//g;    # sanitize
84         my $url = "http://contentcafe2.btol.com/ContentCafe/InventoryAvailability.asmx/CheckInventory?UserID=$user&Password=$pass&Value=$isbn";
85         $debug and warn __PACKAGE__ . " request:\n$url\n";
86         my $content = get($url);
87         $debug and print STDERR $content, "\n";
88         warn "could not retrieve $url" unless $content;
89         my $xmlsimple = XML::Simple->new();
90         my $result = $xmlsimple->XMLin($content);
91         if ($result->{Error}) {
92                 warn "Error returned to " . __PACKAGE__ . " : " . $result->{Error};
93         }
94         my $avail = $result->{Availability};
95         return ($avail and $avail !~ /^false$/i) ? &image_url($isbn) : 0;
96 }
97
98 1;
99
100 __END__
101
102 =head1 NAME
103
104 C4::External::BakerTaylor
105
106 =head1 DESCRIPTION
107
108 Functions for retrieving content from Baker and Taylor, inventory availability and "Content Cafe".
109
110 The settings for this module are controlled by System Preferences:
111
112 These can be overridden for testing purposes using the initialize function.
113
114 =head1 FUNCTIONS
115
116 =head2 availability($isbn);
117
118 $isbn is a isbn string
119
120 =head1 NOTES
121
122 A request with failed authentication might see this back from Baker + Taylor: 
123
124  <?xml version="1.0" encoding="utf-8"?>
125  <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">
126    <Key Type="Undefined">string</Key>
127    <Availability>false</Availability>
128    <Error>Invalid UserID</Error>
129  </InventoryAvailability>
130
131 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.
132
133 =head1 SEE ALSO
134
135 LWP::UserAgent
136
137 =head1 AUTHOR
138
139 Joe Atzberger
140 atz AT liblime DOT com
141
142 =cut