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