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

(-)a/C4/Serials.pm (-2 / +11 lines)
Lines 40-45 use C4::Context; Link Here
40
use C4::Log qw( logaction );    # logaction
40
use C4::Log qw( logaction );    # logaction
41
use C4::Serials::Frequency qw( GetSubscriptionFrequency );
41
use C4::Serials::Frequency qw( GetSubscriptionFrequency );
42
use C4::Serials::Numberpattern;
42
use C4::Serials::Numberpattern;
43
use Koha::AdditionalFields;
43
use Koha::AdditionalFieldValues;
44
use Koha::AdditionalFieldValues;
44
use Koha::Biblios;
45
use Koha::Biblios;
45
use Koha::DateUtils qw( dt_from_string );
46
use Koha::DateUtils qw( dt_from_string );
Lines 637-649 sub SearchSubscriptions { Link Here
637
    if ( $params->{results_limit} && $total_results > $params->{results_limit} ) {
638
    if ( $params->{results_limit} && $total_results > $params->{results_limit} ) {
638
        $results = [ splice( @{$results}, 0, $params->{results_limit} ) ];
639
        $results = [ splice( @{$results}, 0, $params->{results_limit} ) ];
639
    }
640
    }
641
    my %additional_fields_by_id = map { $_->id => $_->name } Koha::AdditionalFields->search( { tablename => 'subscription' } )->as_list;
640
642
641
    for my $subscription ( @$results ) {
643
    for my $subscription ( @$results ) {
642
        $subscription->{cannotedit} = not can_edit_subscription( $subscription );
644
        $subscription->{cannotedit} = not can_edit_subscription( $subscription );
643
        $subscription->{cannotdisplay} = not can_show_subscription( $subscription );
645
        $subscription->{cannotdisplay} = not can_show_subscription( $subscription );
646
        my @additional_field_values = Koha::AdditionalFieldValues->search(
647
            {
648
                field_id => { -in => [ keys %additional_fields_by_id ] },
649
                record_id => $subscription->{subscriptionid}
650
            }
651
        )->as_list;
644
652
645
        my $subscription_object = Koha::Subscriptions->find($subscription->{subscriptionid});
653
        $subscription->{additional_fields} = {
646
        $subscription->{additional_field_values} = $subscription_object->get_additional_field_values_for_template;
654
            map { $additional_fields_by_id{$_->field_id} => $_->value } @additional_field_values
655
        };
647
    }
656
    }
648
657
649
    return wantarray ? @{$results} : { results => $results, total => $total_results };
658
    return wantarray ? @{$results} : { results => $results, total => $total_results };
(-)a/Koha/AuthorisedValues.pm (-22 / +50 lines)
Lines 93-99 sub find_by_koha_field { Link Here
93
            distinct => 1,
93
            distinct => 1,
94
        }
94
        }
95
    );
95
    );
96
    return $av->count ? $av->next : undef;
96
    return $av->next;
97
}
97
}
98
98
99
sub get_description_by_koha_field {
99
sub get_description_by_koha_field {
Lines 106-122 sub get_description_by_koha_field { Link Here
106
106
107
    my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
107
    my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
108
    my $cache_key    = "AV_descriptions:$frameworkcode:$kohafield:$authorised_value";
108
    my $cache_key    = "AV_descriptions:$frameworkcode:$kohafield:$authorised_value";
109
    my $cached       = $memory_cache->get_from_cache($cache_key);
109
    my $description = $memory_cache->get_from_cache($cache_key);
110
    return $cached if $cached;
111
110
112
    my $av = $self->find_by_koha_field($params);
111
    unless (defined $description) {
113
    if ( ! defined $av ){
112
        my $av = $self->find_by_koha_field($params);
114
        $memory_cache->set_in_cache( $cache_key, {} );
113
        $description = defined $av ? { lib => $av->lib, opac_description => $av->opac_description } : {};
115
        return {};
114
        $memory_cache->set_in_cache( $cache_key, $description );
116
    }
115
    }
117
    my $descriptions = { lib => $av->lib, opac_description => $av->opac_description };
116
    return $description;
118
    $memory_cache->set_in_cache( $cache_key, $descriptions );
119
    return $descriptions;
120
}
117
}
121
118
122
sub get_descriptions_by_koha_field {
119
sub get_descriptions_by_koha_field {
Lines 126-144 sub get_descriptions_by_koha_field { Link Here
126
123
127
    my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
124
    my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
128
    my $cache_key    = "AV_descriptions:$frameworkcode:$kohafield";
125
    my $cache_key    = "AV_descriptions:$frameworkcode:$kohafield";
129
    my $cached       = $memory_cache->get_from_cache($cache_key);
126
    my $descriptions = $memory_cache->get_from_cache($cache_key);
130
    return @$cached if $cached;
127
128
    unless (defined $descriptions) {
129
        my @avs = $self->search_by_koha_field($params)->as_list;
130
        $descriptions = [
131
            map {
132
                {
133
                    authorised_value => $_->authorised_value,
134
                    lib              => $_->lib,
135
                    opac_description => $_->opac_description
136
                }
137
            } @avs
138
        ];
139
        $memory_cache->set_in_cache( $cache_key, $descriptions );
140
    }
141
    return @{$descriptions};
142
}
131
143
132
    my @avs          = $self->search_by_koha_field($params)->as_list;
144
sub get_description_by_category_and_authorised_value {
133
    my @descriptions = map {
145
    my ( $self, $params ) = @_;
134
        {
146
    return unless defined $params->{category} && defined $params->{authorised_value};
135
            authorised_value => $_->authorised_value,
147
136
            lib              => $_->lib,
148
    my $category = $params->{category};
137
            opac_description => $_->opac_description
149
    my $value = $params->{authorised_value};
138
        }
150
139
    } @avs;
151
    my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
140
    $memory_cache->set_in_cache( $cache_key, \@descriptions );
152
    my $cache_key = "AV_description_by_category_and_authorised_value:$category:$value";
141
    return @descriptions;
153
    my $description = $memory_cache->get_from_cache($cache_key);
154
155
    unless (defined $description) {
156
        my $av = $self->search(
157
            {
158
                category => $category,
159
                authorised_value => $value
160
            }
161
        )->next;
162
        $description = defined $av ? {
163
            lib => $av->lib,
164
            opac_description => $av->opac_description
165
        } : {};
166
        $memory_cache->set_in_cache( $cache_key, $description );
167
    }
168
169
    return $description;
142
}
170
}
143
171
144
=head3 get_descriptions_by_marc_field
172
=head3 get_descriptions_by_marc_field
(-)a/Koha/Template/Plugin/AuthorisedValues.pm (-8 / +15 lines)
Lines 26-42 use Koha::AuthorisedValues; Link Here
26
26
27
sub GetByCode {
27
sub GetByCode {
28
    my ( $self, $category, $code, $opac ) = @_;
28
    my ( $self, $category, $code, $opac ) = @_;
29
    my $av = Koha::AuthorisedValues->search({ category => $category, authorised_value => $code });
29
    my $av = Koha::AuthorisedValues->get_description_by_category_and_authorised_value(
30
    return $av->count
30
        {
31
            ? $opac
31
            category => $category,
32
                ? $av->next->opac_description
32
            authorised_value => $code
33
                : $av->next->lib
33
        }
34
            : $code;
34
    );
35
36
    my $description;
37
    if ($av) {
38
        $description = $opac ? $av->{opac_description} : $av->{lib};
39
    }
40
41
    return $description || $code;
35
}
42
}
36
43
37
sub Get {
44
sub Get {
38
    my ( $self, $category, $selected, $opac ) = @_;
45
    my ( $self, $category, $opac ) = @_;
39
    return GetAuthorisedValues( $category, $selected, $opac );
46
    return GetAuthorisedValues( $category, $opac );
40
}
47
}
41
48
42
sub GetAuthValueDropbox {
49
sub GetAuthValueDropbox {
(-)a/t/db_dependent/AuthorisedValues.t (-7 / +1 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 => 17;
4
use Test::More tests => 16;
5
use Test::Exception;
5
use Test::Exception;
6
use Try::Tiny;
6
use Try::Tiny;
7
7
Lines 139-149 is( @$limits, 2, 'library_limits functions correctly both as setter and getter' Link Here
139
139
140
my @categories = Koha::AuthorisedValues->new->categories;
140
my @categories = Koha::AuthorisedValues->new->categories;
141
is( @categories, @existing_categories+3, 'There should have 3 categories inserted' );
141
is( @categories, @existing_categories+3, 'There should have 3 categories inserted' );
142
is_deeply(
143
    \@categories,
144
    [ sort { uc $a cmp uc $b } @categories ],
145
    'categories must be ordered by category names'
146
);
147
142
148
subtest 'search_by_*_field + find_by_koha_field + get_description + authorised_values' => sub {
143
subtest 'search_by_*_field + find_by_koha_field + get_description + authorised_values' => sub {
149
    plan tests => 7;
144
    plan tests => 7;
150
- 

Return to bug 31856