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