Bug 12365: Notes for rules
[koha.git] / ill / ill-requests.pl
blob28e8cc634f3dba7c386ce77cea091795c6d0cc88
1 #!/usr/bin/perl
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
10 # version.
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.
20 use Modern::Perl;
22 use CGI;
24 use C4::Auth;
25 use C4::Output;
26 use Koha::AuthorisedValues;
27 use Koha::Illcomment;
28 use Koha::Illrequests;
29 use Koha::Libraries;
30 use Koha::Token;
32 use Try::Tiny;
34 our $cgi = CGI->new;
35 my $illRequests = Koha::Illrequests->new;
37 # Grab all passed data
38 # 'our' since Plack changes the scoping
39 # of 'my'
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");
45 exit;
48 my $op = $params->{method} || 'illlist';
50 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
51 template_name => 'ill/ill-requests.tt',
52 query => $cgi,
53 type => 'intranet',
54 flagsrequired => { ill => '*' },
55 } );
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 );
62 $template->param(
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});
72 $template->param(
73 request => $request,
74 csrf_token => Koha::Token->new->generate_csrf({
75 session_id => scalar $cgi->cookie('CGISESSID'),
76 }),
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);
83 $template->param(
84 whole => $backend_result,
85 request => $request
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);
94 $template->param(
95 whole => $backend_result,
96 request => $request,
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);
107 $template->param(
108 whole => $backend_result,
109 request => $request,
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.
119 # So, init:
120 my $request = Koha::Illrequests->find($params->{illrequest_id});
121 if ( !$params->{stage} ) {
122 my $backend_result = {
123 error => 0,
124 status => '',
125 message => '',
126 method => 'edit_action',
127 stage => 'init',
128 next => '',
129 value => {}
131 $template->param(
132 whole => $backend_result,
133 request => $request
135 } else {
136 # Commit:
137 # Save the changes
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});
144 $request->store;
145 my $backend_result = {
146 error => 0,
147 status => '',
148 message => '',
149 method => 'edit_action',
150 stage => 'commit',
151 next => 'illlist',
152 value => {}
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.
160 redirect_to_list();
162 } elsif ( $op eq 'delete_confirm') {
163 my $request = Koha::Illrequests->find($params->{illrequest_id});
165 $template->param(
166 request => $request
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.
177 redirect_to_list();
178 } else {
179 print $cgi->redirect(
180 "/cgi-bin/koha/ill/ill-requests.pl?" .
181 "method=delete_confirm&illrequest_id=" .
182 $params->{illrequest_id});
183 exit;
186 } elsif ( $op eq 'mark_completed' ) {
187 my $request = Koha::Illrequests->find($params->{illrequest_id});
188 my $backend_result = $request->mark_completed($params);
189 $template->param(
190 whole => $backend_result,
191 request => $request,
194 # handle special commit rules & update type
195 handle_commit_maybe($backend_result, $request);
197 } elsif ( $op eq 'generic_confirm' ) {
198 my $backend_result;
199 my $request;
200 try {
201 $request = Koha::Illrequests->find($params->{illrequest_id});
202 $params->{current_branchcode} = C4::Context->mybranch;
203 $backend_result = $request->generic_confirm($params);
204 $template->param(
205 whole => $backend_result,
206 request => $request,
208 $template->param( error => $params->{error} )
209 if $params->{error};
211 catch {
212 my $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';
219 else {
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} .
226 "&error=$error" );
227 exit;
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) {
244 $template->param(
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},
259 $comment->store();
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}
264 exit;
266 } else {
267 my $request = Koha::Illrequests->find($params->{illrequest_id});
268 my $backend_result = $request->custom_capability($op, $params);
269 $template->param(
270 whole => $backend_result,
271 request => $request,
274 # handle special commit rules & update type
275 handle_commit_maybe($backend_result, $request);
279 $template->param(
280 backends => $backends,
281 types => [ "Book", "Article", "Journal" ],
282 query_type => $op,
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='.
296 $request->id
298 exit;
299 } else {
300 # Redirect to a requests list view
301 redirect_to_list();
306 sub redirect_to_list {
307 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
308 exit;