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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 use C4
::AuthoritiesMarc
;
28 use C4
::Koha
; # XXX subfield_is_koha_internal_p
29 use Date
::Calc
qw(Today);
30 use MARC
::File
::USMARC
;
33 use vars
qw( $tagslib);
34 use vars qw( $authorised_values_sth);
35 use vars qw( $is_a_modif );
37 my $itemtype; # created here because it can be used in build_authorized_values_list sub
38 our($authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
44 =item build_authorized_values_list
46 builds list, depending on authorised value...
50 sub build_authorized_values_list {
51 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
53 my @authorised_values;
58 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
61 "select branchcode,branchname from branches order by branchname");
63 push @authorised_values, ""
64 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
66 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
67 push @authorised_values, $branchcode;
68 $authorised_lib{$branchcode} = $branchname;
73 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
76 "select itemtype,description from itemtypes order by description");
78 push @authorised_values, ""
79 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
83 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
84 push @authorised_values, $itemtype;
85 $authorised_lib{$itemtype} = $description;
87 $value = $itemtype unless ($value);
89 #---- "true" authorised value
92 $authorised_values_sth->execute(
93 $tagslib->{$tag}->{$subfield}->{authorised_value} );
95 push @authorised_values, ""
96 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
98 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
99 push @authorised_values, $value;
100 $authorised_lib{$value} = $lib;
103 return CGI::scrolling_list(
104 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
105 -values => \@authorised_values,
107 -labels => \%authorised_lib,
112 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
113 -class => "input_marceditor",
120 builds the <input ...> entry for a subfield.
125 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
127 my $index_subfield = CreateKey(); # create a specifique key for each subfield
129 $value =~ s/"/"/g;
131 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
132 my $max_length = 9999;
135 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
139 # if there is no value provided but a default value in parameters, get it
141 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
142 if (!defined $value) {
146 # get today date & replace YYYY, MM, DD if provided in the default value
147 my ( $year, $month, $day ) = Today
();
148 $month = sprintf( "%02d", $month );
149 $day = sprintf( "%02d", $day );
150 $value =~ s/YYYY/$year/g;
151 $value =~ s/MM/$month/g;
152 $value =~ s/DD/$day/g;
154 my $dbh = C4
::Context
->dbh;
156 # map '@' as "subfield" label for fixed fields
157 # to something that's allowed in a div id.
158 my $id_subfield = $subfield;
159 $id_subfield = "00" if $id_subfield eq "@";
161 my %subfield_data = (
163 subfield
=> $id_subfield,
164 marc_lib
=> substr( $tagslib->{$tag}->{$subfield}->{lib
}, 0, 22 ),
165 marc_lib_plain
=> $tagslib->{$tag}->{$subfield}->{lib
},
166 tag_mandatory
=> $tagslib->{$tag}->{mandatory
},
167 mandatory
=> $tagslib->{$tag}->{$subfield}->{mandatory
},
168 repeatable
=> $tagslib->{$tag}->{$subfield}->{repeatable
},
169 kohafield
=> $tagslib->{$tag}->{$subfield}->{kohafield
},
171 id
=> "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
173 random
=> CreateKey
(),
176 if(exists $mandatory_z3950->{$tag.$subfield}){
177 $subfield_data{z3950_mandatory
} = $mandatory_z3950->{$tag.$subfield};
180 $subfield_data{visibility
} = "display:none;"
181 if ( ($tagslib->{$tag}->{$subfield}->{hidden
} % 2 == 1) and $value ne ''
182 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory
})
185 # it's an authorised field
186 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} ) {
187 $subfield_data{marc_value
} =
188 build_authorized_values_list
( $tag, $subfield, $value, $dbh,
189 $authorised_values_sth,$index_tag,$index_subfield );
191 # it's a thesaurus / authority field
193 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode
} ) {
194 $subfield_data{marc_value
} =
195 "<input type=\"text\"
196 id=\"".$subfield_data{id
}."\"
197 name=\"".$subfield_data{id
}."\"
199 class=\"input_marceditor\"
201 <a href=\"#\" class=\"buttonDot\"
202 onclick=\"openAuth(this.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode
}."','auth'); return false;\" tabindex=\"1\" title=\"Tag Editor\">...</a>
204 # it's a plugin field
206 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
208 # opening plugin. Just check whether we are on a developer computer on a production one
209 # (the cgidir differs)
210 my $cgidir = C4
::Context
->intranetdir . "/cgi-bin/cataloguing/value_builder";
211 unless (-r
$cgidir and -d
$cgidir) {
212 $cgidir = C4
::Context
->intranetdir . "/cataloguing/value_builder";
214 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
215 do $plugin || die "Plugin Failed: ".$plugin;
218 $extended_param = plugin_parameters
( $dbh, $rec, $tagslib, $subfield_data{id
}, $tabloop );
220 my ( $function_name, $javascript ) = plugin_javascript
( $dbh, $rec, $tagslib, $subfield_data{id
}, $tabloop );
221 # my ( $function_name, $javascript,$extended_param );
223 $subfield_data{marc_value
} =
224 "<input tabindex=\"1\"
226 id=\"".$subfield_data{id
}."\"
228 maxlength=\"$max_length\"
229 name=\"".$subfield_data{id
}."\"
231 class=\"input_marceditor\"
232 onfocus=\"Focus$function_name($index_tag)\"
233 onblur=\"Blur$function_name($index_tag); \" \/>
234 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
236 # it's an hidden field
238 elsif ( $tag eq '' ) {
239 $subfield_data{marc_value
} =
240 "<input tabindex=\"1\"
242 id=\"".$subfield_data{id
}."\"
243 name=\"".$subfield_data{id
}."\"
245 maxlength=\"$max_length\"
249 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
250 $subfield_data{marc_value
} =
251 "<input type=\"text\"
252 id=\"".$subfield_data{id
}."\"
253 name=\"".$subfield_data{id
}."\"
254 class=\"input_marceditor\"
257 maxlength=\"$max_length\"
261 # it's a standard field
267 ( C4
::Context
->preference("marcflavour") eq "UNIMARC" && $tag >= 300
268 and $tag < 400 && $subfield eq 'a' )
271 && C4
::Context
->preference("marcflavour") eq "MARC21" )
274 $subfield_data{marc_value
} =
275 "<textarea cols=\"70\"
277 id=\"".$subfield_data{id
}."\"
278 name=\"".$subfield_data{id
}."\"
279 class=\"input_marceditor\"
282 maxlength=\"$max_length\"
287 $subfield_data{marc_value
} =
288 "<input type=\"text\"
289 id=\"".$subfield_data{id
}."\"
290 name=\"".$subfield_data{id
}."\"
294 maxlength=\"$max_length\"
295 class=\"input_marceditor\"
300 $subfield_data{'index_subfield'} = $index_subfield;
301 return \
%subfield_data;
304 =item format_indicator
306 Translate indicator value for output form - specifically, map
307 indicator = ' ' to ''. This is for the convenience of a cataloger
308 using a mouse to select an indicator input.
312 sub format_indicator
{
313 my $ind_value = shift;
314 return '' if not defined $ind_value;
315 return '' if $ind_value eq ' ';
321 Create a random value to set it into the input name
326 return int(rand(1000000));
330 my ( $template, $record, $dbh, $encoding,$input ) = @_;
336 my $authorised_values_sth = $dbh->prepare(
337 "SELECT authorised_value,lib
338 FROM authorised_values
339 WHERE category=? ORDER BY lib"
342 # in this array, we will push all the 10 tabs
343 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
346 my @tab_data; # all tags to display
348 foreach my $used ( keys %$tagslib ){
349 push @tab_data,$used if not $seen{$used};
354 # loop through each tab 0 through 9
355 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
356 my @loop_data = (); #innerloop in the template.
358 foreach my $tag (sort @tab_data) {
361 my ($indicator1, $indicator2);
362 my $index_tag = CreateKey
;
364 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
365 # if MARC::Record is empty => use tab as master loop.
366 if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
368 if ( $tag ne '000' ) {
369 @fields = $record->field($tag);
372 push @fields, $record->leader(); # if tag == 000
374 # loop through each field
375 foreach my $field (@fields) {
379 my ( $value, $subfield );
380 if ( $tag ne '000' ) {
381 $value = $field->data();
388 next if ( $tagslib->{$tag}->{$subfield}->{tab
} ne $tabloop );
392 $tag, $subfield, $value, $index_tag, $tabloop, $record,
393 $authorised_values_sth,$input
398 my @subfields = $field->subfields();
399 foreach my $subfieldcount ( 0 .. $#subfields ) {
400 my $subfield = $subfields[$subfieldcount][0];
401 my $value = $subfields[$subfieldcount][1];
402 next if ( length $subfield != 1 );
403 next if ( $tagslib->{$tag}->{$subfield}->{tab
} ne $tabloop );
407 $tag, $subfield, $value, $index_tag, $tabloop,
408 $record, $authorised_values_sth,$input
414 # now, loop again to add parameter subfield that are not in the MARC::Record
415 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
417 next if ( length $subfield != 1 );
418 next if ( $tagslib->{$tag}->{$subfield}->{tab
} ne $tabloop );
419 next if ( $tag < 10 );
421 if ( ( $tagslib->{$tag}->{$subfield}->{hidden
} <= -4 )
422 or ( $tagslib->{$tag}->{$subfield}->{hidden
} >= 5 )
423 ); #check for visibility flag
424 next if ( defined( $field->subfield($subfield) ) );
428 $tag, $subfield, '', $index_tag, $tabloop, $record,
429 $authorised_values_sth,$input
433 if ( $#subfields_data >= 0 ) {
434 # build the tag entry.
435 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
436 # have twice the same "name" value, and cgi->param() will return only one, making
437 # all subfields to be merged in a single field.
441 tag_lib
=> $tagslib->{$tag}->{lib
},
442 repeatable
=> $tagslib->{$tag}->{repeatable
},
443 mandatory
=> $tagslib->{$tag}->{mandatory
},
444 subfield_loop
=> \
@subfields_data,
445 fixedfield
=> ($tag < 10)?
(1):(0),
448 if ($tag >= 10){ # no indicator for theses tag
449 $tag_data{indicator1
} = format_indicator
($field->indicator(1)),
450 $tag_data{indicator2
} = format_indicator
($field->indicator(2)),
452 push( @loop_data, \
%tag_data );
454 } # foreach $field end
456 # if breeding is empty
460 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
461 next if ( length $subfield != 1 );
462 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden
} <= -5 )
463 or ( $tagslib->{$tag}->{$subfield}->{hidden
} >= 4 ) )
464 ; #check for visibility flag
465 next if ( $tagslib->{$tag}->{$subfield}->{tab
} ne $tabloop );
469 $tag, $subfield, '', $index_tag, $tabloop, $record,
470 $authorised_values_sth,$input
474 if ( $#subfields_data >= 0 ) {
478 tag_lib
=> $tagslib->{$tag}->{lib
},
479 repeatable
=> $tagslib->{$tag}->{repeatable
},
480 mandatory
=> $tagslib->{$tag}->{mandatory
},
481 indicator1
=> $indicator1,
482 indicator2
=> $indicator2,
483 subfield_loop
=> \
@subfields_data,
484 tagfirstsubfield
=> $subfields_data[0],
485 fixedfield
=> ($tag < 10)?
(1):(0)
488 push @loop_data, \
%tag_data ;
492 if ( $#loop_data >= 0 ) {
495 innerloop
=> \
@loop_data,
499 $template->param( BIG_LOOP
=> \
@BIG_LOOP );
503 sub build_hidden_data
{
504 # build hidden data =>
505 # we store everything, even if we show only requested subfields.
509 foreach my $tag (keys %{$tagslib}) {
510 my $previous_tag = '';
512 # loop through each subfield
513 foreach my $subfield (keys %{$tagslib->{$tag}}) {
514 next if ($subfield eq 'lib');
515 next if ($subfield eq 'tab');
516 next if ($subfield eq 'mandatory');
517 next if ($subfield eq 'repeatable');
518 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
520 $subfield_data{marc_lib
}=$tagslib->{$tag}->{$subfield}->{lib
};
521 $subfield_data{marc_mandatory
}=$tagslib->{$tag}->{$subfield}->{mandatory
};
522 $subfield_data{marc_repeatable
}=$tagslib->{$tag}->{$subfield}->{repeatable
};
523 $subfield_data{marc_value
}="<input type=\"hidden\" name=\"field_value[]\">";
524 push(@loop_data, \
%subfield_data);
535 # ========================
537 #=========================
539 my $z3950 = $input->param('z3950');
540 my $error = $input->param('error');
541 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
542 my $op = $input->param('op');
543 my $nonav = $input->param('nonav');
544 my $myindex = $input->param('index');
545 my $linkid=$input->param('linkid');
546 my $authtypecode = $input->param('authtypecode');
548 my $dbh = C4
::Context
->dbh;
550 $authtypecode = $authid?
&GetAuthTypeCode
($authid): '';
553 my ($template, $loggedinuser, $cookie)
554 = get_template_and_user
({template_name
=> "authorities/authorities.tmpl",
557 authnotrequired
=> 0,
558 flagsrequired
=> {editauthorities
=> 1},
561 $template->param(nonav
=> $nonav,index=>$myindex,authtypecode
=>$authtypecode,);
562 $tagslib = GetTagsLabels
(1,$authtypecode);
565 $record = GetAuthority
($authid) if ($authid);
566 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
567 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
571 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField
("auth_header.authid",$authtypecode);
572 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField
("auth_header.authtypecode",$authtypecode);
575 #------------------------------------------------------------------------------------------------------------------------------
577 #------------------------------------------------------------------------------------------------------------------------------
579 my @tags = $input->param('tag');
580 my @subfields = $input->param('subfield');
581 my @values = $input->param('field_value');
582 # build indicator hash.
583 my @ind_tag = $input->param('ind_tag');
584 my @indicator = $input->param('indicator');
585 my $record = TransformHtmlToMarc
($input);
587 my ($duplicateauthid,$duplicateauthvalue);
588 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority
($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
589 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
590 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
591 if (!$duplicateauthid or $confirm_not_duplicate) {
593 ModAuthority
($authid,$record,$authtypecode);
595 ($authid) = AddAuthority
($record,$authid,$authtypecode);
598 print $input->redirect("blinddetail-biblio-search.pl?authid=$authid&index=$myindex");
600 print $input->redirect("detail.pl?authid=$authid");
604 # it may be a duplicate, warn the user and do nothing
605 build_tabs
($template, $record, $dbh, $encoding,$input);
607 $template->param(authid
=>$authid,
608 duplicateauthid
=> $duplicateauthid,
609 duplicateauthvalue
=> $duplicateauthvalue->{'authorized'}->[0]->{'heading'},
612 } elsif ($op eq "delete") {
613 #------------------------------------------------------------------------------------------------------------------------------
614 &DelAuthority
($authid);
616 print $input->redirect("auth_finder.pl");
618 print $input->redirect("authorities-home.pl?authid=0");
622 if ($op eq "duplicate")
626 build_tabs
($template, $record, $dbh,$encoding,$input);
628 $template->param(oldauthtypetagfield
=>$oldauthtypetagfield, oldauthtypetagsubfield
=>$oldauthtypetagsubfield,
629 oldauthnumtagfield
=>$oldauthnumtagfield, oldauthnumtagsubfield
=>$oldauthnumtagsubfield,
630 authid
=> $authid , authtypecode
=>$authtypecode, );
633 $template->param(authid
=> $authid,
634 authtypecode
=> $authtypecode,
638 my $authtypes = getauthtypes
;
640 foreach my $thisauthtype (keys %$authtypes) {
641 my %row =(value
=> $thisauthtype,
642 selected
=> $thisauthtype eq $authtypecode,
643 authtypetext
=> $authtypes->{$thisauthtype}{'authtypetext'},
645 push @authtypesloop, \
%row;
648 $template->param(authtypesloop
=> \
@authtypesloop,
649 authtypetext
=> $authtypes->{$authtypecode}{'authtypetext'},
650 hide_marc
=> C4
::Context
->preference('hide_marc'),
652 output_html_with_http_headers
$input, $cookie, $template->output;