ff60871ad039ecbc2f519af457980405b94be62d
[koha-equinox.git] / Koha / BiblioUtils / Iterator.pm
1 package Koha::BiblioUtils::Iterator;
2
3 # This contains an iterator over 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::Iterator - iterates over biblios provided by a DBIx::Class::ResultSet
25
26 =head1 DESCRIPTION
27
28 This provides an iterator over a L<DBIx::Class::ResultSet> that contains a
29 biblionumber column.
30 Returns a MARC::Record in scalar context.
31 Returns biblionumber and marc in list context.
32
33 =head1 SYNOPSIS
34
35   use Koha::BiblioUtils::Iterator;
36   my $rs = $schema->resultset('biblioitems');
37   my $iterator = Koha::BiblioUtils::Iterator->new($rs);
38   while (my $record = $iterator->next()) {
39       // do something with $record
40   }
41
42 =head1 METHODS
43
44 =cut
45
46 use C4::Biblio;    # :( - for EmbedItemsInMarcBiblio
47
48 use Carp;
49 use MARC::Record;
50 use MARC::File::XML;
51 use Modern::Perl;
52
53 =head2 new
54
55     my $it = new($sth, option => $value, ...);
56
57 Takes a ResultSet to iterate over, and gives you an iterator on it. Optional
58 options may be specified.
59
60 =head3 Options
61
62 =over 4
63
64 =item items
65
66 Set to true to include item data in the resulting MARC record.
67
68 =back
69
70 =cut
71
72 sub new {
73     my ( $class, $rs, %options ) = @_;
74
75     bless {
76         rs => $rs,
77         %options,
78     }, $class;
79 }
80
81 =head2 next()
82
83 In a scalar context, provides the next MARC::Record from the ResultSet, or
84 C<undef> if there are no more.
85
86 In a list context it will provide ($biblionumber, $record).
87
88 =cut
89
90 sub next {
91     my ($self) = @_;
92
93     my $marc;
94     my $row = $self->{rs}->next();
95     return if !$row;
96     my $marcxml = C4::Biblio::GetXmlBiblio( $row->get_column('biblionumber') );
97     if ( $marcxml ) {
98         $marc = MARC::Record->new_from_xml( $marcxml );
99     }
100     else {
101         confess "No marcxml column returned in the request.";
102     }
103
104     my $bibnum;
105     if ( $self->{items} ) {
106         $bibnum = $row->get_column('biblionumber');
107         confess "No biblionumber column returned in the request."
108           if ( !defined($bibnum) );
109
110         # TODO this should really be in Koha::BiblioUtils or something similar.
111         C4::Biblio::EmbedItemsInMarcBiblio({
112             marc_record  => $marc,
113             biblionumber => $bibnum });
114     }
115
116     if (wantarray) {
117         $bibnum //= $row->get_column('biblionumber');
118         confess "No biblionumber column returned in the request."
119           if ( !defined($bibnum) );
120         return ( $bibnum, $marc );
121     }
122     else {
123         return $marc;
124     }
125 }
126
127 1;