Revert Bug 11081 - Port Koha::Contrib::Tamil indexer into Koha code base
[koha.git] / opac / opac-authorities-home.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::Auth;
26
27 use C4::Context;
28 use C4::Auth;
29 use C4::Output;
30 use C4::AuthoritiesMarc;
31 use C4::Koha;    # XXX subfield_is_koha_internal_p
32 use C4::Search::History;
33
34 use Koha::Authority::Types;
35
36 my $query        = new CGI;
37 my $op           = $query->param('op') || '';
38 my $authtypecode = $query->param('authtypecode') || '';
39 my $dbh          = C4::Context->dbh;
40
41 my $startfrom = $query->param('startfrom');
42 my $authid    = $query->param('authid');
43 $startfrom = 0 if ( !defined $startfrom );
44 my ( $template, $loggedinuser, $cookie );
45 my $resultsperpage;
46
47 my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypetext']});
48
49 if ( $op eq "do_search" ) {
50     my @marclist = ($query->param('marclist'));
51     my @and_or = ($query->param('and_or'));
52     my @excluding = ($query->param('excluding'),);
53     my @operator = ($query->param('operator'));
54     my $orderby = $query->param('orderby');
55     my @value = ($query->param('value') || "",);
56
57     $resultsperpage = $query->param('resultsperpage');
58     $resultsperpage = 20 if ( !defined $resultsperpage );
59     my @tags;
60     my ( $results, $total, @fields ) =
61       SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator,
62         \@value, $startfrom * $resultsperpage,
63         $resultsperpage, $authtypecode, $orderby );
64     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
65         {
66             template_name   => "opac-authoritiessearchresultlist.tt",
67             query           => $query,
68             type            => 'opac',
69             authnotrequired => 1,
70             debug           => 1,
71         }
72     );
73
74     # multi page display gestion
75     my $displaynext = 0;
76     my $displayprev = $startfrom;
77     $total ||= 0;
78     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
79         $displaynext = 1;
80     }
81
82     my @field_data = (
83         { term => "marclist",  val => $marclist[0] },
84         { term => "and_or",    val => $and_or[0] },
85         { term => "excluding", val => $excluding[0] },
86         { term => "operator",  val => $operator[0] },
87         { term => "value",     val => $value[0] },
88     );
89
90     my @numbers = ();
91
92     if ( $total > $resultsperpage ) {
93         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
94             if ( $i < 16 ) {
95                 my $highlight = 0;
96                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
97                 push @numbers,
98                   {
99                     number     => $i,
100                     highlight  => $highlight,
101                     searchdata => \@field_data,
102                     startfrom  => ( $i - 1 )
103                   };
104             }
105         }
106     }
107
108     my $from = $startfrom * $resultsperpage + 1;
109     my $to;
110
111     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
112         $to = $total;
113     }
114     else {
115         $to = ( ( $startfrom + 1 ) * $resultsperpage );
116     }
117     unless (C4::Context->preference('OPACShowUnusedAuthorities')) {
118         my @usedauths = grep { $_->{used} > 0 } @$results;
119         $results = \@usedauths;
120     }
121
122     # Opac search history
123     if (C4::Context->preference('EnableOpacSearchHistory')) {
124         unless ( $startfrom ) {
125             my $path_info = $query->url(-path_info=>1);
126             my $query_cgi_history = $query->url(-query=>1);
127             $query_cgi_history =~ s/^$path_info\?//;
128             $query_cgi_history =~ s/;/&/g;
129
130             unless ( $loggedinuser ) {
131                 my $new_search = C4::Search::History::add_to_session({
132                         cgi => $query,
133                         query_desc => $value[0],
134                         query_cgi => $query_cgi_history,
135                         total => $total,
136                         type => "authority",
137                 });
138             } else {
139                 # To the session (the user is logged in)
140                 C4::Search::History::add({
141                     userid => $loggedinuser,
142                     sessionid => $query->cookie("CGISESSID"),
143                     query_desc => $value[0],
144                     query_cgi => $query_cgi_history,
145                     total => $total,
146                     type => "authority",
147                 });
148             }
149         }
150     }
151
152     $template->param( result => $results ) if $results;
153     $template->param( FIELDS => \@fields );
154     $template->param( orderby => $orderby );
155     $template->param(
156         startfrom      => $startfrom,
157         displaynext    => $displaynext,
158         displayprev    => $displayprev,
159         resultsperpage => $resultsperpage,
160         startfromnext  => $startfrom + 1,
161         startfromprev  => $startfrom - 1,
162         searchdata     => \@field_data,
163         countfuzzy     => !(C4::Context->preference('OPACShowUnusedAuthorities')),
164         total          => $total,
165         from           => $from,
166         to             => $to,
167         resultcount    => scalar @$results,
168         numbers        => \@numbers,
169         authtypecode   => $authtypecode,
170         authtypetext   => $authority_types->find($authtypecode)->authtypetext,
171         isEDITORS      => $authtypecode eq 'EDITORS',
172     );
173
174 }
175 else {
176     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
177         {
178             template_name   => "opac-authorities-home.tt",
179             query           => $query,
180             type            => 'opac',
181             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
182             debug           => 1,
183         }
184     );
185
186 }
187
188 $template->param(
189     authority_types => $authority_types,
190     authtypecode    => $authtypecode,
191 );
192
193 # Print the page
194 output_html_with_http_headers $query, $cookie, $template->output;
195
196 # Local Variables:
197 # tab-width: 4
198 # End: