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