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);
33 my $sth=$dbh->prepare("Select * from auth_subfield_structure where (tagfield like ? and authtypecode=?) order by tagfield");
34 $sth->execute("$searchstring%",$authtypecode);
38 while (my $data=$sth->fetchrow_hashref){
44 return ($cnt,\@results);
47 sub auth_subfield_structure_exists {
48 my ($authtypecode, $tagfield, $tagsubfield) = @_;
49 my $dbh = C4::Context->dbh;
50 my $sql = "select tagfield from auth_subfield_structure where authtypecode = ? and tagfield = ? and tagsubfield = ?";
51 my $rows = $dbh->selectall_arrayref($sql, {}, $authtypecode, $tagfield, $tagsubfield);
56 my $tagfield=$input->param('tagfield');
57 my $tagsubfield=$input->param('tagsubfield');
58 my $authtypecode=$input->param('authtypecode');
59 my $pkfield="tagfield";
60 my $offset=$input->param('offset');
61 my $script_name="/cgi-bin/koha/admin/auth_subfields_structure.pl";
63 my ($template, $borrowernumber, $cookie)
64 = get_template_and_user({template_name => "admin/auth_subfields_structure.tmpl",
68 flagsrequired => {parameters => 1},
72 my $op = $input->param('op');
76 $template->param(script_name => $script_name,
78 authtypecode => $authtypecode,
79 $op => 1); # we show only the TMPL_VAR names $op
81 $template->param(script_name => $script_name,
83 authtypecode => $authtypecode,
84 else => 1); # we show only the TMPL_VAR names $op
87 ################## ADD_FORM ##################################
88 # called by default. Used to create form to add or modify a record
89 if ($op eq 'add_form') {
91 my $dbh = C4::Context->dbh;
92 my $more_subfields = $input->param("more_subfields")+1;
93 # builds kohafield tables
96 my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
98 while ((my $field) = $sth2->fetchrow_array) {
99 push @kohafields, "auth_header.".$field;
102 # build authorised value list
104 $sth2 = $dbh->prepare("select distinct category from authorised_values");
106 my @authorised_values;
107 push @authorised_values,"";
108 while ((my $category) = $sth2->fetchrow_array) {
109 push @authorised_values, $category;
111 push (@authorised_values,"branches");
112 push (@authorised_values,"itemtypes");
114 # build thesaurus categories list
116 $sth2 = $dbh->prepare("select authtypecode from auth_types");
120 while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
121 push @authtypes, $authtypecode;
124 # build value_builder list
125 my @value_builder=('');
127 # read value_builder directory.
128 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
129 # on a standard install, /cgi-bin need to be added.
130 # test one, then the other
131 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
132 unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
133 $cgidir = C4::Context->intranetdir;
134 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
136 while (my $line = readdir(DIR)) {
137 if ($line =~ /\.pl$/) {
138 push (@value_builder,$line);
141 @value_builder= sort {$a cmp $b} @value_builder;
145 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
146 $sth->execute($tagfield,$authtypecode);
150 while ($data =$sth->fetchrow_hashref) {
152 my %row_data; # get a fresh hash for the row data
158 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
161 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
175 -default=>$data->{'tab'},
180 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
182 -values=>['0','1','2'],
183 -labels => {'0'=>'Show','1'=>'Show Collapsed',
186 -default=>substr($data->{'hidden'},0,1),
190 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
192 -values=>['0','1','2'],
193 -labels => {'0'=>'Show','1'=>'Show Collapsed',
196 -default=>substr($data->{'hidden'},1,1),
200 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
202 -values=>['0','1','2'],
203 -labels => {'0'=>'Show','1'=>'Show Collapsed',
206 -default=>substr($data->{'hidden'}." ",2,1),
210 $row_data{tagsubfieldinput} = "<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
211 $row_data{tagsubfield} = $data->{'tagsubfield'};
212 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
213 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
214 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
215 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
217 -values=> \@kohafields,
218 -default=> "$data->{'kohafield'}",
222 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
223 -id=>"authorised_value$i",
224 -values=> \@authorised_values,
225 -default=>$data->{'authorised_value'},
230 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
231 -id=>"frameworkcode$i",
232 -values=> \@authtypes,
233 -default=>$data->{'frameworkcode'},
238 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
239 -id=>"value_builder$i",
240 -values=> \@value_builder,
241 -default=>$data->{'value_builder'},
247 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
248 -checked => $data->{'repeatable'}?'checked':'',
251 -id => "repeatable$i");
252 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
253 -checked => $data->{'mandatory'}?'checked':'',
256 -id => "mandatory$i");
257 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
258 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
260 -checked => $data->{'isurl'}?'checked':'',
264 $row_data{toggle} = $toggle;
265 push(@loop_data, \%row_data);
268 # add more_subfields empty lines for add if needed
269 for (my $i=1;$i<=$more_subfields;$i++) {
270 my %row_data; # get a fresh hash for the row data
271 $row_data{'new_subfield'} = 1;
272 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
275 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
294 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
296 -values=>['0','1','2'],
297 -labels => {'0'=>'Show','1'=>'Show Collapsed',
305 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
307 -values=>['0','1','2'],
308 -labels => {'0'=>'Show','1'=>'Show Collapsed',
315 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
317 -values=>['0','1','2'],
318 -labels => {'0'=>'Show','1'=>'Show Collapsed',
325 $row_data{tagsubfieldinput} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
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 my $dbh = C4::Context->dbh;
394 $template->param(tagfield => "$input->param('tagfield')");
395 # 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)
396 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
397 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)
398 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
399 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=?
400 where authtypecode=? and tagfield=? and tagsubfield=?");
401 my @tagsubfield = $input->param('tagsubfield');
402 my @liblibrarian = $input->param('liblibrarian');
403 my @libopac = $input->param('libopac');
404 my @kohafield = ''.$input->param('kohafield');
405 my @tab = $input->param('tab');
406 my @seealso = $input->param('seealso');
408 my @ohidden = $input->param('ohidden');
409 my @ihidden = $input->param('ihidden');
410 my @ehidden = $input->param('ehidden');
411 my @authorised_values = $input->param('authorised_value');
412 my $authtypecode = $input->param('authtypecode');
413 my @frameworkcodes = $input->param('frameworkcode');
414 my @value_builder =$input->param('value_builder');
415 for (my $i=0; $i<= $#tagsubfield ; $i++) {
416 my $tagfield =$input->param('tagfield');
417 my $tagsubfield =$tagsubfield[$i];
418 $tagsubfield="@" unless $tagsubfield ne '';
419 my $liblibrarian =$liblibrarian[$i];
420 my $libopac =$libopac[$i];
421 my $repeatable =$input->param("repeatable$i")?1:0;
422 my $mandatory =$input->param("mandatory$i")?1:0;
423 my $kohafield =$kohafield[$i];
425 my $seealso =$seealso[$i];
426 my $authorised_value =$authorised_values[$i];
427 my $frameworkcode =$frameworkcodes[$i];
428 my $value_builder=$value_builder[$i];
429 my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$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(
480 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
483 # END $OP eq ADD_VALIDATE
484 ################## DELETE_CONFIRM ##################################
485 # called by default form, used to confirm deletion of data in DB
486 } elsif ($op eq 'delete_confirm') {
487 my $dbh = C4::Context->dbh;
488 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
489 $sth->execute($tagfield,$tagsubfield,$authtypecode);
490 my $data=$sth->fetchrow_hashref;
492 $template->param(liblibrarian => $data->{'liblibrarian'},
493 tagsubfield => $data->{'tagsubfield'},
494 delete_link => $script_name,
495 tagfield =>$tagfield,
496 tagsubfield => $tagsubfield,
497 authtypecode => $authtypecode,
499 # END $OP eq DELETE_CONFIRM
500 ################## DELETE_CONFIRMED ##################################
501 # called by delete_confirm, used to effectively confirm deletion of data in DB
502 } elsif ($op eq 'delete_confirmed') {
503 my $dbh = C4::Context->dbh;
504 unless (C4::Context->config('demo') eq 1) {
505 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
506 $sth->execute($tagfield,$tagsubfield,$authtypecode);
509 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
511 $template->param(tagfield => $tagfield);
512 # END $OP eq DELETE_CONFIRMED
513 ################## DEFAULT ##################################
515 my ($count,$results)=string_search($tagfield,$authtypecode);
518 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
524 my %row_data; # get a fresh hash for the row data
525 $row_data{tagfield} = $results->[$i]{'tagfield'};
526 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
527 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
528 $row_data{kohafield} = $results->[$i]{'kohafield'};
529 $row_data{repeatable} = $results->[$i]{'repeatable'};
530 $row_data{mandatory} = $results->[$i]{'mandatory'};
531 $row_data{tab} = $results->[$i]{'tab'};
532 $row_data{seealso} = $results->[$i]{'seealso'};
533 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
534 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
535 $row_data{value_builder} = $results->[$i]{'value_builder'};
536 $row_data{hidden} = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
537 $row_data{isurl} = $results->[$i]{'isurl'};
538 $row_data{delete} = "$script_name?op=delete_confirm&tagfield=$tagfield&tagsubfield=".$results->[$i]{'tagsubfield'}."&authtypecode=$authtypecode";
539 $row_data{toggle} = $toggle;
540 if ($row_data{tab} eq -1) {
541 $row_data{subfield_ignored} = 1;
544 push(@loop_data, \%row_data);
546 $template->param(loop => \@loop_data);
547 $template->param(edit_tagfield => $tagfield,
548 edit_authtypecode => $authtypecode);
551 my $prevpage = $offset-$pagesize;
552 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
554 if ($offset+$pagesize<$count) {
555 my $nextpage =$offset+$pagesize;
556 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
558 } #---- END $OP eq DEFAULT
559 output_html_with_http_headers $input, $cookie, $template->output;