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 $backends = Koha
::Illrequest
::Config
->new->available_backends;
59 my $backends_available = ( scalar @
{$backends} > 0 );
60 $template->param( backends_available
=> $backends_available );
62 if ( $backends_available ) {
63 if ( $op eq 'illview' ) {
64 # View the details of an ILL
65 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
69 csrf_token
=> Koha
::Token
->new->generate_csrf({
70 session_id
=> scalar $cgi->cookie('CGISESSID'),
74 } elsif ( $op eq 'create' ) {
75 # We're in the process of creating a request
76 my $request = Koha
::Illrequest
->new->load_backend( $params->{backend
} );
77 my $backend_result = $request->backend_create($params);
79 whole
=> $backend_result,
82 handle_commit_maybe
($backend_result, $request);
84 } elsif ( $op eq 'confirm' ) {
85 # Backend 'confirm' method
86 # confirm requires a specific request, so first, find it.
87 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
88 my $backend_result = $request->backend_confirm($params);
90 whole
=> $backend_result,
94 # handle special commit rules & update type
95 handle_commit_maybe
($backend_result, $request);
97 } elsif ( $op eq 'cancel' ) {
98 # Backend 'cancel' method
99 # cancel requires a specific request, so first, find it.
100 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
101 my $backend_result = $request->backend_cancel($params);
103 whole
=> $backend_result,
107 # handle special commit rules & update type
108 handle_commit_maybe
($backend_result, $request);
110 } elsif ( $op eq 'edit_action' ) {
111 # Handle edits to the Illrequest object.
112 # (not the Illrequestattributes)
113 # We simulate the API for backend requests for uniformity.
115 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
116 if ( !$params->{stage
} ) {
117 my $backend_result = {
121 method
=> 'edit_action',
127 whole
=> $backend_result,
133 $request->borrowernumber($params->{borrowernumber
});
134 $request->biblio_id($params->{biblio_id
});
135 $request->branchcode($params->{branchcode
});
136 $request->price_paid($params->{price_paid
});
137 $request->notesopac($params->{notesopac
});
138 $request->notesstaff($params->{notesstaff
});
140 my $backend_result = {
144 method
=> 'edit_action',
149 handle_commit_maybe
($backend_result, $request);
152 } elsif ( $op eq 'moderate_action' ) {
153 # Moderate action is required for an ILL submodule / syspref.
154 # Currently still needs to be implemented.
157 } elsif ( $op eq 'delete_confirm') {
158 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
164 } elsif ( $op eq 'delete' ) {
166 # Check if the request is confirmed, if not, redirect
167 # to the confirmation view
168 if ($params->{confirmed
}) {
169 # We simply delete the request...
170 Koha
::Illrequests
->find( $params->{illrequest_id
} )->delete;
171 # ... then return to list view.
174 print $cgi->redirect(
175 "/cgi-bin/koha/ill/ill-requests.pl?" .
176 "method=delete_confirm&illrequest_id=" .
177 $params->{illrequest_id
});
181 } elsif ( $op eq 'mark_completed' ) {
182 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
183 my $backend_result = $request->mark_completed($params);
185 whole
=> $backend_result,
189 # handle special commit rules & update type
190 handle_commit_maybe
($backend_result, $request);
192 } elsif ( $op eq 'generic_confirm' ) {
196 $request = Koha
::Illrequests
->find($params->{illrequest_id
});
197 $params->{current_branchcode
} = C4
::Context
->mybranch;
198 $backend_result = $request->generic_confirm($params);
200 whole
=> $backend_result,
203 $template->param( error
=> $params->{error
} )
208 if ( $_->isa( 'Koha::Exceptions::Ill::NoTargetEmail' ) ) {
209 $error = 'no_target_email';
211 elsif ( $_->isa( 'Koha::Exceptions::Ill::NoLibraryEmail' ) ) {
212 $error = 'no_library_email';
215 $error = 'unknown_error';
217 print $cgi->redirect(
218 "/cgi-bin/koha/ill/ill-requests.pl?" .
219 "method=generic_confirm&illrequest_id=" .
220 $params->{illrequest_id
} .
225 # handle special commit rules & update type
226 handle_commit_maybe
($backend_result, $request);
227 } elsif ( $op eq 'illlist') {
229 # If we receive a pre-filter, make it available to the template
230 my $possible_filters = ['borrowernumber'];
231 my $active_filters = [];
232 foreach my $filter(@
{$possible_filters}) {
233 if ($params->{$filter}) {
234 push @
{$active_filters},
235 { name
=> $filter, value
=> $params->{$filter}};
238 if (scalar @
{$active_filters} > 0) {
240 prefilters
=> $active_filters
244 } elsif ( $op eq "save_comment" ) {
245 die "Wrong CSRF token" unless Koha
::Token
->new->check_csrf({
246 session_id
=> scalar $cgi->cookie('CGISESSID'),
247 token
=> scalar $cgi->param('csrf_token'),
249 my $comment = Koha
::Illcomment
->new({
250 illrequest_id
=> scalar $params->{illrequest_id
},
251 borrowernumber
=> $patronnumber,
252 comment
=> scalar $params->{comment
},
255 # Redirect to view the whole request
256 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
257 scalar $params->{illrequest_id
}
262 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
263 my $backend_result = $request->custom_capability($op, $params);
265 whole
=> $backend_result,
269 # handle special commit rules & update type
270 handle_commit_maybe
($backend_result, $request);
275 backends
=> $backends,
276 media
=> [ "Book", "Article", "Journal" ],
278 branches
=> scalar Koha
::Libraries
->search,
281 output_html_with_http_headers
( $cgi, $cookie, $template->output );
283 sub handle_commit_maybe
{
284 my ( $backend_result, $request ) = @_;
285 # We need to special case 'commit'
286 if ( $backend_result->{stage
} eq 'commit' ) {
287 if ( $backend_result->{next} eq 'illview' ) {
288 # Redirect to a view of the newly created request
289 print $cgi->redirect(
290 '/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id='.
295 # Redirect to a requests list view
301 sub redirect_to_list
{
302 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');