Bug 12478: fix some issues on rebase
[koha.git] / Koha / BiblioUtils.pm
1 package Koha::BiblioUtilsUtils;
2
3 # This contains functions to do with managing biblio records.
4
5 # Copyright 2014 Catalyst IT
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 3 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 =head1 NAME
23
24 Koha::BiblioUtils - contains fundamental biblio-related functions
25
26 =head1 DESCRIPTION
27
28 This contains functions for normal operations on biblio records.
29
30 Note: really, C4::BiblioUtils does the main functions, but the Koha namespace is
31 the new thing that should be used.
32
33 =cut
34
35 use C4::BiblioUtils; # EmbedItemsInMarcBiblio
36 use Koha::MetadataIterator;
37 use Koha::Database;
38 use Modern::Perl;
39
40 use base qw(Koha::MetadataRecord);
41
42 __PACKAGE__->mk_accessors(qw( record schema idnumber datatype ));
43
44 =head1 FUNCTIONS
45
46 =head2 new
47
48     my $biblio = Koha::BiblioUtils->new($marc_record, [$biblionumber]);
49
50 Creates an instance of C<Koha::BiblioUtils> based on the marc record. If known,
51 the biblionumber can be provided too.
52
53 =cut
54
55 sub new {
56     my $class = shift;
57     my $record = shift;
58     my $biblionumber = shift;
59
60     my $self = $class->SUPER::new(
61         {
62             'record'   => $record,
63             'schema'   => lc C4::Context->preference("marcflavour"),
64             'idnumber' => $biblionumber,
65             'datatype' => 'biblio',
66         }
67     );
68     bless $self, $class;
69     return $self;
70 }
71
72 =head2 get_from_biblionumber
73
74     my $biblio = Koha::BiblioUtils->get_from_biblionumber($biblionumber, %options);
75
76 This will give you an instance of L<Koha::BiblioUtils> that is the biblio that
77 you requested.
78
79 Options are:
80
81 =over 4
82
83 =item C<$item_data>
84
85 If true, then the item data will be merged into the record when it's loaded.
86
87 =back
88
89 It will return C<undef> if the biblio doesn't exist.
90
91 =cut
92
93 sub get_from_biblionumber {
94     my ($class, $bibnum, %options) = @_;
95
96     my $marc = $class->get_marc_biblio($bibnum, %options);
97     return $class->new($marc, $bibnum);
98 }
99
100 =head2 get_all_biblios_iterator
101
102     my $it = Koha::BiblioUtils->get_all_biblios_iterator();
103
104 This will provide an iterator object that will, one by one, provide the
105 Koha::BiblioUtils of each biblio. This will include the item data.
106
107 The iterator is a Koha::MetadataIterator object.
108
109 =cut
110
111 sub get_all_biblios_iterator {
112     my $database = Koha::Database->new();
113     my $schema   = $database->schema();
114     my $rs =
115       $schema->resultset('Biblio')->search( {},
116         { columns => [qw/ biblionumber /] } );
117     my $next_func = sub {
118         my $row = $rs->next();
119         return undef if !$row;
120         my $marc = C4::Biblio::GetMarcBiblio( $row->biblionumber, 1 );
121         return __PACKAGE__->new($marc, $row->biblionumber);
122     };
123     return Koha::MetadataIterator->new($next_func);
124 }
125
126 =head2 get_marc_biblio
127
128     my $marc = Koha::BiblioUtils->get_marc_biblio($bibnum, %options);
129
130 This non-class function fetches the MARC::Record for the given biblio number.
131 Nothing is returned if the biblionumber couldn't be found (or it somehow has no
132 MARC data.)
133
134 Options are:
135
136 =over 4
137
138 =item item_data
139
140 If set to true, item data is embedded in the record. Default is to not do this.
141
142 =back
143
144 =cut
145
146 sub get_marc_biblio {
147     my ($class, $bibnum, %options) = @_;
148
149     return C4::Biblio::GetMarcBiblio( $bibnum, ($options{item_data} ? 1 : 0 ) );
150 }
151
152 1;