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 );
28 use Date::Calc qw( Today );
29 use MARC::File::USMARC;
31 use C4::Biblio qw( TransformHtmlToMarc );
32 use Koha::Authority::Types;
33 use Koha::Import::Records;
35 use vars qw( $tagslib);
36 use vars qw( $authorised_values_sth);
37 use vars qw( $is_a_modif );
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 build_authorized_values_list {
52 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
54 my @authorised_values;
57 my $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'};
58 push @authorised_values, q{} unless $tagslib->{$tag}->{$subfield}->{mandatory} && $value;
60 if ( $category eq "branches" ) {
61 my $sth = $dbh->prepare( "select branchcode,branchname from branches order by branchname" );
63 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
64 push @authorised_values, $branchcode;
65 $authorised_lib{$branchcode} = $branchname;
68 elsif ( $category eq "itemtypes" ) {
69 my $itemtypes = Koha::ItemTypes->search_with_localization;
70 while ( my $itemtype = $itemtypes->next ) {
71 push @authorised_values, $itemtype->itemtype;
72 $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
75 else { # "true" authorised value
76 $authorised_values_sth->execute(
77 $tagslib->{$tag}->{$subfield}->{authorised_value}
79 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
80 push @authorised_values, $value;
81 $authorised_lib{$value} = $lib;
87 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
88 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
89 values => \@authorised_values,
90 labels => \%authorised_lib,
92 ( ( grep { $_ eq $category } ( qw(branches itemtypes cn_source) ) ) ? () : ( category => $category ) ),
98 builds the <input ...> entry for a subfield.
103 my ( $tag, $subfield, $value, $index_tag, $rec, $authorised_values_sth, $cgi ) = @_;
105 my $index_subfield = CreateKey(); # create a specifique key for each subfield
107 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
108 my $max_length = 9999;
111 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
115 # Apply optional framework default value when it is a new record,
116 # or when editing as new (duplicating a record),
117 # based on the ApplyFrameworkDefaults setting.
118 # Substitute date parts, user name
119 my $applydefaults = C4::Context->preference('ApplyFrameworkDefaults');
120 if ( $value eq '' && (
121 ( $applydefaults =~ /new/ && !$cgi->param('authid') ) ||
122 ( $applydefaults =~ /duplicate/ && $cgi->param('op') eq 'duplicate' )
124 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
125 if (!defined $value) {
129 # get today date & replace YYYY, MM, DD if provided in the default value
130 my ( $year, $month, $day ) = Today();
131 $month = sprintf( "%02d", $month );
132 $day = sprintf( "%02d", $day );
133 $value =~ s/YYYY/$year/g;
134 $value =~ s/MM/$month/g;
135 $value =~ s/DD/$day/g;
137 my $dbh = C4::Context->dbh;
139 # map '@' as "subfield" label for fixed fields
140 # to something that's allowed in a div id.
141 my $id_subfield = $subfield;
142 $id_subfield = "00" if $id_subfield eq "@";
144 my %subfield_data = (
146 subfield => $id_subfield,
147 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
148 tag_mandatory => $tagslib->{$tag}->{mandatory},
149 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
150 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
151 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
153 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
155 random => CreateKey(),
158 if(exists $mandatory_z3950->{$tag.$subfield}){
159 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
162 $subfield_data{visibility} = "display:none;"
163 if( $tagslib->{$tag}->{$subfield}->{hidden} and $value ne ''
164 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
167 # it's an authorised field
168 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
169 $subfield_data{marc_value} =
170 build_authorized_values_list( $tag, $subfield, $value, $dbh,
171 $authorised_values_sth,$index_tag,$index_subfield );
173 # it's a thesaurus / authority field
175 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
176 $subfield_data{marc_value} = {
178 id => $subfield_data{id},
179 name => $subfield_data{id},
181 authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode},
184 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) { # plugin
185 require Koha::FrameworkPlugin;
186 my $plugin = Koha::FrameworkPlugin->new({
187 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
189 my $pars= { dbh => $dbh, record => $rec, tagslib =>$tagslib,
190 id => $subfield_data{id} };
191 $plugin->build( $pars );
192 if( !$plugin->errstr ) {
193 $subfield_data{marc_value} = {
195 id => $subfield_data{id},
196 name => $subfield_data{id},
198 maxlength => $max_length,
199 javascript => $plugin->javascript,
200 noclick => $plugin->noclick,
202 } else { # warn and supply default field
203 warn $plugin->errstr;
204 $subfield_data{marc_value} = {
206 id => $subfield_data{id},
207 name => $subfield_data{id},
209 maxlength => $max_length,
213 # it's an hidden field
214 elsif ( $tag eq '' ) {
215 $subfield_data{marc_value} = {
217 id => $subfield_data{id},
218 name => $subfield_data{id},
220 maxlength => $max_length,
223 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
224 $subfield_data{marc_value} = {
226 id => $subfield_data{id},
227 name => $subfield_data{id},
229 maxlength => $max_length,
232 # it's a standard field
238 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
239 and $tag < 400 && $subfield eq 'a' )
242 && C4::Context->preference("marcflavour") eq "MARC21" )
245 $subfield_data{marc_value} = {
247 id => $subfield_data{id},
248 name => $subfield_data{id},
250 maxlength => $max_length,
255 $subfield_data{marc_value} = {
257 id => $subfield_data{id},
258 name => $subfield_data{id},
260 maxlength => $max_length,
265 if ($cgi->param('tagreport') && $subfield_data{tag} == $cgi->param('tagreport')) {
266 $subfield_data{marc_value}{value} = $cgi->param('tag'. $cgi->param('tagbiblio') . 'subfield' . $subfield_data{subfield});
268 $subfield_data{'index_subfield'} = $index_subfield;
269 return \%subfield_data;
272 =item format_indicator
274 Translate indicator value for output form - specifically, map
275 indicator = ' ' to ''. This is for the convenience of a cataloger
276 using a mouse to select an indicator input.
280 sub format_indicator {
281 my $ind_value = shift;
282 return '' if not defined $ind_value;
283 return '' if $ind_value eq ' ';
289 Create a random value to set it into the input name
294 return int(rand(1000000));
297 =item GetMandatoryFieldZ3950
299 This function returns a hashref which contains all mandatory field
300 to search with z3950 server.
304 sub GetMandatoryFieldZ3950 {
305 my $authtypecode = shift;
306 if ( C4::Context->preference('marcflavour') eq 'MARC21' ){
308 '100a' => 'authorpersonal',
309 '110a' => 'authorcorp',
310 '111a' => 'authormeetingcon',
311 '130a' => 'uniformtitle',
316 '200a' => 'authorpersonal',
317 '210a' => 'authorcorp', #210 in UNIMARC is used for both corporation and meeting
318 '230a' => 'uniformtitle',
324 my ( $template, $record, $dbh, $input ) = @_;
330 my $authorised_values_sth = $dbh->prepare(
331 "SELECT authorised_value,lib
332 FROM authorised_values
333 WHERE category=? ORDER BY lib"
336 # in this array, we will push all the 10 tabs
337 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
340 my @tab_data; # all tags to display
342 foreach my $used ( keys %$tagslib ){
343 push @tab_data,$used if not $seen{$used};
348 # loop through each tab 0 through 9
349 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
350 my @loop_data = (); #innerloop in the template.
352 foreach my $tag (sort @tab_data) {
355 my ($indicator1, $indicator2);
356 my $index_tag = CreateKey;
358 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
359 # if MARC::Record is empty => use tab as master loop.
360 if ( $record && ( $record->field($tag) || $tag eq '000' ) ) {
362 if ( $tag ne '000' ) {
363 @fields = $record->field($tag);
366 push @fields, $record->leader(); # if tag == 000
368 # loop through each field
369 foreach my $field (@fields) {
373 my ( $value, $subfield );
374 if ( $tag ne '000' ) {
375 $value = $field->data();
382 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
383 next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
387 $tag, $subfield, $value, $index_tag, $record,
388 $authorised_values_sth,$input
393 my @subfields = $field->subfields();
394 foreach my $subfieldcount ( 0 .. $#subfields ) {
395 my $subfield = $subfields[$subfieldcount][0];
396 my $value = $subfields[$subfieldcount][1];
397 next if ( length $subfield != 1 );
398 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
399 next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
403 $tag, $subfield, $value, $index_tag,
404 $record, $authorised_values_sth,$input
410 # now, loop again to add parameter subfield that are not in the MARC::Record
411 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
413 next if ( length $subfield != 1 );
414 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
415 next if ( $tag < 10 );
416 next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
417 next if ( defined( $field->subfield($subfield) ) );
421 $tag, $subfield, '', $index_tag, $record,
422 $authorised_values_sth,$input
426 if ( $#subfields_data >= 0 ) {
427 # build the tag entry.
428 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
429 # have twice the same "name" value, and cgi->param() will return only one, making
430 # all subfields to be merged in a single field.
434 tag_lib => $tagslib->{$tag}->{lib},
435 repeatable => $tagslib->{$tag}->{repeatable},
436 mandatory => $tagslib->{$tag}->{mandatory},
437 subfield_loop => \@subfields_data,
438 fixedfield => ($tag < 10)?(1):(0),
441 if ($tag >= 10){ # no indicator for theses tag
442 $tag_data{indicator1} = format_indicator($field->indicator(1)),
443 $tag_data{indicator2} = format_indicator($field->indicator(2)),
445 push( @loop_data, \%tag_data );
447 } # foreach $field end
449 # if breeding is empty
453 foreach my $subfield (
454 sort { $a->{display_order} <=> $b->{display_order} || $a->{subfield} cmp $b->{subfield} }
455 grep { ref($_) && %$_ } # Not a subfield (values for "important", "lib", "mandatory", etc.) or empty
456 values %{ $tagslib->{$tag} } )
458 next if $subfield->{hidden} && $subfield->{subfield} ne '9';
459 next if ( $subfield->{tab} ne $tabloop );
463 $tag, $subfield->{subfield}, '', $index_tag, $record,
464 $authorised_values_sth,$input
468 if ( $#subfields_data >= 0 ) {
472 tag_lib => $tagslib->{$tag}->{lib},
473 repeatable => $tagslib->{$tag}->{repeatable},
474 mandatory => $tagslib->{$tag}->{mandatory},
475 indicator1 => $indicator1,
476 indicator2 => $indicator2,
477 subfield_loop => \@subfields_data,
478 tagfirstsubfield => $subfields_data[0],
479 fixedfield => ($tag < 10)?(1):(0)
482 push @loop_data, \%tag_data ;
486 if ( $#loop_data >= 0 ) {
489 innerloop => \@loop_data,
493 $template->param( BIG_LOOP => \@BIG_LOOP );
497 sub build_hidden_data {
498 # build hidden data =>
499 # we store everything, even if we show only requested subfields.
503 foreach my $tag (keys %{$tagslib}) {
504 my $previous_tag = '';
506 # loop through each subfield
507 foreach my $subfield (keys %{$tagslib->{$tag}}) {
508 next if ($subfield eq 'lib');
509 next if ($subfield eq 'tab');
510 next if ($subfield eq 'mandatory');
511 next if ($subfield eq 'repeatable');
512 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
514 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
515 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
516 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
517 $subfield_data{marc_value} = {
518 type => 'hidden_simple',
519 name => 'field_value[]',
521 push(@loop_data, \%subfield_data);
532 # ========================
534 #=========================
535 my $input = CGI->new;
536 my $z3950 = $input->param('z3950');
537 my $error = $input->param('error');
538 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
539 my $op = $input->param('op');
540 my $nonav = $input->param('nonav');
541 my $myindex = $input->param('index');
542 my $linkid=$input->param('linkid');
543 my $authtypecode = $input->param('authtypecode');
544 my $breedingid = $input->param('breedingid');
547 my $dbh = C4::Context->dbh;
549 $authtypecode = $authid ? Koha::Authorities->find($authid)->authtypecode : '';
552 my $authobj = Koha::Authorities->find($authid);
553 my $count = $authobj ? $authobj->get_usage_count : 0;
555 my ($template, $loggedinuser, $cookie)
556 = get_template_and_user({template_name => "authorities/authorities.tt",
559 flagsrequired => {editauthorities => 1},
561 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,breedingid=>$breedingid, count=>$count);
563 $tagslib = GetTagsLabels(1,$authtypecode);
564 $mandatory_z3950 = GetMandatoryFieldZ3950($authtypecode);
568 my $import_record = Koha::Import::Records->find($breedingid);
569 if ($import_record) {
570 $record = $import_record->get_marc_record();
573 $record = GetAuthority($authid);
576 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
577 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
581 ($oldauthnumtagfield,$oldauthnumtagsubfield) = GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
582 ($oldauthtypetagfield,$oldauthtypetagsubfield) = GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
585 #------------------------------------------------------------------------------------------------------------------------------
587 #------------------------------------------------------------------------------------------------------------------------------
589 my @tags = $input->multi_param('tag');
590 my @subfields = $input->multi_param('subfield');
591 my @values = $input->multi_param('field_value');
592 # build indicator hash.
593 my @ind_tag = $input->multi_param('ind_tag');
594 my @indicator = $input->multi_param('indicator');
595 my $record = TransformHtmlToMarc($input, 0);
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);
608 print $input->redirect("blinddetail-biblio-search.pl?authid=$authid&index=$myindex");
610 print $input->redirect("detail.pl?authid=$authid");
614 # it may be a duplicate, warn the user and do nothing
615 build_tabs($template, $record, $dbh, $input);
617 $template->param(authid =>$authid,
618 duplicateauthid => $duplicateauthid,
619 duplicateauthvalue => $duplicateauthvalue->{'authorized'}->[0]->{'heading'},
622 } elsif ($op eq "delete") {
623 #------------------------------------------------------------------------------------------------------------------------------
624 DelAuthority({ authid => $authid });
626 print $input->redirect("auth_finder.pl");
628 print $input->redirect("authorities-home.pl?authid=0");
632 if ($op eq "duplicate")
636 build_tabs ($template, $record, $dbh, $input);
638 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
639 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
640 authid => $authid , authtypecode=>$authtypecode, );
643 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
645 my $type = $authority_types->find($authtypecode);
647 authority_types => $authority_types,
648 authtypecode => $authtypecode,
651 authtypetext => $type ? $type->authtypetext : "",
652 hide_marc => C4::Context->preference('hide_marc'),
654 output_html_with_http_headers $input, $cookie, $template->output;