road to 1.3.1 : marc parameter table management
[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 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 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 $sth=$dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab from marc_subfield_structure where tagfield='$tagfield'"); # and tagsubfield='$tagsubfield'");
79         $sth->execute;
80         # builds kohafield tables
81         my @kohafields;
82         push @kohafields, "";
83         my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
84         $sth2->execute;
85         while ((my $field) = $sth2->fetchrow_array) {
86                 push @kohafields, "biblio.".$field;
87         }
88         my $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
89         $sth2->execute;
90         while ((my $field) = $sth2->fetchrow_array) {
91                 push @kohafields, "biblioitems.".$field;
92         }
93         my $sth2=$dbh->prepare("SHOW COLUMNS from items");
94         $sth2->execute;
95         while ((my $field) = $sth2->fetchrow_array) {
96                 push @kohafields, "items.".$field;
97         }
98         $template->param(action => "Edit subfields",
99                                                         tagfield => "<input type=hidden name=tagfield value='$tagfield'>$tagfield",
100                                                         );
101         my @loop_data = ();
102         my $toggle="white";
103         while ($data =$sth->fetchrow_hashref) {
104                 my %row_data;  # get a fresh hash for the row data
105                 if ($toggle eq 'white'){
106                         $toggle="#ffffcc";
107                 } else {
108                         $toggle="white";
109                 }
110                 $row_data{tab} = CGI::scrolling_list(-name=>'tab[]',
111                                         -values=>['','0','1','2','3','4','5','6','7','8','9','items'],
112                                         -default=>$data->{'tab'},
113                                         -size=>1,
114                                         -multiple=>0,
115                                         );
116                 $row_data{tagsubfield} =$data->{'tagsubfield'}."<input type='hidden' name='tagsubfield[]' value='".$data->{'tagsubfield'}."'>";
117                 $row_data{liblibrarian} = $data->{'liblibrarian'};
118                 $row_data{libopac} = $data->{'libopac'};
119                 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield[]',
120                                         -values=> \@kohafields,
121                                         -default=> "$data->{'kohafield'}",
122                                         -size=>1,
123                                         -multiple=>0,
124                                         );
125 #               $row_data{kohafield} = $data->{'kohafield'};
126                 $row_data{repeatable} = CGI::checkbox('repeatable[]',$data->{'repeatable'}?'checked':'',1,'');
127                 $row_data{mandatory} = CGI::checkbox('mandatory[]',$data->{'mandatory'}?'checked':'',1,'');
128                 $row_data{bgcolor} = $toggle;
129                 push(@loop_data, \%row_data);
130         }
131         # add an empty line for add if needed
132                 my %row_data;  # get a fresh hash for the row data
133                 $row_data{tab} = CGI::scrolling_list(-name=>'tab[]',
134                                         -values=>['','0','1','2','3','4','5','6','7','8','9','items'],
135                                         -default=>"",
136                                         -size=>1,
137                                         -multiple=>0,
138                                         );
139                 $row_data{tagsubfield} = "<input type='text' name='tagsubfield[]' value='".$data->{'tagsubfield'}."' size=3 maxlength=1>";
140                 $row_data{liblibrarian} = "";
141                 $row_data{libopac} = "";
142                 $row_data{repeatable} = CGI::checkbox('repeatable[]','',1,'');
143                 $row_data{mandatory} = CGI::checkbox('mandatory[]','',1,'');
144                 $row_data{kohafield} = '';
145                 $row_data{bgcolor} = $toggle;
146                 push(@loop_data, \%row_data);
147
148         $template->param(loop => \@loop_data);
149
150                                                                                                         # END $OP eq ADD_FORM
151 ################## ADD_VALIDATE ##################################
152 # called by add_form, used to insert/modify data in DB
153 } elsif ($op eq 'add_validate') {
154         my $dbh = C4::Context->dbh;
155         $template->param(tagfield => "$input->param('tagfield')");
156         my $sth=$dbh->prepare("replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab)
157                                                                         values (?,?,?,?,?,?,?,?)");
158         my @tagsubfield = $input->param('tagsubfield[]');
159         my @liblibrarian        = $input->param('liblibrarian[]');
160         my @libopac             = $input->param('libopac[]');
161         my @repeatable  = $input->param('repeatable[]');
162         my @mandatory   = $input->param('mandatory[]');
163         my @kohafield           = $input->param('kohafield[]');
164         my @tab                         = $input->param('tab[]');
165         warn "taille : $#tagsubfield";
166         for (my $i=0; $i<= $#tagsubfield ; $i++) {
167                 my $tagfield                    =$input->param('tagfield');
168                 my $tagsubfield         =$tagsubfield[$i];
169                 my $liblibrarian                =$liblibrarian[$i];
170                 my $libopac                     =$libopac[$i];
171                 my $repeatable          =$repeatable[$i]?1:0;
172                 my $mandatory           =$mandatory[$i]?1:0;
173                 my $kohafield           =$kohafield[$i];
174                 my $tab                         =$tab[$i];
175                 if ($tagsubfield) {
176                         $sth->execute ($tagfield,
177                                                                 $tagsubfield,
178                                                                 $liblibrarian,
179                                                                 $libopac,
180                                                                 $repeatable,
181                                                                 $mandatory,
182                                                                 $kohafield,
183                                                                 $tab);
184                 }
185         }
186         $sth->finish;
187         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marctagstructure.pl\"></html>";
188         exit;
189
190                                                                                                         # END $OP eq ADD_VALIDATE
191 ################## DELETE_CONFIRM ##################################
192 # called by default form, used to confirm deletion of data in DB
193 } elsif ($op eq 'delete_confirm') {
194         my $dbh = C4::Context->dbh;
195         my $sth=$dbh->prepare($reqsel);
196         $sth->execute;
197         my $data=$sth->fetchrow_hashref;
198         $sth->finish;
199         $template->param(liblibrarian => $data->{'liblibrarian'},
200                                                         tagsubfield => $tagsubfield,
201                                                         );
202                                                                                                         # END $OP eq DELETE_CONFIRM
203 ################## DELETE_CONFIRMED ##################################
204 # called by delete_confirm, used to effectively confirm deletion of data in DB
205 } elsif ($op eq 'delete_confirmed') {
206         my $dbh = C4::Context->dbh;
207         my $sth=$dbh->prepare($reqdel);
208         $sth->execute;
209         $sth->finish;
210                                                                                                         # END $OP eq DELETE_CONFIRMED
211 ################## DEFAULT ##################################
212 } else { # DEFAULT
213         my $env;
214         my ($count,$results)=StringSearch($env,$tagfield,'web');
215         my $toggle="white";
216         my @loop_data = ();
217         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
218                 if ($toggle eq 'white'){
219                         $toggle="#ffffcc";
220                 } else {
221                         $toggle="white";
222                 }
223                 my %row_data;  # get a fresh hash for the row data
224                 $row_data{tagfield} = $results->[$i]{'tagfield'};
225                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
226                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
227                 $row_data{kohafield} = $results->[$i]{'kohafield'};
228                 $row_data{repeatable} = $results->[$i]{'repeatable'};
229                 $row_data{mandatory} = $results->[$i]{'mandatory'};
230                 $row_data{tab} = $results->[$i]{'tab'};
231                 $row_data{delete} = "$script_name?op=delete_confirm&tagfield=$tagfield&tagsubfield=".$results->[$i]{'tagfield'};
232                 $row_data{bgcolor} = $toggle;
233                 push(@loop_data, \%row_data);
234         }
235         $template->param(loop => \@loop_data);
236         $template->param(edit => "<a href='$script_name?op=add_form&tagfield=$tagfield'>Edit</a>");
237         if ($offset>0) {
238                 my $prevpage = $offset-$pagesize;
239                 $template->param(prev =>"<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
240         }
241         if ($offset+$pagesize<$count) {
242                 my $nextpage =$offset+$pagesize;
243                 $template->param(next => "a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
244         }
245 } #---- END $OP eq DEFAULT
246
247 print "Content-Type: text/html\n\n", $template->output;