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 'confirm' ) {
90 # Backend 'confirm' method
91 # confirm requires a specific request, so first, find it.
92 my $request = Koha::Illrequests->find($params->{illrequest_id});
93 my $backend_result = $request->backend_confirm($params);
95 whole => $backend_result,
99 # handle special commit rules & update type
100 handle_commit_maybe($backend_result, $request);
102 } elsif ( $op eq 'cancel' ) {
103 # Backend 'cancel' method
104 # cancel requires a specific request, so first, find it.
105 my $request = Koha::Illrequests->find($params->{illrequest_id});
106 my $backend_result = $request->backend_cancel($params);
108 whole => $backend_result,
112 # handle special commit rules & update type
113 handle_commit_maybe($backend_result, $request);
115 } elsif ( $op eq 'edit_action' ) {
116 # Handle edits to the Illrequest object.
117 # (not the Illrequestattributes)
118 # We simulate the API for backend requests for uniformity.
120 my $request = Koha::Illrequests->find($params->{illrequest_id});
121 if ( !$params->{stage} ) {
122 my $backend_result = {
126 method => 'edit_action',
132 whole => $backend_result,
138 $request->borrowernumber($params->{borrowernumber});
139 $request->biblio_id($params->{biblio_id});
140 $request->branchcode($params->{branchcode});
141 $request->price_paid($params->{price_paid});
142 $request->notesopac($params->{notesopac});
143 $request->notesstaff($params->{notesstaff});
145 my $backend_result = {
149 method => 'edit_action',
154 handle_commit_maybe($backend_result, $request);
157 } elsif ( $op eq 'moderate_action' ) {
158 # Moderate action is required for an ILL submodule / syspref.
159 # Currently still needs to be implemented.
162 } elsif ( $op eq 'delete_confirm') {
163 my $request = Koha::Illrequests->find($params->{illrequest_id});
169 } elsif ( $op eq 'delete' ) {
171 # Check if the request is confirmed, if not, redirect
172 # to the confirmation view
173 if ($params->{confirmed}) {
174 # We simply delete the request...
175 Koha::Illrequests->find( $params->{illrequest_id} )->delete;
176 # ... then return to list view.
179 print $cgi->redirect(
180 "/cgi-bin/koha/ill/ill-requests.pl?" .
181 "method=delete_confirm&illrequest_id=" .
182 $params->{illrequest_id});
186 } elsif ( $op eq 'mark_completed' ) {
187 my $request = Koha::Illrequests->find($params->{illrequest_id});
188 my $backend_result = $request->mark_completed($params);
190 whole => $backend_result,
194 # handle special commit rules & update type
195 handle_commit_maybe($backend_result, $request);
197 } elsif ( $op eq 'generic_confirm' ) {
201 $request = Koha::Illrequests->find($params->{illrequest_id});
202 $params->{current_branchcode} = C4::Context->mybranch;
203 $backend_result = $request->generic_confirm($params);
205 whole => $backend_result,
208 $template->param( error => $params->{error} )
213 if ( $_->isa( 'Koha::Exceptions::Ill::NoTargetEmail' ) ) {
214 $error = 'no_target_email';
216 elsif ( $_->isa( 'Koha::Exceptions::Ill::NoLibraryEmail' ) ) {
217 $error = 'no_library_email';
220 $error = 'unknown_error';
222 print $cgi->redirect(
223 "/cgi-bin/koha/ill/ill-requests.pl?" .
224 "method=generic_confirm&illrequest_id=" .
225 $params->{illrequest_id} .
230 # handle special commit rules & update type
231 handle_commit_maybe($backend_result, $request);
232 } elsif ( $op eq 'illlist') {
234 # If we receive a pre-filter, make it available to the template
235 my $possible_filters = ['borrowernumber'];
236 my $active_filters = [];
237 foreach my $filter(@{$possible_filters}) {
238 if ($params->{$filter}) {
239 push @{$active_filters},
240 { name => $filter, value => $params->{$filter}};
243 if (scalar @{$active_filters} > 0) {
245 prefilters => $active_filters
249 } elsif ( $op eq "save_comment" ) {
250 die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
251 session_id => scalar $cgi->cookie('CGISESSID'),
252 token => scalar $cgi->param('csrf_token'),
254 my $comment = Koha::Illcomment->new({
255 illrequest_id => scalar $params->{illrequest_id},
256 borrowernumber => $patronnumber,
257 comment => scalar $params->{comment},
260 # Redirect to view the whole request
261 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
262 scalar $params->{illrequest_id}
267 my $request = Koha::Illrequests->find($params->{illrequest_id});
268 my $backend_result = $request->custom_capability($op, $params);
270 whole => $backend_result,
274 # handle special commit rules & update type
275 handle_commit_maybe($backend_result, $request);
280 backends => $backends,
281 types => [ "Book", "Article", "Journal" ],
283 branches => scalar Koha::Libraries->search,
286 output_html_with_http_headers( $cgi, $cookie, $template->output );
288 sub handle_commit_maybe {
289 my ( $backend_result, $request ) = @_;
290 # We need to special case 'commit'
291 if ( $backend_result->{stage} eq 'commit' ) {
292 if ( $backend_result->{next} eq 'illview' ) {
293 # Redirect to a view of the newly created request
294 print $cgi->redirect(
295 '/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id='.
300 # Redirect to a requests list view
306 sub redirect_to_list {
307 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');