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>.
24 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_and_exit output_html_with_http_headers );
26 use Koha::Notice::Templates;
27 use Koha::AuthorisedValues;
29 use Koha::Illrequests;
31 use Koha::Illrequest::Workflow::Availability;
32 use Koha::Illrequest::Workflow::TypeDisclaimer;
37 use Try::Tiny qw( catch try );
38 use URI::Escape qw( uri_escape_utf8 );
39 use JSON qw( encode_json );
42 my $illRequests = Koha::Illrequests->new;
44 # Grab all passed data
45 # 'our' since Plack changes the scoping
47 our $params = $cgi->Vars();
49 # Leave immediately if ILLModule is disabled
50 unless ( C4::Context->preference('ILLModule') ) {
51 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
55 my $op = $params->{method} || 'illlist';
57 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
58 template_name => 'ill/ill-requests.tt',
61 flagsrequired => { ill => '*' },
64 # Are we able to actually work?
65 my $cfg = Koha::Illrequest::Config->new;
66 my $backends = $cfg->available_backends;
67 my $has_branch = $cfg->has_branch;
68 my $backends_available = ( scalar @{$backends} > 0 );
70 backends_available => $backends_available,
71 has_branch => $has_branch,
72 have_batch => have_batch_backends($backends)
75 if ( $backends_available ) {
76 # Establish what metadata enrichment plugins we have available
77 my $enrichment_services = get_metadata_enrichment();
78 if (scalar @{$enrichment_services} > 0) {
80 metadata_enrichment_services => encode_json($enrichment_services)
83 # Establish whether we have any availability services that can provide availability
84 # for the batch identifier types we support
85 my $batch_availability_services = get_ill_availability($enrichment_services);
86 if (scalar @{$batch_availability_services} > 0) {
88 batch_availability_services => encode_json($batch_availability_services)
92 if ( $op eq 'illview' ) {
93 # View the details of an ILL
94 my $request = Koha::Illrequests->find($params->{illrequest_id});
96 # Get the details for notices that can be sent from here
97 my $notices = Koha::Notice::Templates->search(
100 code => { -in => [ 'ILL_PICKUP_READY' ,'ILL_REQUEST_UNAVAIL' ] },
103 columns => [ qw/code name/ ],
111 csrf_token => Koha::Token->new->generate_csrf({
112 session_id => scalar $cgi->cookie('CGISESSID'),
114 ( $params->{tran_error} ?
115 ( tran_error => $params->{tran_error} ) : () ),
116 ( $params->{tran_success} ?
117 ( tran_success => $params->{tran_success} ) : () ),
120 output_and_exit( $cgi, $cookie, $template, 'unknown_ill_request' ) if !$request;
122 my $backend_result = $request->backend_illview($params);
124 whole => $backend_result,
125 ) if $backend_result;
128 } elsif ( $op eq 'create' ) {
129 # Load the ILL backend
130 my $request = Koha::Illrequest->new->load_backend( $params->{backend} );
132 # Before request creation operations - Preparation
134 Koha::Illrequest::Workflow::Availability->new( $params, 'staff' );
135 my $type_disclaimer =
136 Koha::Illrequest::Workflow::TypeDisclaimer->new( $params, 'staff' );
138 # ILLCheckAvailability operation
139 if ($availability->show_availability($request)) {
140 $op = 'availability';
142 $availability->availability_template_params($params)
144 # ILLModuleDisclaimerByType operation
145 } elsif ( $type_disclaimer->show_type_disclaimer($request)) {
146 $op = 'typedisclaimer';
148 $type_disclaimer->type_disclaimer_template_params($params)
150 # Ready to create ILL request
152 my $backend_result = $request->backend_create($params);
154 # After creation actions
155 if ( $params->{type_disclaimer_submitted} && $request->illrequest_id ) {
156 $type_disclaimer->after_request_created($params, $request);
160 whole => $backend_result,
163 handle_commit_maybe($backend_result, $request);
165 } elsif ( $op eq 'migrate' ) {
166 # We're in the process of migrating a request
167 my $request = Koha::Illrequests->find($params->{illrequest_id});
169 if ( $params->{backend} ) {
170 $backend_result = $request->backend_migrate($params);
171 if ($backend_result) {
173 whole => $backend_result,
177 # Backend failure, redirect back to illview
178 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
182 . '&error=migrate_target' );
187 $backend_result = $request->backend_migrate($params);
189 whole => $backend_result,
193 handle_commit_maybe( $backend_result, $request );
195 } elsif ( $op eq 'confirm' ) {
196 # Backend 'confirm' method
197 # confirm requires a specific request, so first, find it.
198 my $request = Koha::Illrequests->find($params->{illrequest_id});
199 my $backend_result = $request->backend_confirm($params);
201 whole => $backend_result,
205 # handle special commit rules & update type
206 handle_commit_maybe($backend_result, $request);
208 } elsif ( $op eq 'cancel' ) {
209 # Backend 'cancel' method
210 # cancel requires a specific request, so first, find it.
211 my $request = Koha::Illrequests->find($params->{illrequest_id});
212 my $backend_result = $request->backend_cancel($params);
214 whole => $backend_result,
218 # handle special commit rules & update type
219 handle_commit_maybe($backend_result, $request);
221 } elsif ( $op eq 'edit_action' ) {
222 # Handle edits to the Illrequest object.
223 # (not the Illrequestattributes)
224 # We simulate the API for backend requests for uniformity.
226 my $request = Koha::Illrequests->find($params->{illrequest_id});
227 my $batches = Koha::Illbatches->search(undef, {
228 order_by => { -asc => 'name' }
230 if ( !$params->{stage} ) {
231 my $backend_result = {
235 method => 'edit_action',
241 whole => $backend_result,
248 $request->borrowernumber($params->{borrowernumber});
249 $request->biblio_id($params->{biblio_id});
250 $request->batch_id($params->{batch_id});
251 $request->branchcode($params->{branchcode});
252 $request->price_paid($params->{price_paid});
253 $request->notesopac($params->{notesopac});
254 $request->notesstaff($params->{notesstaff});
255 my $alias = (length $params->{status_alias} > 0) ?
256 $params->{status_alias} :
258 $request->status_alias($alias);
260 my $backend_result = {
264 method => 'edit_action',
269 handle_commit_maybe($backend_result, $request);
272 } elsif ( $op eq 'moderate_action' ) {
273 # Moderate action is required for an ILL submodule / syspref.
274 # Currently still needs to be implemented.
277 } elsif ( $op eq 'delete_confirm') {
278 my $request = Koha::Illrequests->find($params->{illrequest_id});
284 } elsif ( $op eq 'delete' ) {
286 # Check if the request is confirmed, if not, redirect
287 # to the confirmation view
288 if ($params->{confirmed}) {
289 # We simply delete the request...
290 Koha::Illrequests->find( $params->{illrequest_id} )->delete;
291 # ... then return to list view.
294 print $cgi->redirect(
295 "/cgi-bin/koha/ill/ill-requests.pl?" .
296 "method=delete_confirm&illrequest_id=" .
297 $params->{illrequest_id});
301 } elsif ( $op eq 'mark_completed' ) {
302 my $request = Koha::Illrequests->find($params->{illrequest_id});
303 my $backend_result = $request->mark_completed($params);
305 whole => $backend_result,
309 # handle special commit rules & update type
310 handle_commit_maybe($backend_result, $request);
312 } elsif ( $op eq 'generic_confirm' ) {
316 $request = Koha::Illrequests->find($params->{illrequest_id});
317 $params->{current_branchcode} = C4::Context->mybranch;
318 $backend_result = $request->generic_confirm($params);
321 whole => $backend_result,
325 # Prepare availability searching, if required
326 # Get the definition for the z39.50 plugin
327 if ( C4::Context->preference('ILLCheckAvailability') ) {
328 my $availability = Koha::Illrequest::Workflow::Availability->new(
330 name => 'ILL availability - z39.50',
331 %{$request->metadata}
335 my $services = $availability->get_services();
336 # Only pass availability searching stuff to the template if
338 if ( scalar @{$services} > 0 ) {
339 my $metadata = $availability->prep_metadata($request->metadata);
340 $template->param( metadata => $metadata );
342 services_json => scalar encode_json($services)
344 $template->param( services => $services );
348 $template->param( error => $params->{error} )
353 if ( ref($_) eq 'Koha::Exceptions::Ill::NoTargetEmail' ) {
354 $error = 'no_target_email';
356 elsif ( ref($_) eq 'Koha::Exceptions::Ill::NoLibraryEmail' ) {
357 $error = 'no_library_email';
360 $error = 'unknown_error';
362 print $cgi->redirect(
363 "/cgi-bin/koha/ill/ill-requests.pl?" .
364 "method=generic_confirm&illrequest_id=" .
365 $params->{illrequest_id} .
370 # handle special commit rules & update type
371 handle_commit_maybe($backend_result, $request);
372 } elsif ( $op eq 'check_out') {
373 my $request = Koha::Illrequests->find($params->{illrequest_id});
374 my $backend_result = $request->check_out($params);
377 whole => $backend_result,
380 } elsif ( $op eq 'illlist') {
382 # If we receive a pre-filter, make it available to the template
383 my $possible_filters = ['borrowernumber', 'batch_id'];
384 my $active_filters = {};
385 foreach my $filter(@{$possible_filters}) {
386 if ($params->{$filter}) {
387 # We shouldn't need to escape $filter here since we're using
388 # a whitelist, but just to be sure...
389 $active_filters->{uri_escape_utf8($filter)} =
390 uri_escape_utf8(scalar $params->{$filter});
394 if (keys %{$active_filters}) {
395 foreach my $key (keys %{$active_filters}) {
396 push @tpl_arr, $key . "=" . $active_filters->{$key};
400 prefilters => join("&", @tpl_arr)
403 if ($active_filters->{batch_id}) {
404 my $batch_id = $active_filters->{batch_id};
406 my $batch = Koha::Illbatches->find($batch_id);
413 } elsif ( $op eq "save_comment" ) {
414 die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
415 session_id => scalar $cgi->cookie('CGISESSID'),
416 token => scalar $cgi->param('csrf_token'),
418 my $comment = Koha::Illcomment->new({
419 illrequest_id => scalar $params->{illrequest_id},
420 borrowernumber => $patronnumber,
421 comment => scalar $params->{comment},
424 # Redirect to view the whole request
425 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
426 scalar $params->{illrequest_id}
430 } elsif ( $op eq "send_notice" ) {
431 my $illrequest_id = $params->{illrequest_id};
432 my $request = Koha::Illrequests->find($illrequest_id);
433 my $ret = $request->send_patron_notice($params->{notice_code});
435 if ($ret->{result} && scalar @{$ret->{result}->{success}} > 0) {
436 $append .= '&tran_success=' . join(',', @{$ret->{result}->{success}});
438 if ($ret->{result} && scalar @{$ret->{result}->{fail}} > 0) {
439 $append .= '&tran_fail=' . join(',', @{$ret->{result}->{fail}}.join(','));
441 # Redirect to view the whole request
442 print $cgi->redirect(
443 "/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
444 scalar $params->{illrequest_id} . $append
447 } elsif ( $op eq "batch_list" ) {
448 # Do not remove, it prevents us falling through to the 'else'
449 } elsif ( $op eq "batch_create" ) {
450 # Do not remove, it prevents us falling through to the 'else'
452 my $request = Koha::Illrequests->find($params->{illrequest_id});
453 my $backend_result = $request->custom_capability($op, $params);
455 whole => $backend_result,
459 # handle special commit rules & update type
460 handle_commit_maybe($backend_result, $request);
465 backends => $backends,
466 types => [ "Book", "Article", "Journal" ],
468 branches => Koha::Libraries->search,
471 output_html_with_http_headers( $cgi, $cookie, $template->output );
473 sub handle_commit_maybe {
474 my ( $backend_result, $request ) = @_;
476 # We need to special case 'commit'
477 if ( $backend_result->{stage} eq 'commit' ) {
478 if ( $backend_result->{next} eq 'illview' ) {
480 # Redirect to a view of the newly created request
481 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
487 elsif ( $backend_result->{next} eq 'emigrate' ) {
489 # Redirect to a view of the newly created request
490 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
498 # Redirect to a requests list view
504 sub redirect_to_list {
505 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
509 # Do any of the available backends provide batch requesting
510 sub have_batch_backends {
511 my ( $backends ) = @_;
515 foreach my $backend(@{$backends}) {
516 my $can_batch = can_batch($backend);
518 push @have_batch, $backend;
524 # Does a given backend provide batch requests
525 # FIXME: This should be moved to Koha::Illbackend
527 my ( $backend ) = @_;
528 my $request = Koha::Illrequest->new->load_backend( $backend );
529 return $request->_backend_capability( 'provides_batch_requests' );
532 # Get available metadata enrichment plugins
533 sub get_metadata_enrichment {
534 return [] unless C4::Context->config("enable_plugins");
535 my @candidates = Koha::Plugins->new()->GetPlugins({
536 method => 'provides_api'
539 foreach my $plugin(@candidates) {
540 my $supported = $plugin->provides_api();
541 if ($supported->{type} eq 'search') {
542 push @services, $supported;
548 # Get ILL availability plugins that can help us with the batch identifier types
550 sub get_ill_availability {
551 my ( $services ) = @_;
554 foreach my $service(@{$services}) {
555 foreach my $id_supported(keys %{$service->{identifiers_supported}}) {
556 $id_types->{$id_supported} = 1;
560 my $availability = Koha::Illrequest::Workflow::Availability->new($id_types, 'staff');
561 return $availability->get_services();