|
Line 0
Link Here
|
|
|
1 |
package Koha::Objects::Record::Collections; |
| 2 |
|
| 3 |
# Copyright 2023 Theke Solutions |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use MARC::File::MiJ; |
| 23 |
use MARC::File::USMARC; |
| 24 |
use MARC::File::XML; |
| 25 |
use MARC::Record; |
| 26 |
|
| 27 |
=head1 NAME |
| 28 |
|
| 29 |
Koha::Objects::Record::Collections - Generic records collection handling class |
| 30 |
|
| 31 |
=head1 SYNOPSIS |
| 32 |
|
| 33 |
use base qw(Koha::Objects Koha::Objects::Record::Collections); |
| 34 |
my $collections = Koha::Objects->print_collection($format); |
| 35 |
|
| 36 |
=head1 DESCRIPTION |
| 37 |
|
| 38 |
This class is provided as a generic way of handling a collection of records for Koha::Objects-based classes |
| 39 |
in Koha. |
| 40 |
|
| 41 |
This class must always be subclassed. |
| 42 |
|
| 43 |
=head1 API |
| 44 |
|
| 45 |
=head2 Class methods |
| 46 |
|
| 47 |
=cut |
| 48 |
|
| 49 |
=head3 print_collection |
| 50 |
my $collection_text = $result_set->print_collection($format) |
| 51 |
|
| 52 |
Return a text representation of a collection (group of records) in the specified format. |
| 53 |
Allowed formats are marcxml, mij, marc and txt. Defaults to marcxml. |
| 54 |
|
| 55 |
=cut |
| 56 |
|
| 57 |
sub print_collection { |
| 58 |
my ( $self, $format ) = @_; |
| 59 |
|
| 60 |
my ( $start, $glue, $end, @parts ); |
| 61 |
|
| 62 |
my %serializers = ( |
| 63 |
'mij' => \&MARC::File::MiJ::encode, |
| 64 |
'marc' => \&MARC::File::USMARC::encode, |
| 65 |
'txt' => \&MARC::Record::as_formatted, |
| 66 |
'marcxml' => \&MARC::File::XML::record |
| 67 |
); |
| 68 |
if ( $format eq 'mij' ) { |
| 69 |
$start = '['; |
| 70 |
$glue = ','; |
| 71 |
$end = ']'; |
| 72 |
} |
| 73 |
elsif ( $format eq 'marc' ) { |
| 74 |
$glue = "\n"; |
| 75 |
} |
| 76 |
elsif ( $format eq 'txt' ) { |
| 77 |
$glue = "\n\n"; |
| 78 |
} |
| 79 |
else { |
| 80 |
$glue = ''; |
| 81 |
$format = 'marcxml'; |
| 82 |
$start = MARC::File::XML::header(); |
| 83 |
$end = MARC::File::XML::footer(); |
| 84 |
} |
| 85 |
while ( my $element = $self->next ) { |
| 86 |
push @parts, $serializers{$format}->( $element->record ); |
| 87 |
} |
| 88 |
return |
| 89 |
( defined $start ? $start : '' ) |
| 90 |
. join( $glue, @parts ) |
| 91 |
. ( defined $end ? $end : '' ); |
| 92 |
} |
| 93 |
|
| 94 |
1; |