+=head2 get_coins
+
+my $coins = $biblio->get_coins;
+
+Returns the COinS (a span) which can be included in a biblio record
+
+=cut
+
+sub get_coins {
+ my ( $self ) = @_;
+
+ my $record = $self->metadata->record;
+
+ my $pos7 = substr $record->leader(), 7, 1;
+ my $pos6 = substr $record->leader(), 6, 1;
+ my $mtx;
+ my $genre;
+ my ( $aulast, $aufirst ) = ( '', '' );
+ my @authors;
+ my $title;
+ my $hosttitle;
+ my $pubyear = '';
+ my $isbn = '';
+ my $issn = '';
+ my $publisher = '';
+ my $pages = '';
+ my $titletype = '';
+
+ # For the purposes of generating COinS metadata, LDR/06-07 can be
+ # considered the same for UNIMARC and MARC21
+ my $fmts6 = {
+ 'a' => 'book',
+ 'b' => 'manuscript',
+ 'c' => 'book',
+ 'd' => 'manuscript',
+ 'e' => 'map',
+ 'f' => 'map',
+ 'g' => 'film',
+ 'i' => 'audioRecording',
+ 'j' => 'audioRecording',
+ 'k' => 'artwork',
+ 'l' => 'document',
+ 'm' => 'computerProgram',
+ 'o' => 'document',
+ 'r' => 'document',
+ };
+ my $fmts7 = {
+ 'a' => 'journalArticle',
+ 's' => 'journal',
+ };
+
+ $genre = $fmts6->{$pos6} ? $fmts6->{$pos6} : 'book';
+
+ if ( $genre eq 'book' ) {
+ $genre = $fmts7->{$pos7} if $fmts7->{$pos7};
+ }
+
+ ##### We must transform mtx to a valable mtx and document type ####
+ if ( $genre eq 'book' ) {
+ $mtx = 'book';
+ $titletype = 'b';
+ } elsif ( $genre eq 'journal' ) {
+ $mtx = 'journal';
+ $titletype = 'j';
+ } elsif ( $genre eq 'journalArticle' ) {
+ $mtx = 'journal';
+ $genre = 'article';
+ $titletype = 'a';
+ } else {
+ $mtx = 'dc';
+ }
+
+ if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
+
+ # Setting datas
+ $aulast = $record->subfield( '700', 'a' ) || '';
+ $aufirst = $record->subfield( '700', 'b' ) || '';
+ push @authors, "$aufirst $aulast" if ($aufirst or $aulast);
+
+ # others authors
+ if ( $record->field('200') ) {
+ for my $au ( $record->field('200')->subfield('g') ) {
+ push @authors, $au;
+ }
+ }
+
+ $title = $record->subfield( '200', 'a' );
+ my $subfield_210d = $record->subfield('210', 'd');
+ if ($subfield_210d and $subfield_210d =~ /(\d{4})/) {
+ $pubyear = $1;
+ }
+ $publisher = $record->subfield( '210', 'c' ) || '';
+ $isbn = $record->subfield( '010', 'a' ) || '';
+ $issn = $record->subfield( '011', 'a' ) || '';
+ } else {
+
+ # MARC21 need some improve
+
+ # Setting datas
+ if ( $record->field('100') ) {
+ push @authors, $record->subfield( '100', 'a' );
+ }
+
+ # others authors
+ if ( $record->field('700') ) {
+ for my $au ( $record->field('700')->subfield('a') ) {
+ push @authors, $au;
+ }
+ }
+ $title = $record->field('245');
+ $title &&= $title->as_string('ab');
+ if ($titletype eq 'a') {
+ $pubyear = $record->field('008') || '';
+ $pubyear = substr($pubyear->data(), 7, 4) if $pubyear;
+ $isbn = $record->subfield( '773', 'z' ) || '';
+ $issn = $record->subfield( '773', 'x' ) || '';
+ $hosttitle = $record->subfield( '773', 't' ) || $record->subfield( '773', 'a') || q{};
+ my @rels = $record->subfield( '773', 'g' );
+ $pages = join(', ', @rels);
+ } else {
+ $pubyear = $record->subfield( '260', 'c' ) || '';
+ $publisher = $record->subfield( '260', 'b' ) || '';
+ $isbn = $record->subfield( '020', 'a' ) || '';
+ $issn = $record->subfield( '022', 'a' ) || '';
+ }
+
+ }
+
+ my @params = (
+ [ 'ctx_ver', 'Z39.88-2004' ],
+ [ 'rft_val_fmt', "info:ofi/fmt:kev:mtx:$mtx" ],
+ [ ($mtx eq 'dc' ? 'rft.type' : 'rft.genre'), $genre ],
+ [ "rft.${titletype}title", $title ],
+ );
+
+ # rft.title is authorized only once, so by checking $titletype
+ # we ensure that rft.title is not already in the list.
+ if ($hosttitle and $titletype) {
+ push @params, [ 'rft.title', $hosttitle ];
+ }
+
+ push @params, (
+ [ 'rft.isbn', $isbn ],
+ [ 'rft.issn', $issn ],
+ );
+
+ # If it's a subscription, these informations have no meaning.
+ if ($genre ne 'journal') {
+ push @params, (
+ [ 'rft.aulast', $aulast ],
+ [ 'rft.aufirst', $aufirst ],
+ (map { [ 'rft.au', $_ ] } @authors),
+ [ 'rft.pub', $publisher ],
+ [ 'rft.date', $pubyear ],
+ [ 'rft.pages', $pages ],
+ );
+ }
+
+ my $coins_value = join( '&',
+ map { $$_[1] ? $$_[0] . '=' . uri_escape_utf8( $$_[1] ) : () } @params );
+
+ return $coins_value;
+}
+
+=head2 get_openurl
+
+my $url = $biblio->get_openurl;
+
+Returns url for OpenURL resolver set in OpenURLResolverURL system preference
+
+=cut
+
+sub get_openurl {
+ my ( $self ) = @_;
+
+ my $OpenURLResolverURL = C4::Context->preference('OpenURLResolverURL');
+
+ if ($OpenURLResolverURL) {
+ my $uri = URI->new($OpenURLResolverURL);
+
+ if (not defined $uri->query) {
+ $OpenURLResolverURL .= '?';
+ } else {
+ $OpenURLResolverURL .= '&';
+ }
+ $OpenURLResolverURL .= $self->get_coins;
+ }
+
+ return $OpenURLResolverURL;
+}
+
+=head3 is_serial
+
+my $serial = $biblio->is_serial
+
+Return boolean true if this bibbliographic record is continuing resource
+
+=cut
+
+sub is_serial {
+ my ( $self ) = @_;
+
+ return 1 if $self->serial;
+
+ my $record = $self->metadata->record;
+ return 1 if substr($record->leader, 7, 1) eq 's';
+
+ return 0;
+}
+
+=head3 custom_cover_image_url
+
+my $image_url = $biblio->custom_cover_image_url
+
+Return the specific url of the cover image for this bibliographic record.
+It is built regaring the value of the system preference CustomCoverImagesURL
+
+=cut
+
+sub custom_cover_image_url {
+ my ( $self ) = @_;
+ my $url = C4::Context->preference('CustomCoverImagesURL');
+ if ( $url =~ m|{isbn}| ) {
+ my $isbn = $self->biblioitem->isbn;
+ return unless $isbn;
+ $url =~ s|{isbn}|$isbn|g;
+ }
+ if ( $url =~ m|{normalized_isbn}| ) {
+ my $normalized_isbn = C4::Koha::GetNormalizedISBN($self->biblioitem->isbn);
+ return unless $normalized_isbn;
+ $url =~ s|{normalized_isbn}|$normalized_isbn|g;
+ }
+ if ( $url =~ m|{issn}| ) {
+ my $issn = $self->biblioitem->issn;
+ return unless $issn;
+ $url =~ s|{issn}|$issn|g;
+ }
+
+ my $re = qr|{(?<field>\d{3})\$(?<subfield>.)}|;
+ if ( $url =~ $re ) {
+ my $field = $+{field};
+ my $subfield = $+{subfield};
+ my $marc_record = $self->metadata->record;
+ my $value = $marc_record->subfield($field, $subfield);
+ return unless $value;
+ $url =~ s|$re|$value|;
+ }
+
+ return $url;
+}
+
+=head3 cover_images
+
+Return the cover images associated with this biblio.
+
+=cut
+
+sub cover_images {
+ my ( $self ) = @_;
+
+ my $cover_images_rs = $self->_result->cover_images;
+ return unless $cover_images_rs;
+ return Koha::CoverImages->_new_from_dbic($cover_images_rs);
+}
+
+=head3 get_marc_notes
+
+ $marcnotesarray = $biblio->get_marc_notes({ marcflavour => $marcflavour });
+
+Get all notes from the MARC record and returns them in an array.
+The notes are stored in different fields depending on MARC flavour.
+MARC21 5XX $u subfields receive special attention as they are URIs.
+
+=cut
+
+sub get_marc_notes {
+ my ( $self, $params ) = @_;
+
+ my $marcflavour = $params->{marcflavour};
+ my $opac = $params->{opac};
+
+ my $scope = $marcflavour eq "UNIMARC"? '3..': '5..';
+ my @marcnotes;
+
+ #MARC21 specs indicate some notes should be private if first indicator 0
+ my %maybe_private = (
+ 541 => 1,
+ 542 => 1,
+ 561 => 1,
+ 583 => 1,
+ 590 => 1
+ );
+
+ my %hiddenlist = map { $_ => 1 }
+ split( /,/, C4::Context->preference('NotesToHide'));
+ foreach my $field ( $self->metadata->record->field($scope) ) {
+ my $tag = $field->tag();
+ next if $hiddenlist{ $tag };
+ next if $opac && $maybe_private{$tag} && !$field->indicator(1);
+ if( $marcflavour ne 'UNIMARC' && $field->subfield('u') ) {
+ # Field 5XX$u always contains URI
+ # Examples: 505u, 506u, 510u, 514u, 520u, 530u, 538u, 540u, 542u, 552u, 555u, 561u, 563u, 583u
+ # We first push the other subfields, then all $u's separately
+ # Leave further actions to the template (see e.g. opac-detail)
+ my $othersub =
+ join '', ( 'a' .. 't', 'v' .. 'z', '0' .. '9' ); # excl 'u'
+ push @marcnotes, { marcnote => $field->as_string($othersub) };
+ foreach my $sub ( $field->subfield('u') ) {
+ $sub =~ s/^\s+|\s+$//g; # trim
+ push @marcnotes, { marcnote => $sub };
+ }
+ } else {
+ push @marcnotes, { marcnote => $field->as_string() };
+ }
+ }
+ return \@marcnotes;
+}
+
+=head3 to_api
+
+ my $json = $biblio->to_api;
+
+Overloaded method that returns a JSON representation of the Koha::Biblio object,
+suitable for API output. The related Koha::Biblioitem object is merged as expected
+on the API.
+
+=cut
+
+sub to_api {
+ my ($self, $args) = @_;
+
+ my $response = $self->SUPER::to_api( $args );
+ my $biblioitem = $self->biblioitem->to_api;
+
+ return { %$response, %$biblioitem };
+}
+
+=head3 to_api_mapping
+
+This method returns the mapping for representing a Koha::Biblio object
+on the API.
+
+=cut
+
+sub to_api_mapping {
+ return {
+ biblionumber => 'biblio_id',
+ frameworkcode => 'framework_id',
+ unititle => 'uniform_title',
+ seriestitle => 'series_title',
+ copyrightdate => 'copyright_date',
+ datecreated => 'creation_date'
+ };
+}
+
+=head3 get_marc_host
+
+ $host = $biblio->get_marc_host;
+ # OR:
+ ( $host, $relatedparts ) = $biblio->get_marc_host;
+
+ Returns host biblio record from MARC21 773 (undef if no 773 present).
+ It looks at the first 773 field with MARCorgCode or only a control
+ number. Complete $w or numeric part is used to search host record.
+ The optional parameter no_items triggers a check if $biblio has items.
+ If there are, the sub returns undef.
+ Called in list context, it also returns 773$g (related parts).
+
+=cut
+
+sub get_marc_host {
+ my ($self, $params) = @_;
+ my $no_items = $params->{no_items};
+ return if C4::Context->preference('marcflavour') eq 'UNIMARC'; # TODO
+ return if $params->{no_items} && $self->items->count > 0;
+
+ my $record;
+ eval { $record = $self->metadata->record };
+ return if !$record;
+
+ # We pick the first $w with your MARCOrgCode or the first $w that has no
+ # code (between parentheses) at all.
+ my $orgcode = C4::Context->preference('MARCOrgCode') // q{};
+ my $hostfld;
+ foreach my $f ( $record->field('773') ) {
+ my $w = $f->subfield('w') or next;
+ if( $w =~ /^\($orgcode\)\s*(\d+)/i or $w =~ /^\d+/ ) {
+ $hostfld = $f;
+ last;
+ }
+ }
+ return if !$hostfld;
+ my $rcn = $hostfld->subfield('w');
+
+ # Look for control number with/without orgcode
+ my $engine = Koha::SearchEngine::Search->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
+ my $bibno;
+ for my $try (1..2) {
+ my ( $error, $results, $total_hits ) = $engine->simple_search_compat( 'Control-number='.$rcn, 0,1 );
+ if( !$error and $total_hits == 1 ) {
+ $bibno = $engine->extract_biblionumber( $results->[0] );
+ last;
+ }
+ # Add or remove orgcode for second try
+ if( $try == 1 && $rcn =~ /\)\s*(\d+)/ ) {
+ $rcn = $1; # number only
+ } elsif( $try == 1 && $rcn =~ /^\d+/ ) {
+ $rcn = "($orgcode)$rcn";
+ } else {
+ last;
+ }
+ }
+ if( $bibno ) {
+ my $host = Koha::Biblios->find($bibno) or return;
+ return wantarray ? ( $host, $hostfld->subfield('g') ) : $host;
+ }
+}
+
+=head2 Internal methods
+