View | Details | Raw Unified | Return to bug 33083
Collapse All | Expand All

(-)a/Koha/Authorities.pm (-1 / +1 lines)
Lines 24-30 use Koha::Database; Link Here
24
24
25
use Koha::Authority;
25
use Koha::Authority;
26
26
27
use base qw(Koha::Objects);
27
use base qw(Koha::Objects Koha::Objects::Record::Collections);
28
28
29
=head1 NAME
29
=head1 NAME
30
30
(-)a/Koha/Biblio.pm (+14 lines)
Lines 88-93 sub metadata { Link Here
88
    return Koha::Biblio::Metadata->_new_from_dbic($metadata);
88
    return Koha::Biblio::Metadata->_new_from_dbic($metadata);
89
}
89
}
90
90
91
=head3 record
92
93
my $record = $biblio->record();
94
95
Returns a Marc::Record object
96
97
=cut
98
99
sub record {
100
    my ( $self ) = @_;
101
102
    return $self->metadata->record;
103
}
104
91
=head3 orders
105
=head3 orders
92
106
93
my $orders = $biblio->orders();
107
my $orders = $biblio->orders();
(-)a/Koha/Biblios.pm (-1 / +1 lines)
Lines 25-31 use Koha::Database; Link Here
25
use Koha::Biblio;
25
use Koha::Biblio;
26
use Koha::Libraries;
26
use Koha::Libraries;
27
27
28
use base qw(Koha::Objects);
28
use base qw(Koha::Objects Koha::Objects::Record::Collections);
29
29
30
=head1 NAME
30
=head1 NAME
31
31
(-)a/Koha/Objects/Record/Collections.pm (+94 lines)
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;
(-)a/t/db_dependent/Biblio.t (-1 / +16 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 15;
20
use Test::More tests => 16;
21
use Test::MockModule;
21
use Test::MockModule;
22
use Test::Warn;
22
use Test::Warn;
23
use List::MoreUtils qw( uniq );
23
use List::MoreUtils qw( uniq );
Lines 852-857 subtest 'autoControlNumber tests' => sub { Link Here
852
852
853
};
853
};
854
854
855
subtest 'record test' => sub {
856
    plan tests => 1;
857
858
    my $marc_record = MARC::Record->new;
859
    $marc_record->append_fields( create_isbn_field( '0590353403', 'MARC21' ) );
860
861
    my ($biblionumber) = C4::Biblio::AddBiblio( $marc_record, '' );
862
863
    my $biblio = Koha::Biblios->find($biblionumber);
864
865
    is( $biblio->record->as_formatted,
866
        $biblio->metadata->record->as_formatted );
867
};
868
869
855
870
856
# Cleanup
871
# Cleanup
857
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
872
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
(-)a/t/db_dependent/Koha/Objects/Record/Collections.t (-1 / +57 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
use Modern::Perl;
4
5
use Test::More tests => 1;
6
7
use Koha::Biblios;
8
use Koha::Database;
9
use JSON qw( decode_json );
10
11
use t::lib::TestBuilder;
12
13
my $schema  = Koha::Database->new->schema;
14
my $builder = t::lib::TestBuilder->new;
15
16
subtest 'print_collection() tests' => sub {
17
    plan tests => 4;
18
19
    $schema->storage->txn_begin;
20
21
    # Two biblios
22
    my $biblio_1 = $builder->build_sample_biblio;
23
    my $biblio_2 = $builder->build_sample_biblio;
24
25
    my $result_set = Koha::Biblios->search(
26
        [
27
            { biblionumber => $biblio_1->biblionumber },
28
            { biblionumber => $biblio_2->biblionumber }
29
        ]
30
    );
31
    my $collection = $result_set->print_collection('marcxml');
32
33
    like( $collection, qr/<(\s*\w*:)?collection[^>]*>/, 'Has collection tag' );
34
35
    $result_set->reset;
36
    $collection = $result_set->print_collection('mij');
37
38
    my $count = scalar( @{ decode_json($collection) } );
39
40
    is( $count, 2, 'Has 2 elements' );
41
42
    $result_set->reset;
43
    $collection = $result_set->print_collection('marc');
44
45
    $count = $collection =~ tr/[\x1D]//;
46
47
    is( $count, 2, 'Has 2 USMARC end of record' );
48
49
    $result_set->reset;
50
    $collection = $result_set->print_collection('txt');
51
52
    $count = scalar( split( /\n\n/, $collection ) );
53
54
    is( $count, 2, 'Has 2 records' );
55
56
    $schema->storage->txn_rollback;
57
};

Return to bug 33083