(bug #3004) fix the opac authority search
[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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22
23 use CGI;
24 use C4::Auth;
25
26 use C4::Context;
27 use C4::Auth;
28 use C4::Output;
29 use C4::AuthoritiesMarc;
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 $dbh          = C4::Context->dbh;
36
37 my $startfrom = $query->param('startfrom');
38 my $authid    = $query->param('authid');
39 $startfrom = 0 if ( !defined $startfrom );
40 my ( $template, $loggedinuser, $cookie );
41 my $resultsperpage;
42
43 my $authtypes = getauthtypes;
44 my @authtypesloop;
45 foreach my $thisauthtype ( sort { $authtypes->{$a}{'authtypetext'} cmp $authtypes->{$b}{'authtypetext'} }
46     keys %$authtypes )
47 {
48     my $selected = 1 if $thisauthtype eq $authtypecode;
49     my %row = (
50         value        => $thisauthtype,
51         selected     => $selected,
52         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
53     );
54     push @authtypesloop, \%row;
55 }
56
57 if ( $op eq "do_search" ) {
58     my @marclist = ($query->param('marclista'),$query->param('marclistb'),$query->param('marclistc'));
59     my @and_or = ($query->param('and_ora'),$query->param('and_orb'),$query->param('and_orc'));
60     my @excluding = ($query->param('excludinga'),$query->param('excludingb'),$query->param('excludingc'),);
61     my @operator = ($query->param('operatora'),$query->param('operatorb'),$query->param('operatorc'));
62     my $orderby = $query->param('orderby');
63     my @value = ($query->param('valuea'),$query->param('valueb'),$query->param('valuec'),);
64
65     $resultsperpage = $query->param('resultsperpage');
66     $resultsperpage = 20 if ( !defined $resultsperpage );
67     my @tags;
68     my ( $results, $total, @fields ) =
69       SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator,
70         \@value, $startfrom * $resultsperpage,
71         $resultsperpage, $authtypecode, $orderby );
72     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
73         {
74             template_name   => "opac-authoritiessearchresultlist.tmpl",
75             query           => $query,
76             type            => 'opac',
77             authnotrequired => 1,
78             debug           => 1,
79         }
80     );
81
82     # multi page display gestion
83     my $displaynext = 0;
84     my $displayprev = $startfrom;
85     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
86         $displaynext = 1;
87     }
88
89     my @field_data = ();
90
91     foreach my $letter (qw/a b c/){
92         push @field_data, { term => "marclist$letter" , val => $query->param("marclist$letter")};
93         push @field_data, { term => "and_or$letter" , val => $query->param("and_or$letter")};
94         push @field_data, { term => "excluding$letter" , val => $query->param("excluding$letter")};
95         push @field_data, { term => "operator$letter" , val => $query->param("operator$letter")};
96         push @field_data, { term => "value$letter" , val => $query->param("value$letter")};
97     }
98
99     my @numbers = ();
100
101     if ( $total > $resultsperpage ) {
102         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
103             if ( $i < 16 ) {
104                 my $highlight = 0;
105                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
106                 push @numbers,
107                   {
108                     number     => $i,
109                     highlight  => $highlight,
110                     searchdata => \@field_data,
111                     startfrom  => ( $i - 1 )
112                   };
113             }
114         }
115     }
116
117     my $from = $startfrom * $resultsperpage + 1;
118     my $to;
119
120     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
121         $to = $total;
122     }
123     else {
124         $to = ( ( $startfrom + 1 ) * $resultsperpage );
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         total          => $total,
138         from           => $from,
139         to             => $to,
140         numbers        => \@numbers,
141         authtypecode   => $authtypecode,
142         isEDITORS      => $authtypecode eq 'EDITORS',
143     );
144
145 }
146 else {
147     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
148         {
149             template_name   => "opac-authorities-home.tmpl",
150             query           => $query,
151             type            => 'opac',
152             authnotrequired => 1,
153             debug           => 1,
154         }
155     );
156
157 }
158
159 $template->param( authtypesloop => \@authtypesloop );
160
161 # Print the page
162 output_html_with_http_headers $query, $cookie, $template->output;
163
164 # Local Variables:
165 # tab-width: 4
166 # End: