Savannah seems out of synch.Code cleaning removal of Date::Manip
[koha.git] / admin / biblios_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
29 sub StringSearch  {
30         my ($env,$searchstring,$frameworkcode)=@_;
31         my $dbh = C4::Context->dbh;
32         $searchstring=~ s/\'/\\\'/g;
33         my @data=split(' ',$searchstring);
34         my $count=@data;
35         my $sth=$dbh->prepare("Select * from biblios_subfield_structure where (tagfield like ? and frameworkcode=?) order by tagfield");
36         $sth->execute("$searchstring%",$frameworkcode);
37         my @results;
38         my $cnt=0;
39         my $u=1;
40         while (my $data=$sth->fetchrow_hashref){
41                 push(@results,$data);
42                 $cnt ++;
43                 $u++;
44         }
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 $frameworkcode=$input->param('frameworkcode');
54 my $pkfield="tagfield";
55 my $offset=$input->param('offset');
56 my $script_name="/cgi-bin/koha/admin/biblios_subfields_structure.pl";
57
58 my ($template, $borrowernumber, $cookie)
59     = get_template_and_user({template_name => "admin/biblios_subfields_structure.tmpl",
60                              query => $input,
61                              type => "intranet",
62                              authnotrequired => 0,
63                              flagsrequired => {parameters => 1},
64                              debug => 1,
65                              });
66 my $pagesize=30;
67 my $op = $input->param('op');
68 $tagfield=~ s/\,//g;
69
70 if ($op) {
71 $template->param(script_name => $script_name,
72                                                 tagfield =>$tagfield,
73                                                 frameworkcode => $frameworkcode,
74                                                 $op              => 1); # we show only the TMPL_VAR names $op
75 } else {
76 $template->param(script_name => $script_name,
77                                                 tagfield =>$tagfield,
78                                                 frameworkcode => $frameworkcode,
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         
89         
90         # build authorised value list
91         
92 my      $sth2 = $dbh->prepare("select distinct category from authorised_values");
93         $sth2->execute;
94         my @authorised_values;
95         push @authorised_values,"";
96         while ((my $category) = $sth2->fetchrow_array) {
97                 push @authorised_values, $category;
98         }
99         push (@authorised_values,"branches");
100         push (@authorised_values,"itemtypes");
101         # build thesaurus categories list
102         $sth2->finish;
103         $sth2 = $dbh->prepare("select authtypecode from auth_types");
104         $sth2->execute;
105         my @authtypes;
106         push @authtypes,"";
107         while ((my $authtypecode) = $sth2->fetchrow_array) {
108                 push @authtypes, $authtypecode;
109         }
110         # build value_builder list
111         my @value_builder=('');
112
113         # read value_builder directory.
114         # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
115         # on a standard install, /cgi-bin need to be added. 
116         # test one, then the other
117         my $cgidir = C4::Context->intranetdir ."/cgi-bin";
118         unless (opendir(DIR, "$cgidir/value_builder")) {
119                 $cgidir = C4::Context->intranetdir;
120                 opendir(DIR, "$cgidir/value_builder") || die "can't opendir $cgidir/value_builder: $!";
121         } 
122         while (my $line = readdir(DIR)) {
123                 if ($line =~ /\.pl$/) {
124                         push (@value_builder,$line);
125                 }
126         }
127         closedir DIR;
128
129         # build values list
130         my $sth=$dbh->prepare("select * from biblios_subfield_structure where tagfield=? and frameworkcode=?"); # and tagsubfield='$tagsubfield'");
131         $sth->execute($tagfield,$frameworkcode);
132         my @loop_data = ();
133         my $toggle=1;
134         my $i=0;
135         while ($data =$sth->fetchrow_hashref) {
136                 my %row_data;  # get a fresh hash for the row data
137                 if ($toggle eq 1){
138                         $toggle=0;
139                 } else {
140                         $toggle=1;
141                 }
142                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
143                                         -id=>"tab$i",
144                                         -values=>['-1','0','1','2','3','4','5','6','7','8','9'],
145                                         -labels => {'-1' =>'ignore','0'=>'0','1'=>'1','2' =>'2','3'=>'3','4'=>'4',
146                                                                         '5' =>'5','6'=>'6','7'=>'7',
147                                                                         '8' =>'8','9'=>'9',},
148                                         -default=>$data->{'tab'},
149                                         -size=>1,
150                                         -multiple=>0,
151                                         );
152                 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
153                                         -id=>"ohidden$i",
154                                         -values=>['0','2'],
155                                         -labels => {'0'=>'Show',
156                                                                         '2' =>'Hide',
157                                                                         },
158                                         -default=>substr($data->{'hidden'},0,1),
159                                         -size=>1,
160                                         -multiple=>0,
161                                         );
162                 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
163                                         -id=>"ihidden$i",
164                                         -values=>['0','2'],
165                                         -labels => {'0'=>'Show',
166                                                                         '2' =>'Hide',
167                                                                         },
168                                         -default=>substr($data->{'hidden'},1,1),
169                                         -size=>1,
170                                         -multiple=>0,
171                                         );
172                 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
173                                         -id=>"ehidden$i",
174                                         -values=>['0','1','2'],
175                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
176                                                                         '2' =>'Hide',
177                                                                         },
178                                         -default=>substr($data->{'hidden'},2,1),
179                                         -size=>1,
180                                         -multiple=>0,
181                                         );
182                 $row_data{tagsubfield} =$data->{'tagsubfield'}."<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\">";
183                 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
184                 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
185                 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
186                 
187                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
188                                         -id=>'authorised_value',
189                                         -values=> \@authorised_values,
190                                         -default=>$data->{'authorised_value'},
191                                         -size=>1,
192                                         -multiple=>0,
193                                         );
194                 $row_data{value_builder}  = CGI::scrolling_list(-name=>'value_builder',
195                                         -id=>'value_builder',
196                                         -values=> \@value_builder,
197                                         -default=>$data->{'value_builder'},
198                                         -size=>1,
199                                         -multiple=>0,
200                                         );
201                 $row_data{authtypes}  = CGI::scrolling_list(-name=>'authtypecode',
202                                         -id=>'authtypecode',
203                                         -values=> \@authtypes,
204                                         -default=>$data->{'authtypecode'},
205                                         -size=>1,
206                                         -multiple=>0,
207                                         );
208                 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
209         -checked => $data->{'repeatable'}?'checked':'',
210         -value => 1,
211         -label => '',
212         -id => "repeatable$i");
213                 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
214         -checked => $data->{'mandatory'}?'checked':'',
215         -value => 1,
216         -label => '',
217         -id => "mandatory$i");
218                 $row_data{hidden} = CGI::escapeHTML($data->{hidden});
219                 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
220                         -id => "isurl$i",
221                         -checked => $data->{'isurl'}?'checked':'',
222                         -value => 1,
223                         -label => '');
224                 $row_data{row} = $i;
225                 $row_data{toggle} = $toggle;
226                 $row_data{link} = CGI::escapeHTML($data->{'link'});
227                 push(@loop_data, \%row_data);
228                 $i++;
229         }
230         # add more_subfields empty lines for add if needed
231         for (my $i=1;$i<=$more_subfields;$i++) {
232                 my %row_data;  # get a fresh hash for the row data
233                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
234                                         -id => "tab$i",
235                                         -values=>['-1','0','1','2','3','4','5','6','7','8','9'],
236                                         -labels => {'-1' =>'ignore','0'=>'0','1'=>'1',
237                                                                         '2' =>'2','3'=>'3','4'=>'4',
238                                                                         '5' =>'5','6'=>'6','7'=>'7',
239                                                                         '8' =>'8','9'=>'9',
240                                                                         },
241                                         -default=>"",
242                                         -size=>1,
243                                         -multiple=>0,
244                                         );
245                 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
246                                         -id=>"ohidden$i",
247                                         -values=>['0','2'],
248                                         -labels => {'0'=>'Show','2' =>'Hide',},
249                                         -default=>"0",
250                                         -size=>1,
251                                         -multiple=>0,
252                                         );
253
254                 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
255                                         -id=>"ihidden$i",
256                                         -values=>['0','2'],
257                                         -labels => {'0'=>'Show','2' =>'Hide',},
258                                         -default=>"0",
259                                         -size=>1,
260                                         -multiple=>0,
261                                         );
262                 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
263                                         -id=>"ehidden$i",
264                                         -values=>['0','1','2'],
265                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
266                                                                         '2' =>'Hide',
267                                                                         },
268                                         -default=>"0",
269                                         -size=>1,
270                                         -multiple=>0,
271                                         );
272                 $row_data{tagsubfield} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\">";
273                 $row_data{liblibrarian} = "";
274                 $row_data{libopac} = "";
275                 $row_data{seealso} = "";
276                 $row_data{hidden} = "000";
277                 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
278                                 -id => "repeatable$i",
279                                 -checked => '',
280                                 -value => 1,
281                                 -label => '');
282                 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
283                         -id => "mandatory$i",
284                         -checked => '',
285                         -value => 1,
286                         -label => '');
287                 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
288                         -id => "isurl$i",
289                         -checked => '',
290                         -value => 1,
291                         -label => '');
292                 
293                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
294                                         -id => 'authorised_value',
295                                         -values=> \@authorised_values,
296                                         -size=>1,
297                                         -multiple=>0,
298                                         );
299                 $row_data{authtypes}  = CGI::scrolling_list(-name=>'authtypecode',
300                                         -id => 'authtypecode',
301                                         -values=> \@authtypes,
302                                         -size=>1,
303                                         -multiple=>0,
304                                         );
305                 $row_data{link} = CGI::escapeHTML($data->{'link'});
306                 $row_data{toggle} = $toggle;
307                 $row_data{row} = $i;
308                 push(@loop_data, \%row_data);
309         }
310         $template->param('use-heading-flags-p' => 1);
311         $template->param('heading-edit-subfields-p' => 1);
312         $template->param(action => "Edit subfields",
313                                                         tagfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\">$tagfield",
314                                                         loop => \@loop_data,
315                                                         more_subfields => $more_subfields,
316                                                         more_tag => $tagfield);
317
318                                                                                                 # END $OP eq ADD_FORM
319 ################## ADD_VALIDATE ##################################
320 # called by add_form, used to insert/modify data in DB
321 } elsif ($op eq 'add_validate') {
322         my $dbh = C4::Context->dbh;
323         $template->param(tagfield => "$input->param('tagfield')");
324         my $sth=$dbh->prepare("replace biblios_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link)
325                                                                         values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
326         my @tagsubfield = $input->param('tagsubfield');
327         my @liblibrarian        = $input->param('liblibrarian');
328         my @libopac             = $input->param('libopac');
329
330         my @tab                         = $input->param('tab');
331         my @seealso             = $input->param('seealso');
332 #       my @hidden              = $input->param('hidden');
333         my @hidden;
334         my @ohidden             = $input->param('ohidden');
335         my @ihidden             = $input->param('ihidden');
336         my @ehidden             = $input->param('ehidden');
337         my @authorised_values   = $input->param('authorised_value');
338         my @authtypecodes       = $input->param('authtypecode');
339         my @value_builder       =$input->param('value_builder');
340         my @link                =$input->param('link');
341         for (my $i=0; $i<= $#tagsubfield ; $i++) {
342                 my $tagfield                    =$input->param('tagfield');
343                 my $tagsubfield         =$tagsubfield[$i];
344                 $tagsubfield="@" unless $tagsubfield ne '';
345                 my $liblibrarian                =$liblibrarian[$i];
346                 my $libopac                     =$libopac[$i];
347                 my $repeatable          =$input->param("repeatable$i")?1:0;
348                 my $mandatory           =$input->param("mandatory$i")?1:0;
349         
350                 my $tab                         =$tab[$i];
351                 my $seealso                             =$seealso[$i];
352                 my $authorised_value            =$authorised_values[$i];
353                 my $authtypecode                =$authtypecodes[$i];
354                 my $value_builder=$value_builder[$i];
355                 my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
356                 my $isurl = $input->param("isurl$i")?1:0;
357                 my $link = $link[$i];
358                 if ($liblibrarian) {
359                         unless (C4::Context->config('demo') eq 1) {
360                                 $sth->execute ($tagfield,
361                                                                         $tagsubfield,
362                                                                         $liblibrarian,
363                                                                         $libopac,
364                                                                         $repeatable,
365                                                                         $mandatory,
366                                                                         
367                                                                         $tab,
368                                                                         $seealso,
369                                                                         $authorised_value,
370                                                                         $authtypecode,
371                                                                         $value_builder,
372                                                                         $hidden,
373                                                                         $isurl,
374                                                                         $frameworkcode,
375
376          $link,
377                                               );
378                         }
379                 }
380         }
381         $sth->finish;
382         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=biblios_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
383         exit;
384
385                                                                                                         # END $OP eq ADD_VALIDATE
386 ################## DELETE_CONFIRM ##################################
387 # called by default form, used to confirm deletion of data in DB
388 } elsif ($op eq 'delete_confirm') {
389         my $dbh = C4::Context->dbh;
390         my $sth=$dbh->prepare("select * from biblios_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?");
391         #FIXME : called with 2 bind variables when 3 are needed
392         $sth->execute($tagfield,$tagsubfield);
393         my $data=$sth->fetchrow_hashref;
394         $sth->finish;
395         $template->param(liblibrarian => $data->{'liblibrarian'},
396                                                         tagsubfield => $data->{'tagsubfield'},
397                                                         delete_link => $script_name,
398                                                         tagfield      =>$tagfield,
399                                                         tagsubfield => $tagsubfield,
400                                                         frameworkcode => $frameworkcode,
401                                                         );
402                                                                                                         # END $OP eq DELETE_CONFIRM
403 ################## DELETE_CONFIRMED ##################################
404 # called by delete_confirm, used to effectively confirm deletion of data in DB
405 } elsif ($op eq 'delete_confirmed') {
406         my $dbh = C4::Context->dbh;
407         unless (C4::Context->config('demo') eq 1) {
408                 my $sth=$dbh->prepare("delete from biblios_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?");
409                 $sth->execute($tagfield,$tagsubfield,$frameworkcode);
410                 $sth->finish;
411         }
412         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=biblios_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
413         exit;
414         $template->param(tagfield => $tagfield);
415                                                                                                         # END $OP eq DELETE_CONFIRMED
416 ################## DEFAULT ##################################
417 } else { # DEFAULT
418         my $env;
419         my ($count,$results)=StringSearch($env,$tagfield,$frameworkcode);
420         my $toggle=1;
421         my @loop_data = ();
422         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
423                 if ($toggle eq 1){
424                         $toggle=0;
425                 } else {
426                         $toggle=1;
427                 }
428                 my %row_data;  # get a fresh hash for the row data
429                 $row_data{tagfield} = $results->[$i]{'tagfield'};
430                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
431                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
432                 $row_data{repeatable} = $results->[$i]{'repeatable'};
433                 $row_data{mandatory} = $results->[$i]{'mandatory'};
434                 $row_data{tab} = $results->[$i]{'tab'};
435                 $row_data{seealso} = $results->[$i]{'seealso'};
436                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
437                 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
438                 $row_data{value_builder}        = $results->[$i]{'value_builder'};
439                 $row_data{hidden}       = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
440                 $row_data{isurl}        = $results->[$i]{'isurl'};
441                 $row_data{link} = $results->[$i]{'link'};
442                 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&frameworkcode=$frameworkcode";
443                 $row_data{toggle} = $toggle;
444                 if ($row_data{tab} eq -1) {
445                         $row_data{subfield_ignored} = 1;
446                 }
447
448                 push(@loop_data, \%row_data);
449         }
450         $template->param(loop => \@loop_data);
451         $template->param(edit_tagfield => $tagfield,
452                 edit_frameworkcode => $frameworkcode);
453         
454         if ($offset>0) {
455                 my $prevpage = $offset-$pagesize;
456                 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
457         }
458         if ($offset+$pagesize<$count) {
459                 my $nextpage =$offset+$pagesize;
460                 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
461         }
462 } #---- END $OP eq DEFAULT
463 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
464                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
465                 IntranetNav => C4::Context->preference("IntranetNav"),
466                 );
467 output_html_with_http_headers $input, $cookie, $template->output;