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