Bug 14699: Reword "Select all/Clear all" and change their behaviour
[koha.git] / Koha / BiblioUtils / Iterator.pm
blobecc3727644bdb232e7358c94864c28f7affb1c43
1 package Koha::BiblioUtils::Iterator;
3 # This contains an iterator over biblio records
5 # Copyright 2014 Catalyst IT
7 # This file is part of Koha.
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.
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.
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.
22 =head1 NAME
24 Koha::BiblioUtils::Iterator - iterates over biblios provided by a DBIx::Class::ResultSet
26 =head1 DESCRIPTION
28 This provides an iterator that gives the MARC::Record of each biblio that's
29 returned by a L<DBIx::Class::ResultSet> that provides a C<biblionumber>, and
30 C<marcxml> column from the biblioitems table.
32 =head1 SYNOPSIS
34 use Koha::BiblioUtils::Iterator;
35 my $rs = $schema->resultset('biblioitems');
36 my $iterator = Koha::BiblioUtils::Iterator->new($rs);
37 while (my $record = $iterator->next()) {
38 // do something with $record
41 =head1 METHODS
43 =cut
45 use C4::Biblio; # :( - for EmbedItemsInMarcBiblio
47 use Carp;
48 use MARC::Record;
49 use MARC::File::XML;
50 use Modern::Perl;
52 =head2 new
54 my $it = new($sth, option => $value, ...);
56 Takes a ResultSet to iterate over, and gives you an iterator on it. Optional
57 options may be specified.
59 =head3 Options
61 =over 4
63 =item items
65 Set to true to include item data in the resulting MARC record.
67 =back
69 =cut
71 sub new {
72 my ( $class, $rs, %options ) = @_;
74 bless {
75 rs => $rs,
76 %options,
77 }, $class;
80 =head2 next()
82 In a scalar context, provides the next MARC::Record from the ResultSet, or
83 C<undef> if there are no more.
85 In a list context it will provide ($biblionumber, $record).
87 =cut
89 sub next {
90 my ($self) = @_;
92 my $marc;
93 my $row = $self->{rs}->next();
94 return if !$row;
95 if ( $row->marcxml ) {
96 $marc = MARC::Record->new_from_xml( $row->marcxml );
98 else {
99 confess "No marcxml column returned in the request.";
102 my $bibnum;
103 if ( $self->{items} ) {
104 $bibnum = $row->get_column('biblionumber');
105 confess "No biblionumber column returned in the request."
106 if ( !defined($bibnum) );
108 # TODO this should really be in Koha::BiblioUtils or something similar.
109 C4::Biblio::EmbedItemsInMarcBiblio( $marc, $bibnum );
112 if (wantarray) {
113 $bibnum //= $row->get_column('biblionumber');
114 confess "No biblionumber column returned in the request."
115 if ( !defined($bibnum) );
116 return ( $bibnum, $marc );
118 else {
119 return $marc;