Bug 16829: Add 'interface' to the log viewer
[koha.git] / C4 / External / BakerTaylor.pm
1 package C4::External::BakerTaylor;
2
3 # Copyright (C) 2008 LibLime
4 # <jmf at liblime dot com>
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use XML::Simple;
22 use LWP::Simple;
23 use HTTP::Request::Common;
24
25 use C4::Context;
26 use C4::Debug;
27
28 use Modern::Perl;
29
30 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
31
32 BEGIN {
33         require Exporter;
34         @ISA = qw(Exporter);
35     $VERSION = 3.07.00.049;
36         @EXPORT_OK = qw(&availability &content_cafe &image_url &link_url &http_jacket_link);
37         %EXPORT_TAGS = (all=>\@EXPORT_OK);
38 }
39
40 # These variables are plack safe: they are initialized each time
41 my ( $user, $pass, $agent, $image_url, $link_url );
42
43 sub _initialize {
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
50 }
51
52 sub image_url {
53     _initialize();
54         ($user and $pass) or return;
55         my $isbn = (@_ ? shift : '');
56         $isbn =~ s/(p|-)//g;    # sanitize
57         return $image_url . $isbn;
58 }
59
60 sub link_url {
61     _initialize();
62         my $isbn = (@_ ? shift : '');
63         $isbn =~ s/(p|-)//g;    # sanitize
64         $link_url or return;
65         return $link_url . $isbn;
66 }
67
68 sub content_cafe_url {
69     _initialize();
70         ($user and $pass) or return;
71         my $isbn = (@_ ? shift : '');
72         $isbn =~ s/(p|-)//g;    # sanitize
73         return "http://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=$user&Password=$pass&Options=Y&ItemKey=$isbn";
74 }
75
76 sub http_jacket_link {
77     _initialize();
78         my $isbn = shift or return;
79         $isbn =~ s/(p|-)//g;    # sanitize
80         my $image = availability($isbn);
81         my $alt = "Buy this book";
82         $image and $image = qq(<img class="btjacket" alt="$alt" src="$image" />);
83         my $link = &link_url($isbn);
84         unless ($link) {return $image || '';}
85         return sprintf qq(<a class="btlink" href="%s">%s</a>),$link,($image||$alt);
86 }
87
88 sub availability {
89     _initialize();
90         my $isbn = shift or return;
91         ($user and $pass) or return;
92         $isbn =~ s/(p|-)//g;    # sanitize
93         my $url = "http://contentcafe2.btol.com/ContentCafe/InventoryAvailability.asmx/CheckInventory?UserID=$user&Password=$pass&Value=$isbn";
94         $debug and warn __PACKAGE__ . " request:\n$url\n";
95         my $content = get($url);
96         $debug and print STDERR $content, "\n";
97         warn "could not retrieve $url" unless $content;
98         my $xmlsimple = XML::Simple->new();
99         my $result = $xmlsimple->XMLin($content);
100         if ($result->{Error}) {
101                 warn "Error returned to " . __PACKAGE__ . " : " . $result->{Error};
102         }
103         my $avail = $result->{Availability};
104         return ($avail and $avail !~ /^false$/i) ? &image_url($isbn) : 0;
105 }
106
107 1;
108
109 __END__
110
111 =head1 NAME
112
113 C4::External::BakerTaylor
114
115 =head1 DESCRIPTION
116
117 Functions for retrieving content from Baker and Taylor, inventory availability and "Content Cafe".
118
119 The settings for this module are controlled by System Preferences:
120
121 These can be overridden for testing purposes using the initialize function.
122
123 =head1 FUNCTIONS
124
125 =head2 availability($isbn);
126
127 $isbn is a isbn string
128
129 =head1 NOTES
130
131 A request with failed authentication might see this back from Baker + Taylor: 
132
133  <?xml version="1.0" encoding="utf-8"?>
134  <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">
135    <Key Type="Undefined">string</Key>
136    <Availability>false</Availability>
137    <Error>Invalid UserID</Error>
138  </InventoryAvailability>
139
140 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
142 =head1 SEE ALSO
143
144 LWP::UserAgent
145
146 =head1 AUTHOR
147
148 Joe Atzberger
149 atz AT liblime DOT com
150
151 =cut