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 # Leave immediately if ILLModule is disabled
39 unless ( C4::Context->preference('ILLModule') ) {
40 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
44 my $op = $params->{method} || 'illlist';
46 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
47 template_name => 'ill/ill-requests.tt',
50 flagsrequired => { ill => '*' },
53 if ( $op eq 'illview' ) {
54 # View the details of an ILL
55 my $request = Koha::Illrequests->find($params->{illrequest_id});
61 } elsif ( $op eq 'create' ) {
62 # We're in the process of creating a request
63 my $request = Koha::Illrequest->new
64 ->load_backend($params->{backend});
65 my $backend_result = $request->backend_create($params);
67 whole => $backend_result,
70 handle_commit_maybe($backend_result, $request);
72 } elsif ( $op eq 'confirm' ) {
73 # Backend 'confirm' method
74 # confirm requires a specific request, so first, find it.
75 my $request = Koha::Illrequests->find($params->{illrequest_id});
76 my $backend_result = $request->backend_confirm($params);
78 whole => $backend_result,
82 # handle special commit rules & update type
83 handle_commit_maybe($backend_result, $request);
85 } elsif ( $op eq 'cancel' ) {
86 # Backend 'cancel' method
87 # cancel requires a specific request, so first, find it.
88 my $request = Koha::Illrequests->find($params->{illrequest_id});
89 my $backend_result = $request->backend_cancel($params);
91 whole => $backend_result,
95 # handle special commit rules & update type
96 handle_commit_maybe($backend_result, $request);
98 } elsif ( $op eq 'edit_action' ) {
99 # Handle edits to the Illrequest object.
100 # (not the Illrequestattributes)
101 # We simulate the API for backend requests for uniformity.
103 my $request = Koha::Illrequests->find($params->{illrequest_id});
104 if ( !$params->{stage} ) {
105 my $backend_result = {
109 method => 'edit_action',
115 whole => $backend_result,
121 $request->borrowernumber($params->{borrowernumber});
122 $request->biblio_id($params->{biblio_id});
123 $request->branchcode($params->{branchcode});
124 $request->notesopac($params->{notesopac});
125 $request->notesstaff($params->{notesstaff});
127 my $backend_result = {
131 method => 'edit_action',
136 handle_commit_maybe($backend_result, $request);
139 } elsif ( $op eq 'moderate_action' ) {
140 # Moderate action is required for an ILL submodule / syspref.
141 # Currently still needs to be implemented.
144 } elsif ( $op eq 'delete_confirm') {
145 my $request = Koha::Illrequests->find($params->{illrequest_id});
151 } elsif ( $op eq 'delete' ) {
153 # Check if the request is confirmed, if not, redirect
154 # to the confirmation view
155 if ($params->{confirmed} == 1) {
156 # We simply delete the request...
157 my $request = Koha::Illrequests->find(
158 $params->{illrequest_id}
160 # ... then return to list view.
163 print $cgi->redirect(
164 "/cgi-bin/koha/ill/ill-requests.pl?" .
165 "method=delete_confirm&illrequest_id=" .
166 $params->{illrequest_id});
169 } elsif ( $op eq 'mark_completed' ) {
170 my $request = Koha::Illrequests->find($params->{illrequest_id});
171 my $backend_result = $request->mark_completed($params);
173 whole => $backend_result,
177 # handle special commit rules & update type
178 handle_commit_maybe($backend_result, $request);
180 } elsif ( $op eq 'generic_confirm' ) {
181 my $request = Koha::Illrequests->find($params->{illrequest_id});
182 $params->{current_branchcode} = C4::Context->mybranch;
183 my $backend_result = $request->generic_confirm($params);
185 whole => $backend_result,
189 # handle special commit rules & update type
190 handle_commit_maybe($backend_result, $request);
192 } elsif ( $op eq 'illlist') {
193 # Display all current ILLs
194 my $requests = $illRequests->search();
197 requests => $requests
200 # If we receive a pre-filter, make it available to the template
201 my $possible_filters = ['borrowernumber'];
202 my $active_filters = [];
203 foreach my $filter(@{$possible_filters}) {
204 if ($params->{$filter}) {
205 push @{$active_filters},
206 { name => $filter, value => $params->{$filter}};
209 if (scalar @{$active_filters} > 0) {
211 prefilters => $active_filters
215 my $request = Koha::Illrequests->find($params->{illrequest_id});
216 my $backend_result = $request->custom_capability($op, $params);
218 whole => $backend_result,
222 # handle special commit rules & update type
223 handle_commit_maybe($backend_result, $request);
226 # Get a list of backends
227 my $ir = Koha::Illrequest->new;
230 backends => $ir->available_backends,
231 media => [ "Book", "Article", "Journal" ],
233 branches => Koha::Libraries->search->unblessed,
234 here_link => "/cgi-bin/koha/ill/ill-requests.pl"
237 output_html_with_http_headers( $cgi, $cookie, $template->output );
239 sub handle_commit_maybe {
240 my ( $backend_result, $request ) = @_;
241 # We need to special case 'commit'
242 if ( $backend_result->{stage} eq 'commit' ) {
243 if ( $backend_result->{next} eq 'illview' ) {
244 # Redirect to a view of the newly created request
245 print $cgi->redirect(
246 '/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id='.
250 # Redirect to a requests list view
256 sub redirect_to_list {
257 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');