Bug 27352: Missed case in bug 25032
[koha.git] / Koha / REST / V1 / Biblios.pm
1 package Koha::REST::V1::Biblios;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Biblios;
23 use Koha::RecordProcessor;
24 use C4::Biblio qw(DelBiblio);
25
26 use MARC::Record::MiJ;
27
28 use Try::Tiny;
29
30 =head1 API
31
32 =head2 Methods
33
34 =head3 get
35
36 Controller function that handles retrieving a single biblio object
37
38 =cut
39
40 sub get {
41     my $c = shift->openapi->valid_input or return;
42
43     my $attributes;
44     $attributes = { prefetch => [ 'metadata' ] } # don't prefetch metadata if not needed
45         unless $c->req->headers->accept =~ m/application\/json/;
46
47     my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, $attributes );
48
49     unless ( $biblio ) {
50         return $c->render(
51             status  => 404,
52             openapi => {
53                 error => "Object not found."
54             }
55         );
56     }
57
58     return try {
59
60         if ( $c->req->headers->accept =~ m/application\/json/ ) {
61             return $c->render(
62                 status => 200,
63                 json   => $biblio->to_api
64             );
65         }
66         else {
67             my $record = $biblio->metadata->record;
68
69             $c->respond_to(
70                 marcxml => {
71                     status => 200,
72                     format => 'marcxml',
73                     text   => $record->as_xml_record
74                 },
75                 mij => {
76                     status => 200,
77                     format => 'mij',
78                     text   => $record->to_mij
79                 },
80                 marc => {
81                     status => 200,
82                     format => 'marc',
83                     text   => $record->as_usmarc
84                 },
85                 txt => {
86                     status => 200,
87                     format => 'text/plain',
88                     text   => $record->as_formatted
89                 },
90                 any => {
91                     status  => 406,
92                     openapi => [
93                         "application/json",
94                         "application/marcxml+xml",
95                         "application/marc-in-json",
96                         "application/marc",
97                         "text/plain"
98                     ]
99                 }
100             );
101         }
102     }
103     catch {
104         $c->unhandled_exception($_);
105     };
106 }
107
108 =head3 delete
109
110 Controller function that handles deleting a biblio object
111
112 =cut
113
114 sub delete {
115     my $c = shift->openapi->valid_input or return;
116
117     my $biblio = Koha::Biblios->find( $c->validation->param('biblio_id') );
118
119     if ( not defined $biblio ) {
120         return $c->render(
121             status  => 404,
122             openapi => { error => "Object not found" }
123         );
124     }
125
126     return try {
127         my $error = DelBiblio( $biblio->id );
128
129         if ($error) {
130             return $c->render(
131                 status  => 409,
132                 openapi => { error => $error }
133             );
134         }
135         else {
136             return $c->render( status => 204, openapi => "" );
137         }
138     }
139     catch {
140         $c->unhandled_exception($_);
141     };
142 }
143
144 =head3 get_public
145
146 Controller function that handles retrieving a single biblio object
147
148 =cut
149
150 sub get_public {
151     my $c = shift->openapi->valid_input or return;
152
153     my $biblio = Koha::Biblios->find(
154         { biblionumber => $c->validation->param('biblio_id') },
155         { prefetch     => ['metadata'] } );
156
157     unless ($biblio) {
158         return $c->render(
159             status  => 404,
160             openapi => {
161                 error => "Object not found."
162             }
163         );
164     }
165
166     return try {
167
168         my $record = $biblio->metadata->record;
169
170         my $opachiddenitems_rules = C4::Context->yaml_preference('OpacHiddenItems');
171         my $patron = $c->stash('koha.user');
172
173         # Check if the biblio should be hidden for unprivileged access
174         # unless there's a logged in user, and there's an exception for it's
175         # category
176         unless ( $patron and $patron->category->override_hidden_items ) {
177             if ( $biblio->hidden_in_opac({ rules => $opachiddenitems_rules }) )
178             {
179                 return $c->render(
180                     status  => 404,
181                     openapi => {
182                         error => "Object not found."
183                     }
184                 );
185             }
186         }
187
188         my $marcflavour = C4::Context->preference("marcflavour");
189
190         my $record_processor = Koha::RecordProcessor->new({
191             filters => 'ViewPolicy',
192             options => {
193                 interface => 'opac',
194                 frameworkcode => $biblio->frameworkcode
195             }
196         });
197         # Apply framework's filtering to MARC::Record object
198         $record_processor->process($record);
199
200         $c->respond_to(
201             marcxml => {
202                 status => 200,
203                 format => 'marcxml',
204                 text   => $record->as_xml_record
205             },
206             mij => {
207                 status => 200,
208                 format => 'mij',
209                 text   => $record->to_mij
210             },
211             marc => {
212                 status => 200,
213                 format => 'marc',
214                 text   => $record->as_usmarc
215             },
216             txt => {
217                 status => 200,
218                 format => 'text/plain',
219                 text   => $record->as_formatted
220             },
221             any => {
222                 status  => 406,
223                 openapi => [
224                     "application/marcxml+xml",
225                     "application/marc-in-json",
226                     "application/marc",
227                     "text/plain"
228                 ]
229             }
230         );
231     }
232     catch {
233         $c->unhandled_exception($_);
234     };
235 }
236
237 =head3 get_items
238
239 Controller function that handles retrieving biblio's items
240
241 =cut
242
243 sub get_items {
244     my $c = shift->openapi->valid_input or return;
245
246     my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, { prefetch => ['items'] } );
247
248     unless ( $biblio ) {
249         return $c->render(
250             status  => 404,
251             openapi => {
252                 error => "Object not found."
253             }
254         );
255     }
256
257     return try {
258
259         my $items_rs = $biblio->items;
260         my $items    = $c->objects->search( $items_rs );
261         return $c->render(
262             status  => 200,
263             openapi => $items
264         );
265     }
266     catch {
267         $c->unhandled_exception($_);
268     };
269 }
270
271 1;