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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
25 use C4::AuthoritiesMarc;
27 use C4::Koha; # XXX subfield_is_koha_internal_p
28 use Date::Calc qw(Today);
29 use MARC::File::USMARC;
32 use vars qw( $tagslib);
33 use vars qw( $authorised_values_sth);
34 use vars qw( $is_a_modif );
36 my $itemtype; # created here because it can be used in build_authorized_values_list sub
37 our($authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
39 =item build_authorized_values_list
43 sub build_authorized_values_list ($$$$$$$) {
44 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
46 my @authorised_values;
49 # builds list, depending on authorised value...
52 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
55 "select branchcode,branchname from branches order by branchname");
57 push @authorised_values, ""
58 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
60 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
61 push @authorised_values, $branchcode;
62 $authorised_lib{$branchcode} = $branchname;
67 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
70 "select itemtype,description from itemtypes order by description");
72 push @authorised_values, ""
73 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
77 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
78 push @authorised_values, $itemtype;
79 $authorised_lib{$itemtype} = $description;
81 $value = $itemtype unless ($value);
83 #---- "true" authorised value
86 $authorised_values_sth->execute(
87 $tagslib->{$tag}->{$subfield}->{authorised_value} );
89 push @authorised_values, ""
90 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
92 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
93 push @authorised_values, $value;
94 $authorised_lib{$value} = $lib;
97 return CGI::scrolling_list(
98 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
99 -values => \@authorised_values,
101 -labels => \%authorised_lib,
106 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
107 -class => "input_marceditor",
113 builds the <input ...> entry for a subfield.
117 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
119 my $index_subfield = CreateKey(); # create a specifique key for each subfield
121 $value =~ s/"/"/g;
123 # if there is no value provided but a default value in parameters, get it
125 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
127 # get today date & replace YYYY, MM, DD if provided in the default value
128 my ( $year, $month, $day ) = Today();
129 $month = sprintf( "%02d", $month );
130 $day = sprintf( "%02d", $day );
131 $value =~ s/YYYY/$year/g;
132 $value =~ s/MM/$month/g;
133 $value =~ s/DD/$day/g;
135 my $dbh = C4::Context->dbh;
136 my %subfield_data = (
138 subfield => $subfield,
139 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
140 marc_lib_plain => $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_".$subfield."_".$index_tag."_".$index_subfield,
149 if($subfield eq '@'){
150 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
152 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
155 if(exists $mandatory_z3950->{$tag.$subfield}){
156 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
159 $subfield_data{visibility} = "display:none;"
160 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
161 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
164 # it's an authorised field
165 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
166 $subfield_data{marc_value} =
167 build_authorized_values_list( $tag, $subfield, $value, $dbh,
168 $authorised_values_sth,$index_tag,$index_subfield );
170 # it's a thesaurus / authority field
172 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
173 $subfield_data{marc_value} =
174 "<input type=\"text\"
175 id=\"".$subfield_data{id}."\"
176 name=\"".$subfield_data{id}."\"
178 class=\"input_marceditor\"
180 disabled=\"disabled\"
181 readonly=\"readonly\" \/>
182 <span class=\"buttonDot\"
183 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}')\">...</span>
185 # it's a plugin field
187 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
189 # opening plugin. Just check wether we are on a developper computer on a production one
190 # (the cgidir differs)
191 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
192 unless (-r $cgidir and -d $cgidir) {
193 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
195 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
196 do $plugin || die "Plugin Failed: ".$plugin;
197 my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
198 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
199 # my ( $function_name, $javascript,$extended_param );
201 $subfield_data{marc_value} =
202 "<input tabindex=\"1\"
204 id=\"".$subfield_data{id}."\"
207 name=\"".$subfield_data{id}."\"
209 class=\"input_marceditor\"
210 onfocus=\"Focus$function_name($index_tag)\"
211 onblur=\"Blur$function_name($index_tag); \" \/>
212 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
214 # it's an hidden field
216 elsif ( $tag eq '' ) {
217 $subfield_data{marc_value} =
218 "<input tabindex=\"1\"
220 id=\"".$subfield_data{id}."\"
221 name=\"".$subfield_data{id}."\"
227 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
228 $subfield_data{marc_value} =
229 "<input type=\"text\"
230 id=\"".$subfield_data{id}."\"
231 name=\"".$subfield_data{id}."\"
232 class=\"input_marceditor\"
239 # it's a standard field
245 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
246 and $tag < 400 && $subfield eq 'a' )
249 && C4::Context->preference("marcflavour") eq "MARC21" )
252 $subfield_data{marc_value} =
253 "<textarea cols=\"70\"
255 id=\"".$subfield_data{id}."\"
256 name=\"".$subfield_data{id}."\"
257 class=\"input_marceditor\"
265 $subfield_data{marc_value} =
266 "<input type=\"text\"
267 id=\"".$subfield_data{id}."\"
268 name=\"".$subfield_data{id}."\"
273 class=\"input_marceditor\"
278 $subfield_data{'index_subfield'} = $index_subfield;
279 return \%subfield_data;
284 Create a random value to set it into the input name
289 return int(rand(1000000));
292 sub build_tabs ($$$$$) {
293 my ( $template, $record, $dbh, $encoding,$input ) = @_;
299 my $authorised_values_sth = $dbh->prepare(
300 "SELECT authorised_value,lib
301 FROM authorised_values
302 WHERE category=? ORDER BY lib"
305 # in this array, we will push all the 10 tabs
306 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
309 my @tab_data; # all tags to display
311 foreach my $used ( keys %$tagslib ){
312 push @tab_data,$used if not $seen{$used};
317 # loop through each tab 0 through 9
318 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
319 my @loop_data = (); #innerloop in the template.
321 foreach my $tag (sort @tab_data) {
325 my $index_tag = CreateKey;
327 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
328 # if MARC::Record is empty => use tab as master loop.
329 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
331 if ( $tag ne '000' ) {
332 @fields = $record->field($tag);
335 push @fields, $record->leader(); # if tag == 000
337 # loop through each field
338 foreach my $field (@fields) {
342 my ( $value, $subfield );
343 if ( $tag ne '000' ) {
344 $value = $field->data();
351 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
355 $tag, $subfield, $value, $index_tag, $tabloop, $record,
356 $authorised_values_sth,$input
361 my @subfields = $field->subfields();
362 foreach my $subfieldcount ( 0 .. $#subfields ) {
363 my $subfield = $subfields[$subfieldcount][0];
364 my $value = $subfields[$subfieldcount][1];
365 next if ( length $subfield != 1 );
366 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
370 $tag, $subfield, $value, $index_tag, $tabloop,
371 $record, $authorised_values_sth,$input
377 # now, loop again to add parameter subfield that are not in the MARC::Record
378 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
380 next if ( length $subfield != 1 );
381 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
382 next if ( $tag < 10 );
384 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
385 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
386 ); #check for visibility flag
387 next if ( defined( $field->subfield($subfield) ) );
391 $tag, $subfield, '', $index_tag, $tabloop, $record,
392 $authorised_values_sth,$input
396 if ( $#subfields_data >= 0 ) {
397 # build the tag entry.
398 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
399 # have twice the same "name" value, and cgi->param() will return only one, making
400 # all subfields to be merged in a single field.
404 tag_lib => $tagslib->{$tag}->{lib},
405 repeatable => $tagslib->{$tag}->{repeatable},
406 subfield_loop => \@subfields_data,
407 fixedfield => ($tag < 10)?(1):(0),
410 if ($tag >= 010){ # no indicator for theses tag
411 $tag_data{indicator} = $field->indicator(1).$field->indicator(2);
413 push( @loop_data, \%tag_data );
415 } # foreach $field end
417 # if breeding is empty
421 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
422 next if ( length $subfield != 1 );
423 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
424 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
425 ; #check for visibility flag
426 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
430 $tag, $subfield, '', $index_tag, $tabloop, $record,
431 $authorised_values_sth,$input
435 if ( $#subfields_data >= 0 ) {
439 tag_lib => $tagslib->{$tag}->{lib},
440 repeatable => $tagslib->{$tag}->{repeatable},
441 indicator => $indicator,
442 subfield_loop => \@subfields_data,
443 tagfirstsubfield => $subfields_data[0],
444 fixedfield => ($tag < 10)?(1):(0)
447 push @loop_data, \%tag_data ;
451 if ( $#loop_data >= 0 ) {
454 innerloop => \@loop_data,
458 $template->param( BIG_LOOP => \@BIG_LOOP );
462 sub build_hidden_data () {
463 # build hidden data =>
464 # we store everything, even if we show only requested subfields.
468 foreach my $tag (keys %{$tagslib}) {
469 my $previous_tag = '';
471 # loop through each subfield
472 foreach my $subfield (keys %{$tagslib->{$tag}}) {
473 next if ($subfield eq 'lib');
474 next if ($subfield eq 'tab');
475 next if ($subfield eq 'mandatory');
476 next if ($subfield eq 'repeatable');
477 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
479 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
480 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
481 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
482 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
483 push(@loop_data, \%subfield_data);
489 # ========================
491 #=========================
493 my $z3950 = $input->param('z3950');
494 my $error = $input->param('error');
495 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
496 my $op = $input->param('op');
497 my $nonav = $input->param('nonav');
498 my $myindex = $input->param('index');
499 my $linkid=$input->param('linkid');
500 my $authtypecode = $input->param('authtypecode');
502 my $dbh = C4::Context->dbh;
503 $authtypecode = &GetAuthTypeCode($authid) if !$authtypecode;
505 my ($template, $loggedinuser, $cookie)
506 = get_template_and_user({template_name => "authorities/authorities.tmpl",
509 authnotrequired => 0,
510 flagsrequired => {editauthorities => 1},
513 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
514 $tagslib = GetTagsLabels(1,$authtypecode);
517 $record = GetAuthority($authid) if ($authid);
518 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
519 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
523 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
524 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
527 #------------------------------------------------------------------------------------------------------------------------------
529 #------------------------------------------------------------------------------------------------------------------------------
531 my @tags = $input->param('tag');
532 my @subfields = $input->param('subfield');
533 my @values = $input->param('field_value');
534 # build indicator hash.
535 my @ind_tag = $input->param('ind_tag');
536 my @indicator = $input->param('indicator');
537 my @params = $input->param();
538 my $record = TransformHtmlToMarc(\@params,$input);
539 if (C4::Context->preference("marcflavour") eq "UNIMARC"){
540 unless ($record->field('100')){
541 use POSIX qw(strftime);
542 my $string = strftime( "%Y%m%d", localtime(time) );
543 # set 50 to position 26 is biblios, 13 if authorities
545 $string = sprintf( "%-*s", 35, $string );
546 substr( $string, $pos , 2, "50" );
547 $record->append_fields(MARC::Field->new('100','','',"a"=>$string));
551 my ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
552 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
553 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
554 if (!$duplicateauthid or $confirm_not_duplicate) {
556 ModAuthority($authid,$record,$authtypecode,1);
558 ($authid) = AddAuthority($record,$authid,$authtypecode);
560 print $input->redirect("detail.pl?authid=$authid");
563 # it may be a duplicate, warn the user and do nothing
564 build_tabs($template, $record, $dbh, $encoding,$input);
566 $template->param(authid =>$authid,
567 duplicateauthid => $duplicateauthid,
568 duplicateauthvalue => $duplicateauthvalue,
571 } elsif ($op eq "delete") {
572 #------------------------------------------------------------------------------------------------------------------------------
573 &AUTHdelauthority($authid);
575 print $input->redirect("auth_finder.pl");
577 print $input->redirect("authorities-home.pl?authid=0");
581 if ($op eq "duplicate")
585 build_tabs ($template, $record, $dbh,$encoding,$input);
587 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
588 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
589 authid => $authid , authtypecode=>$authtypecode, );
592 $template->param(authid => $authid,
593 authtypecode => $authtypecode,
597 my $authtypes = getauthtypes;
599 foreach my $thisauthtype (keys %$authtypes) {
600 my $selected = 1 if $thisauthtype eq $authtypecode;
601 my %row =(value => $thisauthtype,
602 selected => $selected,
603 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
605 push @authtypesloop, \%row;
608 $template->param(authtypesloop => \@authtypesloop,
609 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
610 hide_marc => C4::Context->preference('hide_marc'),
612 output_html_with_http_headers $input, $cookie, $template->output;