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

(-)a/Koha/BiblioFramework.pm (+52 lines)
Lines 18-23 package Koha::BiblioFramework; Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Koha::Database;
20
use Koha::Database;
21
use C4::Biblio qw( GetMarcFromKohaField GetMarcStructure IsMarcStructureInternal );
21
22
22
use base qw(Koha::Object);
23
use base qw(Koha::Object);
23
24
Lines 31-36 Koha::BiblioFramework - Koha BiblioFramework Object class Link Here
31
32
32
=cut
33
=cut
33
34
35
=head2 fill_with_default_values
36
37
Fill the given MARC record with default values from the framework. If the
38
optional parameter C<only_mandatory> is set to a true value, only the mandatory
39
fields/subfields will be filled.
40
41
    $framework->fill_with_default_values( $record, { only_mandatory => 1 } );
42
43
Where C<$framework> is a L<Koha::BiblioFramework> object and C<$record> is a
44
L<MARC::Record> object.
45
46
=cut
47
48
sub fill_with_default_values {
49
    my ( $self, $record, $params ) = @_;
50
    return unless $record;
51
    my $tagslib   = C4::Biblio::GetMarcStructure( 1, $self->frameworkcode, { unsafe => 1 } );
52
    my $mandatory = $params->{only_mandatory};
53
    if ($tagslib) {
54
        my ($itemfield) = C4::Biblio::GetMarcFromKohaField('items.itemnumber');
55
        for my $tag ( sort keys %$tagslib ) {
56
            next unless $tag;
57
            next if $tag == $itemfield;
58
            for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
59
                next if C4::Biblio::IsMarcStructureInternal( $tagslib->{$tag}{$subfield} );
60
                next if $mandatory && !$tagslib->{$tag}{$subfield}{mandatory};
61
                my $defaultvalue = $tagslib->{$tag}{$subfield}{defaultvalue};
62
                if ( defined $defaultvalue and $defaultvalue ne '' ) {
63
                    my @fields = $record->field($tag);
64
                    if (@fields) {
65
                        for my $field (@fields) {
66
                            if ( $field->is_control_field ) {
67
                                $field->update($defaultvalue) if not defined $field->data;
68
                            } elsif ( not defined $field->subfield($subfield) ) {
69
                                $field->add_subfields( $subfield => $defaultvalue );
70
                            }
71
                        }
72
                    } else {
73
                        if ( $tag < 10 ) {    # is_control_field
74
                            $record->insert_fields_ordered( MARC::Field->new( $tag, $defaultvalue ) );
75
                        } else {
76
                            $record->insert_fields_ordered(
77
                                MARC::Field->new( $tag, '', '', $subfield => $defaultvalue ) );
78
                        }
79
                    }
80
                }
81
            }
82
        }
83
    }
84
}
85
34
=head3 type
86
=head3 type
35
87
36
=cut
88
=cut
(-)a/Koha/ILL/Backend/Standard.pm (-1 / +7 lines)
Lines 26-32 use Koha::DateUtils qw/ dt_from_string /; Link Here
26
use Koha::I18N      qw(__);
26
use Koha::I18N      qw(__);
27
use Koha::ILL::Requests;
27
use Koha::ILL::Requests;
28
use Koha::ILL::Request::Attribute;
28
use Koha::ILL::Request::Attribute;
29
use C4::Biblio  qw( AddBiblio );
29
use C4::Biblio qw( AddBiblio );
30
use Koha::BiblioFramework;
31
use Koha::BiblioFrameworks;
30
use C4::Charset qw( MarcToUTF8Record );
32
use C4::Charset qw( MarcToUTF8Record );
31
33
32
=head1 NAME
34
=head1 NAME
Lines 1157-1162 sub _standard_request2biblio { Link Here
1157
    # Suppress the record
1159
    # Suppress the record
1158
    _set_suppression($record);
1160
    _set_suppression($record);
1159
1161
1162
    # Fill with default values from framework
1163
    my $framework = Koha::BiblioFrameworks->find( $self->{framework} );
1164
    $framework->fill_with_default_values($record);
1165
1160
    # Create a biblio record
1166
    # Create a biblio record
1161
    my ( $biblionumber, $biblioitemnumber ) =
1167
    my ( $biblionumber, $biblioitemnumber ) =
1162
        AddBiblio( $record, $self->{framework} );
1168
        AddBiblio( $record, $self->{framework} );
(-)a/t/db_dependent/Koha/BiblioFrameworks.t (-3 / +150 lines)
Lines 19-29 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use Test::NoWarnings;
21
use Test::NoWarnings;
22
use Test::More tests => 4;
22
use Test::More tests => 5;
23
use Test::MockModule;
24
use MARC::Record;
25
use MARC::Field;
26
23
use Koha::BiblioFramework;
27
use Koha::BiblioFramework;
24
use Koha::BiblioFrameworks;
28
use Koha::BiblioFrameworks;
29
use Koha::Database;
30
use t::lib::TestBuilder;
31
32
my $schema  = Koha::Database->new->schema;
33
my $builder = t::lib::TestBuilder->new;
25
34
26
my $schema = Koha::Database->new->schema;
27
$schema->storage->txn_begin;
35
$schema->storage->txn_begin;
28
36
29
my $nb_of_frameworks = Koha::BiblioFrameworks->search->count;
37
my $nb_of_frameworks = Koha::BiblioFrameworks->search->count;
Lines 50-53 is( Link Here
50
58
51
$retrieved_framework_1->delete;
59
$retrieved_framework_1->delete;
52
is( Koha::BiblioFrameworks->search->count, $nb_of_frameworks + 1, 'Delete should have deleted the biblio framework' );
60
is( Koha::BiblioFrameworks->search->count, $nb_of_frameworks + 1, 'Delete should have deleted the biblio framework' );
61
62
subtest 'fill_with_default_values' => sub {
63
    plan tests => 14;
64
65
    # Create a test framework
66
    my $framework     = $builder->build_object( { class => 'Koha::BiblioFrameworks' } );
67
    my $frameworkcode = $framework->frameworkcode;
68
69
    my $default_008    = '######s################';
70
    my $default_author = 'Default Author';
71
72
    my $biblio_module = Test::MockModule->new('C4::Biblio');
73
    $biblio_module->mock(
74
        'GetMarcStructure',
75
        sub {
76
            return {
77
                # default for a control field
78
                '008' => {
79
                    x => { defaultvalue => $default_008 },
80
                },
81
82
                # default value for an existing field
83
                '245' => {
84
                    c          => { defaultvalue => $default_author, mandatory => 1 },
85
                    mandatory  => 0,
86
                    repeatable => 0,
87
                    tab        => 0,
88
                    lib        => 'a lib',
89
                },
90
91
                # default for a nonexisting field
92
                '099' => {
93
                    x => { defaultvalue => 'default_099' },
94
                },
95
                '942' => {
96
                    c => { defaultvalue => 'BK', mandatory => 1 },
97
                    d => { defaultvalue => '942d_val' },
98
                    f => { defaultvalue => '942f_val' },
99
                },
100
            };
101
        }
102
    );
103
104
    # Mock GetMarcFromKohaField to return item field
105
    $biblio_module->mock( 'GetMarcFromKohaField', sub { return ( '952', 'i' ); } );
106
107
    # Mock IsMarcStructureInternal - return true for non-hashref (tag properties), false for subfields
108
    $biblio_module->mock(
109
        'IsMarcStructureInternal',
110
        sub {
111
            my ($subfieldstruct) = @_;
112
            return 1 unless ref($subfieldstruct) eq 'HASH';
113
            return 0;
114
        }
115
    );
116
117
    # Test 1: Fill defaults for existing record
118
    my $record = MARC::Record->new();
119
    $record->leader('03174nam a2200445 a 4500');
120
    my @fields = (
121
        MARC::Field->new(
122
            '008', '120829t20132012nyu bk 001 0ceng',
123
        ),
124
        MARC::Field->new(
125
            100, '1', ' ',
126
            a => 'Knuth, Donald Ervin',
127
            d => '1938',
128
        ),
129
        MARC::Field->new(
130
            245, '1', '4',
131
            a => 'The art of computer programming',
132
            c => 'Donald E. Knuth.',
133
        ),
134
        MARC::Field->new(
135
            245, '1', '4', a => 'my second title',
136
        ),
137
    );
138
139
    $record->append_fields(@fields);
140
    $framework->fill_with_default_values($record);
141
142
    my @fields_245 = $record->field(245);
143
    is( scalar(@fields_245), 2, 'No new 245 field has been created' );
144
    my @subfields_245_0 = $fields_245[0]->subfields;
145
    my @subfields_245_1 = $fields_245[1]->subfields;
146
    is_deeply(
147
        \@subfields_245_0,
148
        [ [ 'a', 'The art of computer programming' ], [ 'c', 'Donald E. Knuth.' ] ],
149
        'first 245 field has not been updated'
150
    );
151
    is_deeply(
152
        \@subfields_245_1,
153
        [ [ 'a', 'my second title' ], [ 'c', $default_author ] ],
154
        'second 245 field has a new subfield c with a default value'
155
    );
156
157
    # Test 2: New field with default is created
158
    my @fields_099 = $record->field('099');
159
    is( scalar(@fields_099), 1, '1 new 099 field has been created' );
160
    my @subfields_099 = $fields_099[0]->subfields;
161
    is_deeply(
162
        \@subfields_099,
163
        [ [ 'x', 'default_099' ] ],
164
        '099$x contains the default value'
165
    );
166
167
    # Test 3: Control field default is applied when undefined
168
    $record->field('008')->update(undef);
169
    $framework->fill_with_default_values($record);
170
    is( $record->field('008')->data, $default_008, 'Controlfield got default' );
171
172
    # Test 4: Check 942 fields
173
    is( $record->subfield( '942', 'd' ), '942d_val', 'Check 942d' );
174
175
    # Test 5: Only mandatory parameter
176
    $record->delete_fields( $record->field('245') );
177
    $record->delete_fields( $record->field('942') );
178
    $record->append_fields( MARC::Field->new( '942', '', '', 'f' => 'f val' ) );
179
180
    # We deleted 245 and replaced 942. If we only apply mandatories, we should get
181
    # back 245c again and 942c but not 942d. 942f should be left alone.
182
    $framework->fill_with_default_values( $record, { only_mandatory => 1 } );
183
    @fields_245 = $record->field(245);
184
    is( scalar @fields_245, 1, 'Only one 245 expected' );
185
    is( $record->subfield( '245', 'c' ), $default_author, '245c restored' );
186
    is( $record->subfield( '942', 'c' ), 'BK',            '942c also restored' );
187
    is( $record->subfield( '942', 'd' ), undef,           '942d should not be there' );
188
    is( $record->subfield( '942', 'f' ), 'f val',         '942f untouched' );
189
190
    # Test 6: Handle undefined record gracefully
191
    my $record_undef;
192
    $framework->fill_with_default_values($record_undef);
193
    ok( !defined $record_undef, 'Undefined record handled without error' );
194
195
    # Test 7: Empty record should get defaults
196
    my $empty_record = MARC::Record->new();
197
    $framework->fill_with_default_values($empty_record);
198
    is( $empty_record->subfield( '942', 'c' ), 'BK', 'Empty record gets default 942c' );
199
};
200
53
$schema->storage->txn_rollback;
201
$schema->storage->txn_rollback;
54
- 

Return to bug 41757