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