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>.
24 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::AuthoritiesMarc qw( AddAuthority ModAuthority GetAuthority GetTagsLabels GetAuthMARCFromKohaField FindDuplicateAuthority );
27 use C4::ImportBatch qw( GetImportRecordMarc );
29 use Date::Calc qw( Today );
30 use MARC::File::USMARC;
32 use C4::Biblio qw( TransformHtmlToMarc );
33 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 my $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'};
76 if ( $category eq "branches" ) {
79 "select branchcode,branchname from branches order by branchname");
81 push @authorised_values, ""
82 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
84 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
85 push @authorised_values, $branchcode;
86 $authorised_lib{$branchcode} = $branchname;
89 elsif ( $category eq "itemtypes" ) {
90 push @authorised_values, ""
91 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
92 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
95 my $itemtypes = Koha::ItemTypes->search_with_localization;
96 while ( $itemtype = $itemtypes->next ) {
97 push @authorised_values, $itemtype->itemtype;
98 $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
100 $value = $itemtype unless ($value);
102 #---- "true" authorised value
105 $authorised_values_sth->execute(
106 $tagslib->{$tag}->{$subfield}->{authorised_value} );
108 push @authorised_values, ""
109 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
110 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
112 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
113 push @authorised_values, $value;
114 $authorised_lib{$value} = $lib;
119 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
120 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
121 values => \@authorised_values,
122 labels => \%authorised_lib,
124 ( ( grep { $_ eq $category } ( qw(branches itemtypes cn_source) ) ) ? () : ( category => $category ) ),
131 builds the <input ...> entry for a subfield.
136 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
138 my $index_subfield = CreateKey(); # create a specifique key for each subfield
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} 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 if ($cgi->param('tagreport') && $subfield_data{tag} == $cgi->param('tagreport')) {
292 $subfield_data{marc_value}{value} = $cgi->param('tag'. $cgi->param('tagbiblio') . 'subfield' . $subfield_data{subfield});
294 $subfield_data{'index_subfield'} = $index_subfield;
295 return \%subfield_data;
298 =item format_indicator
300 Translate indicator value for output form - specifically, map
301 indicator = ' ' to ''. This is for the convenience of a cataloger
302 using a mouse to select an indicator input.
306 sub format_indicator {
307 my $ind_value = shift;
308 return '' if not defined $ind_value;
309 return '' if $ind_value eq ' ';
315 Create a random value to set it into the input name
320 return int(rand(1000000));
323 =item GetMandatoryFieldZ3950
325 This function returns a hashref which contains all mandatory field
326 to search with z3950 server.
330 sub GetMandatoryFieldZ3950 {
331 my $authtypecode = shift;
332 if ( C4::Context->preference('marcflavour') eq 'MARC21' ){
334 '100a' => 'authorpersonal',
335 '110a' => 'authorcorp',
336 '111a' => 'authormeetingcon',
337 '130a' => 'uniformtitle',
342 '200a' => 'authorpersonal',
343 '210a' => 'authormeetingcon', #210 in UNIMARC is used for both corporation and meeting
344 '230a' => 'uniformtitle',
350 my ( $template, $record, $dbh, $encoding,$input ) = @_;
356 my $authorised_values_sth = $dbh->prepare(
357 "SELECT authorised_value,lib
358 FROM authorised_values
359 WHERE category=? ORDER BY lib"
362 # in this array, we will push all the 10 tabs
363 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
366 my @tab_data; # all tags to display
368 foreach my $used ( keys %$tagslib ){
369 push @tab_data,$used if not $seen{$used};
374 # loop through each tab 0 through 9
375 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
376 my @loop_data = (); #innerloop in the template.
378 foreach my $tag (sort @tab_data) {
381 my ($indicator1, $indicator2);
382 my $index_tag = CreateKey;
384 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
385 # if MARC::Record is empty => use tab as master loop.
386 if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
388 if ( $tag ne '000' ) {
389 @fields = $record->field($tag);
392 push @fields, $record->leader(); # if tag == 000
394 # loop through each field
395 foreach my $field (@fields) {
399 my ( $value, $subfield );
400 if ( $tag ne '000' ) {
401 $value = $field->data();
408 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
409 next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
413 $tag, $subfield, $value, $index_tag, $tabloop, $record,
414 $authorised_values_sth,$input
419 my @subfields = $field->subfields();
420 foreach my $subfieldcount ( 0 .. $#subfields ) {
421 my $subfield = $subfields[$subfieldcount][0];
422 my $value = $subfields[$subfieldcount][1];
423 next if ( length $subfield != 1 );
424 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
425 next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
429 $tag, $subfield, $value, $index_tag, $tabloop,
430 $record, $authorised_values_sth,$input
436 # now, loop again to add parameter subfield that are not in the MARC::Record
437 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
439 next if ( length $subfield != 1 );
440 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
441 next if ( $tag < 10 );
442 next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
443 next if ( defined( $field->subfield($subfield) ) );
447 $tag, $subfield, '', $index_tag, $tabloop, $record,
448 $authorised_values_sth,$input
452 if ( $#subfields_data >= 0 ) {
453 # build the tag entry.
454 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
455 # have twice the same "name" value, and cgi->param() will return only one, making
456 # all subfields to be merged in a single field.
460 tag_lib => $tagslib->{$tag}->{lib},
461 repeatable => $tagslib->{$tag}->{repeatable},
462 mandatory => $tagslib->{$tag}->{mandatory},
463 subfield_loop => \@subfields_data,
464 fixedfield => ($tag < 10)?(1):(0),
467 if ($tag >= 10){ # no indicator for theses tag
468 $tag_data{indicator1} = format_indicator($field->indicator(1)),
469 $tag_data{indicator2} = format_indicator($field->indicator(2)),
471 push( @loop_data, \%tag_data );
473 } # foreach $field end
475 # if breeding is empty
479 foreach my $subfield (
480 sort { $a->{display_order} <=> $b->{display_order} || $a->{subfield} cmp $b->{subfield} }
481 grep { ref($_) && %$_ } # Not a subfield (values for "important", "lib", "mandatory", etc.) or empty
482 values %{ $tagslib->{$tag} } )
484 next if $subfield->{hidden} && $subfield->{subfield} ne '9';
485 next if ( $subfield->{tab} ne $tabloop );
489 $tag, $subfield->{subfield}, '', $index_tag, $tabloop, $record,
490 $authorised_values_sth,$input
494 if ( $#subfields_data >= 0 ) {
498 tag_lib => $tagslib->{$tag}->{lib},
499 repeatable => $tagslib->{$tag}->{repeatable},
500 mandatory => $tagslib->{$tag}->{mandatory},
501 indicator1 => $indicator1,
502 indicator2 => $indicator2,
503 subfield_loop => \@subfields_data,
504 tagfirstsubfield => $subfields_data[0],
505 fixedfield => ($tag < 10)?(1):(0)
508 push @loop_data, \%tag_data ;
512 if ( $#loop_data >= 0 ) {
515 innerloop => \@loop_data,
519 $template->param( BIG_LOOP => \@BIG_LOOP );
523 sub build_hidden_data {
524 # build hidden data =>
525 # we store everything, even if we show only requested subfields.
529 foreach my $tag (keys %{$tagslib}) {
530 my $previous_tag = '';
532 # loop through each subfield
533 foreach my $subfield (keys %{$tagslib->{$tag}}) {
534 next if ($subfield eq 'lib');
535 next if ($subfield eq 'tab');
536 next if ($subfield eq 'mandatory');
537 next if ($subfield eq 'repeatable');
538 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
540 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
541 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
542 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
543 $subfield_data{marc_value} = {
544 type => 'hidden_simple',
545 name => 'field_value[]',
547 push(@loop_data, \%subfield_data);
558 # ========================
560 #=========================
561 my $input = CGI->new;
562 my $z3950 = $input->param('z3950');
563 my $error = $input->param('error');
564 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
565 my $op = $input->param('op');
566 my $nonav = $input->param('nonav');
567 my $myindex = $input->param('index');
568 my $linkid=$input->param('linkid');
569 my $authtypecode = $input->param('authtypecode');
570 my $breedingid = $input->param('breedingid');
573 my $dbh = C4::Context->dbh;
575 $authtypecode = $authid ? Koha::Authorities->find($authid)->authtypecode : '';
578 my ($template, $loggedinuser, $cookie)
579 = get_template_and_user({template_name => "authorities/authorities.tt",
582 flagsrequired => {editauthorities => 1},
584 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,breedingid=>$breedingid);
586 $tagslib = GetTagsLabels(1,$authtypecode);
587 $mandatory_z3950 = GetMandatoryFieldZ3950($authtypecode);
591 if (($authid) && !($breedingid)){
592 $record = GetAuthority($authid);
595 ( $record, $encoding ) = MARCfindbreeding_auth( $breedingid );
598 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
599 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
603 ($oldauthnumtagfield,$oldauthnumtagsubfield) = GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
604 ($oldauthtypetagfield,$oldauthtypetagsubfield) = GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
607 #------------------------------------------------------------------------------------------------------------------------------
609 #------------------------------------------------------------------------------------------------------------------------------
611 my @tags = $input->multi_param('tag');
612 my @subfields = $input->multi_param('subfield');
613 my @values = $input->multi_param('field_value');
614 # build indicator hash.
615 my @ind_tag = $input->multi_param('ind_tag');
616 my @indicator = $input->multi_param('indicator');
617 my $record = TransformHtmlToMarc($input, 0);
619 my ($duplicateauthid,$duplicateauthvalue);
620 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
621 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
622 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
623 if (!$duplicateauthid or $confirm_not_duplicate) {
625 ModAuthority($authid,$record,$authtypecode);
627 ($authid) = AddAuthority($record,$authid,$authtypecode);
630 print $input->redirect("blinddetail-biblio-search.pl?authid=$authid&index=$myindex");
632 print $input->redirect("detail.pl?authid=$authid");
636 # it may be a duplicate, warn the user and do nothing
637 build_tabs($template, $record, $dbh, $encoding,$input);
639 $template->param(authid =>$authid,
640 duplicateauthid => $duplicateauthid,
641 duplicateauthvalue => $duplicateauthvalue->{'authorized'}->[0]->{'heading'},
644 } elsif ($op eq "delete") {
645 #------------------------------------------------------------------------------------------------------------------------------
646 DelAuthority({ authid => $authid });
648 print $input->redirect("auth_finder.pl");
650 print $input->redirect("authorities-home.pl?authid=0");
654 if ($op eq "duplicate")
658 build_tabs ($template, $record, $dbh,$encoding,$input);
660 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
661 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
662 authid => $authid , authtypecode=>$authtypecode, );
665 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
667 my $type = $authority_types->find($authtypecode);
669 authority_types => $authority_types,
670 authtypecode => $authtypecode,
673 authtypetext => $type ? $type->authtypetext : "",
674 hide_marc => C4::Context->preference('hide_marc'),
676 output_html_with_http_headers $input, $cookie, $template->output;