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

(-)a/Koha/AdditionalContents.pm (+35 lines)
Lines 110-115 sub search_for_display { Link Here
110
    return $self->SUPER::search({%$search_params, lang => 'default'}, { order_by => 'number'});
110
    return $self->SUPER::search({%$search_params, lang => 'default'}, { order_by => 'number'});
111
}
111
}
112
112
113
=head3 find_best_match
114
115
    Koha::AdditionalContents->find_best_match({
116
        category => , location => , lang => , library_id =>
117
    });
118
119
    When choosing the best match, a match on lang and library is preferred.
120
    Next a match on library and default lang. Then match on All libs and lang.
121
    Finally a match with All libs and default lang.
122
123
=cut
124
125
sub find_best_match {
126
    my ( $self, $params ) = @_;
127
    my $library_id = $params->{library_id};
128
    my $lang = $params->{lang};
129
130
    my $rs = $self->SUPER::search({
131
        category => $params->{category},
132
        location => $params->{location},
133
        lang => [ $lang, 'default' ],
134
        branchcode => [ $library_id, undef ],
135
    });
136
137
    # Pick the best
138
    my ( $alt1, $alt2, $alt3 );
139
    while( my $rec = $rs->next ) {
140
        return $rec if $library_id && $rec->branchcode && $rec->branchcode eq $library_id && $lang && $rec->lang eq $lang;
141
        $alt1 = $rec if !$alt1 && $library_id && $rec->branchcode && $rec->branchcode eq $library_id;
142
        $alt2 = $rec if !$alt2 && $lang && $rec->lang eq $lang;
143
        $alt3 = $rec if !$alt3;
144
    }
145
    return $alt1 // $alt2 // $alt3;
146
}
147
113
=head3 _type
148
=head3 _type
114
149
115
=cut
150
=cut
(-)a/Koha/Library.pm (+20 lines)
Lines 313-318 sub to_api_mapping { Link Here
313
    };
313
    };
314
}
314
}
315
315
316
=head3 opac_info
317
318
    $library->opac_info({ lang => $lang });
319
320
Returns additional contents block OpacLibraryInfo for $lang or 'default'.
321
322
Note: This replaces the former branches.opac_info column.
323
324
=cut
325
326
sub opac_info {
327
    my ( $self, $params ) = @_;
328
    return Koha::AdditionalContents->find_best_match({
329
        category => 'html_customizations',
330
        location => 'OpacLibraryInfo',
331
        lang => $params->{lang},
332
        library_id => $self->branchcode,
333
    });
334
}
335
316
=head2 Internal methods
336
=head2 Internal methods
317
337
318
=head3 _type
338
=head3 _type
(-)a/t/db_dependent/Koha/AdditionalContents.t (-1 / +28 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 5;
22
use Test::More tests => 6;
23
use Test::Exception;
23
use Test::Exception;
24
24
25
use Koha::AdditionalContents;
25
use Koha::AdditionalContents;
Lines 259-261 subtest '->search_for_display' => sub { Link Here
259
259
260
    $schema->storage->txn_rollback;
260
    $schema->storage->txn_rollback;
261
};
261
};
262
263
subtest 'find_best_match' => sub {
264
    plan tests => 3;
265
    $schema->storage->txn_begin;
266
267
    my $library01 = $builder->build_object({ class => 'Koha::Libraries' });
268
    my $html01 = $builder->build_object({
269
        class => 'Koha::AdditionalContents',
270
        value => { category => 'html_customizations', location => 'test_best_match', branchcode => undef, lang => 'default' },
271
    });
272
    my $params = { category => 'html_customizations', location => 'test_best_match', lang => 'nl-NL' };
273
    is( Koha::AdditionalContents->find_best_match($params)->idnew, $html01->idnew, 'Found all branches, lang default' );
274
275
    my $html02 = $builder->build_object({
276
        class => 'Koha::AdditionalContents',
277
        value => { category => 'html_customizations', location => 'test_best_match', branchcode => undef, lang => 'nl-NL' },
278
    });
279
    is( Koha::AdditionalContents->find_best_match($params)->idnew, $html02->idnew, 'Found all branches, lang nl-NL' );
280
281
    $params->{ library_id } = $library01->id;
282
    $html02->branchcode( $library01->id )->store;
283
    is( Koha::AdditionalContents->find_best_match($params)->idnew, $html02->idnew, 'Found library01, lang nl-NL' );
284
285
    # Note: find_best_match is tested further via $libary->opac_info; see t/db_dependent/Koha/Library.t
286
287
    $schema->storage->txn_rollback;
288
}
(-)a/t/db_dependent/Koha/Library.t (-2 / +39 lines)
Lines 19-27 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 1;
22
use Test::More tests => 2;
23
23
24
use Koha::Database;
24
use Koha::Database;
25
use Koha::AdditionalContents;
25
use Koha::SMTP::Servers;
26
use Koha::SMTP::Servers;
26
27
27
use t::lib::TestBuilder;
28
use t::lib::TestBuilder;
Lines 93-95 subtest 'smtp_server() tests' => sub { Link Here
93
94
94
    $schema->storage->txn_rollback;
95
    $schema->storage->txn_rollback;
95
};
96
};
96
- 
97
98
subtest 'opac_info tests' => sub {
99
    plan tests => 8;
100
    $schema->storage->txn_begin;
101
    my $library01 = $builder->build_object({ class => 'Koha::Libraries' });
102
    my $library02 = $builder->build_object({ class => 'Koha::Libraries' });
103
104
    my $html01 = $builder->build_object({
105
        class => 'Koha::AdditionalContents',
106
        value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => undef, lang => 'default', content => '1', expirationdate => undef },
107
    });
108
    my $html02 = $builder->build_object({
109
        class => 'Koha::AdditionalContents',
110
        value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => $library01->id, lang => 'default', content => '2', expirationdate => undef },
111
    });
112
    my $html03 = $builder->build_object({
113
        class => 'Koha::AdditionalContents',
114
        value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => $library01->id, lang => 'nl-NL', content => '3', expirationdate => undef },
115
    });
116
    my $html04 = $builder->build_object({
117
        class => 'Koha::AdditionalContents',
118
        value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => undef, lang => 'fr-FR', content => '4', expirationdate => undef },
119
    });
120
121
    # Start testing
122
    is( $library01->opac_info->content, '2', 'specific library, default language' );
123
    is( $library01->opac_info({ lang => 'nl-NL' })->content, '3', 'specific library, specific language' );
124
    is( $library01->opac_info({ lang => 'nl-BE' })->content, '2', 'specific library, unknown language' );
125
    is( $library02->opac_info->content, '1', 'unknown library, default language' );
126
    is( $library02->opac_info({ lang => 'fr-FR' })->content, '4', 'unknown library, specific language' );
127
    is( $library02->opac_info({ lang => 'de-DE' })->content, '1', 'unknown library, unknown language' );
128
    $html01->delete;
129
    is( $library02->opac_info, undef, 'unknown library, default language (after removing html01)' );
130
    is( $library02->opac_info({ lang => 'de-DE' }), undef, 'unknown library, unknown language (after removing html01)' );
131
132
    $schema->storage->txn_rollback;
133
};

Return to bug 29144