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