Merge remote-tracking branch 'origin/new/bug_8204'
[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 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
33 my $query        = new CGI;
34 my $op           = $query->param('op') || '';
35 my $authtypecode = $query->param('authtypecode') || '';
36 my $dbh          = C4::Context->dbh;
37
38 my $startfrom = $query->param('startfrom');
39 my $authid    = $query->param('authid');
40 $startfrom = 0 if ( !defined $startfrom );
41 my ( $template, $loggedinuser, $cookie );
42 my $resultsperpage;
43
44 my $authtypes     = getauthtypes();
45 my @authtypesloop = ();
46 foreach my $thisauthtype (
47     sort {
48         $authtypes->{$a}->{'authtypetext'}
49           cmp $authtypes->{$b}->{'authtypetext'}
50     }
51     keys %{$authtypes}
52   ) {
53     push @authtypesloop,
54       { value        => $thisauthtype,
55         selected     => $thisauthtype eq $authtypecode,
56         authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
57       };
58 }
59
60 if ( $op eq "do_search" ) {
61     my @marclist = ($query->param('marclist'));
62     my @and_or = ($query->param('and_or'));
63     my @excluding = ($query->param('excluding'),);
64     my @operator = ($query->param('operator'));
65     my $orderby = $query->param('orderby');
66     my @value = ($query->param('value') || "",);
67
68     $resultsperpage = $query->param('resultsperpage');
69     $resultsperpage = 20 if ( !defined $resultsperpage );
70     my @tags;
71     my ( $results, $total, @fields ) =
72       SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator,
73         \@value, $startfrom * $resultsperpage,
74         $resultsperpage, $authtypecode, $orderby );
75     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
76         {
77             template_name   => "opac-authoritiessearchresultlist.tmpl",
78             query           => $query,
79             type            => 'opac',
80             authnotrequired => 1,
81             debug           => 1,
82         }
83     );
84
85     # multi page display gestion
86     my $displaynext = 0;
87     my $displayprev = $startfrom;
88     $total ||= 0;
89     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
90         $displaynext = 1;
91     }
92
93     my @field_data = ( { term => "marclist" , val => $query->param("marclist") || ''} );
94
95     my @numbers = ();
96
97     if ( $total > $resultsperpage ) {
98         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
99             if ( $i < 16 ) {
100                 my $highlight = 0;
101                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
102                 push @numbers,
103                   {
104                     number     => $i,
105                     highlight  => $highlight,
106                     searchdata => \@field_data,
107                     startfrom  => ( $i - 1 )
108                   };
109             }
110         }
111     }
112
113     my $from = $startfrom * $resultsperpage + 1;
114     my $to;
115
116     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
117         $to = $total;
118     }
119     else {
120         $to = ( ( $startfrom + 1 ) * $resultsperpage );
121     }
122     unless (C4::Context->preference('OPACShowUnusedAuthorities')) {
123         my @usedauths = grep { $_->{used} > 0 } @$results;
124         $results = \@usedauths;
125     }
126     $template->param( result => $results ) if $results;
127     $template->param( FIELDS => \@fields );
128     $template->param( orderby => $orderby );
129     $template->param(
130         startfrom      => $startfrom,
131         displaynext    => $displaynext,
132         displayprev    => $displayprev,
133         resultsperpage => $resultsperpage,
134         startfromnext  => $startfrom + 1,
135         startfromprev  => $startfrom - 1,
136         searchdata     => \@field_data,
137         countfuzzy     => !(C4::Context->preference('OPACShowUnusedAuthorities')),
138         total          => $total,
139         from           => $from,
140         to             => $to,
141         resultcount    => scalar @$results,
142         numbers        => \@numbers,
143         authtypecode   => $authtypecode,
144         authtypetext   => $authtypes->{$authtypecode}{'authtypetext'},
145         isEDITORS      => $authtypecode eq 'EDITORS',
146     );
147
148 }
149 else {
150     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
151         {
152             template_name   => "opac-authorities-home.tmpl",
153             query           => $query,
154             type            => 'opac',
155             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
156             debug           => 1,
157         }
158     );
159
160 }
161
162 $template->param( authtypesloop => \@authtypesloop );
163
164 # Print the page
165 output_html_with_http_headers $query, $cookie, $template->output;
166
167 # Local Variables:
168 # tab-width: 4
169 # End: