Enh 6165: Add OPACResultsSidebar system preference
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23
24 use CGI;
25 use C4::Output;
26 use C4::Auth;
27 use C4::Context;
28 use C4::AuthoritiesMarc;
29 use C4::Acquisition;
30 use C4::Koha;    # XXX subfield_is_koha_internal_p
31
32 my $query        = new CGI;
33 my $op           = $query->param('op');
34 my $authtypecode = $query->param('authtypecode');
35 my $index        = $query->param('index');
36 my $tagid        = $query->param('tagid');
37 my $resultstring = $query->param('result');
38 my $dbh          = C4::Context->dbh;
39
40 my $startfrom = $query->param('startfrom');
41 $startfrom = 0 if ( !defined $startfrom );
42 my ( $template, $loggedinuser, $cookie );
43 my $resultsperpage;
44
45 my $authtypes = getauthtypes;
46 my @authtypesloop;
47 foreach my $thisauthtype ( keys %$authtypes ) {
48     my %row = (
49         value        => $thisauthtype,
50         selected     => ($thisauthtype eq $authtypecode),
51         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
52         index        => $index,
53     );
54     push @authtypesloop, \%row;
55 }
56
57 $op ||= q{};
58 if ( $op eq "do_search" ) {
59     my @marclist  = $query->param('marclist');
60     my @and_or    = $query->param('and_or');
61     my @excluding = $query->param('excluding');
62     my @operator  = $query->param('operator');
63     my @value     = ($query->param('value_mainstr')||undef, $query->param('value_main')||undef, $query->param('value_any')||undef);
64     my $orderby   = $query->param('orderby');
65
66     $resultsperpage = $query->param('resultsperpage');
67     $resultsperpage = 20 if ( !defined $resultsperpage );
68
69     my ( $results, $total ) =
70       SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator, \@value,
71         $startfrom * $resultsperpage,
72         $resultsperpage, $authtypecode, $orderby);
73
74     # If an authority heading is repeated, add an arrayref to those repetions
75     # First heading -- Second heading
76     for my $heading ( @$results ) {
77         my @repets = split / -- /, $heading->{summary};
78         if ( @repets > 1 ) {
79             my @repets_loop;
80             for (my $i = 0; $i < @repets; $i++) {
81                 push @repets_loop,
82                     { index => $index, repet => $i+1, value => $repets[$i] };
83             }
84             $heading->{repets} = \@repets_loop;
85         }
86     }
87     # multi page display gestion
88     my $displaynext = 0;
89     my $displayprev = $startfrom;
90     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
91         $displaynext = 1;
92     }
93
94     my @field_data = ();
95
96     my @marclist_ini =
97       $query->param('marclist')
98       ; # get marclist again, as the previous one has been modified by catalogsearch (mainentry replaced by field name
99     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
100         push @field_data, { term => "marclist",  val => $marclist_ini[$i] };
101         push @field_data, { term => "and_or",    val => $and_or[$i] };
102         push @field_data, { term => "excluding", val => $excluding[$i] };
103         push @field_data, { term => "operator",  val => $operator[$i] };
104     }
105
106     push @field_data, { term => "value_mainstr", val => $query->param('value_mainstr') || "" };
107     push @field_data, { term => "value_main",    val => $query->param('value_main')    || "" };
108     push @field_data, { term => "value_any",     val => $query->param('value_any')     || ""};
109
110     my @numbers = ();
111
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
131     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
132         $to = $total;
133     }
134     else {
135         $to = ( ( $startfrom + 1 ) * $resultsperpage );
136     }
137     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
138         {
139             template_name   => "authorities/searchresultlist-auth.tmpl",
140             query           => $query,
141             type            => 'intranet',
142             authnotrequired => 0,
143             flagsrequired   => { catalogue => 1 },
144         }
145     );
146
147     $template->param( result => $results ) if $results;
148     $template->param(
149         orderby      => $orderby,
150         startfrom      => $startfrom,
151     displaynext    => $displaynext,
152     displayprev    => $displayprev,
153     resultsperpage => $resultsperpage,
154     startfromnext  => $startfrom + 1,
155     startfromprev  => $startfrom - 1,
156         searchdata     => \@field_data,
157         total          => $total,
158         from           => $from,
159         to             => $to,
160         numbers        => \@numbers,
161         authtypecode   => $authtypecode,
162         value_mainstr  => $query->param('value_mainstr') || "", 
163         value_main     => $query->param('value_main') || "",
164         value_any      => $query->param('value_any') || "",
165     );
166 } else {
167     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
168         {
169             template_name   => "authorities/auth_finder.tmpl",
170             query           => $query,
171             type            => 'intranet',
172             authnotrequired => 0,
173             flagsrequired   => { catalogue => 1 },
174         }
175     );
176
177     $template->param(
178         resultstring => $resultstring,
179     );
180 }
181
182 $template->param(
183     value_mainstr => $query->param('value_mainstr') || "", 
184     value_main    => $query->param('value_main') || "",
185     value_any     => $query->param('value_any') || "",
186     tagid         => $tagid,
187     index         => $index,
188     authtypesloop => \@authtypesloop,
189     authtypecode  => $authtypecode,
190     value_mainstr  => $query->param('value_mainstr') || "", 
191     value_main     => $query->param('value_main')    || "",
192     value_any      => $query->param('value_any')     || "",
193 );
194
195 # Print the page
196 output_html_with_http_headers $query, $cookie, $template->output;
197
198 # Local Variables:
199 # tab-width: 4
200 # End: