3 # Copyright ByWater Solutions 2014
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 use List::MoreUtils qw(any);
30 use Koha::DateUtils qw( dt_from_string );
32 use base qw(Koha::Object);
34 use Koha::ArticleRequest::Status;
35 use Koha::ArticleRequests;
36 use Koha::Biblio::Metadatas;
37 use Koha::Biblioitems;
38 use Koha::IssuingRules;
39 use Koha::Item::Transfer::Limits;
42 use Koha::Subscriptions;
46 Koha::Biblio - Koha Biblio Object class
56 Overloaded I<store> method to set default values
63 $self->datecreated( dt_from_string ) unless $self->datecreated;
65 return $self->SUPER::store;
70 my $metadata = $biblio->metadata();
72 Returns a Koha::Biblio::Metadata object
79 my $metadata = $self->_result->metadata;
80 return Koha::Biblio::Metadata->_new_from_dbic($metadata);
85 my @subtitles = $biblio->subtitles();
87 Returns list of subtitles for a record.
89 Keyword to MARC mapping for subtitle must be set for this method to return any possible values.
96 return map { $_->{subfield} } @{
97 C4::Biblio::GetRecordValue(
99 C4::Biblio::GetMarcBiblio({ biblionumber => $self->id }),
100 $self->frameworkcode ) };
103 =head3 can_article_request
105 my $bool = $biblio->can_article_request( $borrower );
107 Returns true if article requests can be made for this record
109 $borrower must be a Koha::Patron object
113 sub can_article_request {
114 my ( $self, $borrower ) = @_;
116 my $rule = $self->article_request_type($borrower);
117 return q{} if $rule eq 'item_only' && !$self->items()->count();
118 return 1 if $rule && $rule ne 'no';
123 =head3 can_be_transferred
125 $biblio->can_be_transferred({ to => $to_library, from => $from_library })
127 Checks if at least one item of a biblio can be transferred to given library.
129 This feature is controlled by two system preferences:
130 UseBranchTransferLimits to enable / disable the feature
131 BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
132 for setting the limitations
134 Performance-wise, it is recommended to use this method for a biblio instead of
135 iterating each item of a biblio with Koha::Item->can_be_transferred().
137 Takes HASHref that can have the following parameters:
138 MANDATORY PARAMETERS:
141 $from : Koha::Library # if given, only items from that
142 # holdingbranch are considered
144 Returns 1 if at least one of the item of a biblio can be transferred
145 to $to_library, otherwise 0.
149 sub can_be_transferred {
150 my ($self, $params) = @_;
152 my $to = $params->{to};
153 my $from = $params->{from};
155 return 1 unless C4::Context->preference('UseBranchTransferLimits');
156 my $limittype = C4::Context->preference('BranchTransferLimitsType');
159 foreach my $item_of_bib ($self->items->as_list) {
160 next unless $item_of_bib->holdingbranch;
161 next if $from && $from->branchcode ne $item_of_bib->holdingbranch;
162 return 1 if $item_of_bib->holdingbranch eq $to->branchcode;
163 my $code = $limittype eq 'itemtype'
164 ? $item_of_bib->effective_itemtype
165 : $item_of_bib->ccode;
166 return 1 unless $code;
167 $items->{$code}->{$item_of_bib->holdingbranch} = 1;
170 # At this point we will have a HASHref containing each itemtype/ccode that
171 # this biblio has, inside which are all of the holdingbranches where those
172 # items are located at. Then, we will query Koha::Item::Transfer::Limits to
173 # find out whether a transfer limits for such $limittype from any of the
174 # listed holdingbranches to the given $to library exist. If at least one
175 # holdingbranch for that $limittype does not have a transfer limit to given
176 # $to library, then we know that the transfer is possible.
177 foreach my $code (keys %{$items}) {
178 my @holdingbranches = keys %{$items->{$code}};
179 return 1 if Koha::Item::Transfer::Limits->search({
180 toBranch => $to->branchcode,
181 fromBranch => { 'in' => \@holdingbranches },
184 group_by => [qw/fromBranch/]
185 })->count == scalar(@holdingbranches) ? 0 : 1;
191 =head3 hidden_in_opac
193 my $bool = $biblio->hidden_in_opac({ [ rules => $rules ] })
195 Returns true if the biblio matches the hidding criteria defined in $rules.
196 Returns false otherwise.
198 Takes HASHref that can have the following parameters:
200 $rules : { <field> => [ value_1, ... ], ... }
202 Note: $rules inherits its structure from the parsed YAML from reading
203 the I<OpacHiddenItems> system preference.
208 my ( $self, $params ) = @_;
210 my $rules = $params->{rules} // {};
212 return !(any { !$_->hidden_in_opac({ rules => $rules }) } $self->items->as_list);
215 =head3 article_request_type
217 my $type = $biblio->article_request_type( $borrower );
219 Returns the article request type based on items, or on the record
220 itself if there are no items.
222 $borrower must be a Koha::Patron object
226 sub article_request_type {
227 my ( $self, $borrower ) = @_;
229 return q{} unless $borrower;
231 my $rule = $self->article_request_type_for_items( $borrower );
232 return $rule if $rule;
234 # If the record has no items that are requestable, go by the record itemtype
235 $rule = $self->article_request_type_for_bib($borrower);
236 return $rule if $rule;
241 =head3 article_request_type_for_bib
243 my $type = $biblio->article_request_type_for_bib
245 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record
249 sub article_request_type_for_bib {
250 my ( $self, $borrower ) = @_;
252 return q{} unless $borrower;
254 my $borrowertype = $borrower->categorycode;
255 my $itemtype = $self->itemtype();
257 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype });
259 return q{} unless $issuing_rule;
260 return $issuing_rule->article_requests || q{}
263 =head3 article_request_type_for_items
265 my $type = $biblio->article_request_type_for_items
267 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
269 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
273 sub article_request_type_for_items {
274 my ( $self, $borrower ) = @_;
277 foreach my $item ( $self->items()->as_list() ) {
278 my $rule = $item->article_request_type($borrower);
279 return $rule if $rule eq 'bib_only'; # we don't need to go any further
283 return 'item_only' if $counts->{item_only};
284 return 'yes' if $counts->{yes};
285 return 'no' if $counts->{no};
289 =head3 article_requests
291 my @requests = $biblio->article_requests
293 Returns the article requests associated with this Biblio
297 sub article_requests {
298 my ( $self, $borrower ) = @_;
300 $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } );
302 return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests};
305 =head3 article_requests_current
307 my @requests = $biblio->article_requests_current
309 Returns the article requests associated with this Biblio that are incomplete
313 sub article_requests_current {
314 my ( $self, $borrower ) = @_;
316 $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
318 biblionumber => $self->biblionumber(),
320 { status => Koha::ArticleRequest::Status::Pending },
321 { status => Koha::ArticleRequest::Status::Processing }
326 return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current};
329 =head3 article_requests_finished
331 my @requests = $biblio->article_requests_finished
333 Returns the article requests associated with this Biblio that are completed
337 sub article_requests_finished {
338 my ( $self, $borrower ) = @_;
340 $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
342 biblionumber => $self->biblionumber(),
344 { status => Koha::ArticleRequest::Status::Completed },
345 { status => Koha::ArticleRequest::Status::Canceled }
350 return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished};
355 my $items = $biblio->items();
357 Returns the related Koha::Items object for this biblio
364 my $items_rs = $self->_result->items;
366 return Koha::Items->_new_from_dbic( $items_rs );
371 my $itemtype = $biblio->itemtype();
373 Returns the itemtype for this record.
380 return $self->biblioitem()->itemtype();
385 my $holds = $biblio->holds();
387 return the current holds placed on this record
392 my ( $self, $params, $attributes ) = @_;
393 $attributes->{order_by} = 'priority' unless exists $attributes->{order_by};
394 my $hold_rs = $self->_result->reserves->search( $params, $attributes );
395 return Koha::Holds->_new_from_dbic($hold_rs);
400 my $holds = $biblio->current_holds
402 Return the holds placed on this bibliographic record.
403 It does not include future holds.
409 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
411 { reservedate => { '<=' => $dtf->format_date(dt_from_string) } } );
416 my $field = $self->biblioitem()->itemtype
418 Returns the related Koha::Biblioitem object for this Biblio object
425 $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
427 return $self->{_biblioitem};
432 my $subscriptions = $self->subscriptions
434 Returns the related Koha::Subscriptions object for this Biblio object
441 $self->{_subscriptions} ||= Koha::Subscriptions->search( { biblionumber => $self->biblionumber } );
443 return $self->{_subscriptions};
446 =head3 has_items_waiting_or_intransit
448 my $itemsWaitingOrInTransit = $biblio->has_items_waiting_or_intransit
450 Tells if this bibliographic record has items waiting or in transit.
454 sub has_items_waiting_or_intransit {
457 if ( Koha::Holds->search({ biblionumber => $self->id,
458 found => ['W', 'T'] })->count ) {
462 foreach my $item ( $self->items->as_list ) {
463 return 1 if $item->get_transfer;
471 my $coins = $biblio->get_coins;
473 Returns the COinS (a span) which can be included in a biblio record
480 my $record = $self->metadata->record;
482 my $pos7 = substr $record->leader(), 7, 1;
483 my $pos6 = substr $record->leader(), 6, 1;
486 my ( $aulast, $aufirst ) = ( '', '' );
497 # For the purposes of generating COinS metadata, LDR/06-07 can be
498 # considered the same for UNIMARC and MARC21
507 'i' => 'audioRecording',
508 'j' => 'audioRecording',
511 'm' => 'computerProgram',
516 'a' => 'journalArticle',
520 $genre = $fmts6->{$pos6} ? $fmts6->{$pos6} : 'book';
522 if ( $genre eq 'book' ) {
523 $genre = $fmts7->{$pos7} if $fmts7->{$pos7};
526 ##### We must transform mtx to a valable mtx and document type ####
527 if ( $genre eq 'book' ) {
530 } elsif ( $genre eq 'journal' ) {
533 } elsif ( $genre eq 'journalArticle' ) {
541 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
544 $aulast = $record->subfield( '700', 'a' ) || '';
545 $aufirst = $record->subfield( '700', 'b' ) || '';
546 push @authors, "$aufirst $aulast" if ($aufirst or $aulast);
549 if ( $record->field('200') ) {
550 for my $au ( $record->field('200')->subfield('g') ) {
555 $title = $record->subfield( '200', 'a' );
556 my $subfield_210d = $record->subfield('210', 'd');
557 if ($subfield_210d and $subfield_210d =~ /(\d{4})/) {
560 $publisher = $record->subfield( '210', 'c' ) || '';
561 $isbn = $record->subfield( '010', 'a' ) || '';
562 $issn = $record->subfield( '011', 'a' ) || '';
565 # MARC21 need some improve
568 if ( $record->field('100') ) {
569 push @authors, $record->subfield( '100', 'a' );
573 if ( $record->field('700') ) {
574 for my $au ( $record->field('700')->subfield('a') ) {
578 $title = $record->subfield( '245', 'a' ) . $record->subfield( '245', 'b' );
579 if ($titletype eq 'a') {
580 $pubyear = $record->field('008') || '';
581 $pubyear = substr($pubyear->data(), 7, 4) if $pubyear;
582 $isbn = $record->subfield( '773', 'z' ) || '';
583 $issn = $record->subfield( '773', 'x' ) || '';
584 $hosttitle = $record->subfield( '773', 't' ) || $record->subfield( '773', 'a') || q{};
585 my @rels = $record->subfield( '773', 'g' );
586 $pages = join(', ', @rels);
588 $pubyear = $record->subfield( '260', 'c' ) || '';
589 $publisher = $record->subfield( '260', 'b' ) || '';
590 $isbn = $record->subfield( '020', 'a' ) || '';
591 $issn = $record->subfield( '022', 'a' ) || '';
597 [ 'ctx_ver', 'Z39.88-2004' ],
598 [ 'rft_val_fmt', "info:ofi/fmt:kev:mtx:$mtx" ],
599 [ ($mtx eq 'dc' ? 'rft.type' : 'rft.genre'), $genre ],
600 [ "rft.${titletype}title", $title ],
603 # rft.title is authorized only once, so by checking $titletype
604 # we ensure that rft.title is not already in the list.
605 if ($hosttitle and $titletype) {
606 push @params, [ 'rft.title', $hosttitle ];
610 [ 'rft.isbn', $isbn ],
611 [ 'rft.issn', $issn ],
614 # If it's a subscription, these informations have no meaning.
615 if ($genre ne 'journal') {
617 [ 'rft.aulast', $aulast ],
618 [ 'rft.aufirst', $aufirst ],
619 (map { [ 'rft.au', $_ ] } @authors),
620 [ 'rft.pub', $publisher ],
621 [ 'rft.date', $pubyear ],
622 [ 'rft.pages', $pages ],
626 my $coins_value = join( '&',
627 map { $$_[1] ? $$_[0] . '=' . uri_escape_utf8( $$_[1] ) : () } @params );
634 my $url = $biblio->get_openurl;
636 Returns url for OpenURL resolver set in OpenURLResolverURL system preference
643 my $OpenURLResolverURL = C4::Context->preference('OpenURLResolverURL');
645 if ($OpenURLResolverURL) {
646 my $uri = URI->new($OpenURLResolverURL);
648 if (not defined $uri->query) {
649 $OpenURLResolverURL .= '?';
651 $OpenURLResolverURL .= '&';
653 $OpenURLResolverURL .= $self->get_coins;
656 return $OpenURLResolverURL;
669 Kyle M Hall <kyle@bywatersolutions.com>