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 # if there is no value provided but a default value in parameters, get it
117 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue} if !$cgi->param('authid'); # only for new records
118 if (!defined $value) {
122 # get today date & replace YYYY, MM, DD if provided in the default value
123 my ( $year, $month, $day ) = Today();
124 $month = sprintf( "%02d", $month );
125 $day = sprintf( "%02d", $day );
126 $value =~ s/YYYY/$year/g;
127 $value =~ s/MM/$month/g;
128 $value =~ s/DD/$day/g;
130 my $dbh = C4::Context->dbh;
132 # map '@' as "subfield" label for fixed fields
133 # to something that's allowed in a div id.
134 my $id_subfield = $subfield;
135 $id_subfield = "00" if $id_subfield eq "@";
137 my %subfield_data = (
139 subfield => $id_subfield,
140 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
141 tag_mandatory => $tagslib->{$tag}->{mandatory},
142 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
143 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
144 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
146 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
148 random => CreateKey(),
151 if(exists $mandatory_z3950->{$tag.$subfield}){
152 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
155 $subfield_data{visibility} = "display:none;"
156 if( $tagslib->{$tag}->{$subfield}->{hidden} and $value ne ''
157 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
160 # it's an authorised field
161 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
162 $subfield_data{marc_value} =
163 build_authorized_values_list( $tag, $subfield, $value, $dbh,
164 $authorised_values_sth,$index_tag,$index_subfield );
166 # it's a thesaurus / authority field
168 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
169 $subfield_data{marc_value} = {
171 id => $subfield_data{id},
172 name => $subfield_data{id},
174 authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode},
177 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) { # plugin
178 require Koha::FrameworkPlugin;
179 my $plugin = Koha::FrameworkPlugin->new({
180 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
182 my $pars= { dbh => $dbh, record => $rec, tagslib =>$tagslib,
183 id => $subfield_data{id} };
184 $plugin->build( $pars );
185 if( !$plugin->errstr ) {
186 $subfield_data{marc_value} = {
188 id => $subfield_data{id},
189 name => $subfield_data{id},
191 maxlength => $max_length,
192 javascript => $plugin->javascript,
193 noclick => $plugin->noclick,
195 } else { # warn and supply default field
196 warn $plugin->errstr;
197 $subfield_data{marc_value} = {
199 id => $subfield_data{id},
200 name => $subfield_data{id},
202 maxlength => $max_length,
206 # it's an hidden field
207 elsif ( $tag eq '' ) {
208 $subfield_data{marc_value} = {
210 id => $subfield_data{id},
211 name => $subfield_data{id},
213 maxlength => $max_length,
216 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
217 $subfield_data{marc_value} = {
219 id => $subfield_data{id},
220 name => $subfield_data{id},
222 maxlength => $max_length,
225 # it's a standard field
231 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
232 and $tag < 400 && $subfield eq 'a' )
235 && C4::Context->preference("marcflavour") eq "MARC21" )
238 $subfield_data{marc_value} = {
240 id => $subfield_data{id},
241 name => $subfield_data{id},
243 maxlength => $max_length,
248 $subfield_data{marc_value} = {
250 id => $subfield_data{id},
251 name => $subfield_data{id},
253 maxlength => $max_length,
258 if ($cgi->param('tagreport') && $subfield_data{tag} == $cgi->param('tagreport')) {
259 $subfield_data{marc_value}{value} = $cgi->param('tag'. $cgi->param('tagbiblio') . 'subfield' . $subfield_data{subfield});
261 $subfield_data{'index_subfield'} = $index_subfield;
262 return \%subfield_data;
265 =item format_indicator
267 Translate indicator value for output form - specifically, map
268 indicator = ' ' to ''. This is for the convenience of a cataloger
269 using a mouse to select an indicator input.
273 sub format_indicator {
274 my $ind_value = shift;
275 return '' if not defined $ind_value;
276 return '' if $ind_value eq ' ';
282 Create a random value to set it into the input name
287 return int(rand(1000000));
290 =item GetMandatoryFieldZ3950
292 This function returns a hashref which contains all mandatory field
293 to search with z3950 server.
297 sub GetMandatoryFieldZ3950 {
298 my $authtypecode = shift;
299 if ( C4::Context->preference('marcflavour') eq 'MARC21' ){
301 '100a' => 'authorpersonal',
302 '110a' => 'authorcorp',
303 '111a' => 'authormeetingcon',
304 '130a' => 'uniformtitle',
309 '200a' => 'authorpersonal',
310 '210a' => 'authorcorp', #210 in UNIMARC is used for both corporation and meeting
311 '230a' => 'uniformtitle',
317 my ( $template, $record, $dbh, $input ) = @_;
323 my $authorised_values_sth = $dbh->prepare(
324 "SELECT authorised_value,lib
325 FROM authorised_values
326 WHERE category=? ORDER BY lib"
329 # in this array, we will push all the 10 tabs
330 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
333 my @tab_data; # all tags to display
335 foreach my $used ( keys %$tagslib ){
336 push @tab_data,$used if not $seen{$used};
341 # loop through each tab 0 through 9
342 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
343 my @loop_data = (); #innerloop in the template.
345 foreach my $tag (sort @tab_data) {
348 my ($indicator1, $indicator2);
349 my $index_tag = CreateKey;
351 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
352 # if MARC::Record is empty => use tab as master loop.
353 if ( $record && ( $record->field($tag) || $tag eq '000' ) ) {
355 if ( $tag ne '000' ) {
356 @fields = $record->field($tag);
359 push @fields, $record->leader(); # if tag == 000
361 # loop through each field
362 foreach my $field (@fields) {
366 my ( $value, $subfield );
367 if ( $tag ne '000' ) {
368 $value = $field->data();
375 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
376 next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
380 $tag, $subfield, $value, $index_tag, $record,
381 $authorised_values_sth,$input
386 my @subfields = $field->subfields();
387 foreach my $subfieldcount ( 0 .. $#subfields ) {
388 my $subfield = $subfields[$subfieldcount][0];
389 my $value = $subfields[$subfieldcount][1];
390 next if ( length $subfield != 1 );
391 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
392 next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
396 $tag, $subfield, $value, $index_tag,
397 $record, $authorised_values_sth,$input
403 # now, loop again to add parameter subfield that are not in the MARC::Record
404 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
406 next if ( length $subfield != 1 );
407 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
408 next if ( $tag < 10 );
409 next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
410 next if ( defined( $field->subfield($subfield) ) );
414 $tag, $subfield, '', $index_tag, $record,
415 $authorised_values_sth,$input
419 if ( $#subfields_data >= 0 ) {
420 # build the tag entry.
421 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
422 # have twice the same "name" value, and cgi->param() will return only one, making
423 # all subfields to be merged in a single field.
427 tag_lib => $tagslib->{$tag}->{lib},
428 repeatable => $tagslib->{$tag}->{repeatable},
429 mandatory => $tagslib->{$tag}->{mandatory},
430 subfield_loop => \@subfields_data,
431 fixedfield => ($tag < 10)?(1):(0),
434 if ($tag >= 10){ # no indicator for theses tag
435 $tag_data{indicator1} = format_indicator($field->indicator(1)),
436 $tag_data{indicator2} = format_indicator($field->indicator(2)),
438 push( @loop_data, \%tag_data );
440 } # foreach $field end
442 # if breeding is empty
446 foreach my $subfield (
447 sort { $a->{display_order} <=> $b->{display_order} || $a->{subfield} cmp $b->{subfield} }
448 grep { ref($_) && %$_ } # Not a subfield (values for "important", "lib", "mandatory", etc.) or empty
449 values %{ $tagslib->{$tag} } )
451 next if $subfield->{hidden} && $subfield->{subfield} ne '9';
452 next if ( $subfield->{tab} ne $tabloop );
456 $tag, $subfield->{subfield}, '', $index_tag, $record,
457 $authorised_values_sth,$input
461 if ( $#subfields_data >= 0 ) {
465 tag_lib => $tagslib->{$tag}->{lib},
466 repeatable => $tagslib->{$tag}->{repeatable},
467 mandatory => $tagslib->{$tag}->{mandatory},
468 indicator1 => $indicator1,
469 indicator2 => $indicator2,
470 subfield_loop => \@subfields_data,
471 tagfirstsubfield => $subfields_data[0],
472 fixedfield => ($tag < 10)?(1):(0)
475 push @loop_data, \%tag_data ;
479 if ( $#loop_data >= 0 ) {
482 innerloop => \@loop_data,
486 $template->param( BIG_LOOP => \@BIG_LOOP );
490 sub build_hidden_data {
491 # build hidden data =>
492 # we store everything, even if we show only requested subfields.
496 foreach my $tag (keys %{$tagslib}) {
497 my $previous_tag = '';
499 # loop through each subfield
500 foreach my $subfield (keys %{$tagslib->{$tag}}) {
501 next if ($subfield eq 'lib');
502 next if ($subfield eq 'tab');
503 next if ($subfield eq 'mandatory');
504 next if ($subfield eq 'repeatable');
505 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
507 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
508 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
509 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
510 $subfield_data{marc_value} = {
511 type => 'hidden_simple',
512 name => 'field_value[]',
514 push(@loop_data, \%subfield_data);
525 # ========================
527 #=========================
528 my $input = CGI->new;
529 my $z3950 = $input->param('z3950');
530 my $error = $input->param('error');
531 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
532 my $op = $input->param('op');
533 my $nonav = $input->param('nonav');
534 my $myindex = $input->param('index');
535 my $linkid=$input->param('linkid');
536 my $authtypecode = $input->param('authtypecode');
537 my $breedingid = $input->param('breedingid');
540 my $dbh = C4::Context->dbh;
542 $authtypecode = $authid ? Koha::Authorities->find($authid)->authtypecode : '';
545 my $authobj = Koha::Authorities->find($authid);
546 my $count = $authobj ? $authobj->get_usage_count : 0;
548 my ($template, $loggedinuser, $cookie)
549 = get_template_and_user({template_name => "authorities/authorities.tt",
552 flagsrequired => {editauthorities => 1},
554 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,breedingid=>$breedingid, count=>$count);
556 $tagslib = GetTagsLabels(1,$authtypecode);
557 $mandatory_z3950 = GetMandatoryFieldZ3950($authtypecode);
561 my $import_record = Koha::Import::Records->find($breedingid);
562 if ($import_record) {
563 $record = $import_record->get_marc_record();
566 $record = GetAuthority($authid);
569 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
570 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
574 ($oldauthnumtagfield,$oldauthnumtagsubfield) = GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
575 ($oldauthtypetagfield,$oldauthtypetagsubfield) = GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
578 #------------------------------------------------------------------------------------------------------------------------------
580 #------------------------------------------------------------------------------------------------------------------------------
582 my @tags = $input->multi_param('tag');
583 my @subfields = $input->multi_param('subfield');
584 my @values = $input->multi_param('field_value');
585 # build indicator hash.
586 my @ind_tag = $input->multi_param('ind_tag');
587 my @indicator = $input->multi_param('indicator');
588 my $record = TransformHtmlToMarc($input, 0);
590 my ($duplicateauthid,$duplicateauthvalue);
591 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
592 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
593 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
594 if (!$duplicateauthid or $confirm_not_duplicate) {
596 ModAuthority($authid,$record,$authtypecode);
598 ($authid) = AddAuthority($record,$authid,$authtypecode);
601 print $input->redirect("blinddetail-biblio-search.pl?authid=$authid&index=$myindex");
603 print $input->redirect("detail.pl?authid=$authid");
607 # it may be a duplicate, warn the user and do nothing
608 build_tabs($template, $record, $dbh, $input);
610 $template->param(authid =>$authid,
611 duplicateauthid => $duplicateauthid,
612 duplicateauthvalue => $duplicateauthvalue->{'authorized'}->[0]->{'heading'},
615 } elsif ($op eq "delete") {
616 #------------------------------------------------------------------------------------------------------------------------------
617 DelAuthority({ authid => $authid });
619 print $input->redirect("auth_finder.pl");
621 print $input->redirect("authorities-home.pl?authid=0");
625 if ($op eq "duplicate")
629 build_tabs ($template, $record, $dbh, $input);
631 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
632 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
633 authid => $authid , authtypecode=>$authtypecode, );
636 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
638 my $type = $authority_types->find($authtypecode);
640 authority_types => $authority_types,
641 authtypecode => $authtypecode,
644 authtypetext => $type ? $type->authtypetext : "",
645 hide_marc => C4::Context->preference('hide_marc'),
647 output_html_with_http_headers $input, $cookie, $template->output;