Lines 34-41
if ($session->param("key") eq "autocomplete") {
Link Here
|
34 |
if ($length >= 1) { |
34 |
if ($length >= 1) { |
35 |
my $res = $browser->autocomplete_idx($ses, \@prefix, $session->param("analyzer"), $session->param("token_counter")); |
35 |
my $res = $browser->autocomplete_idx($ses, \@prefix, $session->param("analyzer"), $session->param("token_counter")); |
36 |
|
36 |
|
37 |
filterAutocomplete($res); |
|
|
38 |
|
39 |
print $cgi->header("application/json;charset=UTF-8"); |
37 |
print $cgi->header("application/json;charset=UTF-8"); |
40 |
print to_json($res, {utf8 => 1}); |
38 |
print to_json($res, {utf8 => 1}); |
41 |
} |
39 |
} |
Lines 47-130
if ($session->param("key") eq "autocomplete") {
Link Here
|
47 |
response404JSON(); |
45 |
response404JSON(); |
48 |
} |
46 |
} |
49 |
|
47 |
|
50 |
sub filterAutocomplete { |
|
|
51 |
if (C4::Context->preference('OpacSuppression') || C4::Context->yaml_preference('OpacHiddenItems')) { |
52 |
my $res = shift; |
53 |
my @prefix = $res->{ "prefix" }; |
54 |
@prefix = split(',', $prefix[0]); |
55 |
|
56 |
for (my $i = 0; $i < scalar @prefix; $i++) { |
57 |
my $hits = $res->{ $i }->{ 'hits' }; |
58 |
my $hitlist = $hits->{ "hits" }; |
59 |
if (@{$hitlist}) { |
60 |
# Remove item inside hits in elasticsearch response if the item has |
61 |
# marc field 942$n set to true and OpacSuppression preference on |
62 |
if (C4::Context->preference('OpacSuppression')) { |
63 |
for ( my $i = 0; $i < scalar @{$hitlist}; $i++ ) { |
64 |
my $biblio = Koha::Biblios->find($hitlist->[$i]->{ "_id" }); |
65 |
my $record = $biblio->metadata->record; |
66 |
my $opacsuppressionfield = '942'; |
67 |
my $opacsuppressionfieldvalue = $record->field($opacsuppressionfield); |
68 |
if ( $opacsuppressionfieldvalue && |
69 |
$opacsuppressionfieldvalue->subfield("n") && |
70 |
$opacsuppressionfieldvalue->subfield("n") == 1) { |
71 |
# if OPAC suppression by IP address |
72 |
if (C4::Context->preference('OpacSuppressionByIPRange')) { |
73 |
my $IPAddress = $ENV{'REMOTE_ADDR'}; |
74 |
my $IPRange = C4::Context->preference('OpacSuppressionByIPRange'); |
75 |
if ($IPAddress !~ /^$IPRange/) { |
76 |
splice(@{$hitlist}, $i, 1); |
77 |
$i--; |
78 |
$hits->{ "total" }--; |
79 |
} |
80 |
} else { |
81 |
splice(@{$hitlist}, $i, 1); |
82 |
$i--; |
83 |
$hits->{ "total" }--; |
84 |
} |
85 |
} |
86 |
} |
87 |
} |
88 |
# Remove item inside hits in elasticsearch response if the item is |
89 |
# declared hidden in OPACHiddenItems preference |
90 |
if (C4::Context->yaml_preference('OpacHiddenItems')) { |
91 |
my @biblionumbers; |
92 |
foreach (@{$hitlist}) { |
93 |
push(@biblionumbers, $_->{ "_id" }); |
94 |
} |
95 |
my $autocomplete_items = Koha::Items->search({ |
96 |
biblionumber => { -in => \@biblionumbers } |
97 |
}); |
98 |
my $filtered_items = $autocomplete_items->filter_by_visible_in_opac({ |
99 |
patron => undef |
100 |
}); |
101 |
for ( my $i = 0; $i < scalar @{$hitlist}; $i++ ) { |
102 |
my $item = $filtered_items->find({ |
103 |
biblionumber => $hitlist->[$i]->{ "_id" } |
104 |
}); |
105 |
if (!$item) { |
106 |
splice(@{$hitlist}, $i, 1); |
107 |
$i--; |
108 |
$hits->{ "total" }--; |
109 |
} |
110 |
} |
111 |
} |
112 |
# Adjust the max_score inside hits in elasticsearch response |
113 |
my $maxscore = 0; |
114 |
foreach ( @{$hitlist} ) { |
115 |
my $score = $_->{"_score"}; |
116 |
$maxscore = $score if ($maxscore < $score); |
117 |
} |
118 |
if ($maxscore == 0) { |
119 |
$hits->{ "max_score" } = undef; |
120 |
} else { |
121 |
$hits->{ "max_score" } = $maxscore; |
122 |
} |
123 |
} |
124 |
} |
125 |
} |
126 |
} |
127 |
|
128 |
sub response404JSON { |
48 |
sub response404JSON { |
129 |
my $res = CGI->new; |
49 |
my $res = CGI->new; |
130 |
my $json = JSON->new->utf8; |
50 |
my $json = JSON->new->utf8; |
131 |
- |
|
|