Preliminary fix of the CGI.pm problem of always assuming that everything is
[wip/koha-chris_n.git] / admin / marc_subfields_structure.pl
1 #!/usr/bin/perl
2
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 use C4::Output;
23 use C4::Charset;
24 use C4::Auth;
25 use CGI;
26 use C4::Search;
27 use C4::Context;
28 use HTML::Template;
29
30 sub StringSearch  {
31         my ($env,$searchstring,$type)=@_;
32         my $dbh = C4::Context->dbh;
33         $searchstring=~ s/\'/\\\'/g;
34         my @data=split(' ',$searchstring);
35         my $count=@data;
36         my $query="Select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,thesaurus_category,value_builder from marc_subfield_structure where (tagfield like \"$searchstring%\") order by tagfield";
37         my $sth=$dbh->prepare($query);
38         $sth->execute;
39         my @results;
40         my $cnt=0;
41         while (my $data=$sth->fetchrow_hashref){
42         push(@results,$data);
43         $cnt ++;
44         }
45         #  $sth->execute;
46         $sth->finish;
47         $dbh->disconnect;
48         return ($cnt,\@results);
49 }
50
51 my $input = new CGI;
52 my $tagfield=$input->param('tagfield');
53 my $tagsubfield=$input->param('tagsubfield');
54 my $pkfield="tagfield";
55 my $reqsel="select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,thesaurus_category,value_builder from marc_subfield_structure where tagfield='$tagfield' and tagsubfield='$tagsubfield'";
56 my $reqdel="delete from marc_subfield_structure where tagfield='$tagfield' and tagsubfield='$tagsubfield'";
57 my $offset=$input->param('offset');
58 my $script_name="/cgi-bin/koha/admin/marc_subfields_structure.pl";
59
60 my ($template, $borrowernumber, $cookie)
61     = get_template_and_user({template_name => "parameters/marc_subfields_structure.tmpl",
62                              query => $input,
63                              type => "intranet",
64                              authnotrequired => 0,
65                              flagsrequired => {parameters => 1},
66                              debug => 1,
67                              });
68 my $pagesize=30;
69 my $op = $input->param('op');
70 $tagfield=~ s/\,//g;
71
72 if ($op) {
73 $template->param(script_name => $script_name,
74                                                 tagfield =>$tagfield,
75                                                 $op              => 1); # we show only the TMPL_VAR names $op
76 } else {
77 $template->param(script_name => $script_name,
78                                                 tagfield =>$tagfield,
79                                                 else              => 1); # we show only the TMPL_VAR names $op
80 }
81
82 ################## ADD_FORM ##################################
83 # called by default. Used to create form to add or  modify a record
84 if ($op eq 'add_form') {
85         my $data;
86         my $dbh = C4::Context->dbh;
87         my $more_subfields = $input->param("more_subfields")+1;
88         # builds kohafield tables
89         my @kohafields;
90         push @kohafields, "";
91         my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
92         $sth2->execute;
93         while ((my $field) = $sth2->fetchrow_array) {
94                 push @kohafields, "biblio.".$field;
95         }
96         my $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
97         $sth2->execute;
98         while ((my $field) = $sth2->fetchrow_array) {
99                 push @kohafields, "biblioitems.".$field;
100         }
101         my $sth2=$dbh->prepare("SHOW COLUMNS from items");
102         $sth2->execute;
103         while ((my $field) = $sth2->fetchrow_array) {
104                 push @kohafields, "items.".$field;
105         }
106         # build authorised value list
107         $sth2->finish;
108         $sth2 = $dbh->prepare("select distinct category from authorised_values");
109         $sth2->execute;
110         my @authorised_values;
111         push @authorised_values,"";
112         while ((my $category) = $sth2->fetchrow_array) {
113                 push @authorised_values, $category;
114         }
115         push (@authorised_values,"branches");
116         push (@authorised_values,"itemtypes");
117         # build thesaurus categories list
118         $sth2->finish;
119         $sth2 = $dbh->prepare("select distinct category from bibliothesaurus");
120         $sth2->execute;
121         my @thesaurus_category;
122         push @thesaurus_category,"";
123         while ((my $category) = $sth2->fetchrow_array) {
124                 push @thesaurus_category, $category;
125         }
126         # build value_builder list
127         my @value_builder=('');
128         opendir(DIR, "../value_builder") || die "can't opendir ../value_builder: $!";
129         while (my $line = readdir(DIR)) {
130                 if ($line =~ /\.pl$/) {
131                         push (@value_builder,$line);
132                 }
133         }
134         closedir DIR;
135
136         # build values list
137         my $sth=$dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,thesaurus_category,value_builder from marc_subfield_structure where tagfield='$tagfield'"); # and tagsubfield='$tagsubfield'");
138         $sth->execute;
139         my @loop_data = ();
140         my $toggle="white";
141         my $i=0;
142         while ($data =$sth->fetchrow_hashref) {
143                 my %row_data;  # get a fresh hash for the row data
144                 if ($toggle eq 'white'){
145                         $toggle="#ffffcc";
146                 } else {
147                         $toggle="white";
148                 }
149                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
150                                         -values=>['-1','0','1','2','3','4','5','6','7','8','9','10'],
151                                         -labels => {'-1' =>'ignore','0'=>'0','1'=>'1',
152                                                                         '2' =>'2','3'=>'3','4'=>'4',
153                                                                         '5' =>'5','6'=>'6','7'=>'7',
154                                                                         '8' =>'8','9'=>'9','10'=>'items (10)',
155                                                                         },
156                                         -default=>$data->{'tab'},
157                                         -size=>1,
158                                         -multiple=>0,
159                                         );
160                 $row_data{tagsubfield} =$data->{'tagsubfield'}."<input type='hidden' name='tagsubfield' value='".$data->{'tagsubfield'}."'>";
161                 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
162                 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
163                 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
164                                         -values=> \@kohafields,
165                                         -default=> "$data->{'kohafield'}",
166                                         -size=>1,
167                                         -multiple=>0,
168                                         );
169                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
170                                         -values=> \@authorised_values,
171                                         -default=>$data->{'authorised_value'},
172                                         -size=>1,
173                                         -multiple=>0,
174                                         );
175                 $row_data{value_builder}  = CGI::scrolling_list(-name=>'value_builder',
176                                         -values=> \@value_builder,
177                                         -default=>$data->{'value_builder'},
178                                         -size=>1,
179                                         -multiple=>0,
180                                         );
181                 $row_data{thesaurus_category}  = CGI::scrolling_list(-name=>'thesaurus_category',
182                                         -values=> \@thesaurus_category,
183                                         -default=>$data->{'thesaurus_category'},
184                                         -size=>1,
185                                         -multiple=>0,
186                                         );
187 #               $row_data{kohafield} = $data->{'kohafield'};
188                 $row_data{repeatable} = CGI::checkbox("repeatable$i",$data->{'repeatable'}?'checked':'',1,'');
189                 $row_data{mandatory} = CGI::checkbox("mandatory$i",$data->{'mandatory'}?'checked':'',1,'');
190                 $row_data{bgcolor} = $toggle;
191                 push(@loop_data, \%row_data);
192                 $i++;
193         }
194         # add more_subfields empty lines for add if needed
195         for (my $i=1;$i<=$more_subfields;$i++) {
196                 my %row_data;  # get a fresh hash for the row data
197                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
198                                         -values=>['-1','0','1','2','3','4','5','6','7','8','9','10'],
199                                         -labels => {'-1' =>'ignore','0'=>'0','1'=>'1',
200                                                                         '2' =>'2','3'=>'3','4'=>'4',
201                                                                         '5' =>'5','6'=>'6','7'=>'7',
202                                                                         '8' =>'8','9'=>'9','10'=>'items (10)',
203                                                                         },
204                                         -default=>"",
205                                         -size=>1,
206                                         -multiple=>0,
207                                         );
208                 $row_data{tagsubfield} = "<input type='text' name='tagsubfield' value='".$data->{'tagsubfield'}."' size=3 maxlength=1>";
209                 $row_data{liblibrarian} = "";
210                 $row_data{libopac} = "";
211                 $row_data{repeatable} = CGI::checkbox('repeatable','',1,'');
212                 $row_data{mandatory} = CGI::checkbox('mandatory','',1,'');
213                 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield',
214                                         -values=> \@kohafields,
215                                         -default=> "",
216                                         -size=>1,
217                                         -multiple=>0,
218                                         );
219                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
220                                         -values=> \@authorised_values,
221                                         -size=>1,
222                                         -multiple=>0,
223                                         );
224                 $row_data{thesaurus_category}  = CGI::scrolling_list(-name=>'thesaurus_category',
225                                         -values=> \@thesaurus_category,
226                                         -size=>1,
227                                         -multiple=>0,
228                                         );
229                 $row_data{bgcolor} = $toggle;
230                 push(@loop_data, \%row_data);
231         }
232         $template->param(action => "Edit subfields",
233                                                         tagfield => "<input type=hidden name=tagfield value='$tagfield'>$tagfield",
234                                                         loop => \@loop_data,
235                                                         more_subfields => $more_subfields,
236                                                         more_tag => $tagfield);
237
238                                                                                                 # END $OP eq ADD_FORM
239 ################## ADD_VALIDATE ##################################
240 # called by add_form, used to insert/modify data in DB
241 } elsif ($op eq 'add_validate') {
242         my $dbh = C4::Context->dbh;
243         $template->param(tagfield => "$input->param('tagfield')");
244         my $sth=$dbh->prepare("replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,thesaurus_category,value_builder)
245                                                                         values (?,?,?,?,?,?,?,?,?,?,?)");
246         my @tagsubfield = $input->param('tagsubfield');
247         my @liblibrarian        = $input->param('liblibrarian');
248         my @libopac             = $input->param('libopac');
249         my @kohafield           = $input->param('kohafield');
250         my @tab                         = $input->param('tab');
251         my @authorised_values   = $input->param('authorised_value');
252         my @thesaurus_category  = $input->param('thesaurus_category');
253         my @value_builder       =$input->param('value_builder');
254         for (my $i=0; $i<= $#tagsubfield ; $i++) {
255                 my $tagfield                    =$input->param('tagfield');
256                 my $tagsubfield         =$tagsubfield[$i];
257                 my $liblibrarian                =$liblibrarian[$i];
258                 my $libopac                     =$libopac[$i];
259                 my $repeatable          =$input->param("repeatable$i")?1:0;
260                 my $mandatory           =$input->param("mandatory$i")?1:0;
261                 my $kohafield           =$kohafield[$i];
262                 my $tab                         =$tab[$i];
263                 my $authorised_value            =$authorised_values[$i];
264                 my $thesaurus_category          =$thesaurus_category[$i];
265                 my $value_builder=$value_builder[$i];
266                 if ($tagsubfield) {
267                         $sth->execute ($tagfield,
268                                                                 $tagsubfield,
269                                                                 $liblibrarian,
270                                                                 $libopac,
271                                                                 $repeatable,
272                                                                 $mandatory,
273                                                                 $kohafield,
274                                                                 $tab,
275                                                                 $authorised_value,
276                                                                 $thesaurus_category,
277                                                                 $value_builder);
278                 }
279         }
280         $sth->finish;
281         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield\"></html>";
282         exit;
283
284                                                                                                         # END $OP eq ADD_VALIDATE
285 ################## DELETE_CONFIRM ##################################
286 # called by default form, used to confirm deletion of data in DB
287 } elsif ($op eq 'delete_confirm') {
288         my $dbh = C4::Context->dbh;
289         my $sth=$dbh->prepare($reqsel);
290         $sth->execute;
291         my $data=$sth->fetchrow_hashref;
292         $sth->finish;
293         $template->param(liblibrarian => $data->{'liblibrarian'},
294                                                         tagsubfield => $data->{'tagsubfield'},
295                                                         delete_link => $script_name,
296                                                         tagfield      =>$tagfield,
297                                                         tagsubfield => $tagsubfield,
298                                                         );
299                                                                                                         # END $OP eq DELETE_CONFIRM
300 ################## DELETE_CONFIRMED ##################################
301 # called by delete_confirm, used to effectively confirm deletion of data in DB
302 } elsif ($op eq 'delete_confirmed') {
303         my $dbh = C4::Context->dbh;
304         my $sth=$dbh->prepare($reqdel);
305         $sth->execute;
306         $sth->finish;
307         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield\"></html>";
308         exit;
309         $template->param(tagfield => $tagfield);
310                                                                                                         # END $OP eq DELETE_CONFIRMED
311 ################## DEFAULT ##################################
312 } else { # DEFAULT
313         my $env;
314         my ($count,$results)=StringSearch($env,$tagfield,'web');
315         my $toggle="white";
316         my @loop_data = ();
317         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
318                 if ($toggle eq 'white'){
319                         $toggle="#ffffcc";
320                 } else {
321                         $toggle="white";
322                 }
323                 my %row_data;  # get a fresh hash for the row data
324                 $row_data{tagfield} = $results->[$i]{'tagfield'};
325                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
326                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
327                 $row_data{kohafield} = $results->[$i]{'kohafield'};
328                 $row_data{repeatable} = $results->[$i]{'repeatable'};
329                 $row_data{mandatory} = $results->[$i]{'mandatory'};
330                 $row_data{tab} = $results->[$i]{'tab'};
331                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
332                 $row_data{thesaurus_category}   = $results->[$i]{'thesaurus_category'};
333                 $row_data{value_builder}        = $results->[$i]{'value_builder'};
334                 $row_data{delete} = "$script_name?op=delete_confirm&tagfield=$tagfield&tagsubfield=".$results->[$i]{'tagsubfield'};
335                 $row_data{bgcolor} = $toggle;
336                 push(@loop_data, \%row_data);
337         }
338         $template->param(loop => \@loop_data);
339         $template->param(edit => "<a href='$script_name?op=add_form&tagfield=$tagfield'>Edit</a>");
340         if ($offset>0) {
341                 my $prevpage = $offset-$pagesize;
342                 $template->param(prev =>"<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
343         }
344         if ($offset+$pagesize<$count) {
345                 my $nextpage =$offset+$pagesize;
346                 $template->param(next => "<a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
347         }
348 } #---- END $OP eq DEFAULT
349
350 print $input->header(
351     -type => guesstype($template->output),
352     -cookie => $cookie
353 ), $template->output;