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);
44 =item build_authorized_values_list
46 builds list, depending on authorised value...
50 sub build_authorized_values_list {
51 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
53 my @authorised_values;
58 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
61 "select branchcode,branchname from branches order by branchname");
63 push @authorised_values, ""
64 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
66 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
67 push @authorised_values, $branchcode;
68 $authorised_lib{$branchcode} = $branchname;
73 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
76 "select itemtype,description from itemtypes order by description");
78 push @authorised_values, ""
79 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
83 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
84 push @authorised_values, $itemtype;
85 $authorised_lib{$itemtype} = $description;
87 $value = $itemtype unless ($value);
89 #---- "true" authorised value
92 $authorised_values_sth->execute(
93 $tagslib->{$tag}->{$subfield}->{authorised_value} );
95 push @authorised_values, ""
96 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
98 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
99 push @authorised_values, $value;
100 $authorised_lib{$value} = $lib;
103 return CGI::scrolling_list(
104 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
105 -values => \@authorised_values,
107 -labels => \%authorised_lib,
112 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
113 -class => "input_marceditor",
120 builds the <input ...> entry for a subfield.
125 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
127 my $index_subfield = CreateKey(); # create a specifique key for each subfield
129 $value =~ s/"/"/g;
131 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
132 my $max_length = 9999;
135 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
139 # if there is no value provided but a default value in parameters, get it
141 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
142 if (!defined $value) {
146 # get today date & replace YYYY, MM, DD if provided in the default value
147 my ( $year, $month, $day ) = Today
();
148 $month = sprintf( "%02d", $month );
149 $day = sprintf( "%02d", $day );
150 $value =~ s/YYYY/$year/g;
151 $value =~ s/MM/$month/g;
152 $value =~ s/DD/$day/g;
154 my $dbh = C4
::Context
->dbh;
155 my %subfield_data = (
157 subfield
=> $subfield,
158 marc_lib
=> substr( $tagslib->{$tag}->{$subfield}->{lib
}, 0, 22 ),
159 marc_lib_plain
=> $tagslib->{$tag}->{$subfield}->{lib
},
160 tag_mandatory
=> $tagslib->{$tag}->{mandatory
},
161 mandatory
=> $tagslib->{$tag}->{$subfield}->{mandatory
},
162 repeatable
=> $tagslib->{$tag}->{$subfield}->{repeatable
},
163 kohafield
=> $tagslib->{$tag}->{$subfield}->{kohafield
},
165 id
=> "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
168 if($subfield eq '@'){
169 $subfield_data{id
} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
171 $subfield_data{id
} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
174 if(exists $mandatory_z3950->{$tag.$subfield}){
175 $subfield_data{z3950_mandatory
} = $mandatory_z3950->{$tag.$subfield};
178 $subfield_data{visibility
} = "display:none;"
179 if ( ($tagslib->{$tag}->{$subfield}->{hidden
} % 2 == 1) and $value ne ''
180 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory
})
183 # it's an authorised field
184 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} ) {
185 $subfield_data{marc_value
} =
186 build_authorized_values_list
( $tag, $subfield, $value, $dbh,
187 $authorised_values_sth,$index_tag,$index_subfield );
189 # it's a thesaurus / authority field
191 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode
} ) {
192 $subfield_data{marc_value
} =
193 "<input type=\"text\"
194 id=\"".$subfield_data{id
}."\"
195 name=\"".$subfield_data{id
}."\"
197 class=\"input_marceditor readonly\"
199 readonly=\"readonly\" \/>
200 <a href=\"#\" class=\"buttonDot\"
201 onclick=\"openAuth(this.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode
}."'); return false;\" tabindex=\"1\" title=\"Tag Editor\">...</a>
203 # it's a plugin field
205 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
207 # opening plugin. Just check whether we are on a developer computer on a production one
208 # (the cgidir differs)
209 my $cgidir = C4
::Context
->intranetdir . "/cgi-bin/cataloguing/value_builder";
210 unless (-r
$cgidir and -d
$cgidir) {
211 $cgidir = C4
::Context
->intranetdir . "/cataloguing/value_builder";
213 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
214 do $plugin || die "Plugin Failed: ".$plugin;
217 $extended_param = plugin_parameters
( $dbh, $rec, $tagslib, $subfield_data{id
}, $tabloop );
219 my ( $function_name, $javascript ) = plugin_javascript
( $dbh, $rec, $tagslib, $subfield_data{id
}, $tabloop );
220 # my ( $function_name, $javascript,$extended_param );
222 $subfield_data{marc_value
} =
223 "<input tabindex=\"1\"
225 id=\"".$subfield_data{id
}."\"
227 maxlength=\"$max_length\"
228 name=\"".$subfield_data{id
}."\"
230 class=\"input_marceditor\"
231 onfocus=\"Focus$function_name($index_tag)\"
232 onblur=\"Blur$function_name($index_tag); \" \/>
233 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
235 # it's an hidden field
237 elsif ( $tag eq '' ) {
238 $subfield_data{marc_value
} =
239 "<input tabindex=\"1\"
241 id=\"".$subfield_data{id
}."\"
242 name=\"".$subfield_data{id
}."\"
244 maxlength=\"$max_length\"
248 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
249 $subfield_data{marc_value
} =
250 "<input type=\"text\"
251 id=\"".$subfield_data{id
}."\"
252 name=\"".$subfield_data{id
}."\"
253 class=\"input_marceditor\"
256 maxlength=\"$max_length\"
260 # it's a standard field
266 ( C4
::Context
->preference("marcflavour") eq "UNIMARC" && $tag >= 300
267 and $tag < 400 && $subfield eq 'a' )
270 && C4
::Context
->preference("marcflavour") eq "MARC21" )
273 $subfield_data{marc_value
} =
274 "<textarea cols=\"70\"
276 id=\"".$subfield_data{id
}."\"
277 name=\"".$subfield_data{id
}."\"
278 class=\"input_marceditor\"
281 maxlength=\"$max_length\"
286 $subfield_data{marc_value
} =
287 "<input type=\"text\"
288 id=\"".$subfield_data{id
}."\"
289 name=\"".$subfield_data{id
}."\"
293 maxlength=\"$max_length\"
294 class=\"input_marceditor\"
299 $subfield_data{'index_subfield'} = $index_subfield;
300 return \
%subfield_data;
303 =item format_indicator
305 Translate indicator value for output form - specifically, map
306 indicator = ' ' to ''. This is for the convenience of a cataloger
307 using a mouse to select an indicator input.
311 sub format_indicator
{
312 my $ind_value = shift;
313 return '' if not defined $ind_value;
314 return '' if $ind_value eq ' ';
320 Create a random value to set it into the input name
325 return int(rand(1000000));
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) {
360 my ($indicator1, $indicator2);
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 != -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 mandatory
=> $tagslib->{$tag}->{mandatory
},
443 subfield_loop
=> \
@subfields_data,
444 fixedfield
=> ($tag < 10)?
(1):(0),
447 if ($tag >= 10){ # no indicator for theses tag
448 $tag_data{indicator1
} = format_indicator
($field->indicator(1)),
449 $tag_data{indicator2
} = format_indicator
($field->indicator(2)),
451 push( @loop_data, \
%tag_data );
453 } # foreach $field end
455 # if breeding is empty
459 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
460 next if ( length $subfield != 1 );
461 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden
} <= -5 )
462 or ( $tagslib->{$tag}->{$subfield}->{hidden
} >= 4 ) )
463 ; #check for visibility flag
464 next if ( $tagslib->{$tag}->{$subfield}->{tab
} ne $tabloop );
468 $tag, $subfield, '', $index_tag, $tabloop, $record,
469 $authorised_values_sth,$input
473 if ( $#subfields_data >= 0 ) {
477 tag_lib
=> $tagslib->{$tag}->{lib
},
478 repeatable
=> $tagslib->{$tag}->{repeatable
},
479 mandatory
=> $tagslib->{$tag}->{mandatory
},
480 indicator1
=> $indicator1,
481 indicator2
=> $indicator2,
482 subfield_loop
=> \
@subfields_data,
483 tagfirstsubfield
=> $subfields_data[0],
484 fixedfield
=> ($tag < 10)?
(1):(0)
487 push @loop_data, \
%tag_data ;
491 if ( $#loop_data >= 0 ) {
494 innerloop
=> \
@loop_data,
498 $template->param( BIG_LOOP
=> \
@BIG_LOOP );
502 sub build_hidden_data
{
503 # build hidden data =>
504 # we store everything, even if we show only requested subfields.
508 foreach my $tag (keys %{$tagslib}) {
509 my $previous_tag = '';
511 # loop through each subfield
512 foreach my $subfield (keys %{$tagslib->{$tag}}) {
513 next if ($subfield eq 'lib');
514 next if ($subfield eq 'tab');
515 next if ($subfield eq 'mandatory');
516 next if ($subfield eq 'repeatable');
517 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
519 $subfield_data{marc_lib
}=$tagslib->{$tag}->{$subfield}->{lib
};
520 $subfield_data{marc_mandatory
}=$tagslib->{$tag}->{$subfield}->{mandatory
};
521 $subfield_data{marc_repeatable
}=$tagslib->{$tag}->{$subfield}->{repeatable
};
522 $subfield_data{marc_value
}="<input type=\"hidden\" name=\"field_value[]\">";
523 push(@loop_data, \
%subfield_data);
534 # ========================
536 #=========================
538 my $z3950 = $input->param('z3950');
539 my $error = $input->param('error');
540 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
541 my $op = $input->param('op');
542 my $nonav = $input->param('nonav');
543 my $myindex = $input->param('index');
544 my $linkid=$input->param('linkid');
545 my $authtypecode = $input->param('authtypecode');
547 my $dbh = C4
::Context
->dbh;
549 $authtypecode = $authid?
&GetAuthTypeCode
($authid): '';
552 my ($template, $loggedinuser, $cookie)
553 = get_template_and_user
({template_name
=> "authorities/authorities.tmpl",
556 authnotrequired
=> 0,
557 flagsrequired
=> {editauthorities
=> 1},
560 $template->param(nonav
=> $nonav,index=>$myindex,authtypecode
=>$authtypecode,);
561 $tagslib = GetTagsLabels
(1,$authtypecode);
564 $record = GetAuthority
($authid) if ($authid);
565 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
566 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
570 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField
("auth_header.authid",$authtypecode);
571 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField
("auth_header.authtypecode",$authtypecode);
574 #------------------------------------------------------------------------------------------------------------------------------
576 #------------------------------------------------------------------------------------------------------------------------------
578 my @tags = $input->param('tag');
579 my @subfields = $input->param('subfield');
580 my @values = $input->param('field_value');
581 # build indicator hash.
582 my @ind_tag = $input->param('ind_tag');
583 my @indicator = $input->param('indicator');
584 my $record = TransformHtmlToMarc
($input);
585 if (C4
::Context
->preference("marcflavour") eq "UNIMARC"){
586 unless ($record->field('100')){
587 use POSIX
qw(strftime);
588 my $string = strftime
( "%Y%m%d", localtime(time) );
589 # set 50 to position 26 is biblios, 13 if authorities
591 $string = sprintf( "%-*s", 35, $string );
592 substr( $string, $pos , 2, "50" );
593 $record->append_fields(MARC
::Field
->new('100','','',"a"=>$string));
597 my ($duplicateauthid,$duplicateauthvalue);
598 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority
($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
599 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
600 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
601 if (!$duplicateauthid or $confirm_not_duplicate) {
603 ModAuthority
($authid,$record,$authtypecode);
605 ($authid) = AddAuthority
($record,$authid,$authtypecode);
607 print $input->redirect("detail.pl?authid=$authid");
610 # it may be a duplicate, warn the user and do nothing
611 build_tabs
($template, $record, $dbh, $encoding,$input);
613 $template->param(authid
=>$authid,
614 duplicateauthid
=> $duplicateauthid,
615 duplicateauthvalue
=> $duplicateauthvalue,
618 } elsif ($op eq "delete") {
619 #------------------------------------------------------------------------------------------------------------------------------
620 &DelAuthority
($authid);
622 print $input->redirect("auth_finder.pl");
624 print $input->redirect("authorities-home.pl?authid=0");
628 if ($op eq "duplicate")
632 build_tabs
($template, $record, $dbh,$encoding,$input);
634 $template->param(oldauthtypetagfield
=>$oldauthtypetagfield, oldauthtypetagsubfield
=>$oldauthtypetagsubfield,
635 oldauthnumtagfield
=>$oldauthnumtagfield, oldauthnumtagsubfield
=>$oldauthnumtagsubfield,
636 authid
=> $authid , authtypecode
=>$authtypecode, );
639 $template->param(authid
=> $authid,
640 authtypecode
=> $authtypecode,
644 my $authtypes = getauthtypes
;
646 foreach my $thisauthtype (keys %$authtypes) {
647 my %row =(value
=> $thisauthtype,
648 selected
=> $thisauthtype eq $authtypecode,
649 authtypetext
=> $authtypes->{$thisauthtype}{'authtypetext'},
651 push @authtypesloop, \
%row;
654 $template->param(authtypesloop
=> \
@authtypesloop,
655 authtypetext
=> $authtypes->{$authtypecode}{'authtypetext'},
656 hide_marc
=> C4
::Context
->preference('hide_marc'),
658 output_html_with_http_headers
$input, $cookie, $template->output;