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_html_with_http_headers );
26 use Koha::Notice::Templates;
27 use Koha::AuthorisedValues;
29 use Koha::Illrequests;
30 use Koha::Illrequest::Availability;
34 use Try::Tiny qw( catch try );
35 use URI::Escape qw( uri_escape_utf8 );
36 use JSON qw( encode_json );
39 my $illRequests = Koha::Illrequests->new;
41 # Grab all passed data
42 # 'our' since Plack changes the scoping
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");
52 my $op = $params->{method} || 'illlist';
54 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
55 template_name => 'ill/ill-requests.tt',
58 flagsrequired => { ill => '*' },
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 );
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(
80 code => { -in => [ 'ILL_PICKUP_READY' ,'ILL_REQUEST_UNAVAIL' ] },
83 columns => [ qw/code name/ ],
91 csrf_token => Koha::Token->new->generate_csrf({
92 session_id => scalar $cgi->cookie('CGISESSID'),
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.
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
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);
128 metadata => $metadata,
129 services_json => scalar encode_json($services),
130 services => $services
133 # No services can process this metadata, so continue as normal
134 my $backend_result = $request->backend_create($params);
136 whole => $backend_result,
139 handle_commit_maybe($backend_result, $request);
142 my $backend_result = $request->backend_create($params);
144 whole => $backend_result,
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});
154 if ( $params->{backend} ) {
155 $backend_result = $request->backend_migrate($params);
156 if ($backend_result) {
158 whole => $backend_result,
162 # Backend failure, redirect back to illview
163 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
167 . '&error=migrate_target' );
172 $backend_result = $request->backend_migrate($params);
174 whole => $backend_result,
178 handle_commit_maybe( $backend_result, $request );
180 } elsif ( $op eq 'confirm' ) {
181 # Backend 'confirm' method
182 # confirm requires a specific request, so first, find it.
183 my $request = Koha::Illrequests->find($params->{illrequest_id});
184 my $backend_result = $request->backend_confirm($params);
186 whole => $backend_result,
190 # handle special commit rules & update type
191 handle_commit_maybe($backend_result, $request);
193 } elsif ( $op eq 'cancel' ) {
194 # Backend 'cancel' method
195 # cancel requires a specific request, so first, find it.
196 my $request = Koha::Illrequests->find($params->{illrequest_id});
197 my $backend_result = $request->backend_cancel($params);
199 whole => $backend_result,
203 # handle special commit rules & update type
204 handle_commit_maybe($backend_result, $request);
206 } elsif ( $op eq 'edit_action' ) {
207 # Handle edits to the Illrequest object.
208 # (not the Illrequestattributes)
209 # We simulate the API for backend requests for uniformity.
211 my $request = Koha::Illrequests->find($params->{illrequest_id});
212 if ( !$params->{stage} ) {
213 my $backend_result = {
217 method => 'edit_action',
223 whole => $backend_result,
229 $request->borrowernumber($params->{borrowernumber});
230 $request->biblio_id($params->{biblio_id});
231 $request->branchcode($params->{branchcode});
232 $request->price_paid($params->{price_paid});
233 $request->notesopac($params->{notesopac});
234 $request->notesstaff($params->{notesstaff});
235 my $alias = (length $params->{status_alias} > 0) ?
236 $params->{status_alias} :
238 $request->status_alias($alias);
240 my $backend_result = {
244 method => 'edit_action',
249 handle_commit_maybe($backend_result, $request);
252 } elsif ( $op eq 'moderate_action' ) {
253 # Moderate action is required for an ILL submodule / syspref.
254 # Currently still needs to be implemented.
257 } elsif ( $op eq 'delete_confirm') {
258 my $request = Koha::Illrequests->find($params->{illrequest_id});
264 } elsif ( $op eq 'delete' ) {
266 # Check if the request is confirmed, if not, redirect
267 # to the confirmation view
268 if ($params->{confirmed}) {
269 # We simply delete the request...
270 Koha::Illrequests->find( $params->{illrequest_id} )->delete;
271 # ... then return to list view.
274 print $cgi->redirect(
275 "/cgi-bin/koha/ill/ill-requests.pl?" .
276 "method=delete_confirm&illrequest_id=" .
277 $params->{illrequest_id});
281 } elsif ( $op eq 'mark_completed' ) {
282 my $request = Koha::Illrequests->find($params->{illrequest_id});
283 my $backend_result = $request->mark_completed($params);
285 whole => $backend_result,
289 # handle special commit rules & update type
290 handle_commit_maybe($backend_result, $request);
292 } elsif ( $op eq 'generic_confirm' ) {
296 $request = Koha::Illrequests->find($params->{illrequest_id});
297 $params->{current_branchcode} = C4::Context->mybranch;
298 $backend_result = $request->generic_confirm($params);
301 whole => $backend_result,
305 # Prepare availability searching, if required
306 # Get the definition for the z39.50 plugin
307 if ( C4::Context->preference('ILLCheckAvailability') ) {
308 my $availability = Koha::Illrequest::Availability->new($request->metadata);
309 my $services = $availability->get_services({
310 ui_context => 'partners',
312 name => 'ILL availability - z39.50'
315 # Only pass availability searching stuff to the template if
317 if ( scalar @{$services} > 0 ) {
318 my $metadata = $availability->prep_metadata($request->metadata);
319 $template->param( metadata => $metadata );
321 services_json => scalar encode_json($services)
323 $template->param( services => $services );
327 $template->param( error => $params->{error} )
332 if ( ref($_) eq 'Koha::Exceptions::Ill::NoTargetEmail' ) {
333 $error = 'no_target_email';
335 elsif ( ref($_) eq 'Koha::Exceptions::Ill::NoLibraryEmail' ) {
336 $error = 'no_library_email';
339 $error = 'unknown_error';
341 print $cgi->redirect(
342 "/cgi-bin/koha/ill/ill-requests.pl?" .
343 "method=generic_confirm&illrequest_id=" .
344 $params->{illrequest_id} .
349 # handle special commit rules & update type
350 handle_commit_maybe($backend_result, $request);
351 } elsif ( $op eq 'check_out') {
352 my $request = Koha::Illrequests->find($params->{illrequest_id});
353 my $backend_result = $request->check_out($params);
356 whole => $backend_result,
359 } elsif ( $op eq 'illlist') {
361 # If we receive a pre-filter, make it available to the template
362 my $possible_filters = ['borrowernumber'];
363 my $active_filters = {};
364 foreach my $filter(@{$possible_filters}) {
365 if ($params->{$filter}) {
366 # We shouldn't need to escape $filter here since we're using
367 # a whitelist, but just to be sure...
368 $active_filters->{uri_escape_utf8($filter)} =
369 uri_escape_utf8(scalar $params->{$filter});
373 if (keys %{$active_filters}) {
374 foreach my $key (keys %{$active_filters}) {
375 push @tpl_arr, $key . "=" . $active_filters->{$key};
379 prefilters => join("&", @tpl_arr)
381 } elsif ( $op eq "save_comment" ) {
382 die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
383 session_id => scalar $cgi->cookie('CGISESSID'),
384 token => scalar $cgi->param('csrf_token'),
386 my $comment = Koha::Illcomment->new({
387 illrequest_id => scalar $params->{illrequest_id},
388 borrowernumber => $patronnumber,
389 comment => scalar $params->{comment},
392 # Redirect to view the whole request
393 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
394 scalar $params->{illrequest_id}
398 } elsif ( $op eq "send_notice" ) {
399 my $illrequest_id = $params->{illrequest_id};
400 my $request = Koha::Illrequests->find($illrequest_id);
401 my $ret = $request->send_patron_notice($params->{notice_code});
403 if ($ret->{result} && scalar @{$ret->{result}->{success}} > 0) {
404 $append .= '&tran_success=' . join(',', @{$ret->{result}->{success}});
406 if ($ret->{result} && scalar @{$ret->{result}->{fail}} > 0) {
407 $append .= '&tran_fail=' . join(',', @{$ret->{result}->{fail}}.join(','));
409 # Redirect to view the whole request
410 print $cgi->redirect(
411 "/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
412 scalar $params->{illrequest_id} . $append
416 my $request = Koha::Illrequests->find($params->{illrequest_id});
417 my $backend_result = $request->custom_capability($op, $params);
419 whole => $backend_result,
423 # handle special commit rules & update type
424 handle_commit_maybe($backend_result, $request);
429 backends => $backends,
430 types => [ "Book", "Article", "Journal" ],
432 branches => scalar Koha::Libraries->search,
435 output_html_with_http_headers( $cgi, $cookie, $template->output );
437 sub handle_commit_maybe {
438 my ( $backend_result, $request ) = @_;
440 # We need to special case 'commit'
441 if ( $backend_result->{stage} eq 'commit' ) {
442 if ( $backend_result->{next} eq 'illview' ) {
444 # Redirect to a view of the newly created request
445 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
451 elsif ( $backend_result->{next} eq 'emigrate' ) {
453 # Redirect to a view of the newly created request
454 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
462 # Redirect to a requests list view
468 sub redirect_to_list {
469 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');