Bug 26233: (bug 25553 follow-up) Make date columns sortable on the edit items table
[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 Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use C4::Output;
25 use C4::Auth;
26 use C4::Context;
27 use C4::Acquisition;
28 use C4::Koha;
29 use Koha::SearchEngine::Search;
30 use Koha::SearchEngine::QueryBuilder;
31
32 use Koha::Authority::Types;
33
34 my $query        = new CGI;
35 my $op           = $query->param('op') || '';
36 my $authtypecode = $query->param('authtypecode') || '';
37 my $index        = $query->param('index') || '';
38 my $tagid        = $query->param('tagid') || '';
39 my $source       = $query->param('source') || '';
40 my $relationship = $query->param('relationship') || '';
41
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43     {
44         template_name => ( $op eq 'do_search' )
45         ? 'authorities/searchresultlist-auth.tt'
46         : 'authorities/auth_finder.tt',
47         query           => $query,
48         type            => 'intranet',
49         flagsrequired   => { catalogue => 1 },
50     }
51 );
52
53 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
54
55 # If search form posted
56 if ( $op eq "do_search" ) {
57     my @marclist  = $query->multi_param('marclist');
58     my @and_or    = $query->multi_param('and_or');
59     my @excluding = $query->multi_param('excluding');
60     my @operator  = $query->multi_param('operator');
61     my @value     = (
62         $query->param('value_mainstr') || undef,
63         $query->param('value_main')    || undef,
64         $query->param('value_match')   || undef,
65         $query->param('value_any')     || undef,
66     );
67     my $orderby        = $query->param('orderby')        || '';
68     my $startfrom      = $query->param('startfrom')      || 0;
69     my $resultsperpage = $query->param('resultsperpage') || 20;
70
71     my $builder = Koha::SearchEngine::QueryBuilder->new(
72         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
73     my $searcher = Koha::SearchEngine::Search->new(
74         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
75     my $search_query = $builder->build_authorities_query_compat(
76         \@marclist, \@and_or, \@excluding, \@operator,
77         \@value, $authtypecode, $orderby
78     );
79     my $offset = $startfrom * $resultsperpage;
80     my ( $results, $total ) =
81         $searcher->search_auth_compat( $search_query, $offset,
82         $resultsperpage );
83
84     # multi page display gestion
85     my $displaynext = 0;
86     my $displayprev = $startfrom;
87     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
88         $displaynext = 1;
89     }
90
91     my @field_data = ();
92
93 # get marclist again, as the previous one has been modified by catalogsearch (mainentry replaced by field name)
94     my @marclist_ini = $query->multi_param('marclist');
95     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
96         push @field_data, { term => "marclist",  val => $marclist_ini[$i] };
97         push @field_data, { term => "and_or",    val => $and_or[$i] };
98         push @field_data, { term => "excluding", val => $excluding[$i] };
99         push @field_data, { term => "operator",  val => $operator[$i] };
100     }
101
102     push @field_data,
103       { term => "value_mainstr", val => scalar $query->param('value_mainstr') || "" };
104     push @field_data,
105       { term => "value_main", val => scalar $query->param('value_main') || "" };
106     push @field_data,
107       { term => "value_match", val => scalar $query->param('value_match') || "" };
108     push @field_data,
109       { term => "value_any", val => scalar $query->param('value_any') || "" };
110
111     my @numbers = ();
112     if ( $total > $resultsperpage ) {
113         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
114             if ( $i < 16 ) {
115                 my $highlight = 0;
116                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
117                 push @numbers,
118                   {
119                     number     => $i,
120                     highlight  => $highlight,
121                     searchdata => \@field_data,
122                     startfrom  => ( $i - 1 )
123                   };
124             }
125         }
126     }
127
128     my $from = $startfrom * $resultsperpage + 1;
129     my $to;
130     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
131         $to = $total;
132     }
133     else {
134         $to = ( ( $startfrom + 1 ) * $resultsperpage );
135     }
136
137     $template->param( result => $results ) if $results;
138     $template->param(
139         orderby          => $orderby,
140         startfrom        => $startfrom,
141         displaynext      => $displaynext,
142         displayprev      => $displayprev,
143         resultsperpage   => $resultsperpage,
144         startfromnext    => $startfrom + 1,
145         startfromprev    => $startfrom - 1,
146         searchdata       => \@field_data,
147         total            => $total,
148         from             => $from,
149         to               => $to,
150         numbers          => \@numbers,
151         operator_mainstr => ( @operator > 0 && $operator[0] ) ? $operator[0] : '',
152         operator_main    => ( @operator > 1 && $operator[1] ) ? $operator[1] : '',
153         operator_match   => ( @operator > 2 && $operator[2] ) ? $operator[2] : '',
154         operator_any     => ( @operator > 3 && $operator[3] ) ? $operator[3] : '',
155     );
156 }
157 else {
158
159     # special case for UNIMARC field 210c builder
160     my $resultstring = $query->param('result') || '';
161     $template->param( resultstring => $resultstring, );
162 }
163
164 $template->param(
165     op            => $op,
166     value_mainstr => scalar $query->param('value_mainstr') || '',
167     value_main    => scalar $query->param('value_main') || '',
168     value_any     => scalar $query->param('value_any') || '',
169     value_match   => scalar $query->param('value_match') || '',
170     tagid         => $tagid,
171     index         => $index,
172     authority_types  => $authority_types,
173     authtypecode  => $authtypecode,
174     source        => $source,
175     relationship  => $relationship,
176 );
177
178 # Print the page
179 output_html_with_http_headers $query, $cookie, $template->output;
180
181 # Local Variables:
182 # tab-width: 4
183 # End: