Merge remote-tracking branch 'origin/new/bug_7016'
[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 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 =head1 ysearch.pl
23
24 This script allows ajax call for dynamic authorities search
25 (used in auth_finder.pl)
26
27 =cut
28
29 use CGI;
30 use Modern::Perl;
31 use C4::Context;
32 use C4::Charset;
33 use C4::AuthoritiesMarc;
34 use C4::Auth qw/check_cookie_auth/;
35
36 my $query = new CGI;
37
38 binmode STDOUT, ':encoding(UTF-8)';
39 print $query->header( -type => 'text/plain', -charset => 'UTF-8' );
40
41 my ( $auth_status, $sessionID ) = check_cookie_auth( $query->cookie('CGISESSID'), { } );
42 if ( $auth_status ne "ok" ) {
43     exit 0;
44 }
45
46     my $searchstr = $query->param('query');
47     my $searchtype = $query->param('querytype');
48     my @value;
49     given ($searchtype) {
50         when (/^marclist$/)      { @value = (undef, undef, $searchstr); }
51         when (/^mainentry$/)     { @value = (undef, $searchstr, undef); }
52         when (/^mainmainentry$/) { @value = ($searchstr, undef, undef); }
53     }
54     my @marclist  = ($searchtype);
55     my $authtypecode = $query->param('authtypecode');
56     my @and_or    = $query->param('and_or');
57     my @excluding = $query->param('excluding');
58     my @operator  = $query->param('operator');
59     my $orderby   = $query->param('orderby');
60
61     my $resultsperpage = 50;
62     my $startfrom = 0;
63
64     my ( $results, $total ) = SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator, \@value, $startfrom * $resultsperpage, $resultsperpage, $authtypecode, $orderby );
65     foreach (@$results) {
66         my ($value) = $_->{'summary'};
67         # Removes new lines
68         $value =~ s/<br \/>/ /g;
69         $value =~ s/\n//g;
70         print nsb_clean($value) . "\n";
71     }