Bug 21503: Tests for the existing behavior
[koha.git] / ill / ill-requests.pl
blobf31577eea9e20bdd137510d4e2383a5490405a19
1 #!/usr/bin/perl
3 # Copyright 2013 PTFS-Europe Ltd and Mark Gavillet
4 # Copyright 2014 PTFS-Europe Ltd
6 # 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 Modern::Perl;
22 use CGI;
24 use C4::Auth;
25 use C4::Output;
26 use Koha::AuthorisedValues;
27 use Koha::Illcomment;
28 use Koha::Illrequests;
29 use Koha::Libraries;
30 use Koha::Token;
32 use Try::Tiny;
33 use URI::Escape;
35 our $cgi = CGI->new;
36 my $illRequests = Koha::Illrequests->new;
38 # Grab all passed data
39 # 'our' since Plack changes the scoping
40 # of 'my'
41 our $params = $cgi->Vars();
43 # Leave immediately if ILLModule is disabled
44 unless ( C4::Context->preference('ILLModule') ) {
45 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
46 exit;
49 my $op = $params->{method} || 'illlist';
51 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
52 template_name => 'ill/ill-requests.tt',
53 query => $cgi,
54 type => 'intranet',
55 flagsrequired => { ill => '*' },
56 } );
58 # Are we able to actually work?
59 my $cfg = Koha::Illrequest::Config->new;
60 my $backends = $cfg->available_backends;
61 my $has_branch = $cfg->has_branch;
62 my $backends_available = ( scalar @{$backends} > 0 );
63 $template->param(
64 backends_available => $backends_available,
65 has_branch => $has_branch
68 if ( $backends_available ) {
69 if ( $op eq 'illview' ) {
70 # View the details of an ILL
71 my $request = Koha::Illrequests->find($params->{illrequest_id});
73 $template->param(
74 request => $request,
75 csrf_token => Koha::Token->new->generate_csrf({
76 session_id => scalar $cgi->cookie('CGISESSID'),
77 }),
78 ( $params->{error} ? ( error => $params->{error} ) : () ),
81 } elsif ( $op eq 'create' ) {
82 # We're in the process of creating a request
83 my $request = Koha::Illrequest->new->load_backend( $params->{backend} );
84 my $backend_result = $request->backend_create($params);
85 $template->param(
86 whole => $backend_result,
87 request => $request
89 handle_commit_maybe($backend_result, $request);
91 } elsif ( $op eq 'migrate' ) {
92 # We're in the process of migrating a request
93 my $request = Koha::Illrequests->find($params->{illrequest_id});
94 my $backend_result;
95 if ( $params->{backend} ) {
96 my $new_request = Koha::Illrequest->new->load_backend( $params->{backend} );
97 $backend_result = $new_request->backend_migrate($params);
98 if ($backend_result) {
99 $template->param(
100 whole => $backend_result,
101 request => $new_request
103 $request = $new_request;
104 } else {
105 # Backend failure, redirect back to illview
106 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
107 . '?method=illview'
108 . '&illrequest_id='
109 . $request->id
110 . '&error=migrate_target' );
111 exit;
114 else {
115 $backend_result = $request->backend_migrate($params);
116 $template->param(
117 whole => $backend_result,
118 request => $request
121 handle_commit_maybe( $backend_result, $request );
123 } elsif ( $op eq 'confirm' ) {
124 # Backend 'confirm' method
125 # confirm requires a specific request, so first, find it.
126 my $request = Koha::Illrequests->find($params->{illrequest_id});
127 my $backend_result = $request->backend_confirm($params);
128 $template->param(
129 whole => $backend_result,
130 request => $request,
133 # handle special commit rules & update type
134 handle_commit_maybe($backend_result, $request);
136 } elsif ( $op eq 'cancel' ) {
137 # Backend 'cancel' method
138 # cancel requires a specific request, so first, find it.
139 my $request = Koha::Illrequests->find($params->{illrequest_id});
140 my $backend_result = $request->backend_cancel($params);
141 $template->param(
142 whole => $backend_result,
143 request => $request,
146 # handle special commit rules & update type
147 handle_commit_maybe($backend_result, $request);
149 } elsif ( $op eq 'edit_action' ) {
150 # Handle edits to the Illrequest object.
151 # (not the Illrequestattributes)
152 # We simulate the API for backend requests for uniformity.
153 # So, init:
154 my $request = Koha::Illrequests->find($params->{illrequest_id});
155 if ( !$params->{stage} ) {
156 my $backend_result = {
157 error => 0,
158 status => '',
159 message => '',
160 method => 'edit_action',
161 stage => 'init',
162 next => '',
163 value => {}
165 $template->param(
166 whole => $backend_result,
167 request => $request
169 } else {
170 # Commit:
171 # Save the changes
172 $request->borrowernumber($params->{borrowernumber});
173 $request->biblio_id($params->{biblio_id});
174 $request->branchcode($params->{branchcode});
175 $request->price_paid($params->{price_paid});
176 $request->notesopac($params->{notesopac});
177 $request->notesstaff($params->{notesstaff});
178 my $alias = (length $params->{status_alias} > 0) ?
179 $params->{status_alias} :
180 "-1";
181 $request->status_alias($alias);
182 $request->store;
183 my $backend_result = {
184 error => 0,
185 status => '',
186 message => '',
187 method => 'edit_action',
188 stage => 'commit',
189 next => 'illlist',
190 value => {}
192 handle_commit_maybe($backend_result, $request);
195 } elsif ( $op eq 'moderate_action' ) {
196 # Moderate action is required for an ILL submodule / syspref.
197 # Currently still needs to be implemented.
198 redirect_to_list();
200 } elsif ( $op eq 'delete_confirm') {
201 my $request = Koha::Illrequests->find($params->{illrequest_id});
203 $template->param(
204 request => $request
207 } elsif ( $op eq 'delete' ) {
209 # Check if the request is confirmed, if not, redirect
210 # to the confirmation view
211 if ($params->{confirmed}) {
212 # We simply delete the request...
213 Koha::Illrequests->find( $params->{illrequest_id} )->delete;
214 # ... then return to list view.
215 redirect_to_list();
216 } else {
217 print $cgi->redirect(
218 "/cgi-bin/koha/ill/ill-requests.pl?" .
219 "method=delete_confirm&illrequest_id=" .
220 $params->{illrequest_id});
221 exit;
224 } elsif ( $op eq 'mark_completed' ) {
225 my $request = Koha::Illrequests->find($params->{illrequest_id});
226 my $backend_result = $request->mark_completed($params);
227 $template->param(
228 whole => $backend_result,
229 request => $request,
232 # handle special commit rules & update type
233 handle_commit_maybe($backend_result, $request);
235 } elsif ( $op eq 'generic_confirm' ) {
236 my $backend_result;
237 my $request;
238 try {
239 $request = Koha::Illrequests->find($params->{illrequest_id});
240 $params->{current_branchcode} = C4::Context->mybranch;
241 $backend_result = $request->generic_confirm($params);
242 $template->param(
243 whole => $backend_result,
244 request => $request,
246 $template->param( error => $params->{error} )
247 if $params->{error};
249 catch {
250 my $error;
251 if ( ref($_) eq 'Koha::Exceptions::Ill::NoTargetEmail' ) {
252 $error = 'no_target_email';
254 elsif ( ref($_) eq 'Koha::Exceptions::Ill::NoLibraryEmail' ) {
255 $error = 'no_library_email';
257 else {
258 $error = 'unknown_error';
260 print $cgi->redirect(
261 "/cgi-bin/koha/ill/ill-requests.pl?" .
262 "method=generic_confirm&illrequest_id=" .
263 $params->{illrequest_id} .
264 "&error=$error" );
265 exit;
268 # handle special commit rules & update type
269 handle_commit_maybe($backend_result, $request);
270 } elsif ( $op eq 'check_out') {
271 my $request = Koha::Illrequests->find($params->{illrequest_id});
272 my $backend_result = $request->check_out($params);
273 $template->param(
274 params => $params,
275 whole => $backend_result,
276 request => $request
278 } elsif ( $op eq 'illlist') {
280 # If we receive a pre-filter, make it available to the template
281 my $possible_filters = ['borrowernumber'];
282 my $active_filters = {};
283 foreach my $filter(@{$possible_filters}) {
284 if ($params->{$filter}) {
285 # We shouldn't need to escape $filter here since we're using
286 # a whitelist, but just to be sure...
287 $active_filters->{uri_escape_utf8($filter)} =
288 uri_escape_utf8(scalar $params->{$filter});
291 my @tpl_arr = ();
292 if (keys %{$active_filters}) {
293 foreach my $key (keys %{$active_filters}) {
294 push @tpl_arr, $key . "=" . $active_filters->{$key};
297 $template->param(
298 prefilters => join("&", @tpl_arr)
300 } elsif ( $op eq "save_comment" ) {
301 die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
302 session_id => scalar $cgi->cookie('CGISESSID'),
303 token => scalar $cgi->param('csrf_token'),
305 my $comment = Koha::Illcomment->new({
306 illrequest_id => scalar $params->{illrequest_id},
307 borrowernumber => $patronnumber,
308 comment => scalar $params->{comment},
310 $comment->store();
311 # Redirect to view the whole request
312 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
313 scalar $params->{illrequest_id}
315 exit;
317 } else {
318 my $request = Koha::Illrequests->find($params->{illrequest_id});
319 my $backend_result = $request->custom_capability($op, $params);
320 $template->param(
321 whole => $backend_result,
322 request => $request,
325 # handle special commit rules & update type
326 handle_commit_maybe($backend_result, $request);
330 $template->param(
331 backends => $backends,
332 types => [ "Book", "Article", "Journal" ],
333 query_type => $op,
334 branches => scalar Koha::Libraries->search,
337 output_html_with_http_headers( $cgi, $cookie, $template->output );
339 sub handle_commit_maybe {
340 my ( $backend_result, $request ) = @_;
342 # We need to special case 'commit'
343 if ( $backend_result->{stage} eq 'commit' ) {
344 if ( $backend_result->{next} eq 'illview' ) {
346 # Redirect to a view of the newly created request
347 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
348 . '?method=illview'
349 . '&illrequest_id='
350 . $request->id );
351 exit;
353 elsif ( $backend_result->{next} eq 'emigrate' ) {
355 # Redirect to a view of the newly created request
356 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
357 . '?method=migrate'
358 . '&stage=emigrate'
359 . '&illrequest_id='
360 . $request->id );
361 exit;
363 else {
364 # Redirect to a requests list view
365 redirect_to_list();
370 sub redirect_to_list {
371 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
372 exit;