Bug 7317: (followup) Remove extra columns from backends on the main list
[koha.git] / ill / ill-requests.pl
blob034ce02939c9a44325dbc04e446644e65686b6da
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::Illrequests;
28 use Koha::Libraries;
30 use Try::Tiny;
32 our $cgi = CGI->new;
33 my $illRequests = Koha::Illrequests->new;
35 # Grab all passed data
36 # 'our' since Plack changes the scoping
37 # of 'my'
38 our $params = $cgi->Vars();
40 # Leave immediately if ILLModule is disabled
41 unless ( C4::Context->preference('ILLModule') ) {
42 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
43 exit;
46 my $op = $params->{method} || 'illlist';
48 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
49 template_name => 'ill/ill-requests.tt',
50 query => $cgi,
51 type => 'intranet',
52 flagsrequired => { ill => '*' },
53 } );
55 # Are we able to actually work?
56 my $backends = Koha::Illrequest::Config->new->available_backends;
57 my $backends_available = ( scalar @{$backends} > 0 );
58 $template->param( backends_available => $backends_available );
60 if ( $backends_available ) {
61 if ( $op eq 'illview' ) {
62 # View the details of an ILL
63 my $request = Koha::Illrequests->find($params->{illrequest_id});
65 $template->param(
66 request => $request
69 } elsif ( $op eq 'create' ) {
70 # We're in the process of creating a request
71 my $request = Koha::Illrequest->new->load_backend( $params->{backend} );
72 my $backend_result = $request->backend_create($params);
73 $template->param(
74 whole => $backend_result,
75 request => $request
77 handle_commit_maybe($backend_result, $request);
79 } elsif ( $op eq 'confirm' ) {
80 # Backend 'confirm' method
81 # confirm requires a specific request, so first, find it.
82 my $request = Koha::Illrequests->find($params->{illrequest_id});
83 my $backend_result = $request->backend_confirm($params);
84 $template->param(
85 whole => $backend_result,
86 request => $request,
89 # handle special commit rules & update type
90 handle_commit_maybe($backend_result, $request);
92 } elsif ( $op eq 'cancel' ) {
93 # Backend 'cancel' method
94 # cancel requires a specific request, so first, find it.
95 my $request = Koha::Illrequests->find($params->{illrequest_id});
96 my $backend_result = $request->backend_cancel($params);
97 $template->param(
98 whole => $backend_result,
99 request => $request,
102 # handle special commit rules & update type
103 handle_commit_maybe($backend_result, $request);
105 } elsif ( $op eq 'edit_action' ) {
106 # Handle edits to the Illrequest object.
107 # (not the Illrequestattributes)
108 # We simulate the API for backend requests for uniformity.
109 # So, init:
110 my $request = Koha::Illrequests->find($params->{illrequest_id});
111 if ( !$params->{stage} ) {
112 my $backend_result = {
113 error => 0,
114 status => '',
115 message => '',
116 method => 'edit_action',
117 stage => 'init',
118 next => '',
119 value => {}
121 $template->param(
122 whole => $backend_result,
123 request => $request
125 } else {
126 # Commit:
127 # Save the changes
128 $request->borrowernumber($params->{borrowernumber});
129 $request->biblio_id($params->{biblio_id});
130 $request->branchcode($params->{branchcode});
131 $request->notesopac($params->{notesopac});
132 $request->notesstaff($params->{notesstaff});
133 $request->store;
134 my $backend_result = {
135 error => 0,
136 status => '',
137 message => '',
138 method => 'edit_action',
139 stage => 'commit',
140 next => 'illlist',
141 value => {}
143 handle_commit_maybe($backend_result, $request);
146 } elsif ( $op eq 'moderate_action' ) {
147 # Moderate action is required for an ILL submodule / syspref.
148 # Currently still needs to be implemented.
149 redirect_to_list();
151 } elsif ( $op eq 'delete_confirm') {
152 my $request = Koha::Illrequests->find($params->{illrequest_id});
154 $template->param(
155 request => $request
158 } elsif ( $op eq 'delete' ) {
160 # Check if the request is confirmed, if not, redirect
161 # to the confirmation view
162 if ($params->{confirmed}) {
163 # We simply delete the request...
164 Koha::Illrequests->find( $params->{illrequest_id} )->delete;
165 # ... then return to list view.
166 redirect_to_list();
167 } else {
168 print $cgi->redirect(
169 "/cgi-bin/koha/ill/ill-requests.pl?" .
170 "method=delete_confirm&illrequest_id=" .
171 $params->{illrequest_id});
172 exit;
175 } elsif ( $op eq 'mark_completed' ) {
176 my $request = Koha::Illrequests->find($params->{illrequest_id});
177 my $backend_result = $request->mark_completed($params);
178 $template->param(
179 whole => $backend_result,
180 request => $request,
183 # handle special commit rules & update type
184 handle_commit_maybe($backend_result, $request);
186 } elsif ( $op eq 'generic_confirm' ) {
187 try {
188 my $request = Koha::Illrequests->find($params->{illrequest_id});
189 $params->{current_branchcode} = C4::Context->mybranch;
190 my $backend_result = $request->generic_confirm($params);
191 $template->param(
192 whole => $backend_result,
193 request => $request,
195 $template->param( error => $params->{error} )
196 if $params->{error};
198 # handle special commit rules & update type
199 handle_commit_maybe($backend_result, $request);
201 catch {
202 my $error;
203 if ( $_->isa( 'Koha::Exceptions::Ill::NoTargetEmail' ) ) {
204 $error = 'no_target_email';
206 elsif ( $_->isa( 'Koha::Exceptions::Ill::NoLibraryEmail' ) ) {
207 $error = 'no_library_email';
209 else {
210 $error = 'unknown_error';
212 print $cgi->redirect(
213 "/cgi-bin/koha/ill/ill-requests.pl?" .
214 "method=generic_confirm&illrequest_id=" .
215 $params->{illrequest_id} .
216 "&error=$error" );
217 exit;
219 } elsif ( $op eq 'illlist') {
221 # If we receive a pre-filter, make it available to the template
222 my $possible_filters = ['borrowernumber'];
223 my $active_filters = [];
224 foreach my $filter(@{$possible_filters}) {
225 if ($params->{$filter}) {
226 push @{$active_filters},
227 { name => $filter, value => $params->{$filter}};
230 if (scalar @{$active_filters} > 0) {
231 $template->param(
232 prefilters => $active_filters
235 } else {
236 my $request = Koha::Illrequests->find($params->{illrequest_id});
237 my $backend_result = $request->custom_capability($op, $params);
238 $template->param(
239 whole => $backend_result,
240 request => $request,
243 # handle special commit rules & update type
244 handle_commit_maybe($backend_result, $request);
248 $template->param(
249 backends => $backends,
250 media => [ "Book", "Article", "Journal" ],
251 query_type => $op,
252 branches => Koha::Libraries->search
255 output_html_with_http_headers( $cgi, $cookie, $template->output );
257 sub handle_commit_maybe {
258 my ( $backend_result, $request ) = @_;
259 # We need to special case 'commit'
260 if ( $backend_result->{stage} eq 'commit' ) {
261 if ( $backend_result->{next} eq 'illview' ) {
262 # Redirect to a view of the newly created request
263 print $cgi->redirect(
264 '/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id='.
265 $request->id
267 } else {
268 # Redirect to a requests list view
269 redirect_to_list();
274 sub redirect_to_list {
275 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
276 exit;