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 <span class=\"buttonDot\"
248 onclick=\"Clic$function_name('$subfield_data{id}')\">...</a>
250 # it's an hidden field
252 elsif ( $tag eq '' ) {
253 $subfield_data{marc_value} =
254 "<input tabindex=\"1\"
256 id=\"".$subfield_data{id}."\"
257 name=\"".$subfield_data{id}."\"
263 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
264 $subfield_data{marc_value} =
265 "<input type=\"text\"
266 id=\"".$subfield_data{id}."\"
267 name=\"".$subfield_data{id}."\"
268 class=\"input_marceditor\"
275 # it's a standard field
281 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
282 and $tag < 400 && $subfield eq 'a' )
285 && C4::Context->preference("marcflavour") eq "MARC21" )
288 $subfield_data{marc_value} =
289 "<textarea cols=\"70\"
291 id=\"".$subfield_data{id}."\"
292 name=\"".$subfield_data{id}."\"
293 class=\"input_marceditor\"
301 $subfield_data{marc_value} =
302 "<input type=\"text\"
303 id=\"".$subfield_data{id}."\"
304 name=\"".$subfield_data{id}."\"
309 class=\"input_marceditor\"
314 $subfield_data{'index_subfield'} = $index_subfield;
315 return \%subfield_data;
320 Create a random value to set it into the input name
325 return int(rand(1000000));
328 sub build_tabs ($$$$$) {
329 my ( $template, $record, $dbh, $encoding,$input ) = @_;
335 my $authorised_values_sth = $dbh->prepare(
336 "SELECT authorised_value,lib
337 FROM authorised_values
338 WHERE category=? ORDER BY lib"
341 # in this array, we will push all the 10 tabs
342 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
345 my @tab_data; # all tags to display
347 foreach my $used ( keys %$tagslib ){
348 push @tab_data,$used if not $seen{$used};
353 # loop through each tab 0 through 9
354 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
355 my @loop_data = (); #innerloop in the template.
357 foreach my $tag (sort @tab_data) {
361 my $index_tag = CreateKey;
363 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
364 # if MARC::Record is empty => use tab as master loop.
365 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
367 if ( $tag ne '000' ) {
368 @fields = $record->field($tag);
371 push @fields, $record->leader(); # if tag == 000
373 # loop through each field
374 foreach my $field (@fields) {
378 my ( $value, $subfield );
379 if ( $tag ne '000' ) {
380 $value = $field->data();
387 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
391 $tag, $subfield, $value, $index_tag, $tabloop, $record,
392 $authorised_values_sth,$input
397 my @subfields = $field->subfields();
398 foreach my $subfieldcount ( 0 .. $#subfields ) {
399 my $subfield = $subfields[$subfieldcount][0];
400 my $value = $subfields[$subfieldcount][1];
401 next if ( length $subfield != 1 );
402 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
406 $tag, $subfield, $value, $index_tag, $tabloop,
407 $record, $authorised_values_sth,$input
413 # now, loop again to add parameter subfield that are not in the MARC::Record
414 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
416 next if ( length $subfield != 1 );
417 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
418 next if ( $tag < 10 );
420 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
421 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
422 ); #check for visibility flag
423 next if ( defined( $field->subfield($subfield) ) );
427 $tag, $subfield, '', $index_tag, $tabloop, $record,
428 $authorised_values_sth,$input
432 if ( $#subfields_data >= 0 ) {
433 # build the tag entry.
434 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
435 # have twice the same "name" value, and cgi->param() will return only one, making
436 # all subfields to be merged in a single field.
440 tag_lib => $tagslib->{$tag}->{lib},
441 repeatable => $tagslib->{$tag}->{repeatable},
442 subfield_loop => \@subfields_data,
443 fixedfield => ($tag < 10)?(1):(0),
446 if ($tag >= 010){ # no indicator for theses tag
447 $tag_data{indicator} = $field->indicator(1).$field->indicator(2);
449 push( @loop_data, \%tag_data );
451 } # foreach $field end
453 # if breeding is empty
457 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
458 next if ( length $subfield != 1 );
459 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
460 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
461 ; #check for visibility flag
462 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
466 $tag, $subfield, '', $index_tag, $tabloop, $record,
467 $authorised_values_sth,$input
471 if ( $#subfields_data >= 0 ) {
475 tag_lib => $tagslib->{$tag}->{lib},
476 repeatable => $tagslib->{$tag}->{repeatable},
477 indicator => $indicator,
478 subfield_loop => \@subfields_data,
479 tagfirstsubfield => $subfields_data[0],
480 fixedfield => ($tag < 10)?(1):(0)
483 push @loop_data, \%tag_data ;
487 if ( $#loop_data >= 0 ) {
490 innerloop => \@loop_data,
494 $template->param( BIG_LOOP => \@BIG_LOOP );
498 sub build_hidden_data () {
499 # build hidden data =>
500 # we store everything, even if we show only requested subfields.
504 foreach my $tag (keys %{$tagslib}) {
505 my $previous_tag = '';
507 # loop through each subfield
508 foreach my $subfield (keys %{$tagslib->{$tag}}) {
509 next if ($subfield eq 'lib');
510 next if ($subfield eq 'tab');
511 next if ($subfield eq 'mandatory');
512 next if ($subfield eq 'repeatable');
513 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
515 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
516 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
517 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
518 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
519 push(@loop_data, \%subfield_data);
525 # ========================
527 #=========================
529 my $z3950 = $input->param('z3950');
530 my $error = $input->param('error');
531 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
532 my $op = $input->param('op');
533 my $nonav = $input->param('nonav');
534 my $myindex = $input->param('index');
535 my $linkid=$input->param('linkid');
536 my $authtypecode = $input->param('authtypecode');
538 my $dbh = C4::Context->dbh;
539 $authtypecode = &GetAuthTypeCode($authid) if !$authtypecode;
541 my ($template, $loggedinuser, $cookie)
542 = get_template_and_user({template_name => "authorities/authorities.tmpl",
545 authnotrequired => 0,
546 flagsrequired => {editauthorities => 1},
549 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
550 $tagslib = GetTagsLabels(1,$authtypecode);
553 $record = GetAuthority($authid) if ($authid);
554 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
555 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
559 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
560 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
563 #------------------------------------------------------------------------------------------------------------------------------
565 #------------------------------------------------------------------------------------------------------------------------------
567 my @tags = $input->param('tag');
568 my @subfields = $input->param('subfield');
569 my @values = $input->param('field_value');
570 # build indicator hash.
571 my @ind_tag = $input->param('ind_tag');
572 my @indicator = $input->param('indicator');
573 my @params = $input->param();
574 my $record = TransformHtmlToMarc(\@params,$input);
575 if (C4::Context->preference("marcflavour") eq "UNIMARC"){
576 unless ($record->field('100')){
577 use POSIX qw(strftime);
578 my $string = strftime( "%Y%m%d", localtime(time) );
579 # set 50 to position 26 is biblios, 13 if authorities
581 $string = sprintf( "%-*s", 35, $string );
582 substr( $string, $pos , 2, "50" );
583 $record->append_fields(MARC::Field->new('100','','',"a"=>$string));
587 my ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
588 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
589 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
590 if (!$duplicateauthid or $confirm_not_duplicate) {
592 ModAuthority($authid,$record,$authtypecode,1);
594 ($authid) = AddAuthority($record,$authid,$authtypecode);
596 print $input->redirect("detail.pl?authid=$authid");
599 # it may be a duplicate, warn the user and do nothing
600 build_tabs($template, $record, $dbh, $encoding,$input);
602 $template->param(authid =>$authid,
603 duplicateauthid => $duplicateauthid,
604 duplicateauthvalue => $duplicateauthvalue,
607 } elsif ($op eq "delete") {
608 #------------------------------------------------------------------------------------------------------------------------------
609 &AUTHdelauthority($authid);
611 print $input->redirect("auth_finder.pl");
613 print $input->redirect("authorities-home.pl?authid=0");
617 if ($op eq "duplicate")
621 build_tabs ($template, $record, $dbh,$encoding,$input);
623 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
624 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
625 authid => $authid , authtypecode=>$authtypecode, );
628 $template->param(authid => $authid,
629 authtypecode => $authtypecode,
633 my $authtypes = getauthtypes;
635 foreach my $thisauthtype (keys %$authtypes) {
636 my $selected = 1 if $thisauthtype eq $authtypecode;
637 my %row =(value => $thisauthtype,
638 selected => $selected,
639 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
641 push @authtypesloop, \%row;
644 $template->param(authtypesloop => \@authtypesloop,
645 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
646 hide_marc => C4::Context->preference('hide_marc'),
648 output_html_with_http_headers $input, $cookie, $template->output;