Bug 22818: Add generation and sending of notices
[koha.git] / ill / ill-requests.pl
blobdfc27efcba26ddbda3ecb766dee04e96be8692f9
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::Notice::Templates;
27 use Koha::AuthorisedValues;
28 use Koha::Illcomment;
29 use Koha::Illrequests;
30 use Koha::Illrequest::Availability;
31 use Koha::Libraries;
32 use Koha::Token;
34 use Try::Tiny;
35 use URI::Escape;
36 use JSON;
38 our $cgi = CGI->new;
39 my $illRequests = Koha::Illrequests->new;
41 # Grab all passed data
42 # 'our' since Plack changes the scoping
43 # of 'my'
44 our $params = $cgi->Vars();
46 # Leave immediately if ILLModule is disabled
47 unless ( C4::Context->preference('ILLModule') ) {
48 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
49 exit;
52 my $op = $params->{method} || 'illlist';
54 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
55 template_name => 'ill/ill-requests.tt',
56 query => $cgi,
57 type => 'intranet',
58 flagsrequired => { ill => '*' },
59 } );
61 # Are we able to actually work?
62 my $cfg = Koha::Illrequest::Config->new;
63 my $backends = $cfg->available_backends;
64 my $has_branch = $cfg->has_branch;
65 my $backends_available = ( scalar @{$backends} > 0 );
66 $template->param(
67 backends_available => $backends_available,
68 has_branch => $has_branch
71 if ( $backends_available ) {
72 if ( $op eq 'illview' ) {
73 # View the details of an ILL
74 my $request = Koha::Illrequests->find($params->{illrequest_id});
76 # Get the details for notices that can be sent from here
77 my $notices = Koha::Notice::Templates->search(
79 module => 'ill',
80 code => { -in => [ 'ILL_PICKUP_READY' ,'ILL_REQUEST_UNAVAIL' ] },
83 columns => [ qw/code name/ ],
84 distinct => 1
86 )->unblessed;
88 $template->param(
89 notices => $notices,
90 request => $request,
91 csrf_token => Koha::Token->new->generate_csrf({
92 session_id => scalar $cgi->cookie('CGISESSID'),
93 }),
94 ( $params->{tran_error} ?
95 ( tran_error => $params->{tran_error} ) : () ),
96 ( $params->{tran_success} ?
97 ( tran_success => $params->{tran_success} ) : () ),
100 } elsif ( $op eq 'create' ) {
101 # We're in the process of creating a request
102 my $request = Koha::Illrequest->new->load_backend( $params->{backend} );
103 # Does this backend enable us to insert an availability stage and should
104 # we? If not, proceed as normal.
105 if (
106 # If the user has elected to continue with the request despite
107 # having viewed availability info, this flag will be set
108 C4::Context->preference("ILLCheckAvailability")
109 && !$params->{checked_availability}
110 && $request->_backend_capability( 'should_display_availability', $params )
112 # Establish which of the installed availability providers
113 # can service our metadata
114 my $availability = Koha::Illrequest::Availability->new($params);
115 my $services = $availability->get_services({
116 ui_context => 'staff'
118 if (scalar @{$services} > 0) {
119 # Modify our method so we use the correct part of the
120 # template
121 $op = 'availability';
122 $params->{method} = 'availability';
123 delete $params->{stage};
124 # Prepare the metadata we're sending them
125 my $metadata = $availability->prep_metadata($params);
126 $template->param(
127 whole => $params,
128 metadata => $metadata,
129 services_json => scalar encode_json($services),
130 services => $services
132 } else {
133 # No services can process this metadata, so continue as normal
134 my $backend_result = $request->backend_create($params);
135 $template->param(
136 whole => $backend_result,
137 request => $request
139 handle_commit_maybe($backend_result, $request);
141 } else {
142 my $backend_result = $request->backend_create($params);
143 $template->param(
144 whole => $backend_result,
145 request => $request
147 handle_commit_maybe($backend_result, $request);
150 } elsif ( $op eq 'migrate' ) {
151 # We're in the process of migrating a request
152 my $request = Koha::Illrequests->find($params->{illrequest_id});
153 my $backend_result;
154 if ( $params->{backend} ) {
155 my $new_request = Koha::Illrequest->new->load_backend( $params->{backend} );
156 $backend_result = $new_request->backend_migrate($params);
157 if ($backend_result) {
158 $template->param(
159 whole => $backend_result,
160 request => $new_request
162 $request = $new_request;
163 } else {
164 # Backend failure, redirect back to illview
165 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
166 . '?method=illview'
167 . '&illrequest_id='
168 . $request->id
169 . '&error=migrate_target' );
170 exit;
173 else {
174 $backend_result = $request->backend_migrate($params);
175 $template->param(
176 whole => $backend_result,
177 request => $request
180 handle_commit_maybe( $backend_result, $request );
182 } elsif ( $op eq 'confirm' ) {
183 # Backend 'confirm' method
184 # confirm requires a specific request, so first, find it.
185 my $request = Koha::Illrequests->find($params->{illrequest_id});
186 my $backend_result = $request->backend_confirm($params);
187 $template->param(
188 whole => $backend_result,
189 request => $request,
192 # handle special commit rules & update type
193 handle_commit_maybe($backend_result, $request);
195 } elsif ( $op eq 'cancel' ) {
196 # Backend 'cancel' method
197 # cancel requires a specific request, so first, find it.
198 my $request = Koha::Illrequests->find($params->{illrequest_id});
199 my $backend_result = $request->backend_cancel($params);
200 $template->param(
201 whole => $backend_result,
202 request => $request,
205 # handle special commit rules & update type
206 handle_commit_maybe($backend_result, $request);
208 } elsif ( $op eq 'edit_action' ) {
209 # Handle edits to the Illrequest object.
210 # (not the Illrequestattributes)
211 # We simulate the API for backend requests for uniformity.
212 # So, init:
213 my $request = Koha::Illrequests->find($params->{illrequest_id});
214 if ( !$params->{stage} ) {
215 my $backend_result = {
216 error => 0,
217 status => '',
218 message => '',
219 method => 'edit_action',
220 stage => 'init',
221 next => '',
222 value => {}
224 $template->param(
225 whole => $backend_result,
226 request => $request
228 } else {
229 # Commit:
230 # Save the changes
231 $request->borrowernumber($params->{borrowernumber});
232 $request->biblio_id($params->{biblio_id});
233 $request->branchcode($params->{branchcode});
234 $request->price_paid($params->{price_paid});
235 $request->notesopac($params->{notesopac});
236 $request->notesstaff($params->{notesstaff});
237 my $alias = (length $params->{status_alias} > 0) ?
238 $params->{status_alias} :
239 "-1";
240 $request->status_alias($alias);
241 $request->store;
242 my $backend_result = {
243 error => 0,
244 status => '',
245 message => '',
246 method => 'edit_action',
247 stage => 'commit',
248 next => 'illlist',
249 value => {}
251 handle_commit_maybe($backend_result, $request);
254 } elsif ( $op eq 'moderate_action' ) {
255 # Moderate action is required for an ILL submodule / syspref.
256 # Currently still needs to be implemented.
257 redirect_to_list();
259 } elsif ( $op eq 'delete_confirm') {
260 my $request = Koha::Illrequests->find($params->{illrequest_id});
262 $template->param(
263 request => $request
266 } elsif ( $op eq 'delete' ) {
268 # Check if the request is confirmed, if not, redirect
269 # to the confirmation view
270 if ($params->{confirmed}) {
271 # We simply delete the request...
272 Koha::Illrequests->find( $params->{illrequest_id} )->delete;
273 # ... then return to list view.
274 redirect_to_list();
275 } else {
276 print $cgi->redirect(
277 "/cgi-bin/koha/ill/ill-requests.pl?" .
278 "method=delete_confirm&illrequest_id=" .
279 $params->{illrequest_id});
280 exit;
283 } elsif ( $op eq 'mark_completed' ) {
284 my $request = Koha::Illrequests->find($params->{illrequest_id});
285 my $backend_result = $request->mark_completed($params);
286 $template->param(
287 whole => $backend_result,
288 request => $request,
291 # handle special commit rules & update type
292 handle_commit_maybe($backend_result, $request);
294 } elsif ( $op eq 'generic_confirm' ) {
295 my $backend_result;
296 my $request;
297 try {
298 $request = Koha::Illrequests->find($params->{illrequest_id});
299 $params->{current_branchcode} = C4::Context->mybranch;
300 $backend_result = $request->generic_confirm($params);
302 $template->param(
303 whole => $backend_result,
304 request => $request,
307 # Prepare availability searching, if required
308 # Get the definition for the z39.50 plugin
309 if ( C4::Context->preference('ILLCheckAvailability') ) {
310 my $availability = Koha::Illrequest::Availability->new($request->metadata);
311 my $services = $availability->get_services({
312 ui_context => 'partners',
313 metadata => {
314 name => 'ILL availability - z39.50'
317 # Only pass availability searching stuff to the template if
318 # appropriate
319 if ( scalar @{$services} > 0 ) {
320 my $metadata = $availability->prep_metadata($request->metadata);
321 $template->param( metadata => $metadata );
322 $template->param(
323 services_json => scalar encode_json($services)
325 $template->param( services => $services );
329 $template->param( error => $params->{error} )
330 if $params->{error};
332 catch {
333 my $error;
334 if ( ref($_) eq 'Koha::Exceptions::Ill::NoTargetEmail' ) {
335 $error = 'no_target_email';
337 elsif ( ref($_) eq 'Koha::Exceptions::Ill::NoLibraryEmail' ) {
338 $error = 'no_library_email';
340 else {
341 $error = 'unknown_error';
343 print $cgi->redirect(
344 "/cgi-bin/koha/ill/ill-requests.pl?" .
345 "method=generic_confirm&illrequest_id=" .
346 $params->{illrequest_id} .
347 "&error=$error" );
348 exit;
351 # handle special commit rules & update type
352 handle_commit_maybe($backend_result, $request);
353 } elsif ( $op eq 'check_out') {
354 my $request = Koha::Illrequests->find($params->{illrequest_id});
355 my $backend_result = $request->check_out($params);
356 $template->param(
357 params => $params,
358 whole => $backend_result,
359 request => $request
361 } elsif ( $op eq 'illlist') {
363 # If we receive a pre-filter, make it available to the template
364 my $possible_filters = ['borrowernumber'];
365 my $active_filters = {};
366 foreach my $filter(@{$possible_filters}) {
367 if ($params->{$filter}) {
368 # We shouldn't need to escape $filter here since we're using
369 # a whitelist, but just to be sure...
370 $active_filters->{uri_escape_utf8($filter)} =
371 uri_escape_utf8(scalar $params->{$filter});
374 my @tpl_arr = ();
375 if (keys %{$active_filters}) {
376 foreach my $key (keys %{$active_filters}) {
377 push @tpl_arr, $key . "=" . $active_filters->{$key};
380 $template->param(
381 prefilters => join("&", @tpl_arr)
383 } elsif ( $op eq "save_comment" ) {
384 die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
385 session_id => scalar $cgi->cookie('CGISESSID'),
386 token => scalar $cgi->param('csrf_token'),
388 my $comment = Koha::Illcomment->new({
389 illrequest_id => scalar $params->{illrequest_id},
390 borrowernumber => $patronnumber,
391 comment => scalar $params->{comment},
393 $comment->store();
394 # Redirect to view the whole request
395 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
396 scalar $params->{illrequest_id}
398 exit;
400 } elsif ( $op eq "send_notice" ) {
401 my $illrequest_id = $params->{illrequest_id};
402 my $request = Koha::Illrequests->find($illrequest_id);
403 my $ret = $request->send_patron_notice($params->{notice_code});
404 my $append = '';
405 if ($ret->{result} && scalar @{$ret->{result}->{success}} > 0) {
406 $append .= '&tran_success=' . join(',', @{$ret->{result}->{success}});
408 if ($ret->{result} && scalar @{$ret->{result}->{fail}} > 0) {
409 $append .= '&tran_fail=' . join(',', @{$ret->{result}->{fail}}.join(','));
411 # Redirect to view the whole request
412 print $cgi->redirect(
413 "/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
414 scalar $params->{illrequest_id} . $append
416 exit;
417 } else {
418 my $request = Koha::Illrequests->find($params->{illrequest_id});
419 my $backend_result = $request->custom_capability($op, $params);
420 $template->param(
421 whole => $backend_result,
422 request => $request,
425 # handle special commit rules & update type
426 handle_commit_maybe($backend_result, $request);
430 $template->param(
431 backends => $backends,
432 types => [ "Book", "Article", "Journal" ],
433 query_type => $op,
434 branches => scalar Koha::Libraries->search,
437 output_html_with_http_headers( $cgi, $cookie, $template->output );
439 sub handle_commit_maybe {
440 my ( $backend_result, $request ) = @_;
442 # We need to special case 'commit'
443 if ( $backend_result->{stage} eq 'commit' ) {
444 if ( $backend_result->{next} eq 'illview' ) {
446 # Redirect to a view of the newly created request
447 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
448 . '?method=illview'
449 . '&illrequest_id='
450 . $request->id );
451 exit;
453 elsif ( $backend_result->{next} eq 'emigrate' ) {
455 # Redirect to a view of the newly created request
456 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
457 . '?method=migrate'
458 . '&stage=emigrate'
459 . '&illrequest_id='
460 . $request->id );
461 exit;
463 else {
464 # Redirect to a requests list view
465 redirect_to_list();
470 sub redirect_to_list {
471 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
472 exit;