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