Added comments to ILS-DI code
[koha.git] / opac / ilsdi.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 SARL Biblibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use C4::ILSDI::Services;
21 use C4::Auth;
22 use C4::Output;
23 use C4::Context;
24 use XML::Simple;
25 use CGI;
26
27 =head1 DLF ILS-DI for Koha
28
29 This script is a basic implementation of ILS-DI protocol for Koha.
30 It acts like a dispatcher, that get the CGI request, check required and 
31 optionals arguments, call a function from C4::ILS-DI::Services, and finaly 
32 outputs the returned hashref as XML.
33
34 =cut
35
36 # Instanciate the CGI request
37 my $cgi = new CGI;
38
39 # List of available services, sorted by level
40 my @services = (
41         'Describe',                          # Not part of ILS-DI, online API doc
42 #       Level 1: Basic Discovery Interfaces
43 #       'HarvestBibliographicRecords',       # OAI-PMH
44 #       'HarvestExpandedRecords',            # OAI-PMH
45         'GetAvailability',                   # FIXME Add bibbliographic level
46 #       'GoToBibliographicRequestPage'       # I don't understant this one
47 #       Level 2: Elementary OPAC supplement
48 #       'HarvestAuthorityRecords',           # OAI-PMH
49 #       'HarvestHoldingsRecords',            # OAI-PMH
50         'GetRecords',                        # Note that we can use OAI-PMH for this too
51 #       'Search',                            # TODO
52 #       'Scan',                              # TODO
53         'GetAuthorityRecords',               
54 #       'OutputRewritablePage',              # I don't understant this one
55 #       'OutputIntermediateFormat',          # I don't understant this one
56 #       Level 3: Elementary OPAC alternative
57         'LookupPatron',
58         'AuthenticatePatron',
59         'GetPatronInfo',
60         'GetPatronStatus',
61         'GetServices',                       # FIXME Loans
62         'RenewLoan', 
63         'HoldTitle',                         # FIXME Add dates support
64         'HoldItem',                          # FIXME Add dates support
65         'CancelHold',
66 #       'RecallItem',                        # Not supported by Koha
67 #       'CancelRecall',                      # Not supported by Koha
68 #       Level 4: Robust/domain specific discovery platforms
69 #       'SearchCourseReserves',              # TODO 
70 #       'Explain'                            # TODO
71 );
72
73 # List of required arguments
74 my %required = (
75         'Describe'            => ['verb'],
76         'GetAvailability'     => ['id', 'id_type'],
77         'GetRecords'          => ['id'],
78         'GetAuthorityRecords' => ['id'],
79         'LookupPatron'        => ['id'],
80         'AuthenticatePatron'  => ['username', 'password'],
81         'GetPatronInfo'       => ['patron_id'],
82         'GetPatronStatus'     => ['patron_id'],
83         'GetServices'         => ['patron_id', 'item_id'],
84         'RenewLoan'           => ['patron_id', 'item_id'],
85         'HoldTitle'           => ['patron_id', 'bib_id', 'request_location'],
86         'HoldItem'            => ['patron_id', 'bib_id', 'item_id'],
87         'CancelHold'          => ['patron_id', 'item_id'],
88 );
89
90 # List of optional arguments
91 my %optional = (
92         'Describe'            => [],
93         'GetAvailability'     => ['return_type', 'return_fmt'],
94         'GetRecords'          => ['schema'],
95         'GetAuthorityRecords' => ['schema'],
96         'LookupPatron'        => ['id_type'],
97         'AuthenticatePatron'  => [],
98         'GetPatronInfo'       => ['show_contact', 'show_fines', 'show_holds', 'show_loans'],
99         'GetPatronStatus'     => [],
100         'GetServices'         => [],
101         'RenewLoan'           => ['desired_due_date'],
102         'HoldTitle'           => ['pickup_location', 'needed_before_date', 'pickup_expiry_date'],
103         'HoldItem'            => ['pickup_location', 'needed_before_date', 'pickup_expiry_date'],
104         'CancelHold'          => [],
105 );
106
107 # If ILS-DI module is disabled in System->Preferences, redirect to 404
108 if (not C4::Context->preference('ILS-DI')) {
109         print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
110 }
111
112 # If no service is requested, display the online documentation
113 if (not $cgi->param('service')) {
114         my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
115             {
116                 template_name   => "ilsdi.tmpl",
117                 query           => $cgi,
118                 type            => "opac",
119                 authnotrequired => 1,
120                 debug           => 1,
121             }
122         );
123         output_html_with_http_headers $cgi, $cookie, $template->output;
124         exit 0;
125 }
126
127 # If user requested a service description, then display it
128 if ($cgi->param('service') eq "Describe" and grep {$cgi->param('verb') eq $_} @services) {
129         my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
130             {
131                 template_name   => "ilsdi.tmpl",
132                 query           => $cgi,
133                 type            => "opac",
134                 authnotrequired => 1,
135                 debug           => 1,
136             }
137         );
138         $template->param( $cgi->param('verb') => 1 );
139         output_html_with_http_headers $cgi, $cookie, $template->output;
140         exit 0;
141 }
142
143 my $service = $cgi->param('service') || "ilsdi";
144
145 my $out;
146
147 # Check if the requested service is in the list
148 if ($service and grep {$service eq $_} @services) {
149
150         my @parmsrequired = @{$required{$service}};
151         my @parmsoptional = @{$optional{$service}};
152         my @parmsall = (@parmsrequired, @parmsoptional);
153         my @names = $cgi->param;
154         my %paramhash = ();
155         foreach my $name (@names) {
156                 $paramhash{$name} = 1;
157         }
158
159         # check for missing parameters
160         foreach my $name (@parmsrequired) {
161                 if ((! exists $paramhash{$name})) {
162                         $out->{'message'} = "missing $name parameter";
163                 }
164         }
165         
166         # check for illegal parameters
167         foreach my $name (@names) {
168                 my $found = 0;
169                 foreach my $name2 (@parmsall) {
170                         if ($name eq $name2) { 
171                                 $found = 1; 
172                         }
173                 }
174                 if (($found == 0) && ($name ne 'service')) {
175                         $out->{'message'} = "$name is an illegal parameter";
176                 }
177         }
178         
179         # check for multiple parameters
180         foreach my $name (@names) {
181                 my @values = $cgi->param($name);
182                 if ($#values != 0) {
183                         $out->{'message'} = "multiple values are not allowed for the $name parameter";
184                 }
185         }
186
187         if (! $out->{'message'}) {
188                 # GetAvailability is a special case, as it cannot use XML::Simple
189                 if ($service eq "GetAvailability") {
190                         print CGI::header('text/xml');
191                         print C4::ILSDI::Services::GetAvailability($cgi);
192                         exit 0;
193                 }
194                 else {
195                         # Variable functions
196                         my $sub = do{
197                                 no strict 'refs';
198                                 my $symbol = 'C4::ILSDI::Services::'.$service;
199                                 \&{"$symbol"};
200                         };
201                         # Call the requested service, and get its return value
202                         $out =  &$sub($cgi);
203                 }
204         }
205
206 else {
207         $out->{'message'} = "NotSupported";
208 }
209
210 # Output XML by passing the hashref to XMLOut
211 print CGI::header('text/xml');
212 print XMLout($out,
213         noattr => 1, 
214         noescape => 1,
215         nosort => 1,
216         xmldecl => '<?xml version="1.0" encoding="ISO-8859-1" ?>', 
217         RootName => $service, 
218         SuppressEmpty => 1);
219