3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
28 my ($searchstring,$authtypecode)=@_;
29 my $dbh = C4::Context->dbh;
30 $searchstring=~ s/\'/\\\'/g;
31 my @data=split(' ',$searchstring);
32 my $sth=$dbh->prepare("Select * from auth_subfield_structure where (tagfield like ? and authtypecode=?) order by tagfield");
33 $sth->execute("$searchstring%",$authtypecode);
34 my $results = $sth->fetchall_arrayref({});
35 return (scalar(@$results), $results);
38 sub auth_subfield_structure_exists {
39 my ($authtypecode, $tagfield, $tagsubfield) = @_;
40 my $dbh = C4::Context->dbh;
41 my $sql = "select tagfield from auth_subfield_structure where authtypecode = ? and tagfield = ? and tagsubfield = ?";
42 my $rows = $dbh->selectall_arrayref($sql, {}, $authtypecode, $tagfield, $tagsubfield);
47 my $tagfield = $input->param('tagfield');
48 my $tagsubfield = $input->param('tagsubfield');
49 my $authtypecode = $input->param('authtypecode');
50 my $offset = $input->param('offset');
51 my $op = $input->param('op') || '';
52 my $script_name = "/cgi-bin/koha/admin/auth_subfields_structure.pl";
54 my ($template, $borrowernumber, $cookie) = get_template_and_user(
55 { template_name => "admin/auth_subfields_structure.tmpl",
59 flagsrequired => { parameters => 1 },
67 $template->param(script_name => $script_name,
69 authtypecode => $authtypecode,
70 $op => 1); # we show only the TMPL_VAR names $op
72 $template->param(script_name => $script_name,
74 authtypecode => $authtypecode,
75 else => 1); # we show only the TMPL_VAR names $op
78 my $dbh = C4::Context->dbh;
79 ################## ADD_FORM ##################################
80 # called by default. Used to create form to add or modify a record
81 if ($op eq 'add_form') {
83 my $more_subfields = $input->param("more_subfields")+1;
84 # builds kohafield tables
87 my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
89 while ((my $field) = $sth2->fetchrow_array) {
90 push @kohafields, "auth_header.".$field;
93 # build authorised value list
94 $sth2 = $dbh->prepare("select distinct category from authorised_values");
96 my @authorised_values;
97 push @authorised_values,"";
98 while ((my $category) = $sth2->fetchrow_array) {
99 push @authorised_values, $category;
101 push (@authorised_values,"branches");
102 push (@authorised_values,"itemtypes");
104 # build thesaurus categories list
105 $sth2 = $dbh->prepare("select authtypecode from auth_types");
109 while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
110 push @authtypes, $authtypecode;
113 # build value_builder list
114 my @value_builder=('');
116 # read value_builder directory.
117 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
118 # on a standard install, /cgi-bin need to be added.
119 # test one, then the other
120 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
121 unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
122 $cgidir = C4::Context->intranetdir;
123 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
125 while (my $line = readdir(DIR)) {
126 if ($line =~ /\.pl$/) {
127 push (@value_builder,$line);
130 @value_builder= sort {$a cmp $b} @value_builder;
134 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
135 $sth->execute($tagfield,$authtypecode);
139 while ($data =$sth->fetchrow_hashref) {
141 my %row_data; # get a fresh hash for the row data
147 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
150 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
164 -default=>$data->{'tab'},
169 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
171 #-values=>['0','1','2'],
172 #-labels => {'0'=>'Show','1'=>'Show Collapsed',
176 -labels => {'0'=>'Show All','-5'=>'Hide All',},
177 -default=>$data->{'hidden'},
182 #$row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
184 # -values=>['0','1','2'],
185 # -labels => {'0'=>'Show','1'=>'Show Collapsed',
188 # -default=>substr($data->{'hidden'},1,1),
192 #$row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
194 # -values=>['0','1','2'],
195 # -labels => {'0'=>'Show','1'=>'Show Collapsed',
198 # -default=>substr($data->{'hidden'}." ",2,1),
202 $row_data{tagsubfieldinput} = "<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
203 $row_data{tagsubfield} = $data->{'tagsubfield'};
204 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
205 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
206 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
207 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
209 -values=> \@kohafields,
210 -default=> "$data->{'kohafield'}",
214 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
215 -id=>"authorised_value$i",
216 -values=> \@authorised_values,
217 -default=>$data->{'authorised_value'},
222 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
223 -id=>"frameworkcode$i",
224 -values=> \@authtypes,
225 -default=>$data->{'frameworkcode'},
230 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
231 -id=>"value_builder$i",
232 -values=> \@value_builder,
233 -default=>$data->{'value_builder'},
239 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
240 -checked => $data->{'repeatable'}?'checked':'',
243 -id => "repeatable$i");
244 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
245 -checked => $data->{'mandatory'}?'checked':'',
248 -id => "mandatory$i");
249 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
250 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
252 -checked => $data->{'isurl'}?'checked':'',
256 $row_data{toggle} = $toggle;
257 push(@loop_data, \%row_data);
260 # add more_subfields empty lines for add if needed
261 for (my $i=1;$i<=$more_subfields;$i++) {
262 my %row_data; # get a fresh hash for the row data
263 $row_data{'new_subfield'} = 1;
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',
293 -labels => {'0'=>'Show All','-5'=>'Hide All',},
295 -default=>$data->{'hidden'},
301 #$row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
303 # -values=>['0','1','2'],
304 # -labels => {'0'=>'Show','1'=>'Show Collapsed',
311 #$row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
313 # -values=>['0','1','2'],
314 # -labels => {'0'=>'Show','1'=>'Show Collapsed',
321 $row_data{tagsubfieldinput} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
322 $row_data{tagsubfieldinput} =
323 "<label><input type=\"text\" name=\"tagsubfield\" value=\""
324 . $data->{'tagsubfield'}
325 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" /></label>";
326 $row_data{tagsubfield} = $data->{'tagsubfield'};
327 $row_data{liblibrarian} = "";
328 $row_data{libopac} = "";
329 $row_data{seealso} = "";
330 $row_data{hidden} = "000";
331 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
332 -id => "repeatable$i",
336 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
337 -id => "mandatory$i",
341 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
346 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield',
347 -id => "kohafield$i",
348 -values=> \@kohafields,
353 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
354 -id=>'frameworkcode',
355 -values=> \@authtypes,
356 -default=>$data->{'frameworkcode'},
361 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
362 -id => 'authorised_value',
363 -values=> \@authorised_values,
368 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
369 -id=>'value_builder',
370 -values=> \@value_builder,
371 -default=>$data->{'value_builder'},
376 $row_data{toggle} = $toggle;
378 push(@loop_data, \%row_data);
380 $template->param('use-heading-flags-p' => 1);
381 $template->param('heading-edit-subfields-p' => 1);
382 $template->param(action => "Edit subfields",
383 tagfield => $tagfield,
384 tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
386 more_subfields => $more_subfields,
387 more_tag => $tagfield);
389 # END $OP eq ADD_FORM
390 ################## ADD_VALIDATE ##################################
391 # called by add_form, used to insert/modify data in DB
392 } elsif ($op eq 'add_validate') {
393 $template->param(tagfield => "$input->param('tagfield')");
394 # 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)
395 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
396 my $sth_insert = $dbh->prepare("insert into auth_subfield_structure (authtypecode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,frameworkcode,value_builder,hidden,isurl)
397 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
398 my $sth_update = $dbh->prepare("update auth_subfield_structure set authtypecode=?, tagfield=?, tagsubfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, kohafield=?, tab=?, seealso=?, authorised_value=?, frameworkcode=?, value_builder=?, hidden=?, isurl=?
399 where authtypecode=? and tagfield=? and tagsubfield=?");
400 my @tagsubfield = $input->param('tagsubfield');
401 my @liblibrarian = $input->param('liblibrarian');
402 my @libopac = $input->param('libopac');
403 my @kohafield = ''.$input->param('kohafield');
404 my @tab = $input->param('tab');
405 my @seealso = $input->param('seealso');
407 my @ohidden = $input->param('ohidden');
408 #my @ihidden = $input->param('ihidden');
409 #my @ehidden = $input->param('ehidden');
410 my @authorised_values = $input->param('authorised_value');
411 my $authtypecode = $input->param('authtypecode');
412 my @frameworkcodes = $input->param('frameworkcode');
413 my @value_builder =$input->param('value_builder');
414 for (my $i=0; $i<= $#tagsubfield ; $i++) {
415 my $tagfield =$input->param('tagfield');
416 my $tagsubfield =$tagsubfield[$i];
417 $tagsubfield="@" unless $tagsubfield ne '';
418 my $liblibrarian =$liblibrarian[$i];
419 my $libopac =$libopac[$i];
420 my $repeatable =$input->param("repeatable$i")?1:0;
421 my $mandatory =$input->param("mandatory$i")?1:0;
422 my $kohafield =$kohafield[$i];
424 my $seealso =$seealso[$i];
425 my $authorised_value =$authorised_values[$i];
426 my $frameworkcode =$frameworkcodes[$i];
427 my $value_builder=$value_builder[$i];
428 #my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
429 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
430 my $isurl = $input->param("isurl$i")?1:0;
432 unless (C4::Context->config('demo') eq 1) {
433 if (auth_subfield_structure_exists($authtypecode, $tagfield, $tagsubfield)) {
434 $sth_update->execute(
457 $sth_insert->execute(
478 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
481 # END $OP eq ADD_VALIDATE
482 ################## DELETE_CONFIRM ##################################
483 # called by default form, used to confirm deletion of data in DB
484 } elsif ($op eq 'delete_confirm') {
485 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
486 $sth->execute($tagfield,$tagsubfield,$authtypecode);
487 my $data=$sth->fetchrow_hashref;
488 $template->param(liblibrarian => $data->{'liblibrarian'},
489 tagsubfield => $data->{'tagsubfield'},
490 delete_link => $script_name,
491 tagfield =>$tagfield,
492 tagsubfield => $tagsubfield,
493 authtypecode => $authtypecode,
495 # END $OP eq DELETE_CONFIRM
496 ################## DELETE_CONFIRMED ##################################
497 # called by delete_confirm, used to effectively confirm deletion of data in DB
498 } elsif ($op eq 'delete_confirmed') {
499 unless (C4::Context->config('demo') eq 1) {
500 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
501 $sth->execute($tagfield,$tagsubfield,$authtypecode);
503 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
505 $template->param(tagfield => $tagfield);
506 # END $OP eq DELETE_CONFIRMED
507 ################## DEFAULT ##################################
509 my ($count,$results)=string_search($tagfield,$authtypecode);
512 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
518 my %row_data; # get a fresh hash for the row data
519 $row_data{tagfield} = $results->[$i]{'tagfield'};
520 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
521 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
522 $row_data{kohafield} = $results->[$i]{'kohafield'};
523 $row_data{repeatable} = $results->[$i]{'repeatable'};
524 $row_data{mandatory} = $results->[$i]{'mandatory'};
525 $row_data{tab} = $results->[$i]{'tab'};
526 $row_data{seealso} = $results->[$i]{'seealso'};
527 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
528 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
529 $row_data{value_builder} = $results->[$i]{'value_builder'};
530 $row_data{hidden} = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
531 $row_data{isurl} = $results->[$i]{'isurl'};
532 $row_data{delete} = "$script_name?op=delete_confirm&tagfield=$tagfield&tagsubfield=".$results->[$i]{'tagsubfield'}."&authtypecode=$authtypecode";
533 $row_data{toggle} = $toggle;
534 if ($row_data{tab} eq -1) {
535 $row_data{subfield_ignored} = 1;
538 push(@loop_data, \%row_data);
540 $template->param(loop => \@loop_data);
541 $template->param(edit_tagfield => $tagfield,
542 edit_authtypecode => $authtypecode);
545 my $prevpage = $offset-$pagesize;
546 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
548 if ($offset+$pagesize<$count) {
549 my $nextpage =$offset+$pagesize;
550 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
552 } #---- END $OP eq DEFAULT
553 output_html_with_http_headers $input, $cookie, $template->output;