Bug 12478: fix the way counting is done
[koha.git] / Koha / BiblioUtils.pm
1 package Koha::BiblioUtils;
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 some handy biblio-related functions
25
26 =head1 DESCRIPTION
27
28 This contains functions for operations on biblio records.
29
30 Note: really, C4::Biblio does the main functions, but the Koha namespace is
31 the new thing that should be used.
32
33 =cut
34
35 use C4::Biblio; # EmbedItemsInMarcBiblio
36 use Koha::Biblio::Iterator;
37 use Koha::Database;
38 use Modern::Perl;
39
40 use base qw(Class::Accessor);
41
42 __PACKAGE__->mk_accessors(qw());
43
44 =head1 FUNCTIONS
45
46 =head2 get_all_biblios_iterator
47
48     my $it = get_all_biblios_iterator();
49
50 This will provide an iterator object that will, one by one, provide the
51 MARC::Record of each biblio. This will include the item data.
52
53 The iterator is a Koha::Biblio::Iterator object.
54
55 =cut
56
57 sub get_all_biblios_iterator {
58     my $database = Koha::Database->new();
59     my $schema   = $database->schema();
60     my $rs =
61       $schema->resultset('Biblioitem')->search( { marc => { '!=', undef } },
62         { columns => [qw/ biblionumber marc /] } );
63     return Koha::Biblio::Iterator->new($rs, items => 1);
64 }
65
66 =head2 get_marc_biblio
67
68     my $marc = get_marc_biblio($bibnum, %options);
69
70 This fetches the MARC::Record for the given biblio number. Nothing is returned
71 if the biblionumber couldn't be found (or it somehow has no MARC data.)
72
73 Options are:
74
75 =over 4
76
77 =item item_data
78
79 If set to true, item data is embedded in the record. Default is to not do this.
80
81 =back
82
83 =cut
84
85 sub get_marc_biblio {
86     my ($class,$bibnum, %options) = @_;
87
88     my $database = Koha::Database->new();
89     my $schema   = $database->schema();
90     my $rs =
91       $schema->resultset('Biblioitem')
92       ->search( { marc => { '!=', undef }, biblionumber => $bibnum },
93         { columns => [qw/ marc /] } );
94
95     my $row = $rs->next();
96     return unless $row;
97     my $marc = MARC::Record->new_from_usmarc($row->marc);
98
99     # TODO implement this in this module
100     C4::Biblio::EmbedItemsInMarcBiblio($marc, $bibnum) if $options{item_data};
101
102     return $marc;
103 }
104
105 1;