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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
26 use C4::AuthoritiesMarc;
27 use C4::ImportBatch; #GetImportRecordMarc
30 use Date::Calc qw(Today);
31 use MARC::File::USMARC;
34 use Koha::Authority::Types;
35 use vars qw( $tagslib);
36 use vars qw( $authorised_values_sth);
37 use vars qw( $is_a_modif );
39 my $itemtype; # created here because it can be used in build_authorized_values_list sub
40 our($authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
46 =item build_authorized_values_list
48 builds list, depending on authorised value...
52 sub MARCfindbreeding_auth {
54 my ($marc, $encoding) = GetImportRecordMarc($id);
56 my $record = MARC::Record->new_from_usmarc($marc);
57 if ( !defined(ref($record)) ) {
60 return $record, $encoding;
67 sub build_authorized_values_list {
68 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
70 my @authorised_values;
75 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
78 "select branchcode,branchname from branches order by branchname");
80 push @authorised_values, ""
81 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
83 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
84 push @authorised_values, $branchcode;
85 $authorised_lib{$branchcode} = $branchname;
88 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
89 push @authorised_values, ""
90 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
91 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
94 my $itemtypes = GetItemTypes( style => 'array' );
95 for my $itemtype ( @$itemtypes ) {
96 push @authorised_values, $itemtype->{itemtype};
97 $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description};
99 $value = $itemtype unless ($value);
101 #---- "true" authorised value
104 $authorised_values_sth->execute(
105 $tagslib->{$tag}->{$subfield}->{authorised_value} );
107 push @authorised_values, ""
108 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
109 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
111 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
112 push @authorised_values, $value;
113 $authorised_lib{$value} = $lib;
118 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
119 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
120 values => \@authorised_values,
121 labels => \%authorised_lib,
129 builds the <input ...> entry for a subfield.
134 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
136 my $index_subfield = CreateKey(); # create a specifique key for each subfield
138 $value =~ s/"/"/g;
140 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
141 my $max_length = 9999;
144 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
148 # if there is no value provided but a default value in parameters, get it
150 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
151 if (!defined $value) {
155 # get today date & replace YYYY, MM, DD if provided in the default value
156 my ( $year, $month, $day ) = Today();
157 $month = sprintf( "%02d", $month );
158 $day = sprintf( "%02d", $day );
159 $value =~ s/YYYY/$year/g;
160 $value =~ s/MM/$month/g;
161 $value =~ s/DD/$day/g;
163 my $dbh = C4::Context->dbh;
165 # map '@' as "subfield" label for fixed fields
166 # to something that's allowed in a div id.
167 my $id_subfield = $subfield;
168 $id_subfield = "00" if $id_subfield eq "@";
170 my %subfield_data = (
172 subfield => $id_subfield,
173 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
174 tag_mandatory => $tagslib->{$tag}->{mandatory},
175 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
176 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
177 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
179 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
181 random => CreateKey(),
184 if(exists $mandatory_z3950->{$tag.$subfield}){
185 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
188 $subfield_data{visibility} = "display:none;"
189 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
190 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
193 # it's an authorised field
194 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
195 $subfield_data{marc_value} =
196 build_authorized_values_list( $tag, $subfield, $value, $dbh,
197 $authorised_values_sth,$index_tag,$index_subfield );
199 # it's a thesaurus / authority field
201 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
202 $subfield_data{marc_value} = {
204 id => $subfield_data{id},
205 name => $subfield_data{id},
207 authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode},
210 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) { # plugin
211 require Koha::FrameworkPlugin;
212 my $plugin = Koha::FrameworkPlugin->new({
213 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
215 my $pars= { dbh => $dbh, record => $rec, tagslib =>$tagslib,
216 id => $subfield_data{id}, tabloop => $tabloop };
217 $plugin->build( $pars );
218 if( !$plugin->errstr ) {
219 $subfield_data{marc_value} = {
221 id => $subfield_data{id},
222 name => $subfield_data{id},
224 maxlength => $max_length,
225 javascript => $plugin->javascript,
226 noclick => $plugin->noclick,
228 } else { # warn and supply default field
229 warn $plugin->errstr;
230 $subfield_data{marc_value} = {
232 id => $subfield_data{id},
233 name => $subfield_data{id},
235 maxlength => $max_length,
239 # it's an hidden field
240 elsif ( $tag eq '' ) {
241 $subfield_data{marc_value} = {
243 id => $subfield_data{id},
244 name => $subfield_data{id},
246 maxlength => $max_length,
249 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
250 $subfield_data{marc_value} = {
252 id => $subfield_data{id},
253 name => $subfield_data{id},
255 maxlength => $max_length,
258 # it's a standard field
264 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
265 and $tag < 400 && $subfield eq 'a' )
268 && C4::Context->preference("marcflavour") eq "MARC21" )
271 $subfield_data{marc_value} = {
273 id => $subfield_data{id},
274 name => $subfield_data{id},
276 maxlength => $max_length,
281 $subfield_data{marc_value} = {
283 id => $subfield_data{id},
284 name => $subfield_data{id},
286 maxlength => $max_length,
291 $subfield_data{'index_subfield'} = $index_subfield;
292 return \%subfield_data;
295 =item format_indicator
297 Translate indicator value for output form - specifically, map
298 indicator = ' ' to ''. This is for the convenience of a cataloger
299 using a mouse to select an indicator input.
303 sub format_indicator {
304 my $ind_value = shift;
305 return '' if not defined $ind_value;
306 return '' if $ind_value eq ' ';
312 Create a random value to set it into the input name
317 return int(rand(1000000));
320 =item GetMandatoryFieldZ3950
322 This function return an hashref which containts all mandatory field
323 to search with z3950 server.
327 sub GetMandatoryFieldZ3950 {
328 my $authtypecode = shift;
329 if ( C4::Context->preference('marcflavour') eq 'MARC21' ){
331 '100a' => 'authorpersonal',
332 '110a' => 'authorcorp',
333 '111a' => 'authormeetingcon',
334 '130a' => 'uniformtitle',
339 '200a' => 'authorpersonal',
340 '210a' => 'authormeetingcon', #210 in UNIMARC is used for both corporation and meeting
341 '230a' => 'uniformtitle',
347 my ( $template, $record, $dbh, $encoding,$input ) = @_;
353 my $authorised_values_sth = $dbh->prepare(
354 "SELECT authorised_value,lib
355 FROM authorised_values
356 WHERE category=? ORDER BY lib"
359 # in this array, we will push all the 10 tabs
360 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
363 my @tab_data; # all tags to display
365 foreach my $used ( keys %$tagslib ){
366 push @tab_data,$used if not $seen{$used};
371 # loop through each tab 0 through 9
372 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
373 my @loop_data = (); #innerloop in the template.
375 foreach my $tag (sort @tab_data) {
378 my ($indicator1, $indicator2);
379 my $index_tag = CreateKey;
381 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
382 # if MARC::Record is empty => use tab as master loop.
383 if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
385 if ( $tag ne '000' ) {
386 @fields = $record->field($tag);
389 push @fields, $record->leader(); # if tag == 000
391 # loop through each field
392 foreach my $field (@fields) {
396 my ( $value, $subfield );
397 if ( $tag ne '000' ) {
398 $value = $field->data();
405 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
409 $tag, $subfield, $value, $index_tag, $tabloop, $record,
410 $authorised_values_sth,$input
415 my @subfields = $field->subfields();
416 foreach my $subfieldcount ( 0 .. $#subfields ) {
417 my $subfield = $subfields[$subfieldcount][0];
418 my $value = $subfields[$subfieldcount][1];
419 next if ( length $subfield != 1 );
420 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
424 $tag, $subfield, $value, $index_tag, $tabloop,
425 $record, $authorised_values_sth,$input
431 # now, loop again to add parameter subfield that are not in the MARC::Record
432 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
434 next if ( length $subfield != 1 );
435 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
436 next if ( $tag < 10 );
438 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
439 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
440 ); #check for visibility flag
441 next if ( defined( $field->subfield($subfield) ) );
445 $tag, $subfield, '', $index_tag, $tabloop, $record,
446 $authorised_values_sth,$input
450 if ( $#subfields_data >= 0 ) {
451 # build the tag entry.
452 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
453 # have twice the same "name" value, and cgi->param() will return only one, making
454 # all subfields to be merged in a single field.
458 tag_lib => $tagslib->{$tag}->{lib},
459 repeatable => $tagslib->{$tag}->{repeatable},
460 mandatory => $tagslib->{$tag}->{mandatory},
461 subfield_loop => \@subfields_data,
462 fixedfield => ($tag < 10)?(1):(0),
465 if ($tag >= 10){ # no indicator for theses tag
466 $tag_data{indicator1} = format_indicator($field->indicator(1)),
467 $tag_data{indicator2} = format_indicator($field->indicator(2)),
469 push( @loop_data, \%tag_data );
471 } # foreach $field end
473 # if breeding is empty
477 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
478 next if ( length $subfield != 1 );
479 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
480 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
481 ; #check for visibility flag
482 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
486 $tag, $subfield, '', $index_tag, $tabloop, $record,
487 $authorised_values_sth,$input
491 if ( $#subfields_data >= 0 ) {
495 tag_lib => $tagslib->{$tag}->{lib},
496 repeatable => $tagslib->{$tag}->{repeatable},
497 mandatory => $tagslib->{$tag}->{mandatory},
498 indicator1 => $indicator1,
499 indicator2 => $indicator2,
500 subfield_loop => \@subfields_data,
501 tagfirstsubfield => $subfields_data[0],
502 fixedfield => ($tag < 10)?(1):(0)
505 push @loop_data, \%tag_data ;
509 if ( $#loop_data >= 0 ) {
512 innerloop => \@loop_data,
516 $template->param( BIG_LOOP => \@BIG_LOOP );
520 sub build_hidden_data {
521 # build hidden data =>
522 # we store everything, even if we show only requested subfields.
526 foreach my $tag (keys %{$tagslib}) {
527 my $previous_tag = '';
529 # loop through each subfield
530 foreach my $subfield (keys %{$tagslib->{$tag}}) {
531 next if ($subfield eq 'lib');
532 next if ($subfield eq 'tab');
533 next if ($subfield eq 'mandatory');
534 next if ($subfield eq 'repeatable');
535 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
537 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
538 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
539 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
540 $subfield_data{marc_value} = {
541 type => 'hidden_simple',
542 name => 'field_value[]',
544 push(@loop_data, \%subfield_data);
555 # ========================
557 #=========================
559 my $z3950 = $input->param('z3950');
560 my $error = $input->param('error');
561 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
562 my $op = $input->param('op');
563 my $nonav = $input->param('nonav');
564 my $myindex = $input->param('index');
565 my $linkid=$input->param('linkid');
566 my $authtypecode = $input->param('authtypecode');
567 my $breedingid = $input->param('breedingid');
569 my $dbh = C4::Context->dbh;
571 $authtypecode = $authid ? Koha::Authorities->find($authid)->authtypecode : '';
574 my ($template, $loggedinuser, $cookie)
575 = get_template_and_user({template_name => "authorities/authorities.tt",
578 authnotrequired => 0,
579 flagsrequired => {editauthorities => 1},
582 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,breedingid=>$breedingid,);
584 $tagslib = GetTagsLabels(1,$authtypecode);
585 $mandatory_z3950 = GetMandatoryFieldZ3950($authtypecode);
589 if (($authid) && !($breedingid)){
590 $record = GetAuthority($authid);
593 ( $record, $encoding ) = MARCfindbreeding_auth( $breedingid );
596 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
597 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
601 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
602 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
605 #------------------------------------------------------------------------------------------------------------------------------
607 #------------------------------------------------------------------------------------------------------------------------------
609 my @tags = $input->multi_param('tag');
610 my @subfields = $input->multi_param('subfield');
611 my @values = $input->multi_param('field_value');
612 # build indicator hash.
613 my @ind_tag = $input->multi_param('ind_tag');
614 my @indicator = $input->multi_param('indicator');
615 my $record = TransformHtmlToMarc($input, 0);
617 my ($duplicateauthid,$duplicateauthvalue);
618 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
619 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
620 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
621 if (!$duplicateauthid or $confirm_not_duplicate) {
623 ModAuthority($authid,$record,$authtypecode);
625 ($authid) = AddAuthority($record,$authid,$authtypecode);
628 print $input->redirect("blinddetail-biblio-search.pl?authid=$authid&index=$myindex");
630 print $input->redirect("detail.pl?authid=$authid");
634 # it may be a duplicate, warn the user and do nothing
635 build_tabs($template, $record, $dbh, $encoding,$input);
637 $template->param(authid =>$authid,
638 duplicateauthid => $duplicateauthid,
639 duplicateauthvalue => $duplicateauthvalue->{'authorized'}->[0]->{'heading'},
642 } elsif ($op eq "delete") {
643 #------------------------------------------------------------------------------------------------------------------------------
644 &DelAuthority($authid);
646 print $input->redirect("auth_finder.pl");
648 print $input->redirect("authorities-home.pl?authid=0");
652 if ($op eq "duplicate")
656 build_tabs ($template, $record, $dbh,$encoding,$input);
658 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
659 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
660 authid => $authid , authtypecode=>$authtypecode, );
663 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
666 authority_types => $authority_types,
667 authtypecode => $authtypecode,
670 authtypetext => $authority_types->find($authtypecode)->authtypetext,
671 hide_marc => C4::Context->preference('hide_marc'),
673 output_html_with_http_headers $input, $cookie, $template->output;