removing $dbh as a parameter in AuthoritiesMarc functions
[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 require Exporter;
23 use CGI;
24 use C4::Auth;
25
26 use C4::Context;
27 use C4::Auth;
28 use C4::Output;
29 use C4::Interface::CGI::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 ( sort { $authtypes->{$a} <=> $authtypes->{$b} }
47     keys %$authtypes )
48 {
49     my $selected = 1 if $thisauthtype eq $authtypecode;
50     my %row = (
51         value        => $thisauthtype,
52         selected     => $selected,
53         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
54     );
55     push @authtypesloop, \%row;
56 }
57
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 $orderby = $query->param('orderby');
64         my @value = $query->param('value');
65
66     $resultsperpage = $query->param('resultsperpage');
67     $resultsperpage = 19 if ( !defined $resultsperpage );
68     my @tags;
69     my ( $results, $total, @fields ) =
70       authoritysearch( \@marclist, \@and_or, \@excluding, \@operator,
71         \@value, $startfrom * $resultsperpage,
72         $resultsperpage, $authtypecode );
73     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
74         {
75             template_name   => "opac-authoritiessearchresultlist.tmpl",
76             query           => $query,
77             type            => 'opac',
78             authnotrequired => 1,
79             debug           => 1,
80         }
81     );
82
83     # multi page display gestion
84     my $displaynext = 0;
85     my $displayprev = $startfrom;
86     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
87         $displaynext = 1;
88     }
89
90     my @field_data = ();
91
92 # we must get parameters once again. Because if there is a mainentry, it has been replaced by something else during the search, thus the links next/previous would not work anymore
93     my @marclist_ini = $query->param('marclist');
94     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
95         push @field_data, { term => "marclist",  val => $marclist_ini[$i] };
96         push @field_data, { term => "and_or",    val => $and_or[$i] };
97         push @field_data, { term => "excluding", val => $excluding[$i] };
98         push @field_data, { term => "operator",  val => $operator[$i] };
99         push @field_data, { term => "value",     val => $value[$i] };
100     }
101
102     my @numbers = ();
103
104     if ( $total > $resultsperpage ) {
105         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
106             if ( $i < 16 ) {
107                 my $highlight = 0;
108                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
109                 push @numbers,
110                   {
111                     number     => $i,
112                     highlight  => $highlight,
113                     searchdata => \@field_data,
114                     startfrom  => ( $i - 1 )
115                   };
116             }
117         }
118     }
119
120     my $from = $startfrom * $resultsperpage + 1;
121     my $to;
122
123     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
124         $to = $total;
125     }
126     else {
127         $to = ( ( $startfrom + 1 ) * $resultsperpage );
128     }
129     $template->param( result => $results ) if $results;
130     $template->param( FIELDS => \@fields );
131     $template->param(
132         startfrom      => $startfrom,
133         displaynext    => $displaynext,
134         displayprev    => $displayprev,
135         resultsperpage => $resultsperpage,
136         startfromnext  => $startfrom + 1,
137         startfromprev  => $startfrom - 1,
138         searchdata     => \@field_data,
139         total          => $total,
140         from           => $from,
141         to             => $to,
142         numbers        => \@numbers,
143         authtypecode   => $authtypecode,
144         isEDITORS      => $authtypecode eq 'EDITORS',
145     );
146
147 }
148 elsif ( $op eq "delete" ) {
149
150     &AUTHdelauthority( $authid, 1 );
151
152     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
153         {
154             template_name   => "authorities/authorities-home.tmpl",
155             query           => $query,
156             type            => 'intranet',
157             authnotrequired => 0,
158             flagsrequired   => { catalogue => 1 },
159             debug           => 1,
160         }
161     );
162
163     #   $template->param("statements" => \@statements,
164     #                                           "nbstatements" => $nbstatements);
165 }
166 elsif ( $op eq "AddStatement" ) {
167
168     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
169         {
170             template_name   => "authorities/authorities-home.tmpl",
171             query           => $query,
172             type            => 'intranet',
173             authnotrequired => 0,
174             flagsrequired   => { catalogue => 1 },
175             debug           => 1,
176         }
177     );
178
179     # Gets the entered information
180     my @marcfields = $query->param('marclist');
181     my @and_or     = $query->param('and_or');
182     my @excluding  = $query->param('excluding');
183     my @operator   = $query->param('operator');
184     my @value      = $query->param('value');
185
186     my @statements = ();
187
188     # List of the marc tags to display
189     my $marcarray = create_marclist();
190
191     my $nbstatements = $query->param('nbstatements');
192     $nbstatements = 1 if ( !defined $nbstatements );
193
194     for ( my $i = 0 ; $i < $nbstatements ; $i++ ) {
195         my %fields = ();
196
197         # Recreates the old scrolling lists with the previously selected values
198         my $marclist = create_scrolling_list(
199             {
200                 name     => "marclist",
201                 values   => $marcarray,
202                 size     => 1,
203                 default  => $marcfields[$i],
204                 onChange => "sql_update()"
205             }
206         );
207
208         $fields{'marclist'} = $marclist;
209         $fields{'first'}    = 1 if ( $i == 0 );
210
211 # Restores the and/or parameters (no need to test the 'and' for activation because it's the default value)
212         $fields{'or'} = 1 if ( $and_or[$i] eq "or" );
213
214         #Restores the "not" parameters
215         $fields{'not'} = 1 if ( $excluding[$i] );
216
217         #Restores the operators (most common operators first);
218         if    ( $operator[$i] eq "=" )        { $fields{'eq'}       = 1; }
219         elsif ( $operator[$i] eq "contains" ) { $fields{'contains'} = 1; }
220         elsif ( $operator[$i] eq "start" )    { $fields{'start'}    = 1; }
221         elsif ( $operator[$i] eq ">" )  { $fields{'gt'} = 1; } #greater than
222         elsif ( $operator[$i] eq ">=" ) { $fields{'ge'} = 1; } #greater or equal
223         elsif ( $operator[$i] eq "<" )  { $fields{'lt'} = 1; } #lower than
224         elsif ( $operator[$i] eq "<=" ) { $fields{'le'} = 1; } #lower or equal
225
226         #Restores the value
227         $fields{'value'} = $value[$i];
228
229         push @statements, \%fields;
230     }
231     $nbstatements++;
232
233     # The new scrolling list
234     my $marclist = create_scrolling_list(
235         {
236             name     => "marclist",
237             values   => $marcarray,
238             size     => 1,
239             onChange => "sql_update()"
240         }
241     );
242     push @statements, { "marclist" => $marclist };
243
244     $template->param(
245         "statements"   => \@statements,
246         "nbstatements" => $nbstatements
247     );
248
249 }
250 else {
251     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
252         {
253             template_name   => "opac-authorities-home.tmpl",
254             query           => $query,
255             type            => 'opac',
256             authnotrequired => 1,
257             debug           => 1,
258         }
259     );
260
261 }
262
263 $template->param( authtypesloop => \@authtypesloop );
264
265 # Print the page
266 output_html_with_http_headers $query, $cookie, $template->output;
267
268 # Local Variables:
269 # tab-width: 4
270 # End: