Lines 14-72
use C4::Biblio;
Link Here
|
14 |
use Koha::Biblios; |
14 |
use Koha::Biblios; |
15 |
|
15 |
|
16 |
my $browser = Koha::SearchEngine::Elasticsearch::Browse->new( { index => 'biblios' } ); |
16 |
my $browser = Koha::SearchEngine::Elasticsearch::Browse->new( { index => 'biblios' } ); |
17 |
my $cgi = CGI->new; |
17 |
my $cgi = CGI->new; |
18 |
my $session = CGI::Session->load() or die CGI::Session->errstr(); |
18 |
my $session = CGI::Session->load() or die CGI::Session->errstr(); |
19 |
|
19 |
|
20 |
$session->param(-name=>'analyzer', -value=>"autocomplete"); |
20 |
$session->param( -name => 'analyzer', -value => "autocomplete" ); |
21 |
$session->param(-name=>'prefix', -value=>$cgi->multi_param("prefix")); |
21 |
$session->param( -name => 'prefix', -value => $cgi->multi_param("prefix") ); |
22 |
$session->param(-name=>'q', -value=>$cgi->multi_param("q")); |
22 |
$session->param( -name => 'q', -value => $cgi->multi_param("q") ); |
23 |
$session->param(-name=>'key', -value=>$cgi->multi_param("key")); |
23 |
$session->param( -name => 'key', -value => $cgi->multi_param("key") ); |
24 |
$session->param(-name=>'token_counter', -value=>$cgi->multi_param("token_counter")); |
24 |
$session->param( -name => 'token_counter', -value => $cgi->multi_param("token_counter") ); |
25 |
$session->expire('+1h'); |
25 |
$session->expire('+1h'); |
26 |
|
26 |
|
27 |
if ($session->param("key") eq "autocomplete") { |
27 |
if ( $session->param("key") eq "autocomplete" ) { |
28 |
my @prefix = split /,/, $session->param("prefix"); |
28 |
my @prefix = split /,/, $session->param("prefix"); |
29 |
#fields for autocomplete |
|
|
30 |
my $length = scalar @prefix; |
31 |
my $ses = NFKD( $session->param("q") ); |
32 |
$ses =~ s/\p{NonspacingMark}//g; |
33 |
|
29 |
|
34 |
if ($length >= 1) { |
30 |
#fields for autocomplete |
35 |
my $res = $browser->autocomplete_idx($ses, \@prefix, $session->param("analyzer"), $session->param("token_counter")); |
31 |
my $length = scalar @prefix; |
|
|
32 |
my $ses = NFKD( $session->param("q") ); |
33 |
$ses =~ s/\p{NonspacingMark}//g; |
36 |
|
34 |
|
37 |
print $cgi->header("application/json;charset=UTF-8"); |
35 |
if ( $length >= 1 ) { |
38 |
print to_json($res, {utf8 => 1}); |
36 |
my $res = |
39 |
} |
37 |
$browser->autocomplete_idx( $ses, \@prefix, $session->param("analyzer"), $session->param("token_counter") ); |
40 |
#no prefix 404 |
38 |
|
41 |
else { |
39 |
print $cgi->header("application/json;charset=UTF-8"); |
|
|
40 |
print to_json( $res, { utf8 => 1 } ); |
41 |
} |
42 |
|
43 |
#no prefix 404 |
44 |
else { |
45 |
response404JSON(); |
46 |
} |
47 |
} elsif ( $session->param("key") eq "nb_notice" ) { |
48 |
my $ses = NFKD( $session->param("q") ); |
49 |
$ses =~ s/\p{NonspacingMark}//g; |
50 |
my $res = $browser->count_record($ses); |
51 |
print $cgi->header("application/json"); |
52 |
print to_json($res); |
53 |
} else { |
42 |
response404JSON(); |
54 |
response404JSON(); |
43 |
} |
|
|
44 |
} |
45 |
elsif ($session->param("key") eq "nb_notice") { |
46 |
my $ses = NFKD( $session->param("q") ); |
47 |
$ses =~ s/\p{NonspacingMark}//g; |
48 |
my $res = $browser->count_record($ses); |
49 |
print $cgi->header("application/json"); |
50 |
print to_json($res); |
51 |
} |
52 |
else { |
53 |
response404JSON(); |
54 |
} |
55 |
} |
55 |
|
56 |
|
56 |
sub response404JSON { |
57 |
sub response404JSON { |
57 |
my $res = CGI->new; |
58 |
my $res = CGI->new; |
58 |
my $json = JSON->new->utf8; |
59 |
my $json = JSON->new->utf8; |
59 |
my $header_type = "application/json;charset=UTF-8"; |
60 |
my $header_type = "application/json;charset=UTF-8"; |
60 |
my $header_status = "404"; |
61 |
my $header_status = "404"; |
61 |
my $output = $json->encode({ |
62 |
my $output = $json->encode( |
62 |
"error" => "No data", |
63 |
{ |
63 |
"description" => "Bad request", |
64 |
"error" => "No data", |
64 |
}); |
65 |
"description" => "Bad request", |
65 |
print $res->header( |
66 |
} |
66 |
-type => $header_type, |
67 |
); |
67 |
-charset => "utf-8", |
68 |
print $res->header( |
68 |
-status => $header_status |
69 |
-type => $header_type, |
69 |
); |
70 |
-charset => "utf-8", |
70 |
print $output; |
71 |
-status => $header_status |
71 |
print "\n"; |
72 |
); |
|
|
73 |
print $output; |
74 |
print "\n"; |
72 |
} |
75 |
} |
73 |
- |
|
|