change show columns to use biblioitems bnotes too
[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 C4::Interface::CGI::Output;
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                 if ($field eq 'notes') { $field = 'bnotes'; }
100                 push @kohafields, "biblioitems.".$field;
101         }
102         my $sth2=$dbh->prepare("SHOW COLUMNS from items");
103         $sth2->execute;
104         while ((my $field) = $sth2->fetchrow_array) {
105                 push @kohafields, "items.".$field;
106         }
107         
108         # other subfields
109         push @kohafields, "additionalauthors.author";
110         push @kohafields, "bibliosubject.subject";
111         push @kohafields, "bibliosubtitle.title";
112         # build authorised value list
113         $sth2->finish;
114         $sth2 = $dbh->prepare("select distinct category from authorised_values");
115         $sth2->execute;
116         my @authorised_values;
117         push @authorised_values,"";
118         while ((my $category) = $sth2->fetchrow_array) {
119                 push @authorised_values, $category;
120         }
121         push (@authorised_values,"branches");
122         push (@authorised_values,"itemtypes");
123         # build thesaurus categories list
124         $sth2->finish;
125         $sth2 = $dbh->prepare("select distinct category from bibliothesaurus");
126         $sth2->execute;
127         my @thesaurus_category;
128         push @thesaurus_category,"";
129         while ((my $category) = $sth2->fetchrow_array) {
130                 push @thesaurus_category, $category;
131         }
132         # build value_builder list
133         my @value_builder=('');
134         opendir(DIR, "../value_builder") || die "can't opendir ../value_builder: $!";
135         while (my $line = readdir(DIR)) {
136                 if ($line =~ /\.pl$/) {
137                         push (@value_builder,$line);
138                 }
139         }
140         closedir DIR;
141
142         # build values list
143         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'");
144         $sth->execute;
145         my @loop_data = ();
146         my $toggle="white";
147         my $i=0;
148         while ($data =$sth->fetchrow_hashref) {
149                 my %row_data;  # get a fresh hash for the row data
150                 if ($toggle eq 'white'){
151                         $toggle="#ffffcc";
152                 } else {
153                         $toggle="white";
154                 }
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=>$data->{'tab'},
163                                         -size=>1,
164                                         -multiple=>0,
165                                         );
166                 $row_data{tagsubfield} =$data->{'tagsubfield'}."<input type='hidden' name='tagsubfield' value='".$data->{'tagsubfield'}."'>";
167                 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
168                 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
169                 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
170                                         -values=> \@kohafields,
171                                         -default=> "$data->{'kohafield'}",
172                                         -size=>1,
173                                         -multiple=>0,
174                                         );
175                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
176                                         -values=> \@authorised_values,
177                                         -default=>$data->{'authorised_value'},
178                                         -size=>1,
179                                         -multiple=>0,
180                                         );
181                 $row_data{value_builder}  = CGI::scrolling_list(-name=>'value_builder',
182                                         -values=> \@value_builder,
183                                         -default=>$data->{'value_builder'},
184                                         -size=>1,
185                                         -multiple=>0,
186                                         );
187                 $row_data{thesaurus_category}  = CGI::scrolling_list(-name=>'thesaurus_category',
188                                         -values=> \@thesaurus_category,
189                                         -default=>$data->{'thesaurus_category'},
190                                         -size=>1,
191                                         -multiple=>0,
192                                         );
193 #               $row_data{kohafield} = $data->{'kohafield'};
194                 $row_data{repeatable} = CGI::checkbox("repeatable$i",$data->{'repeatable'}?'checked':'',1,'');
195                 $row_data{mandatory} = CGI::checkbox("mandatory$i",$data->{'mandatory'}?'checked':'',1,'');
196                 $row_data{bgcolor} = $toggle;
197                 push(@loop_data, \%row_data);
198                 $i++;
199         }
200         # add more_subfields empty lines for add if needed
201         for (my $i=1;$i<=$more_subfields;$i++) {
202                 my %row_data;  # get a fresh hash for the row data
203                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
204                                         -values=>['-1','0','1','2','3','4','5','6','7','8','9','10'],
205                                         -labels => {'-1' =>'ignore','0'=>'0','1'=>'1',
206                                                                         '2' =>'2','3'=>'3','4'=>'4',
207                                                                         '5' =>'5','6'=>'6','7'=>'7',
208                                                                         '8' =>'8','9'=>'9','10'=>'items (10)',
209                                                                         },
210                                         -default=>"",
211                                         -size=>1,
212                                         -multiple=>0,
213                                         );
214                 $row_data{tagsubfield} = "<input type='text' name='tagsubfield' value='".$data->{'tagsubfield'}."' size=3 maxlength=1>";
215                 $row_data{liblibrarian} = "";
216                 $row_data{libopac} = "";
217                 $row_data{repeatable} = CGI::checkbox('repeatable','',1,'');
218                 $row_data{mandatory} = CGI::checkbox('mandatory','',1,'');
219                 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield',
220                                         -values=> \@kohafields,
221                                         -default=> "",
222                                         -size=>1,
223                                         -multiple=>0,
224                                         );
225                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
226                                         -values=> \@authorised_values,
227                                         -size=>1,
228                                         -multiple=>0,
229                                         );
230                 $row_data{thesaurus_category}  = CGI::scrolling_list(-name=>'thesaurus_category',
231                                         -values=> \@thesaurus_category,
232                                         -size=>1,
233                                         -multiple=>0,
234                                         );
235                 $row_data{bgcolor} = $toggle;
236                 push(@loop_data, \%row_data);
237         }
238         $template->param(action => "Edit subfields",
239                                                         tagfield => "<input type=hidden name=tagfield value='$tagfield'>$tagfield",
240                                                         loop => \@loop_data,
241                                                         more_subfields => $more_subfields,
242                                                         more_tag => $tagfield);
243
244                                                                                                 # END $OP eq ADD_FORM
245 ################## ADD_VALIDATE ##################################
246 # called by add_form, used to insert/modify data in DB
247 } elsif ($op eq 'add_validate') {
248         my $dbh = C4::Context->dbh;
249         $template->param(tagfield => "$input->param('tagfield')");
250         my $sth=$dbh->prepare("replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,thesaurus_category,value_builder)
251                                                                         values (?,?,?,?,?,?,?,?,?,?,?)");
252         my @tagsubfield = $input->param('tagsubfield');
253         my @liblibrarian        = $input->param('liblibrarian');
254         my @libopac             = $input->param('libopac');
255         my @kohafield           = $input->param('kohafield');
256         my @tab                         = $input->param('tab');
257         my @authorised_values   = $input->param('authorised_value');
258         my @thesaurus_category  = $input->param('thesaurus_category');
259         my @value_builder       =$input->param('value_builder');
260         for (my $i=0; $i<= $#tagsubfield ; $i++) {
261                 my $tagfield                    =$input->param('tagfield');
262                 my $tagsubfield         =$tagsubfield[$i];
263                 $tagsubfield="@" unless $tagsubfield;
264                 my $liblibrarian                =$liblibrarian[$i];
265                 my $libopac                     =$libopac[$i];
266                 my $repeatable          =$input->param("repeatable$i")?1:0;
267                 my $mandatory           =$input->param("mandatory$i")?1:0;
268                 my $kohafield           =$kohafield[$i];
269                 my $tab                         =$tab[$i];
270                 my $authorised_value            =$authorised_values[$i];
271                 my $thesaurus_category          =$thesaurus_category[$i];
272                 my $value_builder=$value_builder[$i];
273                 if ($liblibrarian) {
274                         $sth->execute ($tagfield,
275                                                                 $tagsubfield,
276                                                                 $liblibrarian,
277                                                                 $libopac,
278                                                                 $repeatable,
279                                                                 $mandatory,
280                                                                 $kohafield,
281                                                                 $tab,
282                                                                 $authorised_value,
283                                                                 $thesaurus_category,
284                                                                 $value_builder);
285                 }
286         }
287         $sth->finish;
288         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield\"></html>";
289         exit;
290
291                                                                                                         # END $OP eq ADD_VALIDATE
292 ################## DELETE_CONFIRM ##################################
293 # called by default form, used to confirm deletion of data in DB
294 } elsif ($op eq 'delete_confirm') {
295         my $dbh = C4::Context->dbh;
296         my $sth=$dbh->prepare($reqsel);
297         $sth->execute;
298         my $data=$sth->fetchrow_hashref;
299         $sth->finish;
300         $template->param(liblibrarian => $data->{'liblibrarian'},
301                                                         tagsubfield => $data->{'tagsubfield'},
302                                                         delete_link => $script_name,
303                                                         tagfield      =>$tagfield,
304                                                         tagsubfield => $tagsubfield,
305                                                         );
306                                                                                                         # END $OP eq DELETE_CONFIRM
307 ################## DELETE_CONFIRMED ##################################
308 # called by delete_confirm, used to effectively confirm deletion of data in DB
309 } elsif ($op eq 'delete_confirmed') {
310         my $dbh = C4::Context->dbh;
311         my $sth=$dbh->prepare($reqdel);
312         $sth->execute;
313         $sth->finish;
314         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield\"></html>";
315         exit;
316         $template->param(tagfield => $tagfield);
317                                                                                                         # END $OP eq DELETE_CONFIRMED
318 ################## DEFAULT ##################################
319 } else { # DEFAULT
320         my $env;
321         my ($count,$results)=StringSearch($env,$tagfield,'web');
322         my $toggle="white";
323         my @loop_data = ();
324         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
325                 if ($toggle eq 'white'){
326                         $toggle="#ffffcc";
327                 } else {
328                         $toggle="white";
329                 }
330                 my %row_data;  # get a fresh hash for the row data
331                 $row_data{tagfield} = $results->[$i]{'tagfield'};
332                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
333                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
334                 $row_data{kohafield} = $results->[$i]{'kohafield'};
335                 $row_data{repeatable} = $results->[$i]{'repeatable'};
336                 $row_data{mandatory} = $results->[$i]{'mandatory'};
337                 $row_data{tab} = $results->[$i]{'tab'};
338                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
339                 $row_data{thesaurus_category}   = $results->[$i]{'thesaurus_category'};
340                 $row_data{value_builder}        = $results->[$i]{'value_builder'};
341                 $row_data{delete} = "$script_name?op=delete_confirm&tagfield=$tagfield&tagsubfield=".$results->[$i]{'tagsubfield'};
342                 $row_data{bgcolor} = $toggle;
343                 push(@loop_data, \%row_data);
344         }
345         $template->param(loop => \@loop_data);
346         $template->param(edit => "<a href='$script_name?op=add_form&tagfield=$tagfield'>");
347         if ($offset>0) {
348                 my $prevpage = $offset-$pagesize;
349                 $template->param(prev =>"<a href=$script_name?offset=$prevpage>");
350         }
351         if ($offset+$pagesize<$count) {
352                 my $nextpage =$offset+$pagesize;
353                 $template->param(next => "<a href=$script_name?offset=$nextpage>");
354         }
355 } #---- END $OP eq DEFAULT
356
357 output_html_with_http_headers $input, $cookie, $template->output;