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
29 use C4
::Koha
; # XXX subfield_is_koha_internal_p
30 use Date
::Calc
qw(Today);
31 use MARC
::File
::USMARC
;
34 use vars
qw( $tagslib);
35 use vars qw( $authorised_values_sth);
36 use vars qw( $is_a_modif );
38 my $itemtype; # created here because it can be used in build_authorized_values_list sub
39 our($authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
45 =item build_authorized_values_list
47 builds list, depending on authorised value...
51 sub MARCfindbreeding_auth {
53 my ($marc, $encoding) = GetImportRecordMarc($id);
55 my $record = MARC::Record->new_from_usmarc($marc);
56 if ( !defined(ref($record)) ) {
59 return $record, $encoding;
66 sub build_authorized_values_list {
67 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
69 my @authorised_values;
74 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
77 "select branchcode,branchname from branches order by branchname");
79 push @authorised_values, ""
80 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
82 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
83 push @authorised_values, $branchcode;
84 $authorised_lib{$branchcode} = $branchname;
87 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
88 push @authorised_values, ""
89 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
90 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
93 my $itemtypes = GetItemTypes( style => 'array' );
94 for my $itemtype ( @$itemtypes ) {
95 push @authorised_values, $itemtype->{itemtype};
96 $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description};
98 $value = $itemtype unless ($value);
100 #---- "true" authorised value
103 $authorised_values_sth->execute(
104 $tagslib->{$tag}->{$subfield}->{authorised_value} );
106 push @authorised_values, ""
107 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
108 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
110 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
111 push @authorised_values, $value;
112 $authorised_lib{$value} = $lib;
117 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
118 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
119 values => \@authorised_values,
120 labels => \%authorised_lib,
128 builds the <input ...> entry for a subfield.
133 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
135 my $index_subfield = CreateKey(); # create a specifique key for each subfield
137 $value =~ s/"/"/g;
139 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
140 my $max_length = 9999;
143 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
147 # if there is no value provided but a default value in parameters, get it
149 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
150 if (!defined $value) {
154 # get today date & replace YYYY, MM, DD if provided in the default value
155 my ( $year, $month, $day ) = Today
();
156 $month = sprintf( "%02d", $month );
157 $day = sprintf( "%02d", $day );
158 $value =~ s/YYYY/$year/g;
159 $value =~ s/MM/$month/g;
160 $value =~ s/DD/$day/g;
162 my $dbh = C4
::Context
->dbh;
164 # map '@' as "subfield" label for fixed fields
165 # to something that's allowed in a div id.
166 my $id_subfield = $subfield;
167 $id_subfield = "00" if $id_subfield eq "@";
169 my %subfield_data = (
171 subfield
=> $id_subfield,
172 marc_lib
=> $tagslib->{$tag}->{$subfield}->{lib
},
173 tag_mandatory
=> $tagslib->{$tag}->{mandatory
},
174 mandatory
=> $tagslib->{$tag}->{$subfield}->{mandatory
},
175 repeatable
=> $tagslib->{$tag}->{$subfield}->{repeatable
},
176 kohafield
=> $tagslib->{$tag}->{$subfield}->{kohafield
},
178 id
=> "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
180 random
=> CreateKey
(),
183 if(exists $mandatory_z3950->{$tag.$subfield}){
184 $subfield_data{z3950_mandatory
} = $mandatory_z3950->{$tag.$subfield};
187 $subfield_data{visibility
} = "display:none;"
188 if ( ($tagslib->{$tag}->{$subfield}->{hidden
} % 2 == 1) and $value ne ''
189 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory
})
192 # it's an authorised field
193 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} ) {
194 $subfield_data{marc_value
} =
195 build_authorized_values_list
( $tag, $subfield, $value, $dbh,
196 $authorised_values_sth,$index_tag,$index_subfield );
198 # it's a thesaurus / authority field
200 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode
} ) {
201 $subfield_data{marc_value
} = {
203 id
=> $subfield_data{id
},
204 name
=> $subfield_data{id
},
206 authtypecode
=> $tagslib->{$tag}->{$subfield}->{authtypecode
},
209 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) { # plugin
210 require Koha
::FrameworkPlugin
;
211 my $plugin = Koha
::FrameworkPlugin
->new({
212 name
=> $tagslib->{$tag}->{$subfield}->{'value_builder'},
214 my $pars= { dbh
=> $dbh, record
=> $rec, tagslib
=>$tagslib,
215 id
=> $subfield_data{id
}, tabloop
=> $tabloop };
216 $plugin->build( $pars );
217 if( !$plugin->errstr ) {
218 $subfield_data{marc_value
} = {
220 id
=> $subfield_data{id
},
221 name
=> $subfield_data{id
},
223 maxlength
=> $max_length,
224 javascript
=> $plugin->javascript,
225 noclick
=> $plugin->noclick,
227 } else { # warn and supply default field
228 warn $plugin->errstr;
229 $subfield_data{marc_value
} = {
231 id
=> $subfield_data{id
},
232 name
=> $subfield_data{id
},
234 maxlength
=> $max_length,
238 # it's an hidden field
239 elsif ( $tag eq '' ) {
240 $subfield_data{marc_value
} = {
242 id
=> $subfield_data{id
},
243 name
=> $subfield_data{id
},
245 maxlength
=> $max_length,
248 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
249 $subfield_data{marc_value
} = {
251 id
=> $subfield_data{id
},
252 name
=> $subfield_data{id
},
254 maxlength
=> $max_length,
257 # it's a standard field
263 ( C4
::Context
->preference("marcflavour") eq "UNIMARC" && $tag >= 300
264 and $tag < 400 && $subfield eq 'a' )
267 && C4
::Context
->preference("marcflavour") eq "MARC21" )
270 $subfield_data{marc_value
} = {
272 id
=> $subfield_data{id
},
273 name
=> $subfield_data{id
},
275 maxlength
=> $max_length,
280 $subfield_data{marc_value
} = {
282 id
=> $subfield_data{id
},
283 name
=> $subfield_data{id
},
285 maxlength
=> $max_length,
290 $subfield_data{'index_subfield'} = $index_subfield;
291 return \
%subfield_data;
294 =item format_indicator
296 Translate indicator value for output form - specifically, map
297 indicator = ' ' to ''. This is for the convenience of a cataloger
298 using a mouse to select an indicator input.
302 sub format_indicator
{
303 my $ind_value = shift;
304 return '' if not defined $ind_value;
305 return '' if $ind_value eq ' ';
311 Create a random value to set it into the input name
316 return int(rand(1000000));
319 =item GetMandatoryFieldZ3950
321 This function return an hashref which containts all mandatory field
322 to search with z3950 server.
326 sub GetMandatoryFieldZ3950
{
327 my $authtypecode = shift;
328 if ( C4
::Context
->preference('marcflavour') eq 'MARC21' ){
330 '100a' => 'authorpersonal',
331 '110a' => 'authorcorp',
332 '111a' => 'authormeetingcon',
333 '130a' => 'uniformtitle',
338 '200a' => 'authorpersonal',
339 '210a' => 'authormeetingcon', #210 in UNIMARC is used for both corporation and meeting
340 '230a' => 'uniformtitle',
346 my ( $template, $record, $dbh, $encoding,$input ) = @_;
352 my $authorised_values_sth = $dbh->prepare(
353 "SELECT authorised_value,lib
354 FROM authorised_values
355 WHERE category=? ORDER BY lib"
358 # in this array, we will push all the 10 tabs
359 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
362 my @tab_data; # all tags to display
364 foreach my $used ( keys %$tagslib ){
365 push @tab_data,$used if not $seen{$used};
370 # loop through each tab 0 through 9
371 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
372 my @loop_data = (); #innerloop in the template.
374 foreach my $tag (sort @tab_data) {
377 my ($indicator1, $indicator2);
378 my $index_tag = CreateKey
;
380 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
381 # if MARC::Record is empty => use tab as master loop.
382 if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
384 if ( $tag ne '000' ) {
385 @fields = $record->field($tag);
388 push @fields, $record->leader(); # if tag == 000
390 # loop through each field
391 foreach my $field (@fields) {
395 my ( $value, $subfield );
396 if ( $tag ne '000' ) {
397 $value = $field->data();
404 next if ( $tagslib->{$tag}->{$subfield}->{tab
} ne $tabloop );
408 $tag, $subfield, $value, $index_tag, $tabloop, $record,
409 $authorised_values_sth,$input
414 my @subfields = $field->subfields();
415 foreach my $subfieldcount ( 0 .. $#subfields ) {
416 my $subfield = $subfields[$subfieldcount][0];
417 my $value = $subfields[$subfieldcount][1];
418 next if ( length $subfield != 1 );
419 next if ( $tagslib->{$tag}->{$subfield}->{tab
} ne $tabloop );
423 $tag, $subfield, $value, $index_tag, $tabloop,
424 $record, $authorised_values_sth,$input
430 # now, loop again to add parameter subfield that are not in the MARC::Record
431 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
433 next if ( length $subfield != 1 );
434 next if ( $tagslib->{$tag}->{$subfield}->{tab
} ne $tabloop );
435 next if ( $tag < 10 );
437 if ( ( $tagslib->{$tag}->{$subfield}->{hidden
} <= -4 )
438 or ( $tagslib->{$tag}->{$subfield}->{hidden
} >= 5 )
439 ); #check for visibility flag
440 next if ( defined( $field->subfield($subfield) ) );
444 $tag, $subfield, '', $index_tag, $tabloop, $record,
445 $authorised_values_sth,$input
449 if ( $#subfields_data >= 0 ) {
450 # build the tag entry.
451 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
452 # have twice the same "name" value, and cgi->param() will return only one, making
453 # all subfields to be merged in a single field.
457 tag_lib
=> $tagslib->{$tag}->{lib
},
458 repeatable
=> $tagslib->{$tag}->{repeatable
},
459 mandatory
=> $tagslib->{$tag}->{mandatory
},
460 subfield_loop
=> \
@subfields_data,
461 fixedfield
=> ($tag < 10)?
(1):(0),
464 if ($tag >= 10){ # no indicator for theses tag
465 $tag_data{indicator1
} = format_indicator
($field->indicator(1)),
466 $tag_data{indicator2
} = format_indicator
($field->indicator(2)),
468 push( @loop_data, \
%tag_data );
470 } # foreach $field end
472 # if breeding is empty
476 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
477 next if ( length $subfield != 1 );
478 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden
} <= -5 )
479 or ( $tagslib->{$tag}->{$subfield}->{hidden
} >= 4 ) )
480 ; #check for visibility flag
481 next if ( $tagslib->{$tag}->{$subfield}->{tab
} ne $tabloop );
485 $tag, $subfield, '', $index_tag, $tabloop, $record,
486 $authorised_values_sth,$input
490 if ( $#subfields_data >= 0 ) {
494 tag_lib
=> $tagslib->{$tag}->{lib
},
495 repeatable
=> $tagslib->{$tag}->{repeatable
},
496 mandatory
=> $tagslib->{$tag}->{mandatory
},
497 indicator1
=> $indicator1,
498 indicator2
=> $indicator2,
499 subfield_loop
=> \
@subfields_data,
500 tagfirstsubfield
=> $subfields_data[0],
501 fixedfield
=> ($tag < 10)?
(1):(0)
504 push @loop_data, \
%tag_data ;
508 if ( $#loop_data >= 0 ) {
511 innerloop
=> \
@loop_data,
515 $template->param( BIG_LOOP
=> \
@BIG_LOOP );
519 sub build_hidden_data
{
520 # build hidden data =>
521 # we store everything, even if we show only requested subfields.
525 foreach my $tag (keys %{$tagslib}) {
526 my $previous_tag = '';
528 # loop through each subfield
529 foreach my $subfield (keys %{$tagslib->{$tag}}) {
530 next if ($subfield eq 'lib');
531 next if ($subfield eq 'tab');
532 next if ($subfield eq 'mandatory');
533 next if ($subfield eq 'repeatable');
534 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
536 $subfield_data{marc_lib
}=$tagslib->{$tag}->{$subfield}->{lib
};
537 $subfield_data{marc_mandatory
}=$tagslib->{$tag}->{$subfield}->{mandatory
};
538 $subfield_data{marc_repeatable
}=$tagslib->{$tag}->{$subfield}->{repeatable
};
539 $subfield_data{marc_value
} = {
540 type
=> 'hidden_simple',
541 name
=> 'field_value[]',
543 push(@loop_data, \
%subfield_data);
554 # ========================
556 #=========================
558 my $z3950 = $input->param('z3950');
559 my $error = $input->param('error');
560 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
561 my $op = $input->param('op');
562 my $nonav = $input->param('nonav');
563 my $myindex = $input->param('index');
564 my $linkid=$input->param('linkid');
565 my $authtypecode = $input->param('authtypecode');
566 my $breedingid = $input->param('breedingid');
568 my $dbh = C4
::Context
->dbh;
570 $authtypecode = $authid?
&GetAuthTypeCode
($authid): '';
573 my ($template, $loggedinuser, $cookie)
574 = get_template_and_user
({template_name
=> "authorities/authorities.tt",
577 authnotrequired
=> 0,
578 flagsrequired
=> {editauthorities
=> 1},
581 $template->param(nonav
=> $nonav,index=>$myindex,authtypecode
=>$authtypecode,breedingid
=>$breedingid,);
583 $tagslib = GetTagsLabels
(1,$authtypecode);
584 $mandatory_z3950 = GetMandatoryFieldZ3950
($authtypecode);
588 if (($authid) && !($breedingid)){
589 $record = GetAuthority
($authid);
592 ( $record, $encoding ) = MARCfindbreeding_auth
( $breedingid );
595 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
596 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
600 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField
("auth_header.authid",$authtypecode);
601 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField
("auth_header.authtypecode",$authtypecode);
604 #------------------------------------------------------------------------------------------------------------------------------
606 #------------------------------------------------------------------------------------------------------------------------------
608 my @tags = $input->param('tag');
609 my @subfields = $input->param('subfield');
610 my @values = $input->param('field_value');
611 # build indicator hash.
612 my @ind_tag = $input->param('ind_tag');
613 my @indicator = $input->param('indicator');
614 my $record = TransformHtmlToMarc
($input);
616 my ($duplicateauthid,$duplicateauthvalue);
617 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority
($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
618 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
619 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
620 if (!$duplicateauthid or $confirm_not_duplicate) {
622 ModAuthority
($authid,$record,$authtypecode);
624 ($authid) = AddAuthority
($record,$authid,$authtypecode);
627 print $input->redirect("blinddetail-biblio-search.pl?authid=$authid&index=$myindex");
629 print $input->redirect("detail.pl?authid=$authid");
633 # it may be a duplicate, warn the user and do nothing
634 build_tabs
($template, $record, $dbh, $encoding,$input);
636 $template->param(authid
=>$authid,
637 duplicateauthid
=> $duplicateauthid,
638 duplicateauthvalue
=> $duplicateauthvalue->{'authorized'}->[0]->{'heading'},
641 } elsif ($op eq "delete") {
642 #------------------------------------------------------------------------------------------------------------------------------
643 &DelAuthority
($authid);
645 print $input->redirect("auth_finder.pl");
647 print $input->redirect("authorities-home.pl?authid=0");
651 if ($op eq "duplicate")
655 build_tabs
($template, $record, $dbh,$encoding,$input);
657 $template->param(oldauthtypetagfield
=>$oldauthtypetagfield, oldauthtypetagsubfield
=>$oldauthtypetagsubfield,
658 oldauthnumtagfield
=>$oldauthnumtagfield, oldauthnumtagsubfield
=>$oldauthnumtagsubfield,
659 authid
=> $authid , authtypecode
=>$authtypecode, );
662 $template->param(authid
=> $authid,
663 authtypecode
=> $authtypecode,
667 my $authtypes = getauthtypes
;
669 foreach my $thisauthtype (keys %$authtypes) {
670 my %row =(value
=> $thisauthtype,
671 selected
=> $thisauthtype eq $authtypecode,
672 authtypetext
=> $authtypes->{$thisauthtype}{'authtypetext'},
674 push @authtypesloop, \
%row;
677 $template->param(authtypesloop
=> \
@authtypesloop,
678 authtypetext
=> $authtypes->{$authtypecode}{'authtypetext'},
679 hide_marc
=> C4
::Context
->preference('hide_marc'),
681 output_html_with_http_headers
$input, $cookie, $template->output;