Bug 7317: Handle backend absense more gracefuly
[koha.git] / ill / ill-requests.pl
1 #!/usr/bin/perl
2
3 # Copyright 2013 PTFS-Europe Ltd and Mark Gavillet
4 # Copyright 2014 PTFS-Europe Ltd
5 #
6 # This file is part of Koha.
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 3 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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use CGI;
23
24 use C4::Auth;
25 use C4::Output;
26 use Koha::AuthorisedValues;
27 use Koha::Illrequests;
28 use Koha::Libraries;
29
30 our $cgi = CGI->new;
31 my $illRequests = Koha::Illrequests->new;
32
33 # Grab all passed data
34 # 'our' since Plack changes the scoping
35 # of 'my'
36 our $params = $cgi->Vars();
37
38 # Leave immediately if ILLModule is disabled
39 unless ( C4::Context->preference('ILLModule') ) {
40     print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
41     exit;
42 }
43
44 my $op = $params->{method} || 'illlist';
45
46 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
47     template_name => 'ill/ill-requests.tt',
48     query         => $cgi,
49     type          => 'intranet',
50     flagsrequired => { ill => '*' },
51 } );
52
53 # Are we able to actually work?
54 my $backends = Koha::Illrequest::Config->new->available_backends;
55 my $backends_available = ( scalar @{$backends} > 0 );
56 $template->param( backends_available => $backends_available );
57
58 if ( $backends_available ) {
59     if ( $op eq 'illview' ) {
60         # View the details of an ILL
61         my $request = Koha::Illrequests->find($params->{illrequest_id});
62
63         $template->param(
64             request => $request
65         );
66
67     } elsif ( $op eq 'create' ) {
68         # We're in the process of creating a request
69         my $request = Koha::Illrequest->new->load_backend( $params->{backend} );
70         my $backend_result = $request->backend_create($params);
71         $template->param(
72             whole   => $backend_result,
73             request => $request
74         );
75         handle_commit_maybe($backend_result, $request);
76
77     } elsif ( $op eq 'confirm' ) {
78         # Backend 'confirm' method
79         # confirm requires a specific request, so first, find it.
80         my $request = Koha::Illrequests->find($params->{illrequest_id});
81         my $backend_result = $request->backend_confirm($params);
82         $template->param(
83             whole   => $backend_result,
84             request => $request,
85         );
86
87         # handle special commit rules & update type
88         handle_commit_maybe($backend_result, $request);
89
90     } elsif ( $op eq 'cancel' ) {
91         # Backend 'cancel' method
92         # cancel requires a specific request, so first, find it.
93         my $request = Koha::Illrequests->find($params->{illrequest_id});
94         my $backend_result = $request->backend_cancel($params);
95         $template->param(
96             whole   => $backend_result,
97             request => $request,
98         );
99
100         # handle special commit rules & update type
101         handle_commit_maybe($backend_result, $request);
102
103     } elsif ( $op eq 'edit_action' ) {
104         # Handle edits to the Illrequest object.
105         # (not the Illrequestattributes)
106         # We simulate the API for backend requests for uniformity.
107         # So, init:
108         my $request = Koha::Illrequests->find($params->{illrequest_id});
109         if ( !$params->{stage} ) {
110             my $backend_result = {
111                 error   => 0,
112                 status  => '',
113                 message => '',
114                 method  => 'edit_action',
115                 stage   => 'init',
116                 next    => '',
117                 value   => {}
118             };
119             $template->param(
120                 whole   => $backend_result,
121                 request => $request
122             );
123         } else {
124             # Commit:
125             # Save the changes
126             $request->borrowernumber($params->{borrowernumber});
127             $request->biblio_id($params->{biblio_id});
128             $request->branchcode($params->{branchcode});
129             $request->notesopac($params->{notesopac});
130             $request->notesstaff($params->{notesstaff});
131             $request->store;
132             my $backend_result = {
133                 error   => 0,
134                 status  => '',
135                 message => '',
136                 method  => 'edit_action',
137                 stage   => 'commit',
138                 next    => 'illlist',
139                 value   => {}
140             };
141             handle_commit_maybe($backend_result, $request);
142         }
143
144     } elsif ( $op eq 'moderate_action' ) {
145         # Moderate action is required for an ILL submodule / syspref.
146         # Currently still needs to be implemented.
147         redirect_to_list();
148
149     } elsif ( $op eq 'delete_confirm') {
150         my $request = Koha::Illrequests->find($params->{illrequest_id});
151
152         $template->param(
153             request => $request
154         );
155
156     } elsif ( $op eq 'delete' ) {
157
158         # Check if the request is confirmed, if not, redirect
159         # to the confirmation view
160         if ($params->{confirmed}) {
161             # We simply delete the request...
162             Koha::Illrequests->find( $params->{illrequest_id} )->delete;
163             # ... then return to list view.
164             redirect_to_list();
165         } else {
166             print $cgi->redirect(
167                 "/cgi-bin/koha/ill/ill-requests.pl?" .
168                 "method=delete_confirm&illrequest_id=" .
169                 $params->{illrequest_id});
170             exit;
171         }
172
173     } elsif ( $op eq 'mark_completed' ) {
174         my $request = Koha::Illrequests->find($params->{illrequest_id});
175         my $backend_result = $request->mark_completed($params);
176         $template->param(
177             whole => $backend_result,
178             request => $request,
179         );
180
181         # handle special commit rules & update type
182         handle_commit_maybe($backend_result, $request);
183
184     } elsif ( $op eq 'generic_confirm' ) {
185         my $request = Koha::Illrequests->find($params->{illrequest_id});
186         $params->{current_branchcode} = C4::Context->mybranch;
187         my $backend_result = $request->generic_confirm($params);
188         $template->param(
189             whole => $backend_result,
190             request => $request,
191         );
192
193         # handle special commit rules & update type
194         handle_commit_maybe($backend_result, $request);
195
196     } elsif ( $op eq 'illlist') {
197         # Display all current ILLs
198         my $requests = $illRequests->search();
199
200         $template->param(
201             requests => $requests
202         );
203
204         # If we receive a pre-filter, make it available to the template
205         my $possible_filters = ['borrowernumber'];
206         my $active_filters = [];
207         foreach my $filter(@{$possible_filters}) {
208             if ($params->{$filter}) {
209                 push @{$active_filters},
210                     { name => $filter, value => $params->{$filter}};
211             }
212         }
213         if (scalar @{$active_filters} > 0) {
214             $template->param(
215                 prefilters => $active_filters
216             );
217         }
218     } else {
219         my $request = Koha::Illrequests->find($params->{illrequest_id});
220         my $backend_result = $request->custom_capability($op, $params);
221         $template->param(
222             whole => $backend_result,
223             request => $request,
224         );
225
226         # handle special commit rules & update type
227         handle_commit_maybe($backend_result, $request);
228     }
229 }
230
231 $template->param(
232     backends   => $backends,
233     media      => [ "Book", "Article", "Journal" ],
234     query_type => $op,
235     branches   => Koha::Libraries->search,
236     here_link  => "/cgi-bin/koha/ill/ill-requests.pl"
237 );
238
239 output_html_with_http_headers( $cgi, $cookie, $template->output );
240
241 sub handle_commit_maybe {
242     my ( $backend_result, $request ) = @_;
243     # We need to special case 'commit'
244     if ( $backend_result->{stage} eq 'commit' ) {
245         if ( $backend_result->{next} eq 'illview' ) {
246             # Redirect to a view of the newly created request
247             print $cgi->redirect(
248                 '/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id='.
249                 $request->id
250             );
251         } else {
252             # Redirect to a requests list view
253             redirect_to_list();
254         }
255     }
256 }
257
258 sub redirect_to_list {
259     print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
260     exit;
261 }