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