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' ) ||
123 ( $applydefaults =~ /imported/ && $cgi->param('breedingid') )
125 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
126 if (!defined $value) {
130 # get today date & replace YYYY, MM, DD if provided in the default value
131 my ( $year, $month, $day ) = Today();
132 $month = sprintf( "%02d", $month );
133 $day = sprintf( "%02d", $day );
134 $value =~ s/YYYY/$year/g;
135 $value =~ s/MM/$month/g;
136 $value =~ s/DD/$day/g;
138 my $dbh = C4::Context->dbh;
140 # map '@' as "subfield" label for fixed fields
141 # to something that's allowed in a div id.
142 my $id_subfield = $subfield;
143 $id_subfield = "00" if $id_subfield eq "@";
145 my %subfield_data = (
147 subfield => $id_subfield,
148 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
149 tag_mandatory => $tagslib->{$tag}->{mandatory},
150 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
151 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
152 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
154 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
156 random => CreateKey(),
159 if(exists $mandatory_z3950->{$tag.$subfield}){
160 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
163 $subfield_data{visibility} = "display:none;"
164 if( $tagslib->{$tag}->{$subfield}->{hidden} and $value ne ''
165 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
168 # it's an authorised field
169 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
170 $subfield_data{marc_value} =
171 build_authorized_values_list( $tag, $subfield, $value, $dbh,
172 $authorised_values_sth,$index_tag,$index_subfield );
174 # it's a thesaurus / authority field
176 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
177 $subfield_data{marc_value} = {
179 id => $subfield_data{id},
180 name => $subfield_data{id},
182 authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode},
185 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) { # plugin
186 require Koha::FrameworkPlugin;
187 my $plugin = Koha::FrameworkPlugin->new({
188 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
190 my $pars= { dbh => $dbh, record => $rec, tagslib =>$tagslib,
191 id => $subfield_data{id} };
192 $plugin->build( $pars );
193 if( !$plugin->errstr ) {
194 $subfield_data{marc_value} = {
196 id => $subfield_data{id},
197 name => $subfield_data{id},
199 maxlength => $max_length,
200 javascript => $plugin->javascript,
201 noclick => $plugin->noclick,
203 } else { # warn and supply default field
204 warn $plugin->errstr;
205 $subfield_data{marc_value} = {
207 id => $subfield_data{id},
208 name => $subfield_data{id},
210 maxlength => $max_length,
214 # it's an hidden field
215 elsif ( $tag eq '' ) {
216 $subfield_data{marc_value} = {
218 id => $subfield_data{id},
219 name => $subfield_data{id},
221 maxlength => $max_length,
224 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
225 $subfield_data{marc_value} = {
227 id => $subfield_data{id},
228 name => $subfield_data{id},
230 maxlength => $max_length,
233 # it's a standard field
239 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
240 and $tag < 400 && $subfield eq 'a' )
243 && C4::Context->preference("marcflavour") eq "MARC21" )
246 $subfield_data{marc_value} = {
248 id => $subfield_data{id},
249 name => $subfield_data{id},
251 maxlength => $max_length,
256 $subfield_data{marc_value} = {
258 id => $subfield_data{id},
259 name => $subfield_data{id},
261 maxlength => $max_length,
266 if ($cgi->param('tagreport') && $subfield_data{tag} == $cgi->param('tagreport')) {
267 $subfield_data{marc_value}{value} = $cgi->param('tag'. $cgi->param('tagbiblio') . 'subfield' . $subfield_data{subfield});
269 $subfield_data{'index_subfield'} = $index_subfield;
270 return \%subfield_data;
273 =item format_indicator
275 Translate indicator value for output form - specifically, map
276 indicator = ' ' to ''. This is for the convenience of a cataloger
277 using a mouse to select an indicator input.
281 sub format_indicator {
282 my $ind_value = shift;
283 return '' if not defined $ind_value;
284 return '' if $ind_value eq ' ';
290 Create a random value to set it into the input name
295 return int(rand(1000000));
298 =item GetMandatoryFieldZ3950
300 This function returns a hashref which contains all mandatory field
301 to search with z3950 server.
305 sub GetMandatoryFieldZ3950 {
306 my $authtypecode = shift;
307 if ( C4::Context->preference('marcflavour') eq 'MARC21' ){
309 '100a' => 'authorpersonal',
310 '110a' => 'authorcorp',
311 '111a' => 'authormeetingcon',
312 '130a' => 'uniformtitle',
317 '200a' => 'authorpersonal',
318 '210a' => 'authorcorp', #210 in UNIMARC is used for both corporation and meeting
319 '230a' => 'uniformtitle',
325 my ( $template, $record, $dbh, $input ) = @_;
331 my $authorised_values_sth = $dbh->prepare(
332 "SELECT authorised_value,lib
333 FROM authorised_values
334 WHERE category=? ORDER BY lib"
337 # in this array, we will push all the 10 tabs
338 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
341 my @tab_data; # all tags to display
343 foreach my $used ( keys %$tagslib ){
344 push @tab_data,$used if not $seen{$used};
349 # loop through each tab 0 through 9
350 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
351 my @loop_data = (); #innerloop in the template.
353 foreach my $tag (sort @tab_data) {
356 my ($indicator1, $indicator2);
357 my $index_tag = CreateKey;
359 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
360 # if MARC::Record is empty => use tab as master loop.
361 if ( $record && ( $record->field($tag) || $tag eq '000' ) ) {
363 if ( $tag ne '000' ) {
364 @fields = $record->field($tag);
367 push @fields, $record->leader(); # if tag == 000
369 # loop through each field
370 foreach my $field (@fields) {
374 my ( $value, $subfield );
375 if ( $tag ne '000' ) {
376 $value = $field->data();
383 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
384 next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
388 $tag, $subfield, $value, $index_tag, $record,
389 $authorised_values_sth,$input
394 my @subfields = $field->subfields();
395 foreach my $subfieldcount ( 0 .. $#subfields ) {
396 my $subfield = $subfields[$subfieldcount][0];
397 my $value = $subfields[$subfieldcount][1];
398 next if ( length $subfield != 1 );
399 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
400 next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
404 $tag, $subfield, $value, $index_tag,
405 $record, $authorised_values_sth,$input
411 # now, loop again to add parameter subfield that are not in the MARC::Record
412 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
414 next if ( length $subfield != 1 );
415 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
416 next if ( $tag < 10 );
417 next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
418 next if ( defined( $field->subfield($subfield) ) );
422 $tag, $subfield, '', $index_tag, $record,
423 $authorised_values_sth,$input
427 if ( $#subfields_data >= 0 ) {
428 # build the tag entry.
429 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
430 # have twice the same "name" value, and cgi->param() will return only one, making
431 # all subfields to be merged in a single field.
435 tag_lib => $tagslib->{$tag}->{lib},
436 repeatable => $tagslib->{$tag}->{repeatable},
437 mandatory => $tagslib->{$tag}->{mandatory},
438 subfield_loop => \@subfields_data,
439 fixedfield => ($tag < 10)?(1):(0),
442 if ($tag >= 10){ # no indicator for theses tag
443 $tag_data{indicator1} = format_indicator($field->indicator(1)),
444 $tag_data{indicator2} = format_indicator($field->indicator(2)),
446 push( @loop_data, \%tag_data );
448 } # foreach $field end
450 # if breeding is empty
454 foreach my $subfield (
455 sort { $a->{display_order} <=> $b->{display_order} || $a->{subfield} cmp $b->{subfield} }
456 grep { ref($_) && %$_ } # Not a subfield (values for "important", "lib", "mandatory", etc.) or empty
457 values %{ $tagslib->{$tag} } )
459 next if $subfield->{hidden} && $subfield->{subfield} ne '9';
460 next if ( $subfield->{tab} ne $tabloop );
464 $tag, $subfield->{subfield}, '', $index_tag, $record,
465 $authorised_values_sth,$input
469 if ( $#subfields_data >= 0 ) {
473 tag_lib => $tagslib->{$tag}->{lib},
474 repeatable => $tagslib->{$tag}->{repeatable},
475 mandatory => $tagslib->{$tag}->{mandatory},
476 indicator1 => $indicator1,
477 indicator2 => $indicator2,
478 subfield_loop => \@subfields_data,
479 tagfirstsubfield => $subfields_data[0],
480 fixedfield => ($tag < 10)?(1):(0)
483 push @loop_data, \%tag_data ;
487 if ( $#loop_data >= 0 ) {
490 innerloop => \@loop_data,
494 $template->param( BIG_LOOP => \@BIG_LOOP );
498 sub build_hidden_data {
499 # build hidden data =>
500 # we store everything, even if we show only requested subfields.
504 foreach my $tag (keys %{$tagslib}) {
505 my $previous_tag = '';
507 # loop through each subfield
508 foreach my $subfield (keys %{$tagslib->{$tag}}) {
509 next if ($subfield eq 'lib');
510 next if ($subfield eq 'tab');
511 next if ($subfield eq 'mandatory');
512 next if ($subfield eq 'repeatable');
513 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
515 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
516 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
517 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
518 $subfield_data{marc_value} = {
519 type => 'hidden_simple',
520 name => 'field_value[]',
522 push(@loop_data, \%subfield_data);
533 # ========================
535 #=========================
536 my $input = CGI->new;
537 my $z3950 = $input->param('z3950');
538 my $error = $input->param('error');
539 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
540 my $op = $input->param('op');
541 my $nonav = $input->param('nonav');
542 my $myindex = $input->param('index');
543 my $linkid=$input->param('linkid');
544 my $authtypecode = $input->param('authtypecode');
545 my $breedingid = $input->param('breedingid');
548 my $dbh = C4::Context->dbh;
550 $authtypecode = $authid ? Koha::Authorities->find($authid)->authtypecode : '';
553 my $authobj = Koha::Authorities->find($authid);
554 my $count = $authobj ? $authobj->get_usage_count : 0;
556 my ($template, $loggedinuser, $cookie)
557 = get_template_and_user({template_name => "authorities/authorities.tt",
560 flagsrequired => {editauthorities => 1},
562 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,breedingid=>$breedingid, count=>$count);
564 $tagslib = GetTagsLabels(1,$authtypecode);
565 $mandatory_z3950 = GetMandatoryFieldZ3950($authtypecode);
569 my $import_record = Koha::Import::Records->find($breedingid);
570 if ($import_record) {
571 $record = $import_record->get_marc_record();
574 $record = GetAuthority($authid);
577 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
578 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
582 ($oldauthnumtagfield,$oldauthnumtagsubfield) = GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
583 ($oldauthtypetagfield,$oldauthtypetagsubfield) = GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
586 #------------------------------------------------------------------------------------------------------------------------------
588 #------------------------------------------------------------------------------------------------------------------------------
590 my @tags = $input->multi_param('tag');
591 my @subfields = $input->multi_param('subfield');
592 my @values = $input->multi_param('field_value');
593 # build indicator hash.
594 my @ind_tag = $input->multi_param('ind_tag');
595 my @indicator = $input->multi_param('indicator');
596 my $record = TransformHtmlToMarc($input, 0);
598 my ($duplicateauthid,$duplicateauthvalue);
599 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
600 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
601 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
602 if (!$duplicateauthid or $confirm_not_duplicate) {
604 ModAuthority($authid,$record,$authtypecode);
606 ($authid) = AddAuthority($record,$authid,$authtypecode);
609 print $input->redirect("blinddetail-biblio-search.pl?authid=$authid&index=$myindex");
611 print $input->redirect("detail.pl?authid=$authid");
615 # it may be a duplicate, warn the user and do nothing
616 build_tabs($template, $record, $dbh, $input);
618 $template->param(authid =>$authid,
619 duplicateauthid => $duplicateauthid,
620 duplicateauthvalue => $duplicateauthvalue->{'authorized'}->[0]->{'heading'},
623 } elsif ($op eq "delete") {
624 #------------------------------------------------------------------------------------------------------------------------------
625 DelAuthority({ authid => $authid });
627 print $input->redirect("auth_finder.pl");
629 print $input->redirect("authorities-home.pl?authid=0");
633 if ($op eq "duplicate")
637 build_tabs ($template, $record, $dbh, $input);
639 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
640 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
641 authid => $authid , authtypecode=>$authtypecode, );
644 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
646 my $type = $authority_types->find($authtypecode);
648 authority_types => $authority_types,
649 authtypecode => $authtypecode,
652 authtypetext => $type ? $type->authtypetext : "",
653 hide_marc => C4::Context->preference('hide_marc'),
655 output_html_with_http_headers $input, $cookie, $template->output;