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