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;
30 use Koha::Illrequest::Workflow::Availability;
31 use Koha::Illrequest::Workflow::TypeDisclaimer;
35 use Try::Tiny qw( catch try );
36 use URI::Escape qw( uri_escape_utf8 );
37 use JSON qw( encode_json );
40 my $illRequests = Koha::Illrequests->new;
42 # Grab all passed data
43 # 'our' since Plack changes the scoping
45 our $params = $cgi->Vars();
47 # Leave immediately if ILLModule is disabled
48 unless ( C4::Context->preference('ILLModule') ) {
49 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
53 my $op = $params->{method} || 'illlist';
55 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
56 template_name => 'ill/ill-requests.tt',
59 flagsrequired => { ill => '*' },
62 # Are we able to actually work?
63 my $cfg = Koha::Illrequest::Config->new;
64 my $backends = $cfg->available_backends;
65 my $has_branch = $cfg->has_branch;
66 my $backends_available = ( scalar @{$backends} > 0 );
68 backends_available => $backends_available,
69 has_branch => $has_branch
72 if ( $backends_available ) {
73 if ( $op eq 'illview' ) {
74 # View the details of an ILL
75 my $request = Koha::Illrequests->find($params->{illrequest_id});
77 # Get the details for notices that can be sent from here
78 my $notices = Koha::Notice::Templates->search(
81 code => { -in => [ 'ILL_PICKUP_READY' ,'ILL_REQUEST_UNAVAIL' ] },
84 columns => [ qw/code name/ ],
92 csrf_token => Koha::Token->new->generate_csrf({
93 session_id => scalar $cgi->cookie('CGISESSID'),
95 ( $params->{tran_error} ?
96 ( tran_error => $params->{tran_error} ) : () ),
97 ( $params->{tran_success} ?
98 ( tran_success => $params->{tran_success} ) : () ),
101 output_and_exit( $cgi, $cookie, $template, 'unknown_ill_request' ) if !$request;
103 my $backend_result = $request->backend_illview($params);
105 whole => $backend_result,
106 ) if $backend_result;
109 } elsif ( $op eq 'create' ) {
110 # Load the ILL backend
111 my $request = Koha::Illrequest->new->load_backend( $params->{backend} );
113 # Before request creation operations - Preparation
115 Koha::Illrequest::Workflow::Availability->new( $params, 'staff' );
116 my $type_disclaimer =
117 Koha::Illrequest::Workflow::TypeDisclaimer->new( $params, 'staff' );
119 # ILLCheckAvailability operation
120 if ($availability->show_availability($request)) {
121 $op = 'availability';
123 $availability->availability_template_params($params)
125 # ILLModuleDisclaimerByType operation
126 } elsif ( $type_disclaimer->show_type_disclaimer($request)) {
127 $op = 'typedisclaimer';
129 $type_disclaimer->type_disclaimer_template_params($params)
131 # Ready to create ILL request
133 my $backend_result = $request->backend_create($params);
135 # After creation actions
136 if ( $params->{type_disclaimer_submitted} ) {
137 $type_disclaimer->after_request_created($params, $request);
141 whole => $backend_result,
144 handle_commit_maybe($backend_result, $request);
146 } elsif ( $op eq 'migrate' ) {
147 # We're in the process of migrating a request
148 my $request = Koha::Illrequests->find($params->{illrequest_id});
150 if ( $params->{backend} ) {
151 $backend_result = $request->backend_migrate($params);
152 if ($backend_result) {
154 whole => $backend_result,
158 # Backend failure, redirect back to illview
159 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
163 . '&error=migrate_target' );
168 $backend_result = $request->backend_migrate($params);
170 whole => $backend_result,
174 handle_commit_maybe( $backend_result, $request );
176 } elsif ( $op eq 'confirm' ) {
177 # Backend 'confirm' method
178 # confirm requires a specific request, so first, find it.
179 my $request = Koha::Illrequests->find($params->{illrequest_id});
180 my $backend_result = $request->backend_confirm($params);
182 whole => $backend_result,
186 # handle special commit rules & update type
187 handle_commit_maybe($backend_result, $request);
189 } elsif ( $op eq 'cancel' ) {
190 # Backend 'cancel' method
191 # cancel requires a specific request, so first, find it.
192 my $request = Koha::Illrequests->find($params->{illrequest_id});
193 my $backend_result = $request->backend_cancel($params);
195 whole => $backend_result,
199 # handle special commit rules & update type
200 handle_commit_maybe($backend_result, $request);
202 } elsif ( $op eq 'edit_action' ) {
203 # Handle edits to the Illrequest object.
204 # (not the Illrequestattributes)
205 # We simulate the API for backend requests for uniformity.
207 my $request = Koha::Illrequests->find($params->{illrequest_id});
208 if ( !$params->{stage} ) {
209 my $backend_result = {
213 method => 'edit_action',
219 whole => $backend_result,
225 $request->borrowernumber($params->{borrowernumber});
226 $request->biblio_id($params->{biblio_id});
227 $request->branchcode($params->{branchcode});
228 $request->price_paid($params->{price_paid});
229 $request->notesopac($params->{notesopac});
230 $request->notesstaff($params->{notesstaff});
231 my $alias = (length $params->{status_alias} > 0) ?
232 $params->{status_alias} :
234 $request->status_alias($alias);
236 my $backend_result = {
240 method => 'edit_action',
245 handle_commit_maybe($backend_result, $request);
248 } elsif ( $op eq 'moderate_action' ) {
249 # Moderate action is required for an ILL submodule / syspref.
250 # Currently still needs to be implemented.
253 } elsif ( $op eq 'delete_confirm') {
254 my $request = Koha::Illrequests->find($params->{illrequest_id});
260 } elsif ( $op eq 'delete' ) {
262 # Check if the request is confirmed, if not, redirect
263 # to the confirmation view
264 if ($params->{confirmed}) {
265 # We simply delete the request...
266 Koha::Illrequests->find( $params->{illrequest_id} )->delete;
267 # ... then return to list view.
270 print $cgi->redirect(
271 "/cgi-bin/koha/ill/ill-requests.pl?" .
272 "method=delete_confirm&illrequest_id=" .
273 $params->{illrequest_id});
277 } elsif ( $op eq 'mark_completed' ) {
278 my $request = Koha::Illrequests->find($params->{illrequest_id});
279 my $backend_result = $request->mark_completed($params);
281 whole => $backend_result,
285 # handle special commit rules & update type
286 handle_commit_maybe($backend_result, $request);
288 } elsif ( $op eq 'generic_confirm' ) {
292 $request = Koha::Illrequests->find($params->{illrequest_id});
293 $params->{current_branchcode} = C4::Context->mybranch;
294 $backend_result = $request->generic_confirm($params);
297 whole => $backend_result,
301 # Prepare availability searching, if required
302 # Get the definition for the z39.50 plugin
303 if ( C4::Context->preference('ILLCheckAvailability') ) {
304 my $availability = Koha::Illrequest::Workflow::Availability->new(
306 name => 'ILL availability - z39.50',
307 %{$request->metadata}
311 my $services = $availability->get_services();
312 # Only pass availability searching stuff to the template if
314 if ( scalar @{$services} > 0 ) {
315 my $metadata = $availability->prep_metadata($request->metadata);
316 $template->param( metadata => $metadata );
318 services_json => scalar encode_json($services)
320 $template->param( services => $services );
324 $template->param( error => $params->{error} )
329 if ( ref($_) eq 'Koha::Exceptions::Ill::NoTargetEmail' ) {
330 $error = 'no_target_email';
332 elsif ( ref($_) eq 'Koha::Exceptions::Ill::NoLibraryEmail' ) {
333 $error = 'no_library_email';
336 $error = 'unknown_error';
338 print $cgi->redirect(
339 "/cgi-bin/koha/ill/ill-requests.pl?" .
340 "method=generic_confirm&illrequest_id=" .
341 $params->{illrequest_id} .
346 # handle special commit rules & update type
347 handle_commit_maybe($backend_result, $request);
348 } elsif ( $op eq 'check_out') {
349 my $request = Koha::Illrequests->find($params->{illrequest_id});
350 my $backend_result = $request->check_out($params);
353 whole => $backend_result,
356 } elsif ( $op eq 'illlist') {
358 # If we receive a pre-filter, make it available to the template
359 my $possible_filters = ['borrowernumber'];
360 my $active_filters = {};
361 foreach my $filter(@{$possible_filters}) {
362 if ($params->{$filter}) {
363 # We shouldn't need to escape $filter here since we're using
364 # a whitelist, but just to be sure...
365 $active_filters->{uri_escape_utf8($filter)} =
366 uri_escape_utf8(scalar $params->{$filter});
370 if (keys %{$active_filters}) {
371 foreach my $key (keys %{$active_filters}) {
372 push @tpl_arr, $key . "=" . $active_filters->{$key};
376 prefilters => join("&", @tpl_arr)
378 } elsif ( $op eq "save_comment" ) {
379 die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
380 session_id => scalar $cgi->cookie('CGISESSID'),
381 token => scalar $cgi->param('csrf_token'),
383 my $comment = Koha::Illcomment->new({
384 illrequest_id => scalar $params->{illrequest_id},
385 borrowernumber => $patronnumber,
386 comment => scalar $params->{comment},
389 # Redirect to view the whole request
390 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
391 scalar $params->{illrequest_id}
395 } elsif ( $op eq "send_notice" ) {
396 my $illrequest_id = $params->{illrequest_id};
397 my $request = Koha::Illrequests->find($illrequest_id);
398 my $ret = $request->send_patron_notice($params->{notice_code});
400 if ($ret->{result} && scalar @{$ret->{result}->{success}} > 0) {
401 $append .= '&tran_success=' . join(',', @{$ret->{result}->{success}});
403 if ($ret->{result} && scalar @{$ret->{result}->{fail}} > 0) {
404 $append .= '&tran_fail=' . join(',', @{$ret->{result}->{fail}}.join(','));
406 # Redirect to view the whole request
407 print $cgi->redirect(
408 "/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
409 scalar $params->{illrequest_id} . $append
413 my $request = Koha::Illrequests->find($params->{illrequest_id});
414 my $backend_result = $request->custom_capability($op, $params);
416 whole => $backend_result,
420 # handle special commit rules & update type
421 handle_commit_maybe($backend_result, $request);
426 backends => $backends,
427 types => [ "Book", "Article", "Journal" ],
429 branches => Koha::Libraries->search,
432 output_html_with_http_headers( $cgi, $cookie, $template->output );
434 sub handle_commit_maybe {
435 my ( $backend_result, $request ) = @_;
437 # We need to special case 'commit'
438 if ( $backend_result->{stage} eq 'commit' ) {
439 if ( $backend_result->{next} eq 'illview' ) {
441 # Redirect to a view of the newly created request
442 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
448 elsif ( $backend_result->{next} eq 'emigrate' ) {
450 # Redirect to a view of the newly created request
451 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
459 # Redirect to a requests list view
465 sub redirect_to_list {
466 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');