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';
24 use Koha::RecordProcessor;
25 use C4::Biblio qw( DelBiblio AddBiblio );
26 use C4::Search qw( FindDuplicate );
28 use List::MoreUtils qw( any );
29 use MARC::Record::MiJ;
31 use Try::Tiny qw( catch try );
39 Controller function that handles retrieving a single biblio object
44 my $c = shift->openapi->valid_input or return;
47 $attributes = { prefetch => [ 'metadata' ] } # don't prefetch metadata if not needed
48 unless $c->req->headers->accept =~ m/application\/json/;
50 my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, $attributes );
56 error => "Object not found."
63 if ( $c->req->headers->accept =~ m/application\/json/ ) {
66 json => $biblio->to_api
70 my $record = $biblio->metadata->record;
76 text => $record->as_xml_record
81 data => $record->to_mij
86 text => $record->as_usmarc
90 format => 'text/plain',
91 text => $record->as_formatted
97 "application/marcxml+xml",
98 "application/marc-in-json",
107 $c->unhandled_exception($_);
113 Controller function that handles deleting a biblio object
118 my $c = shift->openapi->valid_input or return;
120 my $biblio = Koha::Biblios->find( $c->validation->param('biblio_id') );
122 if ( not defined $biblio ) {
125 openapi => { error => "Object not found" }
130 my $error = DelBiblio( $biblio->id );
135 openapi => { error => $error }
139 return $c->render( status => 204, openapi => "" );
143 $c->unhandled_exception($_);
149 Controller function that handles retrieving a single biblio object
154 my $c = shift->openapi->valid_input or return;
156 my $biblio = Koha::Biblios->find(
157 { biblionumber => $c->validation->param('biblio_id') },
158 { prefetch => ['metadata'] } );
164 error => "Object not found."
171 my $record = $biblio->metadata->record;
173 my $opachiddenitems_rules = C4::Context->yaml_preference('OpacHiddenItems');
174 my $patron = $c->stash('koha.user');
176 # Check if the biblio should be hidden for unprivileged access
177 # unless there's a logged in user, and there's an exception for it's
179 unless ( $patron and $patron->category->override_hidden_items ) {
180 if ( $biblio->hidden_in_opac({ rules => $opachiddenitems_rules }) )
185 error => "Object not found."
191 my $marcflavour = C4::Context->preference("marcflavour");
193 my $record_processor = Koha::RecordProcessor->new({
194 filters => 'ViewPolicy',
197 frameworkcode => $biblio->frameworkcode
200 # Apply framework's filtering to MARC::Record object
201 $record_processor->process($record);
207 text => $record->as_xml_record
212 data => $record->to_mij
217 text => $record->as_usmarc
221 format => 'text/plain',
222 text => $record->as_formatted
227 "application/marcxml+xml",
228 "application/marc-in-json",
236 $c->unhandled_exception($_);
242 Controller function that handles retrieving biblio's items
247 my $c = shift->openapi->valid_input or return;
249 my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, { prefetch => ['items'] } );
255 error => "Object not found."
262 my $items_rs = $biblio->items;
263 my $items = $c->objects->search( $items_rs );
270 $c->unhandled_exception($_);
276 List Koha::Checkout objects
281 my $c = shift->openapi->valid_input or return;
283 my $checked_in = delete $c->validation->output->{checked_in};
286 my $biblio = Koha::Biblios->find( $c->validation->param('biblio_id') );
291 openapi => { error => 'Object not found' }
297 ? $c->objects->search( $biblio->old_checkouts )
298 : $c->objects->search( $biblio->current_checkouts );
302 openapi => $checkouts
306 $c->unhandled_exception($_);
310 =head3 pickup_locations
312 Method that returns the possible pickup_locations for a given biblio
313 used for building the dropdown selector
317 sub pickup_locations {
318 my $c = shift->openapi->valid_input or return;
320 my $biblio_id = $c->validation->param('biblio_id');
321 my $biblio = Koha::Biblios->find( $biblio_id );
326 openapi => { error => "Biblio not found" }
330 my $patron_id = delete $c->validation->output->{patron_id};
331 my $patron = Koha::Patrons->find( $patron_id );
336 openapi => { error => "Patron not found" }
342 my $pl_set = $biblio->pickup_locations( { patron => $patron } );
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 { $_->branchcode eq $library->{library_id} }
363 my $pickup_locations = $c->objects->search($pl_set);
364 @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
369 openapi => \@response
373 $c->unhandled_exception($_);
377 =head3 get_items_public
379 Controller function that handles retrieving biblio's items, for unprivileged
384 sub get_items_public {
385 my $c = shift->openapi->valid_input or return;
387 my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, { prefetch => ['items'] } );
393 error => "Object not found."
400 my $patron = $c->stash('koha.user');
402 my $items_rs = $biblio->items->filter_by_visible_in_opac({ patron => $patron });
403 my $items = $c->objects->search( $items_rs );
410 $c->unhandled_exception($_);
416 Set rating for the logged in user
422 my $c = shift->openapi->valid_input or return;
424 my $biblio = Koha::Biblios->find( $c->validation->param('biblio_id') );
430 error => "Object not found."
435 my $patron = $c->stash('koha.user');
440 { error => "Cannot rate. Reason: must be logged-in" }
444 my $body = $c->validation->param('body');
445 my $rating_value = $body->{rating};
449 my $rating = Koha::Ratings->find(
451 biblionumber => $biblio->biblionumber,
452 borrowernumber => $patron->borrowernumber,
455 $rating->delete if $rating;
457 if ( $rating_value ) { # Cannot set to 0 from the UI
458 $rating = Koha::Rating->new(
460 biblionumber => $biblio->biblionumber,
461 borrowernumber => $patron->borrowernumber,
462 rating_value => $rating_value,
467 Koha::Ratings->search( { biblionumber => $biblio->biblionumber } );
468 my $average = $ratings->get_avg_rating;
473 rating => $rating && $rating->in_storage ? $rating->rating_value : undef,
475 count => $ratings->count
480 $c->unhandled_exception($_);
486 Controller function that handles creating a biblio object
491 my $c = shift->openapi->valid_input or return;
494 my $body = $c->validation->param('Body');
496 my $flavour = $c->validation->param('x-marc-schema');
497 $flavour = C4::Context->preference('marcflavour') unless $flavour;
501 my $frameworkcode = $c->validation->param('x-framework-id');
502 if ( $c->req->headers->content_type =~ m/application\/marcxml\+xml/ ) {
503 $record = MARC::Record->new_from_xml( $body, 'UTF-8', $flavour );
504 } elsif ( $c->req->headers->content_type =~ m/application\/marc-in-json/ ) {
505 $record = MARC::Record->new_from_mij_structure( $body );
506 } elsif ( $c->req->headers->content_type =~ m/application\/marc/ ) {
507 $record = MARC::Record->new_from_usmarc( $body );
512 "application/marcxml+xml",
513 "application/marc-in-json",
519 my ( $duplicatebiblionumber, $duplicatetitle );
520 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
522 my $confirm_not_duplicate = $c->validation->param('x-confirm-not-duplicate');
527 error => "Duplicate biblio $duplicatebiblionumber"
529 ) unless !$duplicatebiblionumber || $confirm_not_duplicate;
531 my ( $biblionumber, $oldbibitemnum );
532 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
536 openapi => { id => $biblionumber }
540 $c->unhandled_exception($_);