4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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
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.
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
29 my ($searchstring,$authtypecode)=@_;
30 my $dbh = C4::Context->dbh;
31 $searchstring=~ s/\'/\\\'/g;
32 my @data=split(' ',$searchstring);
34 my $sth=$dbh->prepare("Select * from auth_subfield_structure where (tagfield like ? and authtypecode=?) order by tagfield");
35 $sth->execute("$searchstring%",$authtypecode);
39 while (my $data=$sth->fetchrow_hashref){
46 return ($cnt,\@results);
50 my $tagfield=$input->param('tagfield');
51 my $tagsubfield=$input->param('tagsubfield');
52 my $authtypecode=$input->param('authtypecode');
53 my $pkfield="tagfield";
54 my $offset=$input->param('offset');
55 my $script_name="/cgi-bin/koha/admin/auth_subfields_structure.pl";
57 my ($template, $borrowernumber, $cookie)
58 = get_template_and_user({template_name => "admin/auth_subfields_structure.tmpl",
62 flagsrequired => {parameters => 1},
66 my $op = $input->param('op');
70 $template->param(script_name => $script_name,
72 authtypecode => $authtypecode,
73 $op => 1); # we show only the TMPL_VAR names $op
75 $template->param(script_name => $script_name,
77 authtypecode => $authtypecode,
78 else => 1); # we show only the TMPL_VAR names $op
81 ################## ADD_FORM ##################################
82 # called by default. Used to create form to add or modify a record
83 if ($op eq 'add_form') {
85 my $dbh = C4::Context->dbh;
86 my $more_subfields = $input->param("more_subfields")+1;
87 # builds kohafield tables
90 my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
92 while ((my $field) = $sth2->fetchrow_array) {
93 push @kohafields, "auth_header.".$field;
96 # build authorised value list
98 $sth2 = $dbh->prepare("select distinct category from authorised_values");
100 my @authorised_values;
101 push @authorised_values,"";
102 while ((my $category) = $sth2->fetchrow_array) {
103 push @authorised_values, $category;
105 push (@authorised_values,"branches");
106 push (@authorised_values,"itemtypes");
108 # build thesaurus categories list
110 $sth2 = $dbh->prepare("select authtypecode from auth_types");
114 while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
115 push @authtypes, $authtypecode;
118 # build value_builder list
119 my @value_builder=('');
121 # read value_builder directory.
122 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
123 # on a standard install, /cgi-bin need to be added.
124 # test one, then the other
125 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
126 unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
127 $cgidir = C4::Context->intranetdir;
128 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
130 while (my $line = readdir(DIR)) {
131 if ($line =~ /\.pl$/) {
132 push (@value_builder,$line);
135 @value_builder= sort {$a cmp $b} @value_builder;
139 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
140 $sth->execute($tagfield,$authtypecode);
144 while ($data =$sth->fetchrow_hashref) {
146 my %row_data; # get a fresh hash for the row data
152 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
155 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
169 -default=>$data->{'tab'},
174 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
176 -values=>['0','1','2'],
177 -labels => {'0'=>'Show','1'=>'Show Collapsed',
180 -default=>substr($data->{'hidden'},0,1),
184 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
186 -values=>['0','1','2'],
187 -labels => {'0'=>'Show','1'=>'Show Collapsed',
190 -default=>substr($data->{'hidden'},1,1),
194 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
196 -values=>['0','1','2'],
197 -labels => {'0'=>'Show','1'=>'Show Collapsed',
200 -default=>substr($data->{'hidden'}." ",2,1),
204 $row_data{tagsubfield} =$data->{'tagsubfield'}."<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
205 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
206 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
207 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
208 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
210 -values=> \@kohafields,
211 -default=> "$data->{'kohafield'}",
215 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
216 -id=>'authorised_value',
217 -values=> \@authorised_values,
218 -default=>$data->{'authorised_value'},
223 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
224 -id=>'frameworkcode',
225 -values=> \@authtypes,
226 -default=>$data->{'frameworkcode'},
231 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
232 -id=>'value_builder',
233 -values=> \@value_builder,
234 -default=>$data->{'value_builder'},
240 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
241 -checked => $data->{'repeatable'}?'checked':'',
244 -id => "repeatable$i");
245 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
246 -checked => $data->{'mandatory'}?'checked':'',
249 -id => "mandatory$i");
250 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
251 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
253 -checked => $data->{'isurl'}?'checked':'',
257 $row_data{toggle} = $toggle;
258 push(@loop_data, \%row_data);
261 # add more_subfields empty lines for add if needed
262 for (my $i=1;$i<=$more_subfields;$i++) {
263 my %row_data; # get a fresh hash for the row data
264 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
267 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
286 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
288 -values=>['0','1','2'],
289 -labels => {'0'=>'Show','1'=>'Show Collapsed',
297 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
299 -values=>['0','1','2'],
300 -labels => {'0'=>'Show','1'=>'Show Collapsed',
307 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
309 -values=>['0','1','2'],
310 -labels => {'0'=>'Show','1'=>'Show Collapsed',
317 $row_data{tagsubfield} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
318 $row_data{liblibrarian} = "";
319 $row_data{libopac} = "";
320 $row_data{seealso} = "";
321 $row_data{hidden} = "000";
322 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
323 -id => "repeatable$i",
327 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
328 -id => "mandatory$i",
332 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
337 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield',
338 -id => "kohafield$i",
339 -values=> \@kohafields,
344 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
345 -id=>'frameworkcode',
346 -values=> \@authtypes,
347 -default=>$data->{'frameworkcode'},
352 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
353 -id => 'authorised_value',
354 -values=> \@authorised_values,
359 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
360 -id=>'value_builder',
361 -values=> \@value_builder,
362 -default=>$data->{'value_builder'},
367 $row_data{toggle} = $toggle;
369 push(@loop_data, \%row_data);
371 $template->param('use-heading-flags-p' => 1);
372 $template->param('heading-edit-subfields-p' => 1);
373 $template->param(action => "Edit subfields",
374 tagfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />$tagfield",
376 more_subfields => $more_subfields,
377 more_tag => $tagfield);
379 # END $OP eq ADD_FORM
380 ################## ADD_VALIDATE ##################################
381 # called by add_form, used to insert/modify data in DB
382 } elsif ($op eq 'add_validate') {
383 my $dbh = C4::Context->dbh;
384 $template->param(tagfield => "$input->param('tagfield')");
385 my $sth=$dbh->prepare("replace auth_subfield_structure (authtypecode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,frameworkcode,value_builder,hidden,isurl)
386 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
387 my @tagsubfield = $input->param('tagsubfield');
388 my @liblibrarian = $input->param('liblibrarian');
389 my @libopac = $input->param('libopac');
390 my @kohafield = ''.$input->param('kohafield');
391 my @tab = $input->param('tab');
392 my @seealso = $input->param('seealso');
394 my @ohidden = $input->param('ohidden');
395 my @ihidden = $input->param('ihidden');
396 my @ehidden = $input->param('ehidden');
397 my @authorised_values = $input->param('authorised_value');
398 my $authtypecode = $input->param('authtypecode');
399 my @frameworkcodes = $input->param('frameworkcode');
400 my @value_builder =$input->param('value_builder');
401 for (my $i=0; $i<= $#tagsubfield ; $i++) {
402 my $tagfield =$input->param('tagfield');
403 my $tagsubfield =$tagsubfield[$i];
404 $tagsubfield="@" unless $tagsubfield ne '';
405 my $liblibrarian =$liblibrarian[$i];
406 my $libopac =$libopac[$i];
407 my $repeatable =$input->param("repeatable$i")?1:0;
408 my $mandatory =$input->param("mandatory$i")?1:0;
409 my $kohafield =$kohafield[$i];
411 my $seealso =$seealso[$i];
412 my $authorised_value =$authorised_values[$i];
413 my $frameworkcode =$frameworkcodes[$i];
414 my $value_builder=$value_builder[$i];
415 my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
416 my $isurl = $input->param("isurl$i")?1:0;
418 unless (C4::Context->config('demo') eq 1) {
419 $sth->execute($authtypecode,
439 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
442 # END $OP eq ADD_VALIDATE
443 ################## DELETE_CONFIRM ##################################
444 # called by default form, used to confirm deletion of data in DB
445 } elsif ($op eq 'delete_confirm') {
446 my $dbh = C4::Context->dbh;
447 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
448 #FIXME : called with 2 bind variables when 3 are needed
449 $sth->execute($tagfield,$tagsubfield);
450 my $data=$sth->fetchrow_hashref;
452 $template->param(liblibrarian => $data->{'liblibrarian'},
453 tagsubfield => $data->{'tagsubfield'},
454 delete_link => $script_name,
455 tagfield =>$tagfield,
456 tagsubfield => $tagsubfield,
457 authtypecode => $authtypecode,
459 # END $OP eq DELETE_CONFIRM
460 ################## DELETE_CONFIRMED ##################################
461 # called by delete_confirm, used to effectively confirm deletion of data in DB
462 } elsif ($op eq 'delete_confirmed') {
463 my $dbh = C4::Context->dbh;
464 unless (C4::Context->config('demo') eq 1) {
465 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
466 $sth->execute($tagfield,$tagsubfield,$authtypecode);
469 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
471 $template->param(tagfield => $tagfield);
472 # END $OP eq DELETE_CONFIRMED
473 ################## DEFAULT ##################################
475 my ($count,$results)=StringSearch($tagfield,$authtypecode);
478 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
484 my %row_data; # get a fresh hash for the row data
485 $row_data{tagfield} = $results->[$i]{'tagfield'};
486 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
487 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
488 $row_data{kohafield} = $results->[$i]{'kohafield'};
489 $row_data{repeatable} = $results->[$i]{'repeatable'};
490 $row_data{mandatory} = $results->[$i]{'mandatory'};
491 $row_data{tab} = $results->[$i]{'tab'};
492 $row_data{seealso} = $results->[$i]{'seealso'};
493 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
494 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
495 $row_data{value_builder} = $results->[$i]{'value_builder'};
496 $row_data{hidden} = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
497 $row_data{isurl} = $results->[$i]{'isurl'};
498 $row_data{delete} = "$script_name?op=delete_confirm&tagfield=$tagfield&tagsubfield=".$results->[$i]{'tagsubfield'}."&authtypecode=$authtypecode";
499 $row_data{toggle} = $toggle;
500 if ($row_data{tab} eq -1) {
501 $row_data{subfield_ignored} = 1;
504 push(@loop_data, \%row_data);
506 $template->param(loop => \@loop_data);
507 $template->param(edit_tagfield => $tagfield,
508 edit_authtypecode => $authtypecode);
511 my $prevpage = $offset-$pagesize;
512 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
514 if ($offset+$pagesize<$count) {
515 my $nextpage =$offset+$pagesize;
516 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
518 } #---- END $OP eq DEFAULT
519 output_html_with_http_headers $input, $cookie, $template->output;