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