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;
27 use Koha::Illrequests;
31 my $illRequests = Koha::Illrequests->new;
33 # Grab all passed data
34 # 'our' since Plack changes the scoping
36 our $params = $cgi->Vars();
38 my $op = $params->{method} || 'illlist';
40 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
41 template_name => 'ill/ill-requests.tt',
44 flagsrequired => { ill => '*' },
47 if ( $op eq 'illview' ) {
48 # View the details of an ILL
49 my $request = Koha::Illrequests->find($params->{illrequest_id});
55 } elsif ( $op eq 'create' ) {
56 # We're in the process of creating a request
57 my $request = Koha::Illrequest->new
58 ->load_backend($params->{backend});
59 my $backend_result = $request->backend_create($params);
61 whole => $backend_result,
64 handle_commit_maybe($backend_result, $request);
66 } elsif ( $op eq 'confirm' ) {
67 # Backend 'confirm' method
68 # confirm requires a specific request, so first, find it.
69 my $request = Koha::Illrequests->find($params->{illrequest_id});
70 my $backend_result = $request->backend_confirm($params);
72 whole => $backend_result,
76 # handle special commit rules & update type
77 handle_commit_maybe($backend_result, $request);
79 } elsif ( $op eq 'cancel' ) {
80 # Backend 'cancel' method
81 # cancel requires a specific request, so first, find it.
82 my $request = Koha::Illrequests->find($params->{illrequest_id});
83 my $backend_result = $request->backend_cancel($params);
85 whole => $backend_result,
89 # handle special commit rules & update type
90 handle_commit_maybe($backend_result, $request);
92 } elsif ( $op eq 'edit_action' ) {
93 # Handle edits to the Illrequest object.
94 # (not the Illrequestattributes)
95 # We simulate the API for backend requests for uniformity.
97 my $request = Koha::Illrequests->find($params->{illrequest_id});
98 if ( !$params->{stage} ) {
99 my $backend_result = {
103 method => 'edit_action',
109 whole => $backend_result,
115 $request->borrowernumber($params->{borrowernumber});
116 $request->biblio_id($params->{biblio_id});
117 $request->branchcode($params->{branchcode});
118 $request->notesopac($params->{notesopac});
119 $request->notesstaff($params->{notesstaff});
121 my $backend_result = {
125 method => 'edit_action',
130 handle_commit_maybe($backend_result, $request);
133 } elsif ( $op eq 'moderate_action' ) {
134 # Moderate action is required for an ILL submodule / syspref.
135 # Currently still needs to be implemented.
138 } elsif ( $op eq 'delete_confirm') {
139 my $request = Koha::Illrequests->find($params->{illrequest_id});
145 } elsif ( $op eq 'delete' ) {
147 # Check if the request is confirmed, if not, redirect
148 # to the confirmation view
149 if ($params->{confirmed} == 1) {
150 # We simply delete the request...
151 my $request = Koha::Illrequests->find(
152 $params->{illrequest_id}
154 # ... then return to list view.
157 print $cgi->redirect(
158 "/cgi-bin/koha/ill/ill-requests.pl?" .
159 "method=delete_confirm&illrequest_id=" .
160 $params->{illrequest_id});
163 } elsif ( $op eq 'mark_completed' ) {
164 my $request = Koha::Illrequests->find($params->{illrequest_id});
165 my $backend_result = $request->mark_completed($params);
167 whole => $backend_result,
171 # handle special commit rules & update type
172 handle_commit_maybe($backend_result, $request);
174 } elsif ( $op eq 'generic_confirm' ) {
175 my $request = Koha::Illrequests->find($params->{illrequest_id});
176 $params->{current_branchcode} = C4::Context->mybranch;
177 my $backend_result = $request->generic_confirm($params);
179 whole => $backend_result,
183 # handle special commit rules & update type
184 handle_commit_maybe($backend_result, $request);
186 } elsif ( $op eq 'illlist') {
187 # Display all current ILLs
188 my $requests = $illRequests->search();
191 requests => $requests
194 # If we receive a pre-filter, make it available to the template
195 my $possible_filters = ['borrowernumber'];
196 my $active_filters = [];
197 foreach my $filter(@{$possible_filters}) {
198 if ($params->{$filter}) {
199 push @{$active_filters},
200 { name => $filter, value => $params->{$filter}};
203 if (scalar @{$active_filters} > 0) {
205 prefilters => $active_filters
209 my $request = Koha::Illrequests->find($params->{illrequest_id});
210 my $backend_result = $request->custom_capability($op, $params);
212 whole => $backend_result,
216 # handle special commit rules & update type
217 handle_commit_maybe($backend_result, $request);
220 # Get a list of backends
221 my $ir = Koha::Illrequest->new;
224 backends => $ir->available_backends,
225 media => [ "Book", "Article", "Journal" ],
227 branches => Koha::Libraries->search->unblessed,
228 here_link => "/cgi-bin/koha/ill/ill-requests.pl"
231 output_html_with_http_headers( $cgi, $cookie, $template->output );
233 sub handle_commit_maybe {
234 my ( $backend_result, $request ) = @_;
235 # We need to special case 'commit'
236 if ( $backend_result->{stage} eq 'commit' ) {
237 if ( $backend_result->{next} eq 'illview' ) {
238 # Redirect to a view of the newly created request
239 print $cgi->redirect(
240 '/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id='.
244 # Redirect to a requests list view
250 sub redirect_to_list {
251 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');