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);
40 ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
42 Find the given $subfield in the given $tag in the given
43 MARC::Record $record. If the subfield is found, returns
44 the (indicators, value) pair; otherwise, (undef, undef) is
50 my ($tagfield,$insubfield,$record,$encoding) = @_;
54 if ($record->field($tagfield)) {
55 push @result, $record->field($tagfield)->data();
60 foreach my $field ($record->field($tagfield)) {
61 my @subfields = $field->subfields();
62 foreach my $subfield (@subfields) {
63 if (@$subfield[0] eq $insubfield) {
64 push @result,@$subfield[1];
65 $indicator = $field->indicator(1).$field->indicator(2);
70 return($indicator,@result);
74 =item build_authorized_values_list
78 sub build_authorized_values_list ($$$$$$$) {
79 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
81 my @authorised_values;
84 # builds list, depending on authorised value...
87 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
90 "select branchcode,branchname from branches order by branchname");
92 push @authorised_values, ""
93 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
95 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
96 push @authorised_values, $branchcode;
97 $authorised_lib{$branchcode} = $branchname;
102 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
105 "select itemtype,description from itemtypes order by description");
107 push @authorised_values, ""
108 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
112 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
113 push @authorised_values, $itemtype;
114 $authorised_lib{$itemtype} = $description;
116 $value = $itemtype unless ($value);
118 #---- "true" authorised value
121 $authorised_values_sth->execute(
122 $tagslib->{$tag}->{$subfield}->{authorised_value} );
124 push @authorised_values, ""
125 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
127 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
128 push @authorised_values, $value;
129 $authorised_lib{$value} = $lib;
132 return CGI::scrolling_list(
133 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
134 -values => \@authorised_values,
136 -labels => \%authorised_lib,
141 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
142 -class => "input_marceditor",
148 builds the <input ...> entry for a subfield.
152 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
154 my $index_subfield = CreateKey(); # create a specifique key for each subfield
156 $value =~ s/"/"/g;
158 # if there is no value provided but a default value in parameters, get it
160 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
162 # get today date & replace YYYY, MM, DD if provided in the default value
163 my ( $year, $month, $day ) = Today();
164 $month = sprintf( "%02d", $month );
165 $day = sprintf( "%02d", $day );
166 $value =~ s/YYYY/$year/g;
167 $value =~ s/MM/$month/g;
168 $value =~ s/DD/$day/g;
170 my $dbh = C4::Context->dbh;
171 my %subfield_data = (
173 subfield => $subfield,
174 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
175 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
176 tag_mandatory => $tagslib->{$tag}->{mandatory},
177 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
178 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
179 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
181 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
184 if($subfield eq '@'){
185 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
187 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
190 if(exists $mandatory_z3950->{$tag.$subfield}){
191 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
194 $subfield_data{visibility} = "display:none;"
195 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
196 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
199 # it's an authorised field
200 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
201 $subfield_data{marc_value} =
202 build_authorized_values_list( $tag, $subfield, $value, $dbh,
203 $authorised_values_sth,$index_tag,$index_subfield );
205 # it's a thesaurus / authority field
207 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
208 $subfield_data{marc_value} =
209 "<input type=\"text\"
210 id=\"".$subfield_data{id}."\"
211 name=\"".$subfield_data{id}."\"
213 class=\"input_marceditor\"
215 disabled=\"disabled\"
216 readonly=\"readonly\" \/>
217 <span class=\"buttonDot\"
218 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}')\">...</span>
220 # it's a plugin field
222 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
224 # opening plugin. Just check wether we are on a developper computer on a production one
225 # (the cgidir differs)
226 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
227 unless (-r $cgidir and -d $cgidir) {
228 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
230 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
231 do $plugin || die "Plugin Failed: ".$plugin;
232 my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
233 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
234 # my ( $function_name, $javascript,$extended_param );
236 $subfield_data{marc_value} =
237 "<input tabindex=\"1\"
239 id=\"".$subfield_data{id}."\"
242 name=\"".$subfield_data{id}."\"
244 class=\"input_marceditor\"
245 onfocus=\"Focus$function_name($index_tag)\"
246 onblur=\"Blur$function_name($index_tag); \" \/>
247 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
249 # it's an hidden field
251 elsif ( $tag eq '' ) {
252 $subfield_data{marc_value} =
253 "<input tabindex=\"1\"
255 id=\"".$subfield_data{id}."\"
256 name=\"".$subfield_data{id}."\"
262 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
263 $subfield_data{marc_value} =
264 "<input type=\"text\"
265 id=\"".$subfield_data{id}."\"
266 name=\"".$subfield_data{id}."\"
267 class=\"input_marceditor\"
274 # it's a standard field
280 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
281 and $tag < 400 && $subfield eq 'a' )
284 && C4::Context->preference("marcflavour") eq "MARC21" )
287 $subfield_data{marc_value} =
288 "<textarea cols=\"70\"
290 id=\"".$subfield_data{id}."\"
291 name=\"".$subfield_data{id}."\"
292 class=\"input_marceditor\"
300 $subfield_data{marc_value} =
301 "<input type=\"text\"
302 id=\"".$subfield_data{id}."\"
303 name=\"".$subfield_data{id}."\"
308 class=\"input_marceditor\"
313 $subfield_data{'index_subfield'} = $index_subfield;
314 return \%subfield_data;
319 Create a random value to set it into the input name
324 return int(rand(1000000));
327 sub build_tabs ($$$$$) {
328 my ( $template, $record, $dbh, $encoding,$input ) = @_;
334 my $authorised_values_sth = $dbh->prepare(
335 "SELECT authorised_value,lib
336 FROM authorised_values
337 WHERE category=? ORDER BY lib"
340 # in this array, we will push all the 10 tabs
341 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
344 my @tab_data; # all tags to display
346 foreach my $used ( keys %$tagslib ){
347 push @tab_data,$used if not $seen{$used};
352 # loop through each tab 0 through 9
353 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
354 my @loop_data = (); #innerloop in the template.
356 foreach my $tag (sort @tab_data) {
360 my $index_tag = CreateKey;
362 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
363 # if MARC::Record is empty => use tab as master loop.
364 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
366 if ( $tag ne '000' ) {
367 @fields = $record->field($tag);
370 push @fields, $record->leader(); # if tag == 000
372 # loop through each field
373 foreach my $field (@fields) {
377 my ( $value, $subfield );
378 if ( $tag ne '000' ) {
379 $value = $field->data();
386 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
390 $tag, $subfield, $value, $index_tag, $tabloop, $record,
391 $authorised_values_sth,$input
396 my @subfields = $field->subfields();
397 foreach my $subfieldcount ( 0 .. $#subfields ) {
398 my $subfield = $subfields[$subfieldcount][0];
399 my $value = $subfields[$subfieldcount][1];
400 next if ( length $subfield != 1 );
401 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
405 $tag, $subfield, $value, $index_tag, $tabloop,
406 $record, $authorised_values_sth,$input
412 # now, loop again to add parameter subfield that are not in the MARC::Record
413 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
415 next if ( length $subfield != 1 );
416 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
417 next if ( $tag < 10 );
419 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
420 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
421 ); #check for visibility flag
422 next if ( defined( $field->subfield($subfield) ) );
426 $tag, $subfield, '', $index_tag, $tabloop, $record,
427 $authorised_values_sth,$input
431 if ( $#subfields_data >= 0 ) {
432 # build the tag entry.
433 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
434 # have twice the same "name" value, and cgi->param() will return only one, making
435 # all subfields to be merged in a single field.
439 tag_lib => $tagslib->{$tag}->{lib},
440 repeatable => $tagslib->{$tag}->{repeatable},
441 subfield_loop => \@subfields_data,
442 fixedfield => ($tag < 10)?(1):(0),
445 if ($tag >= 010){ # no indicator for theses tag
446 $tag_data{indicator} = $field->indicator(1).$field->indicator(2);
448 push( @loop_data, \%tag_data );
450 } # foreach $field end
452 # if breeding is empty
456 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
457 next if ( length $subfield != 1 );
458 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
459 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
460 ; #check for visibility flag
461 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
465 $tag, $subfield, '', $index_tag, $tabloop, $record,
466 $authorised_values_sth,$input
470 if ( $#subfields_data >= 0 ) {
474 tag_lib => $tagslib->{$tag}->{lib},
475 repeatable => $tagslib->{$tag}->{repeatable},
476 indicator => $indicator,
477 subfield_loop => \@subfields_data,
478 tagfirstsubfield => $subfields_data[0],
479 fixedfield => ($tag < 10)?(1):(0)
482 push @loop_data, \%tag_data ;
486 if ( $#loop_data >= 0 ) {
489 innerloop => \@loop_data,
493 $template->param( BIG_LOOP => \@BIG_LOOP );
497 sub build_hidden_data () {
498 # build hidden data =>
499 # we store everything, even if we show only requested subfields.
503 foreach my $tag (keys %{$tagslib}) {
504 my $previous_tag = '';
506 # loop through each subfield
507 foreach my $subfield (keys %{$tagslib->{$tag}}) {
508 next if ($subfield eq 'lib');
509 next if ($subfield eq 'tab');
510 next if ($subfield eq 'mandatory');
511 next if ($subfield eq 'repeatable');
512 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
514 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
515 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
516 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
517 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
518 push(@loop_data, \%subfield_data);
524 # ========================
526 #=========================
528 my $z3950 = $input->param('z3950');
529 my $error = $input->param('error');
530 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
531 my $op = $input->param('op');
532 my $nonav = $input->param('nonav');
533 my $myindex = $input->param('index');
534 my $linkid=$input->param('linkid');
535 my $authtypecode = $input->param('authtypecode');
537 my $dbh = C4::Context->dbh;
538 $authtypecode = &GetAuthTypeCode($authid) if !$authtypecode;
540 my ($template, $loggedinuser, $cookie)
541 = get_template_and_user({template_name => "authorities/authorities.tmpl",
544 authnotrequired => 0,
545 flagsrequired => {editauthorities => 1},
548 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
549 $tagslib = GetTagsLabels(1,$authtypecode);
552 $record = GetAuthority($authid) if ($authid);
553 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
554 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
558 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
559 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
562 #------------------------------------------------------------------------------------------------------------------------------
564 #------------------------------------------------------------------------------------------------------------------------------
566 my @tags = $input->param('tag');
567 my @subfields = $input->param('subfield');
568 my @values = $input->param('field_value');
569 # build indicator hash.
570 my @ind_tag = $input->param('ind_tag');
571 my @indicator = $input->param('indicator');
572 my @params = $input->param();
573 my $record = TransformHtmlToMarc(\@params,$input);
574 if (C4::Context->preference("marcflavour") eq "UNIMARC"){
575 unless ($record->field('100')){
576 use POSIX qw(strftime);
577 my $string = strftime( "%Y%m%d", localtime(time) );
578 # set 50 to position 26 is biblios, 13 if authorities
580 $string = sprintf( "%-*s", 35, $string );
581 substr( $string, $pos , 2, "50" );
582 $record->append_fields(MARC::Field->new('100','','',"a"=>$string));
586 my ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
587 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
588 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
589 if (!$duplicateauthid or $confirm_not_duplicate) {
591 ModAuthority($authid,$record,$authtypecode,1);
593 ($authid) = AddAuthority($record,$authid,$authtypecode);
595 print $input->redirect("detail.pl?authid=$authid");
598 # it may be a duplicate, warn the user and do nothing
599 build_tabs($template, $record, $dbh, $encoding,$input);
601 $template->param(authid =>$authid,
602 duplicateauthid => $duplicateauthid,
603 duplicateauthvalue => $duplicateauthvalue,
606 } elsif ($op eq "delete") {
607 #------------------------------------------------------------------------------------------------------------------------------
608 &AUTHdelauthority($authid);
610 print $input->redirect("auth_finder.pl");
612 print $input->redirect("authorities-home.pl?authid=0");
616 if ($op eq "duplicate")
620 build_tabs ($template, $record, $dbh,$encoding,$input);
622 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
623 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
624 authid => $authid , authtypecode=>$authtypecode, );
627 $template->param(authid => $authid,
628 authtypecode => $authtypecode,
632 my $authtypes = getauthtypes;
634 foreach my $thisauthtype (keys %$authtypes) {
635 my $selected = 1 if $thisauthtype eq $authtypecode;
636 my %row =(value => $thisauthtype,
637 selected => $selected,
638 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
640 push @authtypesloop, \%row;
643 $template->param(authtypesloop => \@authtypesloop,
644 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
645 hide_marc => C4::Context->preference('hide_marc'),
647 output_html_with_http_headers $input, $cookie, $template->output;