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