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

(-)a/Koha/AdditionalContents.pm (+35 lines)
Lines 109-114 sub search_for_display { Link Here
109
    return $self->SUPER::search({%$search_params, lang => 'default'}, { order_by => 'number'});
109
    return $self->SUPER::search({%$search_params, lang => 'default'}, { order_by => 'number'});
110
}
110
}
111
111
112
=head3 find_best_match
113
114
    Koha::AdditionalContents->find_best_match({
115
        category => , location => , lang => , library_id =>
116
    });
117
118
    When choosing the best match, a match on lang and library is preferred.
119
    Next a match on library and default lang. Then match on All libs and lang.
120
    Finally a match with All libs and default lang.
121
122
=cut
123
124
sub find_best_match {
125
    my ( $self, $params ) = @_;
126
    my $library_id = $params->{library_id};
127
    my $lang = $params->{lang};
128
129
    my $rs = $self->SUPER::search({
130
        category => $params->{category},
131
        location => $params->{location},
132
        lang => [ $lang, 'default' ],
133
        branchcode => [ $library_id, undef ],
134
    });
135
136
    # Pick the best
137
    my ( $alt1, $alt2, $alt3 );
138
    while( my $rec = $rs->next ) {
139
        return $rec if $library_id && $rec->branchcode && $rec->branchcode eq $library_id && $lang && $rec->lang eq $lang;
140
        $alt1 = $rec if !$alt1 && $library_id && $rec->branchcode && $rec->branchcode eq $library_id;
141
        $alt2 = $rec if !$alt2 && $lang && $rec->lang eq $lang;
142
        $alt3 = $rec if !$alt3;
143
    }
144
    return $alt1 // $alt2 // $alt3;
145
}
146
112
=head3 _type
147
=head3 _type
113
148
114
=cut
149
=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