Bug 26922: Regression tests
[koha.git] / opac / opac-illrequests.pl
blob5881dedcc5cfa19a22ae5e91c6c2eb12866e1da5
1 #!/usr/bin/perl
3 # Copyright 2017 PTFS-Europe Ltd
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use JSON qw( encode_json );
24 use CGI qw ( -utf8 );
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Output;
29 use Koha::Illrequest::Config;
30 use Koha::Illrequests;
31 use Koha::Libraries;
32 use Koha::Patrons;
33 use Koha::Illrequest::Availability;
35 my $query = CGI->new;
37 # Grab all passed data
38 # 'our' since Plack changes the scoping
39 # of 'my'
40 our $params = $query->Vars();
42 # if illrequests is disabled, leave immediately
43 if ( ! C4::Context->preference('ILLModule') ) {
44 print $query->redirect("/cgi-bin/koha/errors/404.pl");
45 exit;
48 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
49 template_name => "opac-illrequests.tt",
50 query => $query,
51 type => "opac",
52 });
54 # Are we able to actually work?
55 my $reduced = C4::Context->preference('ILLOpacbackends');
56 my $backends = Koha::Illrequest::Config->new->available_backends($reduced);
57 my $backends_available = ( scalar @{$backends} > 0 );
58 $template->param( backends_available => $backends_available );
60 my $op = $params->{'method'} || 'list';
62 if ( $op eq 'list' ) {
64 my $requests = Koha::Illrequests->search(
65 { borrowernumber => $loggedinuser }
67 my $req = Koha::Illrequest->new;
68 $template->param(
69 requests => $requests,
70 backends => $backends
73 } elsif ( $op eq 'view') {
74 my $request = Koha::Illrequests->find({
75 borrowernumber => $loggedinuser,
76 illrequest_id => $params->{illrequest_id}
77 });
78 $template->param(
79 request => $request
82 } elsif ( $op eq 'update') {
83 my $request = Koha::Illrequests->find({
84 borrowernumber => $loggedinuser,
85 illrequest_id => $params->{illrequest_id}
86 });
87 $request->notesopac($params->{notesopac})->store;
88 # Send a notice to staff alerting them of the update
89 $request->send_staff_notice('ILL_REQUEST_MODIFIED');
90 print $query->redirect(
91 '/cgi-bin/koha/opac-illrequests.pl?method=view&illrequest_id=' .
92 $params->{illrequest_id} .
93 '&message=1'
95 exit;
96 } elsif ( $op eq 'cancreq') {
97 my $request = Koha::Illrequests->find({
98 borrowernumber => $loggedinuser,
99 illrequest_id => $params->{illrequest_id}
101 $request->status('CANCREQ')->store;
102 print $query->redirect(
103 '/cgi-bin/koha/opac-illrequests.pl?method=view&illrequest_id=' .
104 $params->{illrequest_id} .
105 '&message=1'
107 exit;
108 } elsif ( $op eq 'create' ) {
109 if (!$params->{backend}) {
110 my $req = Koha::Illrequest->new;
111 $template->param(
112 backends => $req->available_backends
114 } else {
115 my $request = Koha::Illrequest->new
116 ->load_backend($params->{backend});
118 # Does this backend enable us to insert an availability stage and should
119 # we? If not, proceed as normal.
120 if (
121 C4::Context->preference("ILLCheckAvailability") &&
122 $request->_backend_capability(
123 'should_display_availability',
124 $params
125 ) &&
126 # If the user has elected to continue with the request despite
127 # having viewed availability info, this flag will be set
128 !$params->{checked_availability}
130 # Establish which of the installed availability providers
131 # can service our metadata, if so, jump in
132 my $availability = Koha::Illrequest::Availability->new($params);
133 my $services = $availability->get_services({
134 ui_context => 'opac'
136 if (scalar @{$services} > 0) {
137 # Modify our method so we use the correct part of the
138 # template
139 $op = 'availability';
140 # Prepare the metadata we're sending them
141 my $metadata = $availability->prep_metadata($params);
142 $template->param(
143 metadata => $metadata,
144 services_json => encode_json($services),
145 services => $services,
146 illrequestsview => 1,
147 message => $params->{message},
148 method => $op,
149 whole => $params
151 output_html_with_http_headers $query, $cookie,
152 $template->output, undef, { force_no_caching => 1 };
153 exit;
157 $params->{cardnumber} = Koha::Patrons->find({
158 borrowernumber => $loggedinuser
159 })->cardnumber;
160 $params->{opac} = 1;
161 my $backend_result = $request->backend_create($params);
162 if ($backend_result->{stage} eq 'copyrightclearance') {
163 $template->param(
164 stage => $backend_result->{stage},
165 whole => $backend_result
167 } else {
168 $template->param(
169 types => [ "Book", "Article", "Journal" ],
170 branches => Koha::Libraries->search->unblessed,
171 whole => $backend_result,
172 request => $request
174 if ($backend_result->{stage} eq 'commit') {
175 print $query->redirect('/cgi-bin/koha/opac-illrequests.pl?message=2');
176 exit;
183 $template->param(
184 message => $params->{message},
185 illrequestsview => 1,
186 method => $op
189 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };