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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 use Koha::AuthorisedValues;
28 use Koha::Illrequests;
35 my $illRequests = Koha::Illrequests->new;
37 # Grab all passed data
38 # 'our' since Plack changes the scoping
40 our $params = $cgi->Vars();
42 # Leave immediately if ILLModule is disabled
43 unless ( C4::Context->preference('ILLModule') ) {
44 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
48 my $op = $params->{method} || 'illlist';
50 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
51 template_name => 'ill/ill-requests.tt',
54 flagsrequired => { ill => '*' },
57 # Are we able to actually work?
58 my $cfg = Koha::Illrequest::Config->new;
59 my $backends = $cfg->available_backends;
60 my $has_branch = $cfg->has_branch;
61 my $backends_available = ( scalar @{$backends} > 0 );
63 backends_available => $backends_available,
64 has_branch => $has_branch
67 if ( $backends_available ) {
68 if ( $op eq 'illview' ) {
69 # View the details of an ILL
70 my $request = Koha::Illrequests->find($params->{illrequest_id});
74 csrf_token => Koha::Token->new->generate_csrf({
75 session_id => scalar $cgi->cookie('CGISESSID'),
79 } elsif ( $op eq 'create' ) {
80 # We're in the process of creating a request
81 my $request = Koha::Illrequest->new->load_backend( $params->{backend} );
82 my $backend_result = $request->backend_create($params);
84 whole => $backend_result,
87 handle_commit_maybe($backend_result, $request);
89 } elsif ( $op eq 'migrate' ) {
90 # We're in the process of migrating a request
91 my $request = Koha::Illrequests->find($params->{illrequest_id});
93 if ( $params->{backend} ) {
94 my $new_request = Koha::Illrequest->new->load_backend( $params->{backend} );
95 $backend_result = $new_request->backend_migrate($params);
97 whole => $backend_result,
98 request => $new_request
102 $request = Koha::Illrequests->find( $params->{illrequest_id} );
103 $backend_result = $request->backend_migrate($params);
105 whole => $backend_result,
109 handle_commit_maybe( $backend_result, $request );
111 } elsif ( $op eq 'confirm' ) {
112 # Backend 'confirm' method
113 # confirm requires a specific request, so first, find it.
114 my $request = Koha::Illrequests->find($params->{illrequest_id});
115 my $backend_result = $request->backend_confirm($params);
117 whole => $backend_result,
121 # handle special commit rules & update type
122 handle_commit_maybe($backend_result, $request);
124 } elsif ( $op eq 'cancel' ) {
125 # Backend 'cancel' method
126 # cancel requires a specific request, so first, find it.
127 my $request = Koha::Illrequests->find($params->{illrequest_id});
128 my $backend_result = $request->backend_cancel($params);
130 whole => $backend_result,
134 # handle special commit rules & update type
135 handle_commit_maybe($backend_result, $request);
137 } elsif ( $op eq 'edit_action' ) {
138 # Handle edits to the Illrequest object.
139 # (not the Illrequestattributes)
140 # We simulate the API for backend requests for uniformity.
142 my $request = Koha::Illrequests->find($params->{illrequest_id});
143 if ( !$params->{stage} ) {
144 my $backend_result = {
148 method => 'edit_action',
154 whole => $backend_result,
160 $request->borrowernumber($params->{borrowernumber});
161 $request->biblio_id($params->{biblio_id});
162 $request->branchcode($params->{branchcode});
163 $request->price_paid($params->{price_paid});
164 $request->notesopac($params->{notesopac});
165 $request->notesstaff($params->{notesstaff});
167 my $backend_result = {
171 method => 'edit_action',
176 handle_commit_maybe($backend_result, $request);
179 } elsif ( $op eq 'moderate_action' ) {
180 # Moderate action is required for an ILL submodule / syspref.
181 # Currently still needs to be implemented.
184 } elsif ( $op eq 'delete_confirm') {
185 my $request = Koha::Illrequests->find($params->{illrequest_id});
191 } elsif ( $op eq 'delete' ) {
193 # Check if the request is confirmed, if not, redirect
194 # to the confirmation view
195 if ($params->{confirmed}) {
196 # We simply delete the request...
197 Koha::Illrequests->find( $params->{illrequest_id} )->delete;
198 # ... then return to list view.
201 print $cgi->redirect(
202 "/cgi-bin/koha/ill/ill-requests.pl?" .
203 "method=delete_confirm&illrequest_id=" .
204 $params->{illrequest_id});
208 } elsif ( $op eq 'mark_completed' ) {
209 my $request = Koha::Illrequests->find($params->{illrequest_id});
210 my $backend_result = $request->mark_completed($params);
212 whole => $backend_result,
216 # handle special commit rules & update type
217 handle_commit_maybe($backend_result, $request);
219 } elsif ( $op eq 'generic_confirm' ) {
223 $request = Koha::Illrequests->find($params->{illrequest_id});
224 $params->{current_branchcode} = C4::Context->mybranch;
225 $backend_result = $request->generic_confirm($params);
227 whole => $backend_result,
230 $template->param( error => $params->{error} )
235 if ( $_->isa( 'Koha::Exceptions::Ill::NoTargetEmail' ) ) {
236 $error = 'no_target_email';
238 elsif ( $_->isa( 'Koha::Exceptions::Ill::NoLibraryEmail' ) ) {
239 $error = 'no_library_email';
242 $error = 'unknown_error';
244 print $cgi->redirect(
245 "/cgi-bin/koha/ill/ill-requests.pl?" .
246 "method=generic_confirm&illrequest_id=" .
247 $params->{illrequest_id} .
252 # handle special commit rules & update type
253 handle_commit_maybe($backend_result, $request);
254 } elsif ( $op eq 'illlist') {
256 # If we receive a pre-filter, make it available to the template
257 my $possible_filters = ['borrowernumber'];
258 my $active_filters = [];
259 foreach my $filter(@{$possible_filters}) {
260 if ($params->{$filter}) {
261 push @{$active_filters},
262 { name => $filter, value => $params->{$filter}};
265 if (scalar @{$active_filters} > 0) {
267 prefilters => $active_filters
271 } elsif ( $op eq "save_comment" ) {
272 die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
273 session_id => scalar $cgi->cookie('CGISESSID'),
274 token => scalar $cgi->param('csrf_token'),
276 my $comment = Koha::Illcomment->new({
277 illrequest_id => scalar $params->{illrequest_id},
278 borrowernumber => $patronnumber,
279 comment => scalar $params->{comment},
282 # Redirect to view the whole request
283 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
284 scalar $params->{illrequest_id}
289 my $request = Koha::Illrequests->find($params->{illrequest_id});
290 my $backend_result = $request->custom_capability($op, $params);
292 whole => $backend_result,
296 # handle special commit rules & update type
297 handle_commit_maybe($backend_result, $request);
302 backends => $backends,
303 types => [ "Book", "Article", "Journal" ],
305 branches => scalar Koha::Libraries->search,
308 output_html_with_http_headers( $cgi, $cookie, $template->output );
310 sub handle_commit_maybe {
311 my ( $backend_result, $request ) = @_;
313 # We need to special case 'commit'
314 if ( $backend_result->{stage} eq 'commit' ) {
315 if ( $backend_result->{next} eq 'illview' ) {
317 # Redirect to a view of the newly created request
318 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
324 elsif ( $backend_result->{next} eq 'emigrate' ) {
326 # Redirect to a view of the newly created request
327 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
335 # Redirect to a requests list view
341 sub redirect_to_list {
342 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');