1 package Koha::REST::V1::Biblios;
3 # This file is part of Koha.
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.
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.
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>.
20 use Mojo::Base 'Mojolicious::Controller';
23 use Koha::RecordProcessor;
24 use C4::Biblio qw( DelBiblio );
26 use List::MoreUtils qw( any );
27 use MARC::Record::MiJ;
29 use Try::Tiny qw( catch try );
37 Controller function that handles retrieving a single biblio object
42 my $c = shift->openapi->valid_input or return;
45 $attributes = { prefetch => [ 'metadata' ] } # don't prefetch metadata if not needed
46 unless $c->req->headers->accept =~ m/application\/json/;
48 my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, $attributes );
54 error => "Object not found."
61 if ( $c->req->headers->accept =~ m/application\/json/ ) {
64 json => $biblio->to_api
68 my $record = $biblio->metadata->record;
74 text => $record->as_xml_record
79 data => $record->to_mij
84 text => $record->as_usmarc
88 format => 'text/plain',
89 text => $record->as_formatted
95 "application/marcxml+xml",
96 "application/marc-in-json",
105 $c->unhandled_exception($_);
111 Controller function that handles deleting a biblio object
116 my $c = shift->openapi->valid_input or return;
118 my $biblio = Koha::Biblios->find( $c->validation->param('biblio_id') );
120 if ( not defined $biblio ) {
123 openapi => { error => "Object not found" }
128 my $error = DelBiblio( $biblio->id );
133 openapi => { error => $error }
137 return $c->render( status => 204, openapi => "" );
141 $c->unhandled_exception($_);
147 Controller function that handles retrieving a single biblio object
152 my $c = shift->openapi->valid_input or return;
154 my $biblio = Koha::Biblios->find(
155 { biblionumber => $c->validation->param('biblio_id') },
156 { prefetch => ['metadata'] } );
162 error => "Object not found."
169 my $record = $biblio->metadata->record;
171 my $opachiddenitems_rules = C4::Context->yaml_preference('OpacHiddenItems');
172 my $patron = $c->stash('koha.user');
174 # Check if the biblio should be hidden for unprivileged access
175 # unless there's a logged in user, and there's an exception for it's
177 unless ( $patron and $patron->category->override_hidden_items ) {
178 if ( $biblio->hidden_in_opac({ rules => $opachiddenitems_rules }) )
183 error => "Object not found."
189 my $marcflavour = C4::Context->preference("marcflavour");
191 my $record_processor = Koha::RecordProcessor->new({
192 filters => 'ViewPolicy',
195 frameworkcode => $biblio->frameworkcode
198 # Apply framework's filtering to MARC::Record object
199 $record_processor->process($record);
205 text => $record->as_xml_record
210 data => $record->to_mij
215 text => $record->as_usmarc
219 format => 'text/plain',
220 text => $record->as_formatted
225 "application/marcxml+xml",
226 "application/marc-in-json",
234 $c->unhandled_exception($_);
240 Controller function that handles retrieving biblio's items
245 my $c = shift->openapi->valid_input or return;
247 my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, { prefetch => ['items'] } );
253 error => "Object not found."
260 my $items_rs = $biblio->items;
261 my $items = $c->objects->search( $items_rs );
268 $c->unhandled_exception($_);
274 List Koha::Checkout objects
279 my $c = shift->openapi->valid_input or return;
281 my $checked_in = delete $c->validation->output->{checked_in};
284 my $biblio = Koha::Biblios->find( $c->validation->param('biblio_id') );
289 openapi => { error => 'Object not found' }
295 ? $c->objects->search( scalar $biblio->old_checkouts )
296 : $c->objects->search( scalar $biblio->current_checkouts );
300 openapi => $checkouts
304 $c->unhandled_exception($_);
308 =head3 pickup_locations
310 Method that returns the possible pickup_locations for a given biblio
311 used for building the dropdown selector
315 sub pickup_locations {
316 my $c = shift->openapi->valid_input or return;
318 my $biblio_id = $c->validation->param('biblio_id');
319 my $biblio = Koha::Biblios->find( $biblio_id );
324 openapi => { error => "Biblio not found" }
328 my $patron_id = delete $c->validation->output->{patron_id};
329 my $patron = Koha::Patrons->find( $patron_id );
334 openapi => { error => "Patron not found" }
340 my $ps_set = $biblio->pickup_locations( { patron => $patron } );
342 my $pickup_locations = $c->objects->search( $ps_set );
345 if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
347 my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
348 my $libraries = $c->objects->search($libraries_rs);
352 $library->{needs_override} = (
353 any { $_->{library_id} eq $library->{library_id} }
363 openapi => \@response
367 @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
371 openapi => \@response
375 $c->unhandled_exception($_);
379 =head3 get_items_public
381 Controller function that handles retrieving biblio's items, for unprivileged
386 sub get_items_public {
387 my $c = shift->openapi->valid_input or return;
389 my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, { prefetch => ['items'] } );
395 error => "Object not found."
402 my $patron = $c->stash('koha.user');
404 my $items_rs = $biblio->items->filter_by_visible_in_opac({ patron => $patron });
405 my $items = $c->objects->search( $items_rs );
412 $c->unhandled_exception($_);