Bug 36018: Prevent random failure from api/v1/acquisitions_orders.t
[koha.git] / cataloguing / z3950_auth_search.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2013 Prosentient Systems
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20
21 use CGI qw / -utf8 /;
22
23 use C4::Auth qw( get_template_and_user );
24 use C4::Output qw( output_html_with_http_headers );
25 use C4::Context;
26 use C4::Breeding qw( Z3950Search Z3950SearchAuth );
27 use MARC::Record;
28 use Koha::Authorities;
29 use Koha::Authority::Types;
30 use C4::AuthoritiesMarc qw( GetAuthority );
31
32 my $input        = CGI->new;
33 my $dbh          = C4::Context->dbh;
34 my $error         = $input->param('error');
35 my $authid  = $input->param('authid') || 0;
36 my $op            = $input->param('op')||'';
37
38 my $record         = GetAuthority($authid);
39 my $marc_flavour = C4::Context->preference('marcflavour');
40 my $authfields_mapping = {
41     'authorpersonal'   => $marc_flavour eq 'MARC21' ? '100' : '200',
42     'authorcorp'       => $marc_flavour eq 'MARC21' ? '110' : '210',
43     'authormeetingcon' => $marc_flavour eq 'MARC21' ? '111' : '210',
44     'uniformtitle'     => $marc_flavour eq 'MARC21' ? '130' : '230',
45     'subject'          => $marc_flavour eq 'MARC21' ? '150' : '250',
46 };
47
48 my $nameany          = $input->param('nameany');
49 my $authorany        = $input->param('authorany');
50 my $title            = $input->param('title');
51 my $authorpersonal   = $input->param('authorpersonal');
52 my $authormeetingcon = $input->param('authormeetingcon');
53 my $uniformtitle     = $input->param('uniformtitle');
54 my $subject          = $input->param('subject');
55 my $authorcorp       = $input->param('authorcorp');
56 my $subjectsubdiv    = $input->param('subjectsubdiv');
57 my $srchany          = $input->param('srchany');
58
59 # If replacing an existing record we want to initially populate the form with record info,
60 # however, we want to use entered inputs when searching
61 if ( $record && $op ne 'cud-do_search' ) {
62     $authorcorp ||=
63       $record->subfield( $authfields_mapping->{'authorcorp'}, 'a' );
64     $authorpersonal ||=
65       $record->subfield( $authfields_mapping->{'authorpersonal'}, 'a' );
66     $authormeetingcon ||=
67       $record->subfield( $authfields_mapping->{'authormeetingcon'}, 'a' );
68     $uniformtitle ||=
69       $record->subfield( $authfields_mapping->{'uniformtitle'}, 'a' );
70     $subject ||= $record->subfield( $authfields_mapping->{'subject'}, 'a' );
71 }
72
73 my $page            = $input->param('current_page') || 1;
74 my $index =$input->param('index');
75 $page = $input->param('goto_page') if $input->param('changepage_goto');
76 my $controlnumber    = $input->param('controlnumber');
77
78 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
79         template_name   => "cataloguing/z3950_auth_search.tt",
80         query           => $input,
81         type            => "intranet",
82         flagsrequired   => { catalogue => 1 },
83 });
84
85 $template->param(
86     nameany    => $nameany,
87     authorany    => $authorany,
88     authorcorp    => $authorcorp,
89     authorpersonal    => $authorpersonal,
90     authormeetingcon    => $authormeetingcon,
91     title        => $title,
92     uniformtitle      => $uniformtitle,
93     subject      => $subject,
94     subjectsubdiv   => $subjectsubdiv,
95     srchany      => $srchany,
96     authid => $authid,
97     controlnumber => $controlnumber,
98     index => $index,
99 );
100
101 if ( $op ne "cud-do_search" ) {
102     my $sth = $dbh->prepare("SELECT id,host,servername,checked FROM z3950servers WHERE recordtype = 'authority' ORDER BY `rank`, servername");
103     $sth->execute();
104     my $serverloop = $sth->fetchall_arrayref( {} );
105     $template->param(
106         serverloop   => $serverloop,
107         opsearch     => "cud-search",
108         index        => $index,
109     );
110     output_html_with_http_headers $input, $cookie, $template->output;
111     exit;
112 }
113
114 my @id = $input->multi_param('id');
115 if ( @id==0 ) {
116         # empty server list -> report and exit
117         $template->param( emptyserverlist => 1 );
118         output_html_with_http_headers $input, $cookie, $template->output;
119         exit;
120 }
121
122 my $pars= {
123         page => $page,
124         id => \@id,
125         nameany => $nameany,
126         authorany => $authorany,
127         authorcorp => $authorcorp,
128         authorpersonal => $authorpersonal,
129         authormeetingcon => $authormeetingcon,
130         title => $title,
131         uniformtitle => $uniformtitle,
132         subject => $subject,
133         subjectsubdiv => $subjectsubdiv,
134         srchany => $srchany,
135         authid => $authid,
136         controlnumber => $controlnumber,
137 };
138 Z3950SearchAuth($pars, $template);
139 output_html_with_http_headers $input, $cookie, $template->output;