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