Removed the dependency on Set::Scalar.
[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','10'],
112                                         -labels => {'' =>'','0'=>'0','1'=>'1',
113                                                                         '2' =>'2','3'=>'3','4'=>'4',
114                                                                         '5' =>'5','6'=>'6','7'=>'7',
115                                                                         '8' =>'8','9'=>'9','10'=>'items (10)',
116                                                                         },
117                                         -default=>$data->{'tab'},
118                                         -size=>1,
119                                         -multiple=>0,
120                                         );
121                 $row_data{tagsubfield} =$data->{'tagsubfield'}."<input type='hidden' name='tagsubfield[]' value='".$data->{'tagsubfield'}."'>";
122                 $row_data{liblibrarian} = $data->{'liblibrarian'};
123                 $row_data{libopac} = $data->{'libopac'};
124                 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield[]',
125                                         -values=> \@kohafields,
126                                         -default=> "$data->{'kohafield'}",
127                                         -size=>1,
128                                         -multiple=>0,
129                                         );
130 #               $row_data{kohafield} = $data->{'kohafield'};
131                 $row_data{repeatable} = CGI::checkbox('repeatable[]',$data->{'repeatable'}?'checked':'',1,'');
132                 $row_data{mandatory} = CGI::checkbox('mandatory[]',$data->{'mandatory'}?'checked':'',1,'');
133                 $row_data{bgcolor} = $toggle;
134                 push(@loop_data, \%row_data);
135         }
136         # add an empty line for add if needed
137                 my %row_data;  # get a fresh hash for the row data
138                 $row_data{tab} = CGI::scrolling_list(-name=>'tab[]',
139                                         -values=>['','0','1','2','3','4','5','6','7','8','9','items (10)'],
140                                         -default=>"",
141                                         -size=>1,
142                                         -multiple=>0,
143                                         );
144                 $row_data{tagsubfield} = "<input type='text' name='tagsubfield[]' value='".$data->{'tagsubfield'}."' size=3 maxlength=1>";
145                 $row_data{liblibrarian} = "";
146                 $row_data{libopac} = "";
147                 $row_data{repeatable} = CGI::checkbox('repeatable[]','',1,'');
148                 $row_data{mandatory} = CGI::checkbox('mandatory[]','',1,'');
149                 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield[]',
150                                         -values=> \@kohafields,
151                                         -default=> "",
152                                         -size=>1,
153                                         -multiple=>0,
154                                         );
155                 $row_data{bgcolor} = $toggle;
156                 push(@loop_data, \%row_data);
157
158         $template->param(loop => \@loop_data);
159
160                                                                                                         # END $OP eq ADD_FORM
161 ################## ADD_VALIDATE ##################################
162 # called by add_form, used to insert/modify data in DB
163 } elsif ($op eq 'add_validate') {
164         my $dbh = C4::Context->dbh;
165         $template->param(tagfield => "$input->param('tagfield')");
166         my $sth=$dbh->prepare("replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab)
167                                                                         values (?,?,?,?,?,?,?,?)");
168         my @tagsubfield = $input->param('tagsubfield[]');
169         my @liblibrarian        = $input->param('liblibrarian[]');
170         my @libopac             = $input->param('libopac[]');
171         my @repeatable  = $input->param('repeatable[]');
172         my @mandatory   = $input->param('mandatory[]');
173         my @kohafield           = $input->param('kohafield[]');
174         my @tab                         = $input->param('tab[]');
175         for (my $i=0; $i<= $#tagsubfield ; $i++) {
176                 my $tagfield                    =$input->param('tagfield');
177                 my $tagsubfield         =$tagsubfield[$i];
178                 my $liblibrarian                =$liblibrarian[$i];
179                 my $libopac                     =$libopac[$i];
180                 my $repeatable          =$repeatable[$i]?1:0;
181                 my $mandatory           =$mandatory[$i]?1:0;
182                 my $kohafield           =$kohafield[$i];
183                 my $tab                         =$tab[$i];
184                 if ($tagsubfield) {
185                         $sth->execute ($tagfield,
186                                                                 $tagsubfield,
187                                                                 $liblibrarian,
188                                                                 $libopac,
189                                                                 $repeatable,
190                                                                 $mandatory,
191                                                                 $kohafield,
192                                                                 $tab);
193                 }
194         }
195         $sth->finish;
196         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield\"></html>";
197         exit;
198
199                                                                                                         # END $OP eq ADD_VALIDATE
200 ################## DELETE_CONFIRM ##################################
201 # called by default form, used to confirm deletion of data in DB
202 } elsif ($op eq 'delete_confirm') {
203         my $dbh = C4::Context->dbh;
204         my $sth=$dbh->prepare($reqsel);
205         $sth->execute;
206         my $data=$sth->fetchrow_hashref;
207         $sth->finish;
208         $template->param(liblibrarian => $data->{'liblibrarian'},
209                                                         tagsubfield => $tagsubfield,
210                                                         delete_link => $script_name,
211                                                         tagfield      =>$tagfield,
212                                                         tagsubfield => $tagsubfield,
213                                                         );
214                                                                                                         # END $OP eq DELETE_CONFIRM
215 ################## DELETE_CONFIRMED ##################################
216 # called by delete_confirm, used to effectively confirm deletion of data in DB
217 } elsif ($op eq 'delete_confirmed') {
218         my $dbh = C4::Context->dbh;
219         my $sth=$dbh->prepare($reqdel);
220         $sth->execute;
221         $sth->finish;
222         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield\"></html>";
223         exit;
224         $template->param(tagfield => $tagfield);
225                                                                                                         # END $OP eq DELETE_CONFIRMED
226 ################## DEFAULT ##################################
227 } else { # DEFAULT
228         my $env;
229         my ($count,$results)=StringSearch($env,$tagfield,'web');
230         my $toggle="white";
231         my @loop_data = ();
232         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
233                 if ($toggle eq 'white'){
234                         $toggle="#ffffcc";
235                 } else {
236                         $toggle="white";
237                 }
238                 my %row_data;  # get a fresh hash for the row data
239                 $row_data{tagfield} = $results->[$i]{'tagfield'};
240                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
241                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
242                 $row_data{kohafield} = $results->[$i]{'kohafield'};
243                 $row_data{repeatable} = $results->[$i]{'repeatable'};
244                 $row_data{mandatory} = $results->[$i]{'mandatory'};
245                 $row_data{tab} = $results->[$i]{'tab'};
246                 $row_data{delete} = "$script_name?op=delete_confirm&tagfield=$tagfield&tagsubfield=".$results->[$i]{'tagsubfield'};
247                 $row_data{bgcolor} = $toggle;
248                 push(@loop_data, \%row_data);
249         }
250         $template->param(loop => \@loop_data);
251         $template->param(edit => "<a href='$script_name?op=add_form&tagfield=$tagfield'>Edit</a>");
252         if ($offset>0) {
253                 my $prevpage = $offset-$pagesize;
254                 $template->param(prev =>"<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
255         }
256         if ($offset+$pagesize<$count) {
257                 my $nextpage =$offset+$pagesize;
258                 $template->param(next => "<a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
259         }
260 } #---- END $OP eq DEFAULT
261
262 print "Content-Type: text/html\n\n", $template->output;