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
23 use C4::Interface::CGI::Output;
30 my ($searchstring,$authtypecode)=@_;
31 my $dbh = C4::Context->dbh;
32 $searchstring=~ s/\'/\\\'/g;
33 my @data=split(' ',$searchstring);
35 my $sth=$dbh->prepare("Select * from auth_subfield_structure where (tagfield like ? and authtypecode=?) order by tagfield");
36 $sth->execute("$searchstring%",$authtypecode);
40 while (my $data=$sth->fetchrow_hashref){
47 return ($cnt,\@results);
51 my $tagfield=$input->param('tagfield');
52 my $tagsubfield=$input->param('tagsubfield');
53 my $authtypecode=$input->param('authtypecode');
54 my $pkfield="tagfield";
55 my $offset=$input->param('offset');
56 my $script_name="/cgi-bin/koha/admin/auth_subfields_structure.pl";
58 my ($template, $borrowernumber, $cookie)
59 = get_template_and_user({template_name => "admin/auth_subfields_structure.tmpl",
63 flagsrequired => {parameters => 1},
67 my $op = $input->param('op');
71 $template->param(script_name => $script_name,
73 authtypecode => $authtypecode,
74 $op => 1); # we show only the TMPL_VAR names $op
76 $template->param(script_name => $script_name,
78 authtypecode => $authtypecode,
79 else => 1); # we show only the TMPL_VAR names $op
82 ################## ADD_FORM ##################################
83 # called by default. Used to create form to add or modify a record
84 if ($op eq 'add_form') {
86 my $dbh = C4::Context->dbh;
87 my $more_subfields = $input->param("more_subfields")+1;
88 # builds kohafield tables
91 my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
93 while ((my $field) = $sth2->fetchrow_array) {
94 push @kohafields, "auth_header.".$field;
97 # build authorised value list
99 $sth2 = $dbh->prepare("select distinct category from authorised_values");
101 my @authorised_values;
102 push @authorised_values,"";
103 while ((my $category) = $sth2->fetchrow_array) {
104 push @authorised_values, $category;
106 push (@authorised_values,"branches");
107 push (@authorised_values,"itemtypes");
109 # build thesaurus categories list
111 $sth2 = $dbh->prepare("select authtypecode from auth_types");
115 while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
116 push @authtypes, $authtypecode;
119 # build value_builder list
120 my @value_builder=('');
122 # read value_builder directory.
123 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
124 # on a standard install, /cgi-bin need to be added.
125 # test one, then the other
126 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
127 unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
128 $cgidir = C4::Context->intranetdir;
129 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
131 while (my $line = readdir(DIR)) {
132 if ($line =~ /\.pl$/) {
133 push (@value_builder,$line);
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 -labels => {'-1' =>'ignore','0'=>'0',
157 -default=>$data->{'tab'},
162 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
164 -values=>['0','1','2'],
165 -labels => {'0'=>'Show','1'=>'Show Collapsed',
168 -default=>substr($data->{'hidden'},0,1),
172 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
174 -values=>['0','1','2'],
175 -labels => {'0'=>'Show','1'=>'Show Collapsed',
178 -default=>substr($data->{'hidden'},1,1),
182 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
184 -values=>['0','1','2'],
185 -labels => {'0'=>'Show','1'=>'Show Collapsed',
188 -default=>substr($data->{'hidden'}." ",2,1),
192 $row_data{tagsubfield} =$data->{'tagsubfield'}."<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\">";
193 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
194 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
195 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
196 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
198 -values=> \@kohafields,
199 -default=> "$data->{'kohafield'}",
203 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
204 -id=>'authorised_value',
205 -values=> \@authorised_values,
206 -default=>$data->{'authorised_value'},
211 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
212 -id=>'frameworkcode',
213 -values=> \@authtypes,
214 -default=>$data->{'frameworkcode'},
219 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
220 -id=>'value_builder',
221 -values=> \@value_builder,
222 -default=>$data->{'value_builder'},
228 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
229 -checked => $data->{'repeatable'}?'checked':'',
232 -id => "repeatable$i");
233 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
234 -checked => $data->{'mandatory'}?'checked':'',
237 -id => "mandatory$i");
238 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
239 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
241 -checked => $data->{'isurl'}?'checked':'',
244 $row_data{link} = CGI::checkbox( -name => "link$i",
246 -checked => $data->{'link'}?'checked':'',
250 $row_data{toggle} = $toggle;
251 # $row_data{link} = CGI::escapeHTML($data->{'link'});
252 push(@loop_data, \%row_data);
255 # add more_subfields empty lines for add if needed
256 for (my $i=1;$i<=$more_subfields;$i++) {
257 my %row_data; # get a fresh hash for the row data
258 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
261 -labels => {'-1' =>'ignore','0'=>'0',
268 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
270 -values=>['0','1','2'],
271 -labels => {'0'=>'Show','1'=>'Show Collapsed',
279 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
281 -values=>['0','1','2'],
282 -labels => {'0'=>'Show','1'=>'Show Collapsed',
289 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
291 -values=>['0','1','2'],
292 -labels => {'0'=>'Show','1'=>'Show Collapsed',
299 $row_data{tagsubfield} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\">";
300 $row_data{liblibrarian} = "";
301 $row_data{libopac} = "";
302 $row_data{seealso} = "";
303 $row_data{hidden} = "000";
304 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
305 -id => "repeatable$i",
309 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
310 -id => "mandatory$i",
314 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
319 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield',
320 -id => "kohafield$i",
321 -values=> \@kohafields,
326 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
327 -id=>'frameworkcode',
328 -values=> \@authtypes,
329 -default=>$data->{'frameworkcode'},
334 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
335 -id => 'authorised_value',
336 -values=> \@authorised_values,
341 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
342 -id=>'value_builder',
343 -values=> \@value_builder,
344 -default=>$data->{'value_builder'},
349 $row_data{link} = CGI::checkbox( -name => "link",
354 # $row_data{link} = CGI::escapeHTML($data->{'link'});
355 $row_data{toggle} = $toggle;
357 push(@loop_data, \%row_data);
359 $template->param('use-heading-flags-p' => 1);
360 $template->param('heading-edit-subfields-p' => 1);
361 $template->param(action => "Edit subfields",
362 tagfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />$tagfield",
364 more_subfields => $more_subfields,
365 more_tag => $tagfield);
367 # END $OP eq ADD_FORM
368 ################## ADD_VALIDATE ##################################
369 # called by add_form, used to insert/modify data in DB
370 } elsif ($op eq 'add_validate') {
371 my $dbh = C4::Context->dbh;
372 $template->param(tagfield => "$input->param('tagfield')");
373 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, link)
374 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
375 my @tagsubfield = $input->param('tagsubfield');
376 my @liblibrarian = $input->param('liblibrarian');
377 my @libopac = $input->param('libopac');
378 my @kohafield = $input->param('kohafield');
379 my @tab = $input->param('tab');
380 my @seealso = $input->param('seealso');
382 my @ohidden = $input->param('ohidden');
383 my @ihidden = $input->param('ihidden');
384 my @ehidden = $input->param('ehidden');
385 my @authorised_values = $input->param('authorised_value');
386 my $authtypecode = $input->param('authtypecode');
387 my @frameworkcodes = $input->param('frameworkcode');
388 my @value_builder =$input->param('value_builder');
389 my @link =$input->param('link');
390 for (my $i=0; $i<= $#tagsubfield ; $i++) {
391 my $tagfield =$input->param('tagfield');
392 my $tagsubfield =$tagsubfield[$i];
393 $tagsubfield="@" unless $tagsubfield ne '';
394 my $liblibrarian =$liblibrarian[$i];
395 my $libopac =$libopac[$i];
396 my $repeatable =$input->param("repeatable$i")?1:0;
397 my $mandatory =$input->param("mandatory$i")?1:0;
398 my $kohafield =$kohafield[$i];
400 my $seealso =$seealso[$i];
401 my $authorised_value =$authorised_values[$i];
402 my $frameworkcode =$frameworkcodes[$i];
403 my $value_builder=$value_builder[$i];
404 my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
405 my $isurl = $input->param("isurl$i")?1:0;
406 my $link = $input->param("link$i")?1:0;
408 unless (C4::Context->config('demo') eq 1) {
409 $sth->execute($authtypecode,
430 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
433 # END $OP eq ADD_VALIDATE
434 ################## DELETE_CONFIRM ##################################
435 # called by default form, used to confirm deletion of data in DB
436 } elsif ($op eq 'delete_confirm') {
437 my $dbh = C4::Context->dbh;
438 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
439 #FIXME : called with 2 bind variables when 3 are needed
440 $sth->execute($tagfield,$tagsubfield);
441 my $data=$sth->fetchrow_hashref;
443 $template->param(liblibrarian => $data->{'liblibrarian'},
444 tagsubfield => $data->{'tagsubfield'},
445 delete_link => $script_name,
446 tagfield =>$tagfield,
447 tagsubfield => $tagsubfield,
448 authtypecode => $authtypecode,
450 # END $OP eq DELETE_CONFIRM
451 ################## DELETE_CONFIRMED ##################################
452 # called by delete_confirm, used to effectively confirm deletion of data in DB
453 } elsif ($op eq 'delete_confirmed') {
454 my $dbh = C4::Context->dbh;
455 unless (C4::Context->config('demo') eq 1) {
456 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
457 $sth->execute($tagfield,$tagsubfield,$authtypecode);
460 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
462 $template->param(tagfield => $tagfield);
463 # END $OP eq DELETE_CONFIRMED
464 ################## DEFAULT ##################################
466 my ($count,$results)=StringSearch($tagfield,$authtypecode);
469 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
475 my %row_data; # get a fresh hash for the row data
476 $row_data{tagfield} = $results->[$i]{'tagfield'};
477 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
478 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
479 $row_data{kohafield} = $results->[$i]{'kohafield'};
480 $row_data{repeatable} = $results->[$i]{'repeatable'};
481 $row_data{mandatory} = $results->[$i]{'mandatory'};
482 $row_data{tab} = $results->[$i]{'tab'};
483 $row_data{seealso} = $results->[$i]{'seealso'};
484 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
485 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
486 $row_data{value_builder} = $results->[$i]{'value_builder'};
487 $row_data{hidden} = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
488 $row_data{isurl} = $results->[$i]{'isurl'};
489 $row_data{link} = $results->[$i]{'link'};
490 $row_data{delete} = "$script_name?op=delete_confirm&tagfield=$tagfield&tagsubfield=".$results->[$i]{'tagsubfield'}."&authtypecode=$authtypecode";
491 $row_data{toggle} = $toggle;
492 if ($row_data{tab} eq -1) {
493 $row_data{subfield_ignored} = 1;
496 push(@loop_data, \%row_data);
498 $template->param(loop => \@loop_data);
499 $template->param(edit_tagfield => $tagfield,
500 edit_authtypecode => $authtypecode);
503 my $prevpage = $offset-$pagesize;
504 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
506 if ($offset+$pagesize<$count) {
507 my $nextpage =$offset+$pagesize;
508 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
510 } #---- END $OP eq DEFAULT
511 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
512 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
513 IntranetNav => C4::Context->preference("IntranetNav"),
515 output_html_with_http_headers $input, $cookie, $template->output;