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>.
26 use Koha
::Notice
::Templates
;
27 use Koha
::AuthorisedValues
;
29 use Koha
::Illrequests
;
30 use Koha
::Illrequest
::Availability
;
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 my $new_request = Koha
::Illrequest
->new->load_backend( $params->{backend
} );
156 $backend_result = $new_request->backend_migrate($params);
157 if ($backend_result) {
159 whole
=> $backend_result,
160 request
=> $new_request
162 $request = $new_request;
164 # Backend failure, redirect back to illview
165 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
169 . '&error=migrate_target' );
174 $backend_result = $request->backend_migrate($params);
176 whole
=> $backend_result,
180 handle_commit_maybe
( $backend_result, $request );
182 } elsif ( $op eq 'confirm' ) {
183 # Backend 'confirm' method
184 # confirm requires a specific request, so first, find it.
185 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
186 my $backend_result = $request->backend_confirm($params);
188 whole
=> $backend_result,
192 # handle special commit rules & update type
193 handle_commit_maybe
($backend_result, $request);
195 } elsif ( $op eq 'cancel' ) {
196 # Backend 'cancel' method
197 # cancel requires a specific request, so first, find it.
198 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
199 my $backend_result = $request->backend_cancel($params);
201 whole
=> $backend_result,
205 # handle special commit rules & update type
206 handle_commit_maybe
($backend_result, $request);
208 } elsif ( $op eq 'edit_action' ) {
209 # Handle edits to the Illrequest object.
210 # (not the Illrequestattributes)
211 # We simulate the API for backend requests for uniformity.
213 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
214 if ( !$params->{stage
} ) {
215 my $backend_result = {
219 method
=> 'edit_action',
225 whole
=> $backend_result,
231 $request->borrowernumber($params->{borrowernumber
});
232 $request->biblio_id($params->{biblio_id
});
233 $request->branchcode($params->{branchcode
});
234 $request->price_paid($params->{price_paid
});
235 $request->notesopac($params->{notesopac
});
236 $request->notesstaff($params->{notesstaff
});
237 my $alias = (length $params->{status_alias
} > 0) ?
238 $params->{status_alias
} :
240 $request->status_alias($alias);
242 my $backend_result = {
246 method
=> 'edit_action',
251 handle_commit_maybe
($backend_result, $request);
254 } elsif ( $op eq 'moderate_action' ) {
255 # Moderate action is required for an ILL submodule / syspref.
256 # Currently still needs to be implemented.
259 } elsif ( $op eq 'delete_confirm') {
260 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
266 } elsif ( $op eq 'delete' ) {
268 # Check if the request is confirmed, if not, redirect
269 # to the confirmation view
270 if ($params->{confirmed
}) {
271 # We simply delete the request...
272 Koha
::Illrequests
->find( $params->{illrequest_id
} )->delete;
273 # ... then return to list view.
276 print $cgi->redirect(
277 "/cgi-bin/koha/ill/ill-requests.pl?" .
278 "method=delete_confirm&illrequest_id=" .
279 $params->{illrequest_id
});
283 } elsif ( $op eq 'mark_completed' ) {
284 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
285 my $backend_result = $request->mark_completed($params);
287 whole
=> $backend_result,
291 # handle special commit rules & update type
292 handle_commit_maybe
($backend_result, $request);
294 } elsif ( $op eq 'generic_confirm' ) {
298 $request = Koha
::Illrequests
->find($params->{illrequest_id
});
299 $params->{current_branchcode
} = C4
::Context
->mybranch;
300 $backend_result = $request->generic_confirm($params);
303 whole
=> $backend_result,
307 # Prepare availability searching, if required
308 # Get the definition for the z39.50 plugin
309 if ( C4
::Context
->preference('ILLCheckAvailability') ) {
310 my $availability = Koha
::Illrequest
::Availability
->new($request->metadata);
311 my $services = $availability->get_services({
312 ui_context
=> 'partners',
314 name
=> 'ILL availability - z39.50'
317 # Only pass availability searching stuff to the template if
319 if ( scalar @
{$services} > 0 ) {
320 my $metadata = $availability->prep_metadata($request->metadata);
321 $template->param( metadata
=> $metadata );
323 services_json
=> scalar encode_json
($services)
325 $template->param( services
=> $services );
329 $template->param( error
=> $params->{error
} )
334 if ( ref($_) eq 'Koha::Exceptions::Ill::NoTargetEmail' ) {
335 $error = 'no_target_email';
337 elsif ( ref($_) eq 'Koha::Exceptions::Ill::NoLibraryEmail' ) {
338 $error = 'no_library_email';
341 $error = 'unknown_error';
343 print $cgi->redirect(
344 "/cgi-bin/koha/ill/ill-requests.pl?" .
345 "method=generic_confirm&illrequest_id=" .
346 $params->{illrequest_id
} .
351 # handle special commit rules & update type
352 handle_commit_maybe
($backend_result, $request);
353 } elsif ( $op eq 'check_out') {
354 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
355 my $backend_result = $request->check_out($params);
358 whole
=> $backend_result,
361 } elsif ( $op eq 'illlist') {
363 # If we receive a pre-filter, make it available to the template
364 my $possible_filters = ['borrowernumber'];
365 my $active_filters = {};
366 foreach my $filter(@
{$possible_filters}) {
367 if ($params->{$filter}) {
368 # We shouldn't need to escape $filter here since we're using
369 # a whitelist, but just to be sure...
370 $active_filters->{uri_escape_utf8
($filter)} =
371 uri_escape_utf8
(scalar $params->{$filter});
375 if (keys %{$active_filters}) {
376 foreach my $key (keys %{$active_filters}) {
377 push @tpl_arr, $key . "=" . $active_filters->{$key};
381 prefilters
=> join("&", @tpl_arr)
383 } elsif ( $op eq "save_comment" ) {
384 die "Wrong CSRF token" unless Koha
::Token
->new->check_csrf({
385 session_id
=> scalar $cgi->cookie('CGISESSID'),
386 token
=> scalar $cgi->param('csrf_token'),
388 my $comment = Koha
::Illcomment
->new({
389 illrequest_id
=> scalar $params->{illrequest_id
},
390 borrowernumber
=> $patronnumber,
391 comment
=> scalar $params->{comment
},
394 # Redirect to view the whole request
395 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
396 scalar $params->{illrequest_id
}
400 } elsif ( $op eq "send_notice" ) {
401 my $illrequest_id = $params->{illrequest_id
};
402 my $request = Koha
::Illrequests
->find($illrequest_id);
403 my $ret = $request->send_patron_notice($params->{notice_code
});
405 if ($ret->{result
} && scalar @
{$ret->{result
}->{success
}} > 0) {
406 $append .= '&tran_success=' . join(',', @
{$ret->{result
}->{success
}});
408 if ($ret->{result
} && scalar @
{$ret->{result
}->{fail
}} > 0) {
409 $append .= '&tran_fail=' . join(',', @
{$ret->{result
}->{fail
}}.join(','));
411 # Redirect to view the whole request
412 print $cgi->redirect(
413 "/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
414 scalar $params->{illrequest_id
} . $append
418 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
419 my $backend_result = $request->custom_capability($op, $params);
421 whole
=> $backend_result,
425 # handle special commit rules & update type
426 handle_commit_maybe
($backend_result, $request);
431 backends
=> $backends,
432 types
=> [ "Book", "Article", "Journal" ],
434 branches
=> scalar Koha
::Libraries
->search,
437 output_html_with_http_headers
( $cgi, $cookie, $template->output );
439 sub handle_commit_maybe
{
440 my ( $backend_result, $request ) = @_;
442 # We need to special case 'commit'
443 if ( $backend_result->{stage
} eq 'commit' ) {
444 if ( $backend_result->{next} eq 'illview' ) {
446 # Redirect to a view of the newly created request
447 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
453 elsif ( $backend_result->{next} eq 'emigrate' ) {
455 # Redirect to a view of the newly created request
456 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
464 # Redirect to a requests list view
470 sub redirect_to_list
{
471 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');