synch'ing 2.2 and head
[wip/koha-chris_n.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 $startfrom=$query->param('startfrom');
42 my $authid=$query->param('authid');
43 $startfrom=0 if(!defined $startfrom);
44 my ($template, $loggedinuser, $cookie);
45 my $resultsperpage;
46
47 my $authtypes = getauthtypes;
48 my @authtypesloop;
49 foreach my $thisauthtype (sort { $authtypes->{$a} <=> $authtypes->{$b} } keys %$authtypes) {
50         my $selected = 1 if $thisauthtype eq $authtypecode;
51         my %row =(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 @value = $query->param('value');
64
65         $resultsperpage= $query->param('resultsperpage');
66         $resultsperpage = 19 if(!defined $resultsperpage);
67         my @tags;
68         my ($results,$total) = authoritysearch($dbh, \@marclist,\@and_or,
69                                                                                 \@excluding, \@operator, \@value,
70                                                                                 $startfrom*$resultsperpage, $resultsperpage,$authtypecode);
71         ($template, $loggedinuser, $cookie)
72                 = get_template_and_user({template_name => "authorities/searchresultlist.tmpl",
73                                 query => $query,
74                                 type => 'intranet',
75                                 authnotrequired => 0,
76                                 flagsrequired => {borrowers => 1},
77                                 flagsrequired => {catalogue => 1},
78                                 debug => 1,
79                                 });
80
81         # multi page display gestion
82         my $displaynext=0;
83         my $displayprev=$startfrom;
84         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
85                 $displaynext = 1;
86         }
87
88         my @field_data = ();
89
90         # 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 
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         my @numbers = ();
102
103         if ($total>$resultsperpage)
104         {
105                 for (my $i=1; $i<$total/$resultsperpage+1; $i++)
106                 {
107                         if ($i<16)
108                         {
109                         my $highlight=0;
110                         ($startfrom==($i-1)) && ($highlight=1);
111                         push @numbers, { number => $i,
112                                         highlight => $highlight ,
113                                         searchdata=> \@field_data,
114                                         startfrom => ($i-1)};
115                         }
116         }
117         }
118
119         my $from = $startfrom*$resultsperpage+1;
120         my $to;
121
122         if($total < (($startfrom+1)*$resultsperpage))
123         {
124                 $to = $total;
125         } else {
126                 $to = (($startfrom+1)*$resultsperpage);
127         }
128         $template->param(result => $results) if $results;
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                                                         );
143
144 } elsif ($op eq "delete") {
145
146         &AUTHdelauthority($dbh,$authid, 1);
147
148         ($template, $loggedinuser, $cookie)
149                 = get_template_and_user({template_name => "authorities/authorities-home.tmpl",
150                                 query => $query,
151                                 type => 'intranet',
152                                 authnotrequired => 0,
153                                 flagsrequired => {catalogue => 1},
154                                 debug => 1,
155                                 });
156 #       $template->param("statements" => \@statements,
157 #                                               "nbstatements" => $nbstatements);
158 }
159 elsif ($op eq "AddStatement") {
160
161         ($template, $loggedinuser, $cookie)
162                 = get_template_and_user({template_name => "authorities/authorities-home.tmpl",
163                                 query => $query,
164                                 type => 'intranet',
165                                 authnotrequired => 0,
166                                 flagsrequired => {catalogue => 1},
167                                 debug => 1,
168                                 });
169
170         # Gets the entered information
171         my @marcfields = $query->param('marclist');
172         my @and_or = $query->param('and_or');
173         my @excluding = $query->param('excluding');
174         my @operator = $query->param('operator');
175         my @value = $query->param('value');
176
177         my @statements = ();
178
179         # List of the marc tags to display
180         my $marcarray = create_marclist();
181
182         my $nbstatements = $query->param('nbstatements');
183         $nbstatements = 1 if(!defined $nbstatements);
184
185         for(my $i = 0 ; $i < $nbstatements ; $i++)
186         {
187                 my %fields = ();
188
189                 # Recreates the old scrolling lists with the previously selected values
190                 my $marclist = create_scrolling_list({name=>"marclist",
191                                         values=> $marcarray,
192                                         size=> 1,
193                                         default=>$marcfields[$i],
194                                         onChange => "sql_update()"}
195                                         );
196
197                 $fields{'marclist'} = $marclist;
198                 $fields{'first'} = 1 if($i == 0);
199
200                 # Restores the and/or parameters (no need to test the 'and' for activation because it's the default value)
201                 $fields{'or'} = 1 if($and_or[$i] eq "or");
202
203                 #Restores the "not" parameters
204                 $fields{'not'} = 1 if($excluding[$i]);
205
206                 #Restores the operators (most common operators first);
207                 if($operator[$i] eq "=") { $fields{'eq'} = 1; }
208                 elsif($operator[$i] eq "contains") { $fields{'contains'} = 1; }
209                 elsif($operator[$i] eq "start") { $fields{'start'} = 1; }
210                 elsif($operator[$i] eq ">") { $fields{'gt'} = 1; }      #greater than
211                 elsif($operator[$i] eq ">=") { $fields{'ge'} = 1; } #greater or equal
212                 elsif($operator[$i] eq "<") { $fields{'lt'} = 1; } #lower than
213                 elsif($operator[$i] eq "<=") { $fields{'le'} = 1; } #lower or equal
214
215                 #Restores the value
216                 $fields{'value'} = $value[$i];
217
218                 push @statements, \%fields;
219         }
220         $nbstatements++;
221
222         # The new scrolling list
223         my $marclist = create_scrolling_list({name=>"marclist",
224                                 values=> $marcarray,
225                                 size=>1,
226                                 onChange => "sql_update()"});
227         push @statements, {"marclist" => $marclist };
228
229         $template->param("statements" => \@statements,
230                                                 "nbstatements" => $nbstatements);
231
232 }
233 else {
234         ($template, $loggedinuser, $cookie)
235                 = get_template_and_user({template_name => "authorities/authorities-home.tmpl",
236                                 query => $query,
237                                 type => 'intranet',
238                                 authnotrequired => 0,
239                                 flagsrequired => {catalogue => 1},
240                                 debug => 1,
241                                 });
242
243 }
244
245 $template->param(authtypesloop => \@authtypesloop);
246
247 # Print the page
248 output_html_with_http_headers $query, $cookie, $template->output;
249
250 # Local Variables:
251 # tab-width: 4
252 # End: