Bug 7108 - OPAC Translations Display Patch
[koha.git] / authorities / authorities.pl
blob2f82f5a25351a8b46e0a15937836a0d45909164f
1 #!/usr/bin/perl
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
11 # version.
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.
21 use strict;
22 use warnings;
23 use CGI;
24 use C4::Auth;
25 use C4::Output;
26 use C4::AuthoritiesMarc;
27 use C4::Context;
28 use C4::Koha; # XXX subfield_is_koha_internal_p
29 use Date::Calc qw(Today);
30 use MARC::File::USMARC;
31 use MARC::File::XML;
32 use C4::Biblio;
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);
40 =head1 FUNCTIONS
42 =over
44 =item build_authorized_values_list
46 builds list, depending on authorised value...
48 =cut
50 sub build_authorized_values_list {
51 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
53 my @authorised_values;
54 my %authorised_lib;
57 #---- branch
58 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
59 my $sth =
60 $dbh->prepare(
61 "select branchcode,branchname from branches order by branchname");
62 $sth->execute;
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;
71 #----- itemtypes
73 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
74 my $sth =
75 $dbh->prepare(
76 "select itemtype,description from itemtypes order by description");
77 $sth->execute;
78 push @authorised_values, ""
79 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
81 my $itemtype;
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
91 else {
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,
106 -default => $value,
107 -labels => \%authorised_lib,
108 -override => 1,
109 -size => 1,
110 -multiple => 0,
111 -tabindex => 1,
112 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
113 -class => "input_marceditor",
118 =item create_input
120 builds the <input ...> entry for a subfield.
122 =cut
124 sub create_input {
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/"/&quot;/g;
131 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
132 my $max_length = 9999;
133 if ($tag eq '000') {
134 $max_length = 24;
135 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
136 $max_length = 40;
139 # if there is no value provided but a default value in parameters, get it
140 if ($value eq '') {
141 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
142 if (!defined $value) {
143 $value = q{};
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;
155 my %subfield_data = (
156 tag => $tag,
157 subfield => $subfield,
158 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
159 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
160 tag_mandatory => $tagslib->{$tag}->{mandatory},
161 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
162 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
163 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
164 index => $index_tag,
165 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
166 value => $value,
168 if($subfield eq '@'){
169 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
170 } else {
171 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
174 if(exists $mandatory_z3950->{$tag.$subfield}){
175 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
178 $subfield_data{visibility} = "display:none;"
179 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
180 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
183 # it's an authorised field
184 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
185 $subfield_data{marc_value} =
186 build_authorized_values_list( $tag, $subfield, $value, $dbh,
187 $authorised_values_sth,$index_tag,$index_subfield );
189 # it's a thesaurus / authority field
191 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
192 $subfield_data{marc_value} =
193 "<input type=\"text\"
194 id=\"".$subfield_data{id}."\"
195 name=\"".$subfield_data{id}."\"
196 value=\"$value\"
197 class=\"input_marceditor readonly\"
198 tabindex=\"1\"
199 readonly=\"readonly\" \/>
200 <a href=\"#\" class=\"buttonDot\"
201 onclick=\"openAuth(this.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode}."'); return false;\" tabindex=\"1\" title=\"Tag Editor\">...</a>
203 # it's a plugin field
205 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
207 # opening plugin. Just check wether we are on a developper computer on a production one
208 # (the cgidir differs)
209 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
210 unless (-r $cgidir and -d $cgidir) {
211 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
213 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
214 do $plugin || die "Plugin Failed: ".$plugin;
215 my $extended_param;
216 eval{
217 $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
219 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
220 # my ( $function_name, $javascript,$extended_param );
222 $subfield_data{marc_value} =
223 "<input tabindex=\"1\"
224 type=\"text\"
225 id=\"".$subfield_data{id}."\"
226 size=\"67\"
227 maxlength=\"$max_length\"
228 name=\"".$subfield_data{id}."\"
229 value=\"$value\"
230 class=\"input_marceditor\"
231 onfocus=\"Focus$function_name($index_tag)\"
232 onblur=\"Blur$function_name($index_tag); \" \/>
233 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
234 $javascript";
235 # it's an hidden field
237 elsif ( $tag eq '' ) {
238 $subfield_data{marc_value} =
239 "<input tabindex=\"1\"
240 type=\"hidden\"
241 id=\"".$subfield_data{id}."\"
242 name=\"".$subfield_data{id}."\"
243 size=\"67\"
244 maxlength=\"$max_length\"
245 value=\"$value\" \/>
248 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
249 $subfield_data{marc_value} =
250 "<input type=\"text\"
251 id=\"".$subfield_data{id}."\"
252 name=\"".$subfield_data{id}."\"
253 class=\"input_marceditor\"
254 tabindex=\"1\"
255 size=\"67\"
256 maxlength=\"$max_length\"
257 value=\"$value\"
258 \/>";
260 # it's a standard field
262 else {
263 if (
264 length($value) > 100
266 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
267 and $tag < 400 && $subfield eq 'a' )
268 or ( $tag >= 600
269 and $tag < 700
270 && C4::Context->preference("marcflavour") eq "MARC21" )
273 $subfield_data{marc_value} =
274 "<textarea cols=\"70\"
275 rows=\"4\"
276 id=\"".$subfield_data{id}."\"
277 name=\"".$subfield_data{id}."\"
278 class=\"input_marceditor\"
279 tabindex=\"1\"
280 size=\"67\"
281 maxlength=\"$max_length\"
282 >$value</textarea>
285 else {
286 $subfield_data{marc_value} =
287 "<input type=\"text\"
288 id=\"".$subfield_data{id}."\"
289 name=\"".$subfield_data{id}."\"
290 value=\"$value\"
291 tabindex=\"1\"
292 size=\"67\"
293 maxlength=\"$max_length\"
294 class=\"input_marceditor\"
299 $subfield_data{'index_subfield'} = $index_subfield;
300 return \%subfield_data;
303 =item format_indicator
305 Translate indicator value for output form - specifically, map
306 indicator = ' ' to ''. This is for the convenience of a cataloger
307 using a mouse to select an indicator input.
309 =cut
311 sub format_indicator {
312 my $ind_value = shift;
313 return '' if not defined $ind_value;
314 return '' if $ind_value eq ' ';
315 return $ind_value;
318 =item CreateKey
320 Create a random value to set it into the input name
322 =cut
324 sub CreateKey {
325 return int(rand(1000000));
328 sub build_tabs {
329 my ( $template, $record, $dbh, $encoding,$input ) = @_;
331 # fill arrays
332 my @loop_data = ();
333 my $tag;
335 my $authorised_values_sth = $dbh->prepare(
336 "SELECT authorised_value,lib
337 FROM authorised_values
338 WHERE category=? ORDER BY lib"
341 # in this array, we will push all the 10 tabs
342 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
343 my @BIG_LOOP;
344 my %seen;
345 my @tab_data; # all tags to display
347 foreach my $used ( keys %$tagslib ){
348 push @tab_data,$used if not $seen{$used};
349 $seen{$used}++;
352 my $max_num_tab=9;
353 # loop through each tab 0 through 9
354 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
355 my @loop_data = (); #innerloop in the template.
356 my $i = 0;
357 foreach my $tag (sort @tab_data) {
358 $i++;
359 next if ! $tag;
360 my ($indicator1, $indicator2);
361 my $index_tag = CreateKey;
363 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
364 # if MARC::Record is empty => use tab as master loop.
365 if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
366 my @fields;
367 if ( $tag ne '000' ) {
368 @fields = $record->field($tag);
370 else {
371 push @fields, $record->leader(); # if tag == 000
373 # loop through each field
374 foreach my $field (@fields) {
376 my @subfields_data;
377 if ( $tag < 10 ) {
378 my ( $value, $subfield );
379 if ( $tag ne '000' ) {
380 $value = $field->data();
381 $subfield = "@";
383 else {
384 $value = $field;
385 $subfield = '@';
387 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
388 push(
389 @subfields_data,
390 &create_input(
391 $tag, $subfield, $value, $index_tag, $tabloop, $record,
392 $authorised_values_sth,$input
396 else {
397 my @subfields = $field->subfields();
398 foreach my $subfieldcount ( 0 .. $#subfields ) {
399 my $subfield = $subfields[$subfieldcount][0];
400 my $value = $subfields[$subfieldcount][1];
401 next if ( length $subfield != 1 );
402 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
403 push(
404 @subfields_data,
405 &create_input(
406 $tag, $subfield, $value, $index_tag, $tabloop,
407 $record, $authorised_values_sth,$input
413 # now, loop again to add parameter subfield that are not in the MARC::Record
414 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
416 next if ( length $subfield != 1 );
417 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
418 next if ( $tag < 10 );
419 next
420 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
421 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
422 ); #check for visibility flag
423 next if ( defined( $field->subfield($subfield) ) );
424 push(
425 @subfields_data,
426 &create_input(
427 $tag, $subfield, '', $index_tag, $tabloop, $record,
428 $authorised_values_sth,$input
432 if ( $#subfields_data >= 0 ) {
433 # build the tag entry.
434 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
435 # have twice the same "name" value, and cgi->param() will return only one, making
436 # all subfields to be merged in a single field.
437 my %tag_data = (
438 tag => $tag,
439 index => $index_tag,
440 tag_lib => $tagslib->{$tag}->{lib},
441 repeatable => $tagslib->{$tag}->{repeatable},
442 mandatory => $tagslib->{$tag}->{mandatory},
443 subfield_loop => \@subfields_data,
444 fixedfield => ($tag < 10)?(1):(0),
445 random => CreateKey,
447 if ($tag >= 10){ # no indicator for theses tag
448 $tag_data{indicator1} = format_indicator($field->indicator(1)),
449 $tag_data{indicator2} = format_indicator($field->indicator(2)),
451 push( @loop_data, \%tag_data );
453 } # foreach $field end
455 # if breeding is empty
457 else {
458 my @subfields_data;
459 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
460 next if ( length $subfield != 1 );
461 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
462 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
463 ; #check for visibility flag
464 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
465 push(
466 @subfields_data,
467 &create_input(
468 $tag, $subfield, '', $index_tag, $tabloop, $record,
469 $authorised_values_sth,$input
473 if ( $#subfields_data >= 0 ) {
474 my %tag_data = (
475 tag => $tag,
476 index => $index_tag,
477 tag_lib => $tagslib->{$tag}->{lib},
478 repeatable => $tagslib->{$tag}->{repeatable},
479 mandatory => $tagslib->{$tag}->{mandatory},
480 indicator1 => $indicator1,
481 indicator2 => $indicator2,
482 subfield_loop => \@subfields_data,
483 tagfirstsubfield => $subfields_data[0],
484 fixedfield => ($tag < 10)?(1):(0)
487 push @loop_data, \%tag_data ;
491 if ( $#loop_data >= 0 ) {
492 push @BIG_LOOP, {
493 number => $tabloop,
494 innerloop => \@loop_data,
498 $template->param( BIG_LOOP => \@BIG_LOOP );
502 sub build_hidden_data {
503 # build hidden data =>
504 # we store everything, even if we show only requested subfields.
506 my @loop_data =();
507 my $i=0;
508 foreach my $tag (keys %{$tagslib}) {
509 my $previous_tag = '';
511 # loop through each subfield
512 foreach my $subfield (keys %{$tagslib->{$tag}}) {
513 next if ($subfield eq 'lib');
514 next if ($subfield eq 'tab');
515 next if ($subfield eq 'mandatory');
516 next if ($subfield eq 'repeatable');
517 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
518 my %subfield_data;
519 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
520 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
521 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
522 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
523 push(@loop_data, \%subfield_data);
524 $i++
529 =back
531 =cut
534 # ========================
535 # MAIN
536 #=========================
537 my $input = new CGI;
538 my $z3950 = $input->param('z3950');
539 my $error = $input->param('error');
540 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
541 my $op = $input->param('op');
542 my $nonav = $input->param('nonav');
543 my $myindex = $input->param('index');
544 my $linkid=$input->param('linkid');
545 my $authtypecode = $input->param('authtypecode');
547 my $dbh = C4::Context->dbh;
548 if(!$authtypecode) {
549 $authtypecode = $authid? &GetAuthTypeCode($authid): '';
552 my ($template, $loggedinuser, $cookie)
553 = get_template_and_user({template_name => "authorities/authorities.tmpl",
554 query => $input,
555 type => "intranet",
556 authnotrequired => 0,
557 flagsrequired => {editauthorities => 1},
558 debug => 1,
560 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
561 $tagslib = GetTagsLabels(1,$authtypecode);
562 my $record=-1;
563 my $encoding="";
564 $record = GetAuthority($authid) if ($authid);
565 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
566 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
567 $is_a_modif=0;
568 if ($authid) {
569 $is_a_modif=1;
570 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
571 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
573 $op ||= q{};
574 #------------------------------------------------------------------------------------------------------------------------------
575 if ($op eq "add") {
576 #------------------------------------------------------------------------------------------------------------------------------
577 # rebuild
578 my @tags = $input->param('tag');
579 my @subfields = $input->param('subfield');
580 my @values = $input->param('field_value');
581 # build indicator hash.
582 my @ind_tag = $input->param('ind_tag');
583 my @indicator = $input->param('indicator');
584 my $record = TransformHtmlToMarc($input);
585 if (C4::Context->preference("marcflavour") eq "UNIMARC"){
586 unless ($record->field('100')){
587 use POSIX qw(strftime);
588 my $string = strftime( "%Y%m%d", localtime(time) );
589 # set 50 to position 26 is biblios, 13 if authorities
590 my $pos=13;
591 $string = sprintf( "%-*s", 35, $string );
592 substr( $string, $pos , 2, "50" );
593 $record->append_fields(MARC::Field->new('100','','',"a"=>$string));
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) {
602 if ($is_a_modif ) {
603 ModAuthority($authid,$record,$authtypecode);
604 } else {
605 ($authid) = AddAuthority($record,$authid,$authtypecode);
607 print $input->redirect("detail.pl?authid=$authid");
608 exit;
609 } else {
610 # it may be a duplicate, warn the user and do nothing
611 build_tabs($template, $record, $dbh, $encoding,$input);
612 build_hidden_data;
613 $template->param(authid =>$authid,
614 duplicateauthid => $duplicateauthid,
615 duplicateauthvalue => $duplicateauthvalue,
618 } elsif ($op eq "delete") {
619 #------------------------------------------------------------------------------------------------------------------------------
620 &DelAuthority($authid);
621 if ($nonav){
622 print $input->redirect("auth_finder.pl");
623 }else{
624 print $input->redirect("authorities-home.pl?authid=0");
626 exit;
627 } else {
628 if ($op eq "duplicate")
630 $authid = "";
632 build_tabs ($template, $record, $dbh,$encoding,$input);
633 build_hidden_data;
634 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
635 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
636 authid => $authid , authtypecode=>$authtypecode, );
639 $template->param(authid => $authid,
640 authtypecode => $authtypecode,
641 linkid=>$linkid,
644 my $authtypes = getauthtypes;
645 my @authtypesloop;
646 foreach my $thisauthtype (keys %$authtypes) {
647 my %row =(value => $thisauthtype,
648 selected => $thisauthtype eq $authtypecode,
649 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
651 push @authtypesloop, \%row;
654 $template->param(authtypesloop => \@authtypesloop,
655 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
656 hide_marc => C4::Context->preference('hide_marc'),
658 output_html_with_http_headers $input, $cookie, $template->output;