Bug 19014: Unit tests
[koha.git] / authorities / ysearch.pl
blob9861f877e669fff429cd544d451853b43f7d1fd6
1 #!/usr/bin/perl
3 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
5 # Copyright 2011 BibLibre
6 # Parts copyright 2012 Athens County Public Libraries
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 =head1 ysearch.pl
25 This script allows ajax call for dynamic authorities search
26 (used in auth_finder.pl)
28 =cut
30 use CGI qw ( -utf8 );
31 use Modern::Perl;
32 use JSON;
34 use C4::Context;
35 use C4::Charset;
36 use C4::Auth qw/check_cookie_auth/;
37 use C4::Output;
38 use Koha::SearchEngine::Search;
39 use Koha::SearchEngine::QueryBuilder;
41 my $query = new CGI;
43 my ( $auth_status, $sessionID ) = check_cookie_auth( $query->cookie('CGISESSID'), { catalogue => 1 } );
45 if ( $auth_status ne "ok" ) {
46 # send empty response
47 my $reply = CGI->new("");
48 print $reply->header(-type => 'text/html');
49 exit 0;
52 my @value = $query->multi_param('term');
53 my $searchtype = $query->param('querytype');
54 my @marclist = ($searchtype);
55 my $authtypecode = $query->param('authtypecode');
56 my @and_or = $query->multi_param('and_or');
57 my @excluding = $query->multi_param('excluding');
58 my @operator = $query->multi_param('operator');
59 my $orderby = $query->param('orderby');
61 my $resultsperpage = 50;
62 my $startfrom = 0;
64 my $builder = Koha::SearchEngine::QueryBuilder->new(
65 { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
66 my $searcher = Koha::SearchEngine::Search->new(
67 { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
68 my $search_query = $builder->build_authorities_query_compat(
69 \@marclist, \@and_or, \@excluding, \@operator,
70 \@value, $authtypecode, $orderby
72 my $offset = $startfrom * $resultsperpage;
73 my ( $results, $total ) =
74 $searcher->search_auth_compat( $search_query, $offset,
75 $resultsperpage );
77 my %used_summaries; # hash to avoid duplicates
78 my @summaries;
79 foreach my $result (@$results) {
80 my $authorized = $result->{'summary'}->{'authorized'};
81 my $summary = join(
82 ' ',
83 map {
84 ( $searchtype eq 'mainmainentry' )
85 ? $_->{'hemain'}
86 : $_->{'heading'}
87 } @$authorized
89 $summary =~ s/^\s+//;
90 $summary =~ s/\s+$//;
91 $summary = nsb_clean($summary);
92 # test if already added ignoring case
93 unless ( exists $used_summaries{ lc($summary) } ) {
94 push @summaries, { 'summary' => $summary };
95 $used_summaries{ lc($summary) } = 1;
99 output_with_http_headers $query, undef, to_json(\@summaries, { utf8 => 1 }), 'json';