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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 use C4::AuthoritiesMarc;
28 use C4::Koha; # XXX subfield_is_koha_internal_p
29 use Date::Calc qw(Today);
30 use MARC::File::USMARC;
33 use vars qw( $tagslib);
34 use vars qw( $authorised_values_sth);
35 use vars qw( $is_a_modif );
37 my $itemtype; # created here because it can be used in build_authorized_values_list sub
38 our($authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
40 =item build_authorized_values_list
44 sub build_authorized_values_list ($$$$$$$) {
45 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
47 my @authorised_values;
50 # builds list, depending on authorised value...
53 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
56 "select branchcode,branchname from branches order by branchname");
58 push @authorised_values, ""
59 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
61 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
62 push @authorised_values, $branchcode;
63 $authorised_lib{$branchcode} = $branchname;
68 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
71 "select itemtype,description from itemtypes order by description");
73 push @authorised_values, ""
74 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
78 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
79 push @authorised_values, $itemtype;
80 $authorised_lib{$itemtype} = $description;
82 $value = $itemtype unless ($value);
84 #---- "true" authorised value
87 $authorised_values_sth->execute(
88 $tagslib->{$tag}->{$subfield}->{authorised_value} );
90 push @authorised_values, ""
91 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
93 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
94 push @authorised_values, $value;
95 $authorised_lib{$value} = $lib;
98 return CGI::scrolling_list(
99 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
100 -values => \@authorised_values,
102 -labels => \%authorised_lib,
107 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
108 -class => "input_marceditor",
114 builds the <input ...> entry for a subfield.
118 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
120 my $index_subfield = CreateKey(); # create a specifique key for each subfield
122 $value =~ s/"/"/g;
124 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
125 my $max_length = 9999;
128 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
132 # if there is no value provided but a default value in parameters, get it
134 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
135 if (!defined $value) {
139 # get today date & replace YYYY, MM, DD if provided in the default value
140 my ( $year, $month, $day ) = Today();
141 $month = sprintf( "%02d", $month );
142 $day = sprintf( "%02d", $day );
143 $value =~ s/YYYY/$year/g;
144 $value =~ s/MM/$month/g;
145 $value =~ s/DD/$day/g;
147 my $dbh = C4::Context->dbh;
148 my %subfield_data = (
150 subfield => $subfield,
151 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
152 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
153 tag_mandatory => $tagslib->{$tag}->{mandatory},
154 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
155 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
156 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
158 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
161 if($subfield eq '@'){
162 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
164 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
167 if(exists $mandatory_z3950->{$tag.$subfield}){
168 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
171 $subfield_data{visibility} = "display:none;"
172 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
173 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
176 # it's an authorised field
177 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
178 $subfield_data{marc_value} =
179 build_authorized_values_list( $tag, $subfield, $value, $dbh,
180 $authorised_values_sth,$index_tag,$index_subfield );
182 # it's a thesaurus / authority field
184 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
185 $subfield_data{marc_value} =
186 "<input type=\"text\"
187 id=\"".$subfield_data{id}."\"
188 name=\"".$subfield_data{id}."\"
190 class=\"input_marceditor\"
192 readonly=\"readonly\" \/>
193 <a href=\"#\" class=\"buttonDot\"
194 onclick=\"openAuth(this.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode}."'); return false;\" tabindex=\"1\" title=\"Tag Editor\">...</a>
196 # it's a plugin field
198 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
200 # opening plugin. Just check wether we are on a developper computer on a production one
201 # (the cgidir differs)
202 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
203 unless (-r $cgidir and -d $cgidir) {
204 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
206 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
207 do $plugin || die "Plugin Failed: ".$plugin;
210 $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
212 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
213 # my ( $function_name, $javascript,$extended_param );
215 $subfield_data{marc_value} =
216 "<input tabindex=\"1\"
218 id=\"".$subfield_data{id}."\"
220 maxlength=\"$max_length\"
221 name=\"".$subfield_data{id}."\"
223 class=\"input_marceditor\"
224 onfocus=\"Focus$function_name($index_tag)\"
225 onblur=\"Blur$function_name($index_tag); \" \/>
226 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
228 # it's an hidden field
230 elsif ( $tag eq '' ) {
231 $subfield_data{marc_value} =
232 "<input tabindex=\"1\"
234 id=\"".$subfield_data{id}."\"
235 name=\"".$subfield_data{id}."\"
237 maxlength=\"$max_length\"
241 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
242 $subfield_data{marc_value} =
243 "<input type=\"text\"
244 id=\"".$subfield_data{id}."\"
245 name=\"".$subfield_data{id}."\"
246 class=\"input_marceditor\"
249 maxlength=\"$max_length\"
253 # it's a standard field
259 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
260 and $tag < 400 && $subfield eq 'a' )
263 && C4::Context->preference("marcflavour") eq "MARC21" )
266 $subfield_data{marc_value} =
267 "<textarea cols=\"70\"
269 id=\"".$subfield_data{id}."\"
270 name=\"".$subfield_data{id}."\"
271 class=\"input_marceditor\"
274 maxlength=\"$max_length\"
279 $subfield_data{marc_value} =
280 "<input type=\"text\"
281 id=\"".$subfield_data{id}."\"
282 name=\"".$subfield_data{id}."\"
286 maxlength=\"$max_length\"
287 class=\"input_marceditor\"
292 $subfield_data{'index_subfield'} = $index_subfield;
293 return \%subfield_data;
296 =item format_indicator
298 Translate indicator value for output form - specifically, map
299 indicator = ' ' to ''. This is for the convenience of a cataloger
300 using a mouse to select an indicator input.
304 sub format_indicator {
305 my $ind_value = shift;
306 return '' if not defined $ind_value;
307 return '' if $ind_value eq ' ';
313 Create a random value to set it into the input name
318 return int(rand(1000000));
321 sub build_tabs ($$$$$) {
322 my ( $template, $record, $dbh, $encoding,$input ) = @_;
328 my $authorised_values_sth = $dbh->prepare(
329 "SELECT authorised_value,lib
330 FROM authorised_values
331 WHERE category=? ORDER BY lib"
334 # in this array, we will push all the 10 tabs
335 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
338 my @tab_data; # all tags to display
340 foreach my $used ( keys %$tagslib ){
341 push @tab_data,$used if not $seen{$used};
346 # loop through each tab 0 through 9
347 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
348 my @loop_data = (); #innerloop in the template.
350 foreach my $tag (sort @tab_data) {
353 my ($indicator1, $indicator2);
354 my $index_tag = CreateKey;
356 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
357 # if MARC::Record is empty => use tab as master loop.
358 if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
360 if ( $tag ne '000' ) {
361 @fields = $record->field($tag);
364 push @fields, $record->leader(); # if tag == 000
366 # loop through each field
367 foreach my $field (@fields) {
371 my ( $value, $subfield );
372 if ( $tag ne '000' ) {
373 $value = $field->data();
380 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
384 $tag, $subfield, $value, $index_tag, $tabloop, $record,
385 $authorised_values_sth,$input
390 my @subfields = $field->subfields();
391 foreach my $subfieldcount ( 0 .. $#subfields ) {
392 my $subfield = $subfields[$subfieldcount][0];
393 my $value = $subfields[$subfieldcount][1];
394 next if ( length $subfield != 1 );
395 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
399 $tag, $subfield, $value, $index_tag, $tabloop,
400 $record, $authorised_values_sth,$input
406 # now, loop again to add parameter subfield that are not in the MARC::Record
407 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
409 next if ( length $subfield != 1 );
410 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
411 next if ( $tag < 10 );
413 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
414 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
415 ); #check for visibility flag
416 next if ( defined( $field->subfield($subfield) ) );
420 $tag, $subfield, '', $index_tag, $tabloop, $record,
421 $authorised_values_sth,$input
425 if ( $#subfields_data >= 0 ) {
426 # build the tag entry.
427 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
428 # have twice the same "name" value, and cgi->param() will return only one, making
429 # all subfields to be merged in a single field.
433 tag_lib => $tagslib->{$tag}->{lib},
434 repeatable => $tagslib->{$tag}->{repeatable},
435 mandatory => $tagslib->{$tag}->{mandatory},
436 subfield_loop => \@subfields_data,
437 fixedfield => ($tag < 10)?(1):(0),
440 if ($tag >= 10){ # no indicator for theses tag
441 $tag_data{indicator1} = format_indicator($field->indicator(1)),
442 $tag_data{indicator2} = format_indicator($field->indicator(2)),
444 push( @loop_data, \%tag_data );
446 } # foreach $field end
448 # if breeding is empty
452 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
453 next if ( length $subfield != 1 );
454 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
455 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
456 ; #check for visibility flag
457 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
461 $tag, $subfield, '', $index_tag, $tabloop, $record,
462 $authorised_values_sth,$input
466 if ( $#subfields_data >= 0 ) {
470 tag_lib => $tagslib->{$tag}->{lib},
471 repeatable => $tagslib->{$tag}->{repeatable},
472 mandatory => $tagslib->{$tag}->{mandatory},
473 indicator1 => $indicator1,
474 indicator2 => $indicator2,
475 subfield_loop => \@subfields_data,
476 tagfirstsubfield => $subfields_data[0],
477 fixedfield => ($tag < 10)?(1):(0)
480 push @loop_data, \%tag_data ;
484 if ( $#loop_data >= 0 ) {
487 innerloop => \@loop_data,
491 $template->param( BIG_LOOP => \@BIG_LOOP );
495 sub build_hidden_data () {
496 # build hidden data =>
497 # we store everything, even if we show only requested subfields.
501 foreach my $tag (keys %{$tagslib}) {
502 my $previous_tag = '';
504 # loop through each subfield
505 foreach my $subfield (keys %{$tagslib->{$tag}}) {
506 next if ($subfield eq 'lib');
507 next if ($subfield eq 'tab');
508 next if ($subfield eq 'mandatory');
509 next if ($subfield eq 'repeatable');
510 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
512 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
513 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
514 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
515 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
516 push(@loop_data, \%subfield_data);
522 # ========================
524 #=========================
526 my $z3950 = $input->param('z3950');
527 my $error = $input->param('error');
528 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
529 my $op = $input->param('op');
530 my $nonav = $input->param('nonav');
531 my $myindex = $input->param('index');
532 my $linkid=$input->param('linkid');
533 my $authtypecode = $input->param('authtypecode');
535 my $dbh = C4::Context->dbh;
536 $authtypecode = &GetAuthTypeCode($authid) if !$authtypecode;
538 my ($template, $loggedinuser, $cookie)
539 = get_template_and_user({template_name => "authorities/authorities.tmpl",
542 authnotrequired => 0,
543 flagsrequired => {editauthorities => 1},
546 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
547 $tagslib = GetTagsLabels(1,$authtypecode);
550 $record = GetAuthority($authid) if ($authid);
551 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
552 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
556 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
557 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
560 #------------------------------------------------------------------------------------------------------------------------------
562 #------------------------------------------------------------------------------------------------------------------------------
564 my @tags = $input->param('tag');
565 my @subfields = $input->param('subfield');
566 my @values = $input->param('field_value');
567 # build indicator hash.
568 my @ind_tag = $input->param('ind_tag');
569 my @indicator = $input->param('indicator');
570 my @params = $input->param();
571 my $record = TransformHtmlToMarc(\@params,$input);
572 if (C4::Context->preference("marcflavour") eq "UNIMARC"){
573 unless ($record->field('100')){
574 use POSIX qw(strftime);
575 my $string = strftime( "%Y%m%d", localtime(time) );
576 # set 50 to position 26 is biblios, 13 if authorities
578 $string = sprintf( "%-*s", 35, $string );
579 substr( $string, $pos , 2, "50" );
580 $record->append_fields(MARC::Field->new('100','','',"a"=>$string));
584 my ($duplicateauthid,$duplicateauthvalue);
585 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
586 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
587 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
588 if (!$duplicateauthid or $confirm_not_duplicate) {
590 ModAuthority($authid,$record,$authtypecode);
592 ($authid) = AddAuthority($record,$authid,$authtypecode);
594 print $input->redirect("detail.pl?authid=$authid");
597 # it may be a duplicate, warn the user and do nothing
598 build_tabs($template, $record, $dbh, $encoding,$input);
600 $template->param(authid =>$authid,
601 duplicateauthid => $duplicateauthid,
602 duplicateauthvalue => $duplicateauthvalue,
605 } elsif ($op eq "delete") {
606 #------------------------------------------------------------------------------------------------------------------------------
607 &DelAuthority($authid);
609 print $input->redirect("auth_finder.pl");
611 print $input->redirect("authorities-home.pl?authid=0");
615 if ($op eq "duplicate")
619 build_tabs ($template, $record, $dbh,$encoding,$input);
621 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
622 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
623 authid => $authid , authtypecode=>$authtypecode, );
626 $template->param(authid => $authid,
627 authtypecode => $authtypecode,
631 my $authtypes = getauthtypes;
633 foreach my $thisauthtype (keys %$authtypes) {
634 my %row =(value => $thisauthtype,
635 selected => $thisauthtype eq $authtypecode,
636 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
638 push @authtypesloop, \%row;
641 $template->param(authtypesloop => \@authtypesloop,
642 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
643 hide_marc => C4::Context->preference('hide_marc'),
645 output_html_with_http_headers $input, $cookie, $template->output;