Bug 7317: (QA folowup) 404 if module is disabled (intranet)
[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 if ( $op eq 'illview' ) {
54     # View the details of an ILL
55     my $request = Koha::Illrequests->find($params->{illrequest_id});
56
57     $template->param(
58         request => $request
59     );
60
61 } elsif ( $op eq 'create' ) {
62     # We're in the process of creating a request
63     my $request = Koha::Illrequest->new
64         ->load_backend($params->{backend});
65     my $backend_result = $request->backend_create($params);
66     $template->param(
67         whole   => $backend_result,
68         request => $request
69     );
70     handle_commit_maybe($backend_result, $request);
71
72 } elsif ( $op eq 'confirm' ) {
73     # Backend 'confirm' method
74     # confirm requires a specific request, so first, find it.
75     my $request = Koha::Illrequests->find($params->{illrequest_id});
76     my $backend_result = $request->backend_confirm($params);
77     $template->param(
78         whole   => $backend_result,
79         request => $request,
80     );
81
82     # handle special commit rules & update type
83     handle_commit_maybe($backend_result, $request);
84
85 } elsif ( $op eq 'cancel' ) {
86     # Backend 'cancel' method
87     # cancel requires a specific request, so first, find it.
88     my $request = Koha::Illrequests->find($params->{illrequest_id});
89     my $backend_result = $request->backend_cancel($params);
90     $template->param(
91         whole   => $backend_result,
92         request => $request,
93     );
94
95     # handle special commit rules & update type
96     handle_commit_maybe($backend_result, $request);
97
98 } elsif ( $op eq 'edit_action' ) {
99     # Handle edits to the Illrequest object.
100     # (not the Illrequestattributes)
101     # We simulate the API for backend requests for uniformity.
102     # So, init:
103     my $request = Koha::Illrequests->find($params->{illrequest_id});
104     if ( !$params->{stage} ) {
105         my $backend_result = {
106             error   => 0,
107             status  => '',
108             message => '',
109             method  => 'edit_action',
110             stage   => 'init',
111             next    => '',
112             value   => {}
113         };
114         $template->param(
115             whole   => $backend_result,
116             request => $request
117         );
118     } else {
119         # Commit:
120         # Save the changes
121         $request->borrowernumber($params->{borrowernumber});
122         $request->biblio_id($params->{biblio_id});
123         $request->branchcode($params->{branchcode});
124         $request->notesopac($params->{notesopac});
125         $request->notesstaff($params->{notesstaff});
126         $request->store;
127         my $backend_result = {
128             error   => 0,
129             status  => '',
130             message => '',
131             method  => 'edit_action',
132             stage   => 'commit',
133             next    => 'illlist',
134             value   => {}
135         };
136         handle_commit_maybe($backend_result, $request);
137     }
138
139 } elsif ( $op eq 'moderate_action' ) {
140     # Moderate action is required for an ILL submodule / syspref.
141     # Currently still needs to be implemented.
142     redirect_to_list();
143
144 } elsif ( $op eq 'delete_confirm') {
145     my $request = Koha::Illrequests->find($params->{illrequest_id});
146
147     $template->param(
148         request => $request
149     );
150
151 } elsif ( $op eq 'delete' ) {
152
153     # Check if the request is confirmed, if not, redirect
154     # to the confirmation view
155     if ($params->{confirmed} == 1) {
156         # We simply delete the request...
157         my $request = Koha::Illrequests->find(
158             $params->{illrequest_id}
159         )->delete;
160         # ... then return to list view.
161         redirect_to_list();
162     } else {
163         print $cgi->redirect(
164             "/cgi-bin/koha/ill/ill-requests.pl?" .
165             "method=delete_confirm&illrequest_id=" .
166             $params->{illrequest_id});
167     }
168
169 } elsif ( $op eq 'mark_completed' ) {
170     my $request = Koha::Illrequests->find($params->{illrequest_id});
171     my $backend_result = $request->mark_completed($params);
172     $template->param(
173         whole => $backend_result,
174         request => $request,
175     );
176
177     # handle special commit rules & update type
178     handle_commit_maybe($backend_result, $request);
179
180 } elsif ( $op eq 'generic_confirm' ) {
181     my $request = Koha::Illrequests->find($params->{illrequest_id});
182     $params->{current_branchcode} = C4::Context->mybranch;
183     my $backend_result = $request->generic_confirm($params);
184     $template->param(
185         whole => $backend_result,
186         request => $request,
187     );
188
189     # handle special commit rules & update type
190     handle_commit_maybe($backend_result, $request);
191
192 } elsif ( $op eq 'illlist') {
193     # Display all current ILLs
194     my $requests = $illRequests->search();
195
196     $template->param(
197         requests => $requests
198     );
199
200     # If we receive a pre-filter, make it available to the template
201     my $possible_filters = ['borrowernumber'];
202     my $active_filters = [];
203     foreach my $filter(@{$possible_filters}) {
204         if ($params->{$filter}) {
205             push @{$active_filters},
206                 { name => $filter, value => $params->{$filter}};
207         }
208     }
209     if (scalar @{$active_filters} > 0) {
210         $template->param(
211             prefilters => $active_filters
212         );
213     }
214 } else {
215     my $request = Koha::Illrequests->find($params->{illrequest_id});
216     my $backend_result = $request->custom_capability($op, $params);
217     $template->param(
218         whole => $backend_result,
219         request => $request,
220     );
221
222     # handle special commit rules & update type
223     handle_commit_maybe($backend_result, $request);
224 }
225
226 # Get a list of backends
227 my $ir = Koha::Illrequest->new;
228
229 $template->param(
230     backends    => $ir->available_backends,
231     media       => [ "Book", "Article", "Journal" ],
232     query_type  => $op,
233     branches    => Koha::Libraries->search->unblessed,
234     here_link   => "/cgi-bin/koha/ill/ill-requests.pl"
235 );
236
237 output_html_with_http_headers( $cgi, $cookie, $template->output );
238
239 sub handle_commit_maybe {
240     my ( $backend_result, $request ) = @_;
241     # We need to special case 'commit'
242     if ( $backend_result->{stage} eq 'commit' ) {
243         if ( $backend_result->{next} eq 'illview' ) {
244             # Redirect to a view of the newly created request
245             print $cgi->redirect(
246                 '/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id='.
247                 $request->id
248             );
249         } else {
250             # Redirect to a requests list view
251             redirect_to_list();
252         }
253     }
254 }
255
256 sub redirect_to_list {
257     print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
258 }