Bug 12263: Fix startup issues blocking response to HUP
[koha.git] / authorities / authorities.pl
blobde8bbf8875e0e9c0e51fa31c2ceac9427cce043e
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::ImportBatch; #GetImportRecordMarc
28 use C4::Context;
29 use C4::Koha; # XXX subfield_is_koha_internal_p
30 use Date::Calc qw(Today);
31 use MARC::File::USMARC;
32 use MARC::File::XML;
33 use C4::Biblio;
34 use vars qw( $tagslib);
35 use vars qw( $authorised_values_sth);
36 use vars qw( $is_a_modif );
38 my $itemtype; # created here because it can be used in build_authorized_values_list sub
39 our($authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
41 =head1 FUNCTIONS
43 =over
45 =item build_authorized_values_list
47 builds list, depending on authorised value...
49 =cut
51 sub MARCfindbreeding_auth {
52 my ( $id ) = @_;
53 my ($marc, $encoding) = GetImportRecordMarc($id);
54 if ($marc) {
55 my $record = MARC::Record->new_from_usmarc($marc);
56 if ( !defined(ref($record)) ) {
57 return -1;
58 } else {
59 return $record, $encoding;
61 } else {
62 return -1;
66 sub build_authorized_values_list {
67 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
69 my @authorised_values;
70 my %authorised_lib;
73 #---- branch
74 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
75 my $sth =
76 $dbh->prepare(
77 "select branchcode,branchname from branches order by branchname");
78 $sth->execute;
79 push @authorised_values, ""
80 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
82 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
83 push @authorised_values, $branchcode;
84 $authorised_lib{$branchcode} = $branchname;
87 #----- itemtypes
89 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
90 my $sth =
91 $dbh->prepare(
92 "select itemtype,description from itemtypes order by description");
93 $sth->execute;
94 push @authorised_values, ""
95 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
96 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
98 my $itemtype;
100 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
101 push @authorised_values, $itemtype;
102 $authorised_lib{$itemtype} = $description;
104 $value = $itemtype unless ($value);
106 #---- "true" authorised value
108 else {
109 $authorised_values_sth->execute(
110 $tagslib->{$tag}->{$subfield}->{authorised_value} );
112 push @authorised_values, ""
113 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
114 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
116 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
117 push @authorised_values, $value;
118 $authorised_lib{$value} = $lib;
121 return CGI::scrolling_list(
122 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
123 -values => \@authorised_values,
124 -default => $value,
125 -labels => \%authorised_lib,
126 -override => 1,
127 -size => 1,
128 -multiple => 0,
129 -tabindex => 1,
130 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
131 -class => "input_marceditor",
136 =item create_input
138 builds the <input ...> entry for a subfield.
140 =cut
142 sub create_input {
143 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
145 my $index_subfield = CreateKey(); # create a specifique key for each subfield
147 $value =~ s/"/&quot;/g;
149 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
150 my $max_length = 9999;
151 if ($tag eq '000') {
152 $max_length = 24;
153 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
154 $max_length = 40;
157 # if there is no value provided but a default value in parameters, get it
158 if ($value eq '') {
159 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
160 if (!defined $value) {
161 $value = q{};
164 # get today date & replace YYYY, MM, DD if provided in the default value
165 my ( $year, $month, $day ) = Today();
166 $month = sprintf( "%02d", $month );
167 $day = sprintf( "%02d", $day );
168 $value =~ s/YYYY/$year/g;
169 $value =~ s/MM/$month/g;
170 $value =~ s/DD/$day/g;
172 my $dbh = C4::Context->dbh;
174 # map '@' as "subfield" label for fixed fields
175 # to something that's allowed in a div id.
176 my $id_subfield = $subfield;
177 $id_subfield = "00" if $id_subfield eq "@";
179 my %subfield_data = (
180 tag => $tag,
181 subfield => $id_subfield,
182 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
183 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
184 tag_mandatory => $tagslib->{$tag}->{mandatory},
185 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
186 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
187 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
188 index => $index_tag,
189 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
190 value => $value,
191 random => CreateKey(),
194 if(exists $mandatory_z3950->{$tag.$subfield}){
195 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
198 $subfield_data{visibility} = "display:none;"
199 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
200 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
203 # it's an authorised field
204 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
205 $subfield_data{marc_value} =
206 build_authorized_values_list( $tag, $subfield, $value, $dbh,
207 $authorised_values_sth,$index_tag,$index_subfield );
209 # it's a thesaurus / authority field
211 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
212 $subfield_data{marc_value} =
213 "<input type=\"text\"
214 id=\"".$subfield_data{id}."\"
215 name=\"".$subfield_data{id}."\"
216 value=\"$value\"
217 class=\"input_marceditor\"
218 tabindex=\"1\" \/>
219 <a href=\"#\" class=\"buttonDot\"
220 onclick=\"openAuth(this.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode}."','auth'); return false;\" tabindex=\"1\" title=\"Tag Editor\">...</a>
222 # it's a plugin field
224 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
226 # opening plugin. Just check whether we are on a developer computer on a production one
227 # (the cgidir differs)
228 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
229 unless (-r $cgidir and -d $cgidir) {
230 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
232 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
233 do $plugin || die "Plugin Failed: ".$plugin;
234 my $extended_param;
235 eval{
236 $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
238 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
239 # my ( $function_name, $javascript,$extended_param );
241 $subfield_data{marc_value} =
242 "<input tabindex=\"1\"
243 type=\"text\"
244 id=\"".$subfield_data{id}."\"
245 size=\"67\"
246 maxlength=\"$max_length\"
247 name=\"".$subfield_data{id}."\"
248 value=\"$value\"
249 class=\"input_marceditor\"
250 onfocus=\"Focus$function_name($index_tag)\"
251 onblur=\"Blur$function_name($index_tag); \" \/>
252 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
253 $javascript";
254 # it's an hidden field
256 elsif ( $tag eq '' ) {
257 $subfield_data{marc_value} =
258 "<input tabindex=\"1\"
259 type=\"hidden\"
260 id=\"".$subfield_data{id}."\"
261 name=\"".$subfield_data{id}."\"
262 size=\"67\"
263 maxlength=\"$max_length\"
264 value=\"$value\" \/>
267 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
268 $subfield_data{marc_value} =
269 "<input type=\"text\"
270 id=\"".$subfield_data{id}."\"
271 name=\"".$subfield_data{id}."\"
272 class=\"input_marceditor\"
273 tabindex=\"1\"
274 size=\"67\"
275 maxlength=\"$max_length\"
276 value=\"$value\"
277 \/>";
279 # it's a standard field
281 else {
282 if (
283 length($value) > 100
285 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
286 and $tag < 400 && $subfield eq 'a' )
287 or ( $tag >= 600
288 and $tag < 700
289 && C4::Context->preference("marcflavour") eq "MARC21" )
292 $subfield_data{marc_value} =
293 "<textarea cols=\"70\"
294 rows=\"4\"
295 id=\"".$subfield_data{id}."\"
296 name=\"".$subfield_data{id}."\"
297 class=\"input_marceditor\"
298 tabindex=\"1\"
299 size=\"67\"
300 maxlength=\"$max_length\"
301 >$value</textarea>
304 else {
305 $subfield_data{marc_value} =
306 "<input type=\"text\"
307 id=\"".$subfield_data{id}."\"
308 name=\"".$subfield_data{id}."\"
309 value=\"$value\"
310 tabindex=\"1\"
311 size=\"67\"
312 maxlength=\"$max_length\"
313 class=\"input_marceditor\"
318 $subfield_data{'index_subfield'} = $index_subfield;
319 return \%subfield_data;
322 =item format_indicator
324 Translate indicator value for output form - specifically, map
325 indicator = ' ' to ''. This is for the convenience of a cataloger
326 using a mouse to select an indicator input.
328 =cut
330 sub format_indicator {
331 my $ind_value = shift;
332 return '' if not defined $ind_value;
333 return '' if $ind_value eq ' ';
334 return $ind_value;
337 =item CreateKey
339 Create a random value to set it into the input name
341 =cut
343 sub CreateKey {
344 return int(rand(1000000));
347 sub build_tabs {
348 my ( $template, $record, $dbh, $encoding,$input ) = @_;
350 # fill arrays
351 my @loop_data = ();
352 my $tag;
354 my $authorised_values_sth = $dbh->prepare(
355 "SELECT authorised_value,lib
356 FROM authorised_values
357 WHERE category=? ORDER BY lib"
360 # in this array, we will push all the 10 tabs
361 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
362 my @BIG_LOOP;
363 my %seen;
364 my @tab_data; # all tags to display
366 foreach my $used ( keys %$tagslib ){
367 push @tab_data,$used if not $seen{$used};
368 $seen{$used}++;
371 my $max_num_tab=9;
372 # loop through each tab 0 through 9
373 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
374 my @loop_data = (); #innerloop in the template.
375 my $i = 0;
376 foreach my $tag (sort @tab_data) {
377 $i++;
378 next if ! $tag;
379 my ($indicator1, $indicator2);
380 my $index_tag = CreateKey;
382 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
383 # if MARC::Record is empty => use tab as master loop.
384 if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
385 my @fields;
386 if ( $tag ne '000' ) {
387 @fields = $record->field($tag);
389 else {
390 push @fields, $record->leader(); # if tag == 000
392 # loop through each field
393 foreach my $field (@fields) {
395 my @subfields_data;
396 if ( $tag < 10 ) {
397 my ( $value, $subfield );
398 if ( $tag ne '000' ) {
399 $value = $field->data();
400 $subfield = "@";
402 else {
403 $value = $field;
404 $subfield = '@';
406 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
407 push(
408 @subfields_data,
409 &create_input(
410 $tag, $subfield, $value, $index_tag, $tabloop, $record,
411 $authorised_values_sth,$input
415 else {
416 my @subfields = $field->subfields();
417 foreach my $subfieldcount ( 0 .. $#subfields ) {
418 my $subfield = $subfields[$subfieldcount][0];
419 my $value = $subfields[$subfieldcount][1];
420 next if ( length $subfield != 1 );
421 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
422 push(
423 @subfields_data,
424 &create_input(
425 $tag, $subfield, $value, $index_tag, $tabloop,
426 $record, $authorised_values_sth,$input
432 # now, loop again to add parameter subfield that are not in the MARC::Record
433 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
435 next if ( length $subfield != 1 );
436 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
437 next if ( $tag < 10 );
438 next
439 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
440 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
441 ); #check for visibility flag
442 next if ( defined( $field->subfield($subfield) ) );
443 push(
444 @subfields_data,
445 &create_input(
446 $tag, $subfield, '', $index_tag, $tabloop, $record,
447 $authorised_values_sth,$input
451 if ( $#subfields_data >= 0 ) {
452 # build the tag entry.
453 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
454 # have twice the same "name" value, and cgi->param() will return only one, making
455 # all subfields to be merged in a single field.
456 my %tag_data = (
457 tag => $tag,
458 index => $index_tag,
459 tag_lib => $tagslib->{$tag}->{lib},
460 repeatable => $tagslib->{$tag}->{repeatable},
461 mandatory => $tagslib->{$tag}->{mandatory},
462 subfield_loop => \@subfields_data,
463 fixedfield => ($tag < 10)?(1):(0),
464 random => CreateKey,
466 if ($tag >= 10){ # no indicator for theses tag
467 $tag_data{indicator1} = format_indicator($field->indicator(1)),
468 $tag_data{indicator2} = format_indicator($field->indicator(2)),
470 push( @loop_data, \%tag_data );
472 } # foreach $field end
474 # if breeding is empty
476 else {
477 my @subfields_data;
478 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
479 next if ( length $subfield != 1 );
480 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
481 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
482 ; #check for visibility flag
483 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
484 push(
485 @subfields_data,
486 &create_input(
487 $tag, $subfield, '', $index_tag, $tabloop, $record,
488 $authorised_values_sth,$input
492 if ( $#subfields_data >= 0 ) {
493 my %tag_data = (
494 tag => $tag,
495 index => $index_tag,
496 tag_lib => $tagslib->{$tag}->{lib},
497 repeatable => $tagslib->{$tag}->{repeatable},
498 mandatory => $tagslib->{$tag}->{mandatory},
499 indicator1 => $indicator1,
500 indicator2 => $indicator2,
501 subfield_loop => \@subfields_data,
502 tagfirstsubfield => $subfields_data[0],
503 fixedfield => ($tag < 10)?(1):(0)
506 push @loop_data, \%tag_data ;
510 if ( $#loop_data >= 0 ) {
511 push @BIG_LOOP, {
512 number => $tabloop,
513 innerloop => \@loop_data,
517 $template->param( BIG_LOOP => \@BIG_LOOP );
521 sub build_hidden_data {
522 # build hidden data =>
523 # we store everything, even if we show only requested subfields.
525 my @loop_data =();
526 my $i=0;
527 foreach my $tag (keys %{$tagslib}) {
528 my $previous_tag = '';
530 # loop through each subfield
531 foreach my $subfield (keys %{$tagslib->{$tag}}) {
532 next if ($subfield eq 'lib');
533 next if ($subfield eq 'tab');
534 next if ($subfield eq 'mandatory');
535 next if ($subfield eq 'repeatable');
536 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
537 my %subfield_data;
538 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
539 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
540 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
541 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
542 push(@loop_data, \%subfield_data);
543 $i++
548 =back
550 =cut
553 # ========================
554 # MAIN
555 #=========================
556 my $input = new CGI;
557 my $z3950 = $input->param('z3950');
558 my $error = $input->param('error');
559 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
560 my $op = $input->param('op');
561 my $nonav = $input->param('nonav');
562 my $myindex = $input->param('index');
563 my $linkid=$input->param('linkid');
564 my $authtypecode = $input->param('authtypecode');
565 my $breedingid = $input->param('breedingid');
567 my $dbh = C4::Context->dbh;
568 if(!$authtypecode) {
569 $authtypecode = $authid? &GetAuthTypeCode($authid): '';
572 my ($template, $loggedinuser, $cookie)
573 = get_template_and_user({template_name => "authorities/authorities.tmpl",
574 query => $input,
575 type => "intranet",
576 authnotrequired => 0,
577 flagsrequired => {editauthorities => 1},
578 debug => 1,
580 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,breedingid=>$breedingid,);
582 $tagslib = GetTagsLabels(1,$authtypecode);
583 my $record=-1;
584 my $encoding="";
585 if (($authid) && !($breedingid)){
586 $record = GetAuthority($authid);
588 if ($breedingid) {
589 ( $record, $encoding ) = MARCfindbreeding_auth( $breedingid );
592 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
593 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
594 $is_a_modif=0;
595 if ($authid) {
596 $is_a_modif=1;
597 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
598 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
600 $op ||= q{};
601 #------------------------------------------------------------------------------------------------------------------------------
602 if ($op eq "add") {
603 #------------------------------------------------------------------------------------------------------------------------------
604 # rebuild
605 my @tags = $input->param('tag');
606 my @subfields = $input->param('subfield');
607 my @values = $input->param('field_value');
608 # build indicator hash.
609 my @ind_tag = $input->param('ind_tag');
610 my @indicator = $input->param('indicator');
611 my $record = TransformHtmlToMarc($input);
613 my ($duplicateauthid,$duplicateauthvalue);
614 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
615 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
616 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
617 if (!$duplicateauthid or $confirm_not_duplicate) {
618 if ($is_a_modif ) {
619 ModAuthority($authid,$record,$authtypecode);
620 } else {
621 ($authid) = AddAuthority($record,$authid,$authtypecode);
623 if ($myindex) {
624 print $input->redirect("blinddetail-biblio-search.pl?authid=$authid&index=$myindex");
625 } else {
626 print $input->redirect("detail.pl?authid=$authid");
628 exit;
629 } else {
630 # it may be a duplicate, warn the user and do nothing
631 build_tabs($template, $record, $dbh, $encoding,$input);
632 build_hidden_data;
633 $template->param(authid =>$authid,
634 duplicateauthid => $duplicateauthid,
635 duplicateauthvalue => $duplicateauthvalue->{'authorized'}->[0]->{'heading'},
638 } elsif ($op eq "delete") {
639 #------------------------------------------------------------------------------------------------------------------------------
640 &DelAuthority($authid);
641 if ($nonav){
642 print $input->redirect("auth_finder.pl");
643 }else{
644 print $input->redirect("authorities-home.pl?authid=0");
646 exit;
647 } else {
648 if ($op eq "duplicate")
650 $authid = "";
652 build_tabs ($template, $record, $dbh,$encoding,$input);
653 build_hidden_data;
654 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
655 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
656 authid => $authid , authtypecode=>$authtypecode, );
659 $template->param(authid => $authid,
660 authtypecode => $authtypecode,
661 linkid=>$linkid,
664 my $authtypes = getauthtypes;
665 my @authtypesloop;
666 foreach my $thisauthtype (keys %$authtypes) {
667 my %row =(value => $thisauthtype,
668 selected => $thisauthtype eq $authtypecode,
669 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
671 push @authtypesloop, \%row;
674 $template->param(authtypesloop => \@authtypesloop,
675 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
676 hide_marc => C4::Context->preference('hide_marc'),
678 output_html_with_http_headers $input, $cookie, $template->output;