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

(-)a/C4/Koha.pm (-1 lines)
Lines 554-560 sub GetAuthorisedValues { Link Here
554
        $av_unblessed->{lib} = $av->lib;
554
        $av_unblessed->{lib} = $av->lib;
555
        $av_unblessed->{lib_opac} = $av->lib_opac;
555
        $av_unblessed->{lib_opac} = $av->lib_opac;
556
        push @results, $av_unblessed;
556
        push @results, $av_unblessed;
557
558
    }
557
    }
559
558
560
    $cache->set_in_cache( $cache_key, \@results, { expiry => 5 } );
559
    $cache->set_in_cache( $cache_key, \@results, { expiry => 5 } );
(-)a/Koha/AuthorisedValues.pm (-5 / +22 lines)
Lines 192-209 my $avs = Koha::AuthorisedValues->search_with_localization Link Here
192
sub search_with_localization {
192
sub search_with_localization {
193
    my ( $self, $params, $attributes ) = @_;
193
    my ( $self, $params, $attributes ) = @_;
194
194
195
    my $language = C4::Languages::getlanguage();
195
    my $language  = C4::Languages::getlanguage();
196
    $Koha::Schema::Result::AuthorisedValue::LANGUAGE = $language;
196
    my $interface = C4::Context->interface;
197
    $attributes->{order_by} = 'translated_description' unless exists $attributes->{order_by};
197
    $Koha::Schema::Result::AuthorisedValue::LANGUAGE  = $language;
198
    $Koha::Schema::Result::AuthorisedValue::INTERFACE = $interface;
199
200
    unless ( exists $attributes->{order_by} ) {
201
        $attributes->{order_by} =
202
          $interface eq 'intranet'
203
          ? 'translated_description'
204
          : 'opac_translated_description';
205
    }
206
198
    my @join = $attributes->{join} || ();
207
    my @join = $attributes->{join} || ();
199
    push @join, 'localization';
208
    push @join, 'localization';
200
    $attributes->{join} = \@join;
209
    $attributes->{join} = \@join;
201
    $attributes->{'+select'} = [
210
    my $select = $interface eq 'intranet'
211
      ? [
202
        {
212
        {
203
            coalesce => [qw( localization.translation me.lib )],
213
            coalesce => [qw( localization.translation me.lib )],
204
            -as      => 'translated_description'
214
            -as      => 'translated_description'
205
        }
215
        }
206
    ];
216
      ]
217
      : [
218
        {
219
            coalesce => [qw( localization.translation me.lib_opac )],
220
            -as      => 'opac_translated_description'
221
        }
222
      ];
223
    $attributes->{'+select'} = $select;
207
    if(defined $params->{branchcode}) {
224
    if(defined $params->{branchcode}) {
208
        my $branchcode = delete $params->{branchcode};
225
        my $branchcode = delete $params->{branchcode};
209
        $self->search_with_library_limits( $params, $attributes, $branchcode );
226
        $self->search_with_library_limits( $params, $attributes, $branchcode );
(-)a/Koha/Schema/Result/AuthorisedValue.pm (-3 / +5 lines)
Lines 148-165 __PACKAGE__->has_many( Link Here
148
# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-02-22 14:32:48
148
# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-02-22 14:32:48
149
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:hDlebhEn+f+thqwBo/LOqQ
149
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:hDlebhEn+f+thqwBo/LOqQ
150
150
151
# Use the ItemtypeLocalization view to create the join on localization
151
# Use the AuthorisedValueLocalization view to create the join on localization
152
our $LANGUAGE;
152
our ( $LANGUAGE, $INTERFACE );
153
__PACKAGE__->has_many(
153
__PACKAGE__->has_many(
154
  "localization" => "Koha::Schema::Result::AuthorisedValueLocalization",
154
  "localization" => "Koha::Schema::Result::AuthorisedValueLocalization",
155
    sub {
155
    sub {
156
        my $args = shift;
156
        my $args = shift;
157
157
158
        die "no lang specified!" unless $LANGUAGE;
158
        die "no lang specified!" unless $LANGUAGE;
159
        die "no interface specified!" unless $INTERFACE;
159
160
160
        return ({
161
        return ({
161
            "$args->{self_alias}.authorised_value" => { -ident => "$args->{foreign_alias}.code" },
162
            "$args->{foreign_alias}.code" => \["= CONCAT(me.category, '_', me.authorised_value)"],
162
            "$args->{foreign_alias}.lang" => $LANGUAGE,
163
            "$args->{foreign_alias}.lang" => $LANGUAGE,
164
            "$args->{foreign_alias}.interface" => $INTERFACE,
163
        });
165
        });
164
166
165
    }
167
    }
(-)a/Koha/Schema/Result/AuthorisedValueLocalization.pm (-1 / +3 lines)
Lines 9-15 __PACKAGE__->table_class('DBIx::Class::ResultSource::View'); Link Here
9
__PACKAGE__->table('authorised_value_localizations');
9
__PACKAGE__->table('authorised_value_localizations');
10
__PACKAGE__->result_source_instance->is_virtual(1);
10
__PACKAGE__->result_source_instance->is_virtual(1);
11
__PACKAGE__->result_source_instance->view_definition(
11
__PACKAGE__->result_source_instance->view_definition(
12
    "SELECT localization_id, code, lang, translation FROM localization WHERE entity='authorised_values'"
12
    "SELECT localization_id, code, lang, translation, interface FROM localization WHERE entity='authorised_values'"
13
);
13
);
14
14
15
__PACKAGE__->add_columns(
15
__PACKAGE__->add_columns(
Lines 21-26 __PACKAGE__->add_columns( Link Here
21
  { data_type => "varchar", is_nullable => 0, size => 25 },
21
  { data_type => "varchar", is_nullable => 0, size => 25 },
22
  "translation",
22
  "translation",
23
  { data_type => "text", is_nullable => 1 },
23
  { data_type => "text", is_nullable => 1 },
24
  "interface",
25
  { data_type => "text", is_nullable => 1 },
24
);
26
);
25
27
26
__PACKAGE__->belongs_to(
28
__PACKAGE__->belongs_to(
(-)a/t/db_dependent/AuthorisedValues.t (-2 / +129 lines)
Lines 1-7 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use Test::More tests => 15;
4
use Test::More tests => 16;
5
use Test::MockModule;
5
6
6
use t::lib::TestBuilder;
7
use t::lib::TestBuilder;
7
use t::lib::Mocks;
8
use t::lib::Mocks;
Lines 12-17 use Koha::AuthorisedValue; Link Here
12
use Koha::AuthorisedValues;
13
use Koha::AuthorisedValues;
13
use Koha::AuthorisedValueCategories;
14
use Koha::AuthorisedValueCategories;
14
use Koha::MarcSubfieldStructures;
15
use Koha::MarcSubfieldStructures;
16
use utf8;
15
17
16
my $schema  = Koha::Database->new->schema;
18
my $schema  = Koha::Database->new->schema;
17
$schema->storage->txn_begin;
19
$schema->storage->txn_begin;
Lines 229-232 subtest 'search_by_*_field + find_by_koha_field + get_description' => sub { Link Here
229
    };
231
    };
230
};
232
};
231
233
234
subtest "localization" => sub {
235
    plan tests => 14;
236
237
    Koha::AuthorisedValues->delete;
238
239
    my $category =
240
      Koha::AuthorisedValueCategory->new( { category_name => 'av_for_t' } );
241
    my $av1 = $builder->build_object(
242
        {
243
            class => 'Koha::AuthorisedValues',
244
            value => { category => $category->category_name, authorised_value => 'av1' }
245
        }
246
    );    # translated in es-ES and fr-FR
247
    my $av2 = $builder->build_object(
248
        {
249
            class => 'Koha::AuthorisedValues',
250
            value => { category => $category->category_name, authorised_value => 'av2' }
251
        }
252
    );    # translated in es-ES and de-DE
253
    my $av3 = $builder->build_object(
254
        {
255
            class => 'Koha::AuthorisedValues',
256
            value => { category => $category->category_name, authorised_value => 'av3' }
257
        }
258
    );    # no translation
259
    Koha::Localization->new(
260
        {
261
            entity => 'authorised_values',
262
            code => sprintf( "%s_%s", $av1->category, $av1->authorised_value ),
263
            lang => 'es-ES',
264
            interface => 'intranet',
265
            translation => 'traducción 1'
266
        }
267
    )->store;
268
    Koha::Localization->new(
269
        {
270
            entity => 'authorised_values',
271
            code => sprintf( "%s_%s", $av1->category, $av1->authorised_value ),
272
            lang => 'es-ES',
273
            interface => 'opac',
274
            translation => 'opac traducción 1'
275
        }
276
    )->store;
277
    Koha::Localization->new(
278
        {
279
            entity => 'authorised_values',
280
            code => sprintf( "%s_%s", $av1->category, $av2->authorised_value ),
281
            lang => 'es-ES',
282
            interface => 'intranet',
283
            translation => 'traducción 2'
284
        }
285
    )->store;
286
287
    Koha::Localization->new(
288
        {
289
            entity => 'authorised_values',
290
            code => sprintf( "%s_%s", $av1->category, $av1->authorised_value ),
291
            lang => 'fr-FR',
292
            interface => 'intranet',
293
            translation => 'traduction 1'
294
        }
295
    )->store;
296
    Koha::Localization->new(
297
        {
298
            entity => 'authorised_values',
299
            code => sprintf( "%s_%s", $av2->category, $av2->authorised_value ),
300
            lang => 'de-DE',
301
            interface => 'intranet',
302
            translation => 'Übersetzung 2'
303
        }
304
    )->store;
305
306
    my $c4_languages = Test::MockModule->new('C4::Languages');
307
    $c4_languages->mock('getlanguage', sub { 'es-ES' });
308
    my $c4_context = Test::MockModule->new('C4::Context');
309
    $c4_context->mock('interface', 'intranet');
310
311
    my $avs = Koha::AuthorisedValues->search_with_localization;
312
    is( $avs->count, 3);
313
314
    my $av1_loc = $avs->search( { authorised_value => $av1->authorised_value } )->next;
315
    is( $av1_loc->translated_description, 'traducción 1', 'av1 has a translated description fetched from search_with_localization' );
316
317
    my $av2_loc = $avs->search( { authorised_value => $av2->authorised_value } )->next;
318
    is( $av2_loc->translated_description, 'traducción 2', 'av2 has a translated description fetched from search_with_localization' );
319
320
    my $av3_loc = $avs->search( { authorised_value => $av3->authorised_value } )->next;
321
    is( $av3_loc->translated_description, $av3->lib, 'av3 does not have a translated description for intranet, lib is returned' );
322
323
    is( $av1_loc->opac_translated_description, 'opac traducción 1', 'opac_translated_description is not fetched from search_with localization, but accessor is available' );
324
325
    is( $av2_loc->opac_translated_description, $av2->lib_opac, 'av2 does not have a translated description for opac, lib_opac is returned' );
326
327
328
    $c4_context->mock('opac', 'intranet');
329
330
    $avs = Koha::AuthorisedValues->search_with_localization;
331
    is( $avs->count, 3);
332
333
    $av1_loc = $avs->search( { authorised_value => $av1->authorised_value } )->next;
334
    is( $av1_loc->opac_translated_description, 'opac traducción 1', 'opac_translated_description is fetched from search_with_localization' );
335
336
    $av2_loc = $avs->search( { authorised_value => $av2->authorised_value } )->next;
337
    is( $av2_loc->opac_translated_description, $av2->lib_opac, 'av2 does not have a translated description, lib_opac is returned' );
338
339
    $av3_loc = $avs->search( { authorised_value => $av3->authorised_value } )->next;
340
    is( $av3_loc->opac_translated_description, $av3->lib_opac, 'same than av2 for av3' );
341
342
    is( $av1_loc->translated_description, 'traducción 1', 'translated description for intranet is not fetched from search_with_localization but accessor is available' );
343
344
    is( $av2_loc->translated_description, 'traducción 2', 'translated description for intranet is not fetched from search_with_localization but accessor is available' );
345
346
    is( $av3_loc->translated_description, $av3->lib, 'av3 does not have a translated description, lib is returned' );
347
348
    my $translated_descriptions = $av1->translated_descriptions;
349
    is_deeply(
350
        $translated_descriptions,
351
        [
352
            { lang => "es-ES", translation => "traducción 1" },
353
            { lang => "fr-FR", translation => "traduction 1" },
354
            { lang => "es-ES", translation => "opac traducción 1" }
355
        ]
356
    , 'translated_descriptions return all the translations for a given av');
357
358
};
359
232
$schema->storage->txn_rollback;
360
$schema->storage->txn_rollback;
233
- 

Return to bug 20307