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