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($request->metadata);
305 my $services = $availability->get_services({
306 ui_context => 'partners',
308 name => 'ILL availability - z39.50'
311 # Only pass availability searching stuff to the template if
313 if ( scalar @{$services} > 0 ) {
314 my $metadata = $availability->prep_metadata($request->metadata);
315 $template->param( metadata => $metadata );
317 services_json => scalar encode_json($services)
319 $template->param( services => $services );
323 $template->param( error => $params->{error} )
328 if ( ref($_) eq 'Koha::Exceptions::Ill::NoTargetEmail' ) {
329 $error = 'no_target_email';
331 elsif ( ref($_) eq 'Koha::Exceptions::Ill::NoLibraryEmail' ) {
332 $error = 'no_library_email';
335 $error = 'unknown_error';
337 print $cgi->redirect(
338 "/cgi-bin/koha/ill/ill-requests.pl?" .
339 "method=generic_confirm&illrequest_id=" .
340 $params->{illrequest_id} .
345 # handle special commit rules & update type
346 handle_commit_maybe($backend_result, $request);
347 } elsif ( $op eq 'check_out') {
348 my $request = Koha::Illrequests->find($params->{illrequest_id});
349 my $backend_result = $request->check_out($params);
352 whole => $backend_result,
355 } elsif ( $op eq 'illlist') {
357 # If we receive a pre-filter, make it available to the template
358 my $possible_filters = ['borrowernumber'];
359 my $active_filters = {};
360 foreach my $filter(@{$possible_filters}) {
361 if ($params->{$filter}) {
362 # We shouldn't need to escape $filter here since we're using
363 # a whitelist, but just to be sure...
364 $active_filters->{uri_escape_utf8($filter)} =
365 uri_escape_utf8(scalar $params->{$filter});
369 if (keys %{$active_filters}) {
370 foreach my $key (keys %{$active_filters}) {
371 push @tpl_arr, $key . "=" . $active_filters->{$key};
375 prefilters => join("&", @tpl_arr)
377 } elsif ( $op eq "save_comment" ) {
378 die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
379 session_id => scalar $cgi->cookie('CGISESSID'),
380 token => scalar $cgi->param('csrf_token'),
382 my $comment = Koha::Illcomment->new({
383 illrequest_id => scalar $params->{illrequest_id},
384 borrowernumber => $patronnumber,
385 comment => scalar $params->{comment},
388 # Redirect to view the whole request
389 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
390 scalar $params->{illrequest_id}
394 } elsif ( $op eq "send_notice" ) {
395 my $illrequest_id = $params->{illrequest_id};
396 my $request = Koha::Illrequests->find($illrequest_id);
397 my $ret = $request->send_patron_notice($params->{notice_code});
399 if ($ret->{result} && scalar @{$ret->{result}->{success}} > 0) {
400 $append .= '&tran_success=' . join(',', @{$ret->{result}->{success}});
402 if ($ret->{result} && scalar @{$ret->{result}->{fail}} > 0) {
403 $append .= '&tran_fail=' . join(',', @{$ret->{result}->{fail}}.join(','));
405 # Redirect to view the whole request
406 print $cgi->redirect(
407 "/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
408 scalar $params->{illrequest_id} . $append
412 my $request = Koha::Illrequests->find($params->{illrequest_id});
413 my $backend_result = $request->custom_capability($op, $params);
415 whole => $backend_result,
419 # handle special commit rules & update type
420 handle_commit_maybe($backend_result, $request);
425 backends => $backends,
426 types => [ "Book", "Article", "Journal" ],
428 branches => Koha::Libraries->search,
431 output_html_with_http_headers( $cgi, $cookie, $template->output );
433 sub handle_commit_maybe {
434 my ( $backend_result, $request ) = @_;
436 # We need to special case 'commit'
437 if ( $backend_result->{stage} eq 'commit' ) {
438 if ( $backend_result->{next} eq 'illview' ) {
440 # Redirect to a view of the newly created request
441 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
447 elsif ( $backend_result->{next} eq 'emigrate' ) {
449 # Redirect to a view of the newly created request
450 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
458 # Redirect to a requests list view
464 sub redirect_to_list {
465 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');