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;
28 use Koha::ILL::Comment;
29 use Koha::ILL::Requests;
30 use Koha::ILL::Request;
31 use Koha::ILL::Batches;
32 use Koha::ILL::Request::Workflow::Availability;
33 use Koha::ILL::Request::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::ILL::Requests->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 = Koha::ILL::Request->get_op_param_deprecation( 'intranet', $params );
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::ILL::Request::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::ILL::Requests->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 ( $params->{tran_error} ?
112 ( tran_error => $params->{tran_error} ) : () ),
113 ( $params->{tran_success} ?
114 ( tran_success => $params->{tran_success} ) : () ),
117 output_and_exit( $cgi, $cookie, $template, 'unknown_ill_request' ) if !$request;
119 my $backend_result = $request->backend_illview($params);
121 whole => $backend_result,
122 ) if $backend_result;
125 } elsif ( $op eq 'cud-create' ) {
126 # Load the ILL backend
127 my $request = Koha::ILL::Request->new->load_backend( $params->{backend} );
129 # Before request creation operations - Preparation
131 Koha::ILL::Request::Workflow::Availability->new( $params, 'staff' );
132 my $type_disclaimer =
133 Koha::ILL::Request::Workflow::TypeDisclaimer->new( $params, 'staff' );
135 # ILLCheckAvailability operation
136 if ($availability->show_availability($request)) {
137 $op = 'availability';
139 $availability->availability_template_params($params)
141 # ILLModuleDisclaimerByType operation
142 } elsif ( $type_disclaimer->show_type_disclaimer($request)) {
143 $op = 'typedisclaimer';
145 $type_disclaimer->type_disclaimer_template_params($params)
147 # Ready to create ILL request
149 my $backend_result = $request->backend_create($params);
151 # After creation actions
152 if ( $params->{type_disclaimer_submitted} && $request->illrequest_id ) {
153 $type_disclaimer->after_request_created($params, $request);
157 whole => $backend_result,
160 handle_commit_maybe($backend_result, $request);
162 } elsif ( $op eq 'migrate' ) {
163 # We're in the process of migrating a request
164 my $request = Koha::ILL::Requests->find($params->{illrequest_id});
166 if ( $params->{backend} ) {
167 $backend_result = $request->backend_migrate($params);
168 if ($backend_result) {
170 whole => $backend_result,
174 # Backend failure, redirect back to illview
175 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
179 . '&error=migrate_target' );
184 $backend_result = $request->backend_migrate($params);
186 whole => $backend_result,
190 handle_commit_maybe( $backend_result, $request );
192 } elsif ( $op eq 'confirm' ) {
193 # Backend 'confirm' method
194 # confirm requires a specific request, so first, find it.
195 my $request = Koha::ILL::Requests->find($params->{illrequest_id});
196 my $backend_result = $request->backend_confirm($params);
198 whole => $backend_result,
202 # handle special commit rules & update type
203 handle_commit_maybe($backend_result, $request);
205 } elsif ( $op eq 'cud-cancel' ) {
206 # Backend 'cancel' method
207 # cancel requires a specific request, so first, find it.
208 my $request = Koha::ILL::Requests->find($params->{illrequest_id});
209 my $backend_result = $request->backend_cancel($params);
211 whole => $backend_result,
215 # handle special commit rules & update type
216 handle_commit_maybe($backend_result, $request);
218 } elsif ( $op eq 'cud-edit_action' ) {
219 # Handle edits to the Illrequest object.
220 # (not the Illrequestattributes)
221 # We simulate the API for backend requests for uniformity.
223 my $request = Koha::ILL::Requests->find($params->{illrequest_id});
224 my $batches = Koha::ILL::Batches->search(undef, {
225 order_by => { -asc => 'name' }
227 if ( !$params->{stage} ) {
229 my $backend_result = {
239 whole => $backend_result,
246 $request->borrowernumber($params->{borrowernumber});
247 $request->biblio_id($params->{biblio_id});
248 $request->batch_id($params->{batch_id});
249 $request->branchcode($params->{branchcode});
250 $request->price_paid($params->{price_paid});
251 $request->notesopac($params->{notesopac});
252 $request->notesstaff($params->{notesstaff});
253 my $alias = (length $params->{status_alias} > 0) ?
254 $params->{status_alias} :
256 $request->status_alias($alias);
258 my $backend_result = {
267 handle_commit_maybe($backend_result, $request);
270 } elsif ( $op eq 'moderate_action' ) {
271 # Moderate action is required for an ILL submodule / syspref.
272 # Currently still needs to be implemented.
275 } elsif ( $op eq 'delete_confirm') {
276 my $request = Koha::ILL::Requests->find($params->{illrequest_id});
282 } elsif ( $op eq 'cud-delete' ) {
284 # Check if the request is confirmed, if not, redirect
285 # to the confirmation view
286 if ($params->{confirmed}) {
287 # We simply delete the request...
288 Koha::ILL::Requests->find( $params->{illrequest_id} )->delete;
289 # ... then return to list view.
292 print $cgi->redirect(
293 "/cgi-bin/koha/ill/ill-requests.pl?" .
294 "op=delete_confirm&illrequest_id=" .
295 $params->{illrequest_id});
299 } elsif ( $op eq 'mark_completed' ) {
300 my $request = Koha::ILL::Requests->find($params->{illrequest_id});
301 my $backend_result = $request->mark_completed($params);
303 whole => $backend_result,
307 # handle special commit rules & update type
308 handle_commit_maybe($backend_result, $request);
310 } elsif ( $op eq 'cud-generic_confirm' ) {
315 $request = Koha::ILL::Requests->find($params->{illrequest_id});
316 $params->{current_branchcode} = C4::Context->mybranch;
317 $backend_result = $request->generic_confirm($params);
320 whole => $backend_result,
324 # Prepare availability searching, if required
325 # Get the definition for the z39.50 plugin
326 if ( C4::Context->preference('ILLCheckAvailability') ) {
327 my $availability = Koha::ILL::Request::Workflow::Availability->new(
329 name => 'ILL availability - z39.50',
330 %{$request->metadata}
334 my $services = $availability->get_services();
335 # Only pass availability searching stuff to the template if
337 if ( scalar @{$services} > 0 ) {
338 my $metadata = $availability->prep_metadata($request->metadata);
339 $template->param( metadata => $metadata );
341 services_json => scalar encode_json($services)
343 $template->param( services => $services );
347 $template->param( error => $params->{error} )
352 if ( ref($_) eq 'Koha::Exceptions::Ill::NoTargetEmail' ) {
353 $error = 'no_target_email';
355 elsif ( ref($_) eq 'Koha::Exceptions::Ill::NoLibraryEmail' ) {
356 $error = 'no_library_email';
359 $error = 'unknown_error';
361 print $cgi->redirect(
362 "/cgi-bin/koha/ill/ill-requests.pl?" .
363 "op=generic_confirm&illrequest_id=" .
364 $params->{illrequest_id} .
369 # handle special commit rules & update type
370 handle_commit_maybe($backend_result, $request);
371 } elsif ( $op eq 'cud-check_out') {
372 my $request = Koha::ILL::Requests->find($params->{illrequest_id});
373 my $backend_result = $request->check_out($params);
376 whole => $backend_result,
379 } elsif ( $op eq 'illlist') {
381 # If we receive a pre-filter, make it available to the template
382 my $possible_filters = ['borrowernumber', 'batch_id'];
383 my $active_filters = {};
384 foreach my $filter(@{$possible_filters}) {
385 if ($params->{$filter}) {
386 # We shouldn't need to escape $filter here since we're using
387 # a whitelist, but just to be sure...
388 $active_filters->{uri_escape_utf8($filter)} =
389 uri_escape_utf8(scalar $params->{$filter});
393 if (keys %{$active_filters}) {
394 foreach my $key (keys %{$active_filters}) {
395 push @tpl_arr, $key . "=" . $active_filters->{$key};
399 prefilters => join("&", @tpl_arr)
402 if ($active_filters->{batch_id}) {
403 my $batch_id = $active_filters->{batch_id};
405 my $batch = Koha::ILL::Batches->find($batch_id);
412 $template->param( table_actions => encode_json( Koha::ILL::Request->get_staff_table_actions ) );
413 } elsif ( $op eq "cud-save_comment" ) {
414 my $comment = Koha::ILL::Comment->new({
415 illrequest_id => scalar $params->{illrequest_id},
416 borrowernumber => $patronnumber,
417 comment => scalar $params->{comment},
420 # Redirect to view the whole request
421 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?op=illview&illrequest_id=".
422 scalar $params->{illrequest_id}
426 } elsif ( $op eq "send_notice" ) {
427 my $illrequest_id = $params->{illrequest_id};
428 my $request = Koha::ILL::Requests->find($illrequest_id);
429 my $ret = $request->send_patron_notice($params->{notice_code});
431 if ($ret->{result} && scalar @{$ret->{result}->{success}} > 0) {
432 $append .= '&tran_success=' . join(',', @{$ret->{result}->{success}});
434 if ($ret->{result} && scalar @{$ret->{result}->{fail}} > 0) {
435 $append .= '&tran_fail=' . join(',', @{$ret->{result}->{fail}}.join(','));
437 # Redirect to view the whole request
438 print $cgi->redirect(
439 "/cgi-bin/koha/ill/ill-requests.pl?op=illview&illrequest_id=".
440 scalar $params->{illrequest_id} . $append
443 } elsif ( $op eq "batch_list" ) {
444 # Do not remove, it prevents us falling through to the 'else'
445 } elsif ( $op eq "batch_create" ) {
446 # Do not remove, it prevents us falling through to the 'else'
449 my $request = Koha::ILL::Requests->find( $params->{illrequest_id} );
450 my $backend_result = $request->custom_capability($op, $params);
452 whole => $backend_result,
456 # handle special commit rules & update type
457 handle_commit_maybe($backend_result, $request);
462 backends => $backends,
463 types => [ "Book", "Article", "Journal" ],
465 branches => Koha::Libraries->search,
468 output_html_with_http_headers( $cgi, $cookie, $template->output );
470 sub handle_commit_maybe {
471 my ( $backend_result, $request ) = @_;
473 # We need to special case 'commit'
474 if ( $backend_result->{stage} eq 'commit' ) {
475 if ( $backend_result->{next} eq 'illview' ) {
477 # Redirect to a view of the newly created request
478 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
484 elsif ( $backend_result->{next} eq 'emigrate' ) {
486 # Redirect to a view of the newly created request
487 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
495 # Redirect to a requests list view
501 sub redirect_to_list {
502 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
506 # Do any of the available backends provide batch requesting
507 sub have_batch_backends {
508 my ( $backends ) = @_;
512 foreach my $backend(@{$backends}) {
513 my $can_batch = can_batch($backend);
515 push @have_batch, $backend;
521 # Does a given backend provide batch requests
522 # FIXME: This should be moved to Koha::Illbackend
524 my ( $backend ) = @_;
525 my $request = Koha::ILL::Request->new->load_backend( $backend );
526 return $request->_backend_capability( 'provides_batch_requests' );
529 # Get available metadata enrichment plugins
530 sub get_metadata_enrichment {
531 return [] unless C4::Context->config("enable_plugins");
532 my @candidates = Koha::Plugins->new()->GetPlugins({
533 method => 'provides_api'
536 foreach my $plugin(@candidates) {
537 my $supported = $plugin->provides_api();
538 if ($supported->{type} eq 'search') {
539 push @services, $supported;
545 # Get ILL availability plugins that can help us with the batch identifier types
547 sub get_ill_availability {
548 my ( $services ) = @_;
551 foreach my $service(@{$services}) {
552 foreach my $id_supported(keys %{$service->{identifiers_supported}}) {
553 $id_types->{$id_supported} = 1;
557 my $availability = Koha::ILL::Request::Workflow::Availability->new( $id_types, 'staff' );
558 return $availability->get_services();