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
25 use C4::AuthoritiesMarc;
27 use C4::Koha; # XXX subfield_is_koha_internal_p
28 use Date::Calc qw(Today);
29 use MARC::File::USMARC;
32 use vars qw( $tagslib);
33 use vars qw( $authorised_values_sth);
34 use vars qw( $is_a_modif );
36 my $itemtype; # created here because it can be used in build_authorized_values_list sub
37 our($authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
39 =item build_authorized_values_list
43 sub build_authorized_values_list ($$$$$$$) {
44 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
46 my @authorised_values;
49 # builds list, depending on authorised value...
52 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
55 "select branchcode,branchname from branches order by branchname");
57 push @authorised_values, ""
58 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
60 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
61 push @authorised_values, $branchcode;
62 $authorised_lib{$branchcode} = $branchname;
67 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
70 "select itemtype,description from itemtypes order by description");
72 push @authorised_values, ""
73 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
77 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
78 push @authorised_values, $itemtype;
79 $authorised_lib{$itemtype} = $description;
81 $value = $itemtype unless ($value);
83 #---- "true" authorised value
86 $authorised_values_sth->execute(
87 $tagslib->{$tag}->{$subfield}->{authorised_value} );
89 push @authorised_values, ""
90 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
92 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
93 push @authorised_values, $value;
94 $authorised_lib{$value} = $lib;
97 return CGI::scrolling_list(
98 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
99 -values => \@authorised_values,
101 -labels => \%authorised_lib,
106 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
107 -class => "input_marceditor",
113 builds the <input ...> entry for a subfield.
117 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
119 my $index_subfield = CreateKey(); # create a specifique key for each subfield
121 $value =~ s/"/"/g;
123 # if there is no value provided but a default value in parameters, get it
125 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
127 # get today date & replace YYYY, MM, DD if provided in the default value
128 my ( $year, $month, $day ) = Today();
129 $month = sprintf( "%02d", $month );
130 $day = sprintf( "%02d", $day );
131 $value =~ s/YYYY/$year/g;
132 $value =~ s/MM/$month/g;
133 $value =~ s/DD/$day/g;
135 my $dbh = C4::Context->dbh;
136 my %subfield_data = (
138 subfield => $subfield,
139 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
140 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
141 tag_mandatory => $tagslib->{$tag}->{mandatory},
142 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
143 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
144 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
146 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
149 if($subfield eq '@'){
150 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
152 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
155 if(exists $mandatory_z3950->{$tag.$subfield}){
156 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
159 $subfield_data{visibility} = "display:none;"
160 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
161 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
164 # it's an authorised field
165 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
166 $subfield_data{marc_value} =
167 build_authorized_values_list( $tag, $subfield, $value, $dbh,
168 $authorised_values_sth,$index_tag,$index_subfield );
170 # it's a thesaurus / authority field
172 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
173 $subfield_data{marc_value} =
174 "<input type=\"text\"
175 id=\"".$subfield_data{id}."\"
176 name=\"".$subfield_data{id}."\"
178 class=\"input_marceditor\"
180 disabled=\"disabled\"
181 readonly=\"readonly\" \/>
182 <span class=\"buttonDot\"
183 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}')\">...</span>
185 # it's a plugin field
187 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
189 # opening plugin. Just check wether we are on a developper computer on a production one
190 # (the cgidir differs)
191 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
192 unless (-r $cgidir and -d $cgidir) {
193 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
195 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
196 do $plugin || die "Plugin Failed: ".$plugin;
197 my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
198 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
199 # my ( $function_name, $javascript,$extended_param );
201 $subfield_data{marc_value} =
202 "<input tabindex=\"1\"
204 id=\"".$subfield_data{id}."\"
207 name=\"".$subfield_data{id}."\"
209 class=\"input_marceditor\"
210 onfocus=\"Focus$function_name($index_tag)\"
211 onblur=\"Blur$function_name($index_tag); \" \/>
212 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
214 # it's an hidden field
216 elsif ( $tag eq '' ) {
217 $subfield_data{marc_value} =
218 "<input tabindex=\"1\"
220 id=\"".$subfield_data{id}."\"
221 name=\"".$subfield_data{id}."\"
227 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
228 $subfield_data{marc_value} =
229 "<input type=\"text\"
230 id=\"".$subfield_data{id}."\"
231 name=\"".$subfield_data{id}."\"
232 class=\"input_marceditor\"
239 # it's a standard field
245 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
246 and $tag < 400 && $subfield eq 'a' )
249 && C4::Context->preference("marcflavour") eq "MARC21" )
252 $subfield_data{marc_value} =
253 "<textarea cols=\"70\"
255 id=\"".$subfield_data{id}."\"
256 name=\"".$subfield_data{id}."\"
257 class=\"input_marceditor\"
265 $subfield_data{marc_value} =
266 "<input type=\"text\"
267 id=\"".$subfield_data{id}."\"
268 name=\"".$subfield_data{id}."\"
273 class=\"input_marceditor\"
278 $subfield_data{'index_subfield'} = $index_subfield;
279 return \%subfield_data;
282 =item format_indicator
284 Translate indicator value for output form - specifically, map
285 indicator = ' ' to ''. This is for the convenience of a cataloger
286 using a mouse to select an indicator input.
290 sub format_indicator {
291 my $ind_value = shift;
292 return '' if not defined $ind_value;
293 return '' if $ind_value eq ' ';
299 Create a random value to set it into the input name
304 return int(rand(1000000));
307 sub build_tabs ($$$$$) {
308 my ( $template, $record, $dbh, $encoding,$input ) = @_;
314 my $authorised_values_sth = $dbh->prepare(
315 "SELECT authorised_value,lib
316 FROM authorised_values
317 WHERE category=? ORDER BY lib"
320 # in this array, we will push all the 10 tabs
321 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
324 my @tab_data; # all tags to display
326 foreach my $used ( keys %$tagslib ){
327 push @tab_data,$used if not $seen{$used};
332 # loop through each tab 0 through 9
333 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
334 my @loop_data = (); #innerloop in the template.
336 foreach my $tag (sort @tab_data) {
339 my ($indicator1, $indicator2);
340 my $index_tag = CreateKey;
342 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
343 # if MARC::Record is empty => use tab as master loop.
344 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
346 if ( $tag ne '000' ) {
347 @fields = $record->field($tag);
350 push @fields, $record->leader(); # if tag == 000
352 # loop through each field
353 foreach my $field (@fields) {
357 my ( $value, $subfield );
358 if ( $tag ne '000' ) {
359 $value = $field->data();
366 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
370 $tag, $subfield, $value, $index_tag, $tabloop, $record,
371 $authorised_values_sth,$input
376 my @subfields = $field->subfields();
377 foreach my $subfieldcount ( 0 .. $#subfields ) {
378 my $subfield = $subfields[$subfieldcount][0];
379 my $value = $subfields[$subfieldcount][1];
380 next if ( length $subfield != 1 );
381 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
385 $tag, $subfield, $value, $index_tag, $tabloop,
386 $record, $authorised_values_sth,$input
392 # now, loop again to add parameter subfield that are not in the MARC::Record
393 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
395 next if ( length $subfield != 1 );
396 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
397 next if ( $tag < 10 );
399 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
400 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
401 ); #check for visibility flag
402 next if ( defined( $field->subfield($subfield) ) );
406 $tag, $subfield, '', $index_tag, $tabloop, $record,
407 $authorised_values_sth,$input
411 if ( $#subfields_data >= 0 ) {
412 # build the tag entry.
413 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
414 # have twice the same "name" value, and cgi->param() will return only one, making
415 # all subfields to be merged in a single field.
419 tag_lib => $tagslib->{$tag}->{lib},
420 repeatable => $tagslib->{$tag}->{repeatable},
421 subfield_loop => \@subfields_data,
422 fixedfield => ($tag < 10)?(1):(0),
425 if ($tag >= 010){ # no indicator for theses tag
426 $tag_data{indicator1} = format_indicator($field->indicator(1)),
427 $tag_data{indicator2} = format_indicator($field->indicator(2)),
429 push( @loop_data, \%tag_data );
431 } # foreach $field end
433 # if breeding is empty
437 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
438 next if ( length $subfield != 1 );
439 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
440 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
441 ; #check for visibility flag
442 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
446 $tag, $subfield, '', $index_tag, $tabloop, $record,
447 $authorised_values_sth,$input
451 if ( $#subfields_data >= 0 ) {
455 tag_lib => $tagslib->{$tag}->{lib},
456 repeatable => $tagslib->{$tag}->{repeatable},
457 indicator1 => $indicator1,
458 indicator2 => $indicator2,
459 subfield_loop => \@subfields_data,
460 tagfirstsubfield => $subfields_data[0],
461 fixedfield => ($tag < 10)?(1):(0)
464 push @loop_data, \%tag_data ;
468 if ( $#loop_data >= 0 ) {
471 innerloop => \@loop_data,
475 $template->param( BIG_LOOP => \@BIG_LOOP );
479 sub build_hidden_data () {
480 # build hidden data =>
481 # we store everything, even if we show only requested subfields.
485 foreach my $tag (keys %{$tagslib}) {
486 my $previous_tag = '';
488 # loop through each subfield
489 foreach my $subfield (keys %{$tagslib->{$tag}}) {
490 next if ($subfield eq 'lib');
491 next if ($subfield eq 'tab');
492 next if ($subfield eq 'mandatory');
493 next if ($subfield eq 'repeatable');
494 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
496 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
497 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
498 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
499 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
500 push(@loop_data, \%subfield_data);
506 # ========================
508 #=========================
510 my $z3950 = $input->param('z3950');
511 my $error = $input->param('error');
512 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
513 my $op = $input->param('op');
514 my $nonav = $input->param('nonav');
515 my $myindex = $input->param('index');
516 my $linkid=$input->param('linkid');
517 my $authtypecode = $input->param('authtypecode');
519 my $dbh = C4::Context->dbh;
520 $authtypecode = &GetAuthTypeCode($authid) if !$authtypecode;
522 my ($template, $loggedinuser, $cookie)
523 = get_template_and_user({template_name => "authorities/authorities.tmpl",
526 authnotrequired => 0,
527 flagsrequired => {editauthorities => 1},
530 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
531 $tagslib = GetTagsLabels(1,$authtypecode);
534 $record = GetAuthority($authid) if ($authid);
535 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
536 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
540 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
541 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
544 #------------------------------------------------------------------------------------------------------------------------------
546 #------------------------------------------------------------------------------------------------------------------------------
548 my @tags = $input->param('tag');
549 my @subfields = $input->param('subfield');
550 my @values = $input->param('field_value');
551 # build indicator hash.
552 my @ind_tag = $input->param('ind_tag');
553 my @indicator = $input->param('indicator');
554 my @params = $input->param();
555 my $record = TransformHtmlToMarc(\@params,$input);
556 if (C4::Context->preference("marcflavour") eq "UNIMARC"){
557 unless ($record->field('100')){
558 use POSIX qw(strftime);
559 my $string = strftime( "%Y%m%d", localtime(time) );
560 # set 50 to position 26 is biblios, 13 if authorities
562 $string = sprintf( "%-*s", 35, $string );
563 substr( $string, $pos , 2, "50" );
564 $record->append_fields(MARC::Field->new('100','','',"a"=>$string));
568 my ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
569 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
570 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
571 if (!$duplicateauthid or $confirm_not_duplicate) {
573 ModAuthority($authid,$record,$authtypecode,1);
575 ($authid) = AddAuthority($record,$authid,$authtypecode);
577 print $input->redirect("detail.pl?authid=$authid");
580 # it may be a duplicate, warn the user and do nothing
581 build_tabs($template, $record, $dbh, $encoding,$input);
583 $template->param(authid =>$authid,
584 duplicateauthid => $duplicateauthid,
585 duplicateauthvalue => $duplicateauthvalue,
588 } elsif ($op eq "delete") {
589 #------------------------------------------------------------------------------------------------------------------------------
590 &AUTHdelauthority($authid);
592 print $input->redirect("auth_finder.pl");
594 print $input->redirect("authorities-home.pl?authid=0");
598 if ($op eq "duplicate")
602 build_tabs ($template, $record, $dbh,$encoding,$input);
604 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
605 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
606 authid => $authid , authtypecode=>$authtypecode, );
609 $template->param(authid => $authid,
610 authtypecode => $authtypecode,
614 my $authtypes = getauthtypes;
616 foreach my $thisauthtype (keys %$authtypes) {
617 my $selected = 1 if $thisauthtype eq $authtypecode;
618 my %row =(value => $thisauthtype,
619 selected => $selected,
620 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
622 push @authtypesloop, \%row;
625 $template->param(authtypesloop => \@authtypesloop,
626 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
627 hide_marc => C4::Context->preference('hide_marc'),
629 output_html_with_http_headers $input, $cookie, $template->output;