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

(-)a/Koha/SearchEngine/Elasticsearch/Search.pm (-1 / +7 lines)
Lines 183-189 sub search_compat { Link Here
183
    my %result;
183
    my %result;
184
    $result{biblioserver}{hits} = $hits->{'total'};
184
    $result{biblioserver}{hits} = $hits->{'total'};
185
    $result{biblioserver}{RECORDS} = \@records;
185
    $result{biblioserver}{RECORDS} = \@records;
186
    return (undef, \%result, $self->_convert_facets($results->{aggregations}));
186
187
    my $facets = $self->_convert_facets( $results->{aggregations} );
188
    if ( C4::Context->interface eq 'opac' ) {
189
        my $rules = C4::Context->yaml_preference('OpacHiddenItems');
190
        $facets = Koha::SearchEngine::Search->post_filter_opac_facets( { facets => $facets, rules => $rules } );
191
    }
192
    return (undef, \%result, $facets);
187
}
193
}
188
194
189
=head2 search_auth_compat
195
=head2 search_auth_compat
(-)a/Koha/SearchEngine/Search.pm (+50 lines)
Lines 179-182 sub pagination_bar { Link Here
179
179
180
}
180
}
181
181
182
=head2 post_filter_opac_facets
183
184
    if ( C4::Context->interface eq 'opac' ) {
185
        my $rules = C4::Context->yaml_preference('OpacHiddenItems');
186
        $facets = Koha::SearchEngine::Search->post_filter_facets({ facets => $facets, rules => $rules });
187
    }
188
189
=cut
190
191
sub post_filter_opac_facets {
192
    my ( $class, $args ) = @_;
193
    my $facets = $args->{facets};
194
    my $rules  = $args->{rules};
195
    if ( $rules && $facets ) {
196
197
        #Build a hashmap of the rules to provide efficient lookups
198
        my $rule_map = {};
199
        my @keys     = keys %$rules;
200
        foreach my $key (@keys) {
201
            $rule_map->{$key} = {};
202
            foreach my $rule_value ( @{ $rules->{$key} } ) {
203
                $rule_map->{$key}->{$rule_value} = 1;
204
            }
205
        }
206
207
        #Iterate through the facets
208
        foreach my $facet_type (@$facets) {
209
            my $type_link_value  = $facet_type->{type_link_value};
210
            my $rule_map_by_type = $rule_map->{$type_link_value};
211
212
            #If the facet type has a rule map, process the facets
213
            if ($rule_map_by_type) {
214
215
                #Filter out any facet which has a value in the rule map
216
                my @filtered_facets = ();
217
                foreach my $facet ( @{ $facet_type->{facets} } ) {
218
                    my $facet_link_value = $facet->{facet_link_value};
219
                    if ($facet_link_value) {
220
                        if ( !$rule_map_by_type->{$facet_link_value} ) {
221
                            push( @filtered_facets, $facet );
222
                        }
223
                    }
224
                }
225
                $facet_type->{facets} = \@filtered_facets;
226
            }
227
        }
228
    }
229
    return $facets;
230
}
231
182
1;
232
1;
(-)a/Koha/SearchEngine/Zebra/Search.pm (-2 / +7 lines)
Lines 60-68 This passes straight through to C4::Search::getRecords. Link Here
60
=cut
60
=cut
61
61
62
sub search_compat {
62
sub search_compat {
63
    shift; # get rid of $self
63
    shift;    # get rid of $self
64
64
65
    return C4::Search::getRecords(@_);
65
    my ( $error, $results_hashref, $facets ) = C4::Search::getRecords(@_);
66
    if ( C4::Context->interface eq 'opac' ) {
67
        my $rules = C4::Context->yaml_preference('OpacHiddenItems');
68
        $facets = Koha::SearchEngine::Search->post_filter_opac_facets( { facets => $facets, rules => $rules } );
69
    }
70
    return ( $error, $results_hashref, $facets );
66
}
71
}
67
72
68
=head2 simple_search_compat
73
=head2 simple_search_compat
(-)a/t/Koha/SearchEngine/Search.t (-2 / +74 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 1;
20
use Test::More tests => 2;
21
use Test::Exception;
21
use Test::Exception;
22
22
23
use t::lib::Mocks;
23
use t::lib::Mocks;
Lines 85-87 subtest "pagination_bar tests" => sub { Link Here
85
    is( @$PAGE_NUMBERS, 20, "If past first ten pages we show 20 pages" );
85
    is( @$PAGE_NUMBERS, 20, "If past first ten pages we show 20 pages" );
86
86
87
};
87
};
88
- 
88
89
subtest "post_filter_opac_facets" => sub {
90
    plan tests => 4;
91
92
    my $facets = _get_mock_facet_data();
93
    my $rules  = { 'itype' => ['MP'] };
94
95
    my $filtered_facets = Koha::SearchEngine::Search->post_filter_opac_facets( { facets => $facets, rules => $rules } );
96
    is( scalar @$filtered_facets,                                 2,    'Facet type length the same' );
97
    is( scalar @{ $filtered_facets->[0]->{facets} },              3,    'author facet length the same' );
98
    is( scalar @{ $filtered_facets->[1]->{facets} },              1,    'itype facet has been filtered' );
99
    is( $filtered_facets->[1]->{facets}->[0]->{facet_link_value}, 'BK', 'correct itype facet has been filtered' );
100
};
101
102
sub _get_mock_facet_data {
103
    my $facets = [
104
        {
105
            'type_label_Authors' => 1,
106
            'facets'             => [
107
                {
108
                    'facet_link_value'  => 'Farley, David',
109
                    'type_link_value'   => 'author',
110
                    'facet_title_value' => 'Farley, David',
111
                    'facet_count'       => 1,
112
                    'facet_label_value' => 'Farley, David'
113
                },
114
                {
115
                    'facet_label_value' => 'Humble, Jez',
116
                    'facet_count'       => 1,
117
                    'facet_title_value' => 'Humble, Jez',
118
                    'type_link_value'   => 'author',
119
                    'facet_link_value'  => 'Humble, Jez'
120
                },
121
                {
122
                    'facet_count'       => 1,
123
                    'facet_title_value' => 'Martin, Robert C.',
124
                    'facet_label_value' => 'Martin, Robert C.',
125
                    'type_link_value'   => 'author',
126
                    'facet_link_value'  => 'Martin, Robert C.'
127
                }
128
            ],
129
            'av_cat'          => '',
130
            'order'           => 1,
131
            'label'           => 'Authors',
132
            'type_id'         => 'author_id',
133
            'type_link_value' => 'author'
134
        },
135
        {
136
            'type_label_Item types' => 1,
137
            'facets'                => [
138
                {
139
                    'type_link_value'   => 'itype',
140
                    'facet_link_value'  => 'BK',
141
                    'facet_count'       => 4,
142
                    'facet_title_value' => 'BK',
143
                    'facet_label_value' => 'Books'
144
                },
145
                {
146
                    'facet_title_value' => 'MP',
147
                    'facet_count'       => 1,
148
                    'facet_label_value' => 'Maps',
149
                    'type_link_value'   => 'itype',
150
                    'facet_link_value'  => 'MP'
151
                }
152
            ],
153
            'order'           => 2,
154
            'av_cat'          => undef,
155
            'type_id'         => 'itype_id',
156
            'type_link_value' => 'itype',
157
            'label'           => 'Item types'
158
        }
159
    ];
160
}

Return to bug 14007