Bug 19583: Remove use of C4::AuthoritiesMarc
[koha.git] / authorities / auth_finder.pl
1 #!/usr/bin/perl
2 # WARNING: 4-character tab stops here
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use strict;
22 use warnings;
23
24 use CGI qw ( -utf8 );
25 use C4::Output;
26 use C4::Auth;
27 use C4::Context;
28 use C4::Acquisition;
29 use C4::Koha;
30 use Koha::SearchEngine::Search;
31 use Koha::SearchEngine::QueryBuilder;
32
33 use Koha::Authority::Types;
34
35 my $query        = new CGI;
36 my $op           = $query->param('op') || '';
37 my $authtypecode = $query->param('authtypecode') || '';
38 my $index        = $query->param('index') || '';
39 my $tagid        = $query->param('tagid') || '';
40 my $source       = $query->param('source') || '';
41 my $relationship = $query->param('relationship') || '';
42
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44     {
45         template_name => ( $op eq 'do_search' )
46         ? 'authorities/searchresultlist-auth.tt'
47         : 'authorities/auth_finder.tt',
48         query           => $query,
49         type            => 'intranet',
50         authnotrequired => 0,
51         flagsrequired   => { catalogue => 1 },
52     }
53 );
54
55 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
56
57 # If search form posted
58 if ( $op eq "do_search" ) {
59     my @marclist  = $query->multi_param('marclist');
60     my @and_or    = $query->multi_param('and_or');
61     my @excluding = $query->multi_param('excluding');
62     my @operator  = $query->multi_param('operator');
63     my @value     = (
64         $query->param('value_mainstr') || undef,
65         $query->param('value_main')    || undef,
66         $query->param('value_match')   || undef,
67         $query->param('value_any')     || undef,
68     );
69     my $orderby        = $query->param('orderby')        || '';
70     my $startfrom      = $query->param('startfrom')      || 0;
71     my $resultsperpage = $query->param('resultsperpage') || 20;
72
73     my $builder = Koha::SearchEngine::QueryBuilder->new(
74         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
75     my $searcher = Koha::SearchEngine::Search->new(
76         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
77     my $search_query = $builder->build_authorities_query_compat(
78         \@marclist, \@and_or, \@excluding, \@operator,
79         \@value, $authtypecode, $orderby
80     );
81     my $offset = $startfrom * $resultsperpage;
82     my ( $results, $total ) =
83         $searcher->search_auth_compat( $search_query, $offset,
84         $resultsperpage );
85
86     # multi page display gestion
87     my $displaynext = 0;
88     my $displayprev = $startfrom;
89     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
90         $displaynext = 1;
91     }
92
93     my @field_data = ();
94
95 # get marclist again, as the previous one has been modified by catalogsearch (mainentry replaced by field name)
96     my @marclist_ini = $query->multi_param('marclist');
97     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
98         push @field_data, { term => "marclist",  val => $marclist_ini[$i] };
99         push @field_data, { term => "and_or",    val => $and_or[$i] };
100         push @field_data, { term => "excluding", val => $excluding[$i] };
101         push @field_data, { term => "operator",  val => $operator[$i] };
102     }
103
104     push @field_data,
105       { term => "value_mainstr", val => scalar $query->param('value_mainstr') || "" };
106     push @field_data,
107       { term => "value_main", val => scalar $query->param('value_main') || "" };
108     push @field_data,
109       { term => "value_any", val => scalar $query->param('value_any') || "" };
110     push @field_data,
111       { term => "value_match", val => scalar $query->param('value_match') || "" };
112
113     my @numbers = ();
114     if ( $total > $resultsperpage ) {
115         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
116             if ( $i < 16 ) {
117                 my $highlight = 0;
118                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
119                 push @numbers,
120                   {
121                     number     => $i,
122                     highlight  => $highlight,
123                     searchdata => \@field_data,
124                     startfrom  => ( $i - 1 )
125                   };
126             }
127         }
128     }
129
130     my $from = $startfrom * $resultsperpage + 1;
131     my $to;
132     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
133         $to = $total;
134     }
135     else {
136         $to = ( ( $startfrom + 1 ) * $resultsperpage );
137     }
138
139     $template->param( result => $results ) if $results;
140     $template->param(
141         orderby          => $orderby,
142         startfrom        => $startfrom,
143         displaynext      => $displaynext,
144         displayprev      => $displayprev,
145         resultsperpage   => $resultsperpage,
146         startfromnext    => $startfrom + 1,
147         startfromprev    => $startfrom - 1,
148         searchdata       => \@field_data,
149         total            => $total,
150         from             => $from,
151         to               => $to,
152         numbers          => \@numbers,
153         operator_mainstr => ( @operator > 0 && $operator[0] )
154         ? $operator[0]
155         : '',
156         operator_main  => ( @operator > 1 && $operator[1] ) ? $operator[1] : '',
157         operator_any   => ( @operator > 2 && $operator[2] ) ? $operator[2] : '',
158         operator_match => ( @operator > 3 && $operator[3] ) ? $operator[3] : '',
159     );
160 }
161 else {
162
163     # special case for UNIMARC field 210c builder
164     my $resultstring = $query->param('result') || '';
165     $template->param( resultstring => $resultstring, );
166 }
167
168 $template->param(
169     op            => $op,
170     value_mainstr => scalar $query->param('value_mainstr') || '',
171     value_main    => scalar $query->param('value_main') || '',
172     value_any     => scalar $query->param('value_any') || '',
173     value_match   => scalar $query->param('value_match') || '',
174     tagid         => $tagid,
175     index         => $index,
176     authority_types  => $authority_types,
177     authtypecode  => $authtypecode,
178     source        => $source,
179     relationship  => $relationship,
180 );
181
182 # Print the page
183 output_html_with_http_headers $query, $cookie, $template->output;
184
185 # Local Variables:
186 # tab-width: 4
187 # End: