Bug 19074: Fix category display in Batch patron modification.
[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
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.
14 #
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.
19 #
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>.
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 qw ( -utf8 );
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 @value      = $query->multi_param('term');
52     my $searchtype = $query->param('querytype');
53     my @marclist  = ($searchtype);
54     my $authtypecode = $query->param('authtypecode');
55     my @and_or    = $query->multi_param('and_or');
56     my @excluding = $query->multi_param('excluding');
57     my @operator  = $query->multi_param('operator');
58     my $orderby   = $query->param('orderby');
59
60     my $resultsperpage = 50;
61     my $startfrom = 0;
62
63     my ( $results, $total ) = SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator, \@value, $startfrom * $resultsperpage, $resultsperpage, $authtypecode, $orderby );
64
65     my %used_summaries; # hash to avoid duplicates
66     my @summaries;
67     foreach my $result (@$results) {
68         my $authorized = $result->{'summary'}->{'authorized'};
69         my $summary    = join(
70             ' ',
71             map {
72                 ( $searchtype eq 'mainmainentry' )
73                   ? $_->{'hemain'}
74                   : $_->{'heading'}
75               } @$authorized
76         );
77         $summary =~ s/^\s+//;
78         $summary =~ s/\s+$//;
79         $summary = nsb_clean($summary);
80         # test if already added ignoring case
81         unless ( exists $used_summaries{ lc($summary) } ) {
82             push @summaries, { 'summary' => $summary };
83             $used_summaries{ lc($summary) } = 1;
84         }
85     }
86
87 output_with_http_headers $query, undef, to_json(\@summaries, { utf8 => 1 }), 'json';