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'),
77 ( $params->{error
} ?
( error
=> $params->{error
} ) : () ),
80 } elsif ( $op eq 'create' ) {
81 # We're in the process of creating a request
82 my $request = Koha
::Illrequest
->new->load_backend( $params->{backend
} );
83 my $backend_result = $request->backend_create($params);
85 whole
=> $backend_result,
88 handle_commit_maybe
($backend_result, $request);
90 } elsif ( $op eq 'migrate' ) {
91 # We're in the process of migrating a request
92 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
94 if ( $params->{backend
} ) {
95 my $new_request = Koha
::Illrequest
->new->load_backend( $params->{backend
} );
96 $backend_result = $new_request->backend_migrate($params);
97 if ($backend_result) {
99 whole
=> $backend_result,
100 request
=> $new_request
102 $request = $new_request;
104 # Backend failure, redirect back to illview
105 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
109 . '&error=migrate_target' );
114 $backend_result = $request->backend_migrate($params);
116 whole
=> $backend_result,
120 handle_commit_maybe
( $backend_result, $request );
122 } elsif ( $op eq 'confirm' ) {
123 # Backend 'confirm' method
124 # confirm requires a specific request, so first, find it.
125 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
126 my $backend_result = $request->backend_confirm($params);
128 whole
=> $backend_result,
132 # handle special commit rules & update type
133 handle_commit_maybe
($backend_result, $request);
135 } elsif ( $op eq 'cancel' ) {
136 # Backend 'cancel' method
137 # cancel requires a specific request, so first, find it.
138 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
139 my $backend_result = $request->backend_cancel($params);
141 whole
=> $backend_result,
145 # handle special commit rules & update type
146 handle_commit_maybe
($backend_result, $request);
148 } elsif ( $op eq 'edit_action' ) {
149 # Handle edits to the Illrequest object.
150 # (not the Illrequestattributes)
151 # We simulate the API for backend requests for uniformity.
153 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
154 if ( !$params->{stage
} ) {
155 my $backend_result = {
159 method
=> 'edit_action',
165 whole
=> $backend_result,
171 $request->borrowernumber($params->{borrowernumber
});
172 $request->biblio_id($params->{biblio_id
});
173 $request->branchcode($params->{branchcode
});
174 $request->price_paid($params->{price_paid
});
175 $request->notesopac($params->{notesopac
});
176 $request->notesstaff($params->{notesstaff
});
178 my $backend_result = {
182 method
=> 'edit_action',
187 handle_commit_maybe
($backend_result, $request);
190 } elsif ( $op eq 'moderate_action' ) {
191 # Moderate action is required for an ILL submodule / syspref.
192 # Currently still needs to be implemented.
195 } elsif ( $op eq 'delete_confirm') {
196 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
202 } elsif ( $op eq 'delete' ) {
204 # Check if the request is confirmed, if not, redirect
205 # to the confirmation view
206 if ($params->{confirmed
}) {
207 # We simply delete the request...
208 Koha
::Illrequests
->find( $params->{illrequest_id
} )->delete;
209 # ... then return to list view.
212 print $cgi->redirect(
213 "/cgi-bin/koha/ill/ill-requests.pl?" .
214 "method=delete_confirm&illrequest_id=" .
215 $params->{illrequest_id
});
219 } elsif ( $op eq 'mark_completed' ) {
220 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
221 my $backend_result = $request->mark_completed($params);
223 whole
=> $backend_result,
227 # handle special commit rules & update type
228 handle_commit_maybe
($backend_result, $request);
230 } elsif ( $op eq 'generic_confirm' ) {
234 $request = Koha
::Illrequests
->find($params->{illrequest_id
});
235 $params->{current_branchcode
} = C4
::Context
->mybranch;
236 $backend_result = $request->generic_confirm($params);
238 whole
=> $backend_result,
241 $template->param( error
=> $params->{error
} )
246 if ( $_->isa( 'Koha::Exceptions::Ill::NoTargetEmail' ) ) {
247 $error = 'no_target_email';
249 elsif ( $_->isa( 'Koha::Exceptions::Ill::NoLibraryEmail' ) ) {
250 $error = 'no_library_email';
253 $error = 'unknown_error';
255 print $cgi->redirect(
256 "/cgi-bin/koha/ill/ill-requests.pl?" .
257 "method=generic_confirm&illrequest_id=" .
258 $params->{illrequest_id
} .
263 # handle special commit rules & update type
264 handle_commit_maybe
($backend_result, $request);
265 } elsif ( $op eq 'illlist') {
267 # If we receive a pre-filter, make it available to the template
268 my $possible_filters = ['borrowernumber'];
269 my $active_filters = [];
270 foreach my $filter(@
{$possible_filters}) {
271 if ($params->{$filter}) {
272 push @
{$active_filters},
273 { name
=> $filter, value
=> $params->{$filter}};
276 if (scalar @
{$active_filters} > 0) {
278 prefilters
=> $active_filters
282 } elsif ( $op eq "save_comment" ) {
283 die "Wrong CSRF token" unless Koha
::Token
->new->check_csrf({
284 session_id
=> scalar $cgi->cookie('CGISESSID'),
285 token
=> scalar $cgi->param('csrf_token'),
287 my $comment = Koha
::Illcomment
->new({
288 illrequest_id
=> scalar $params->{illrequest_id
},
289 borrowernumber
=> $patronnumber,
290 comment
=> scalar $params->{comment
},
293 # Redirect to view the whole request
294 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
295 scalar $params->{illrequest_id
}
300 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
301 my $backend_result = $request->custom_capability($op, $params);
303 whole
=> $backend_result,
307 # handle special commit rules & update type
308 handle_commit_maybe
($backend_result, $request);
313 backends
=> $backends,
314 types
=> [ "Book", "Article", "Journal" ],
316 branches
=> scalar Koha
::Libraries
->search,
319 output_html_with_http_headers
( $cgi, $cookie, $template->output );
321 sub handle_commit_maybe
{
322 my ( $backend_result, $request ) = @_;
324 # We need to special case 'commit'
325 if ( $backend_result->{stage
} eq 'commit' ) {
326 if ( $backend_result->{next} eq 'illview' ) {
328 # Redirect to a view of the newly created request
329 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
335 elsif ( $backend_result->{next} eq 'emigrate' ) {
337 # Redirect to a view of the newly created request
338 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
346 # Redirect to a requests list view
352 sub redirect_to_list
{
353 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');