Corrections to ensure message list appears in the proper box (Bug 3668).
[koha.git] / authorities / authorities.pl
blobaacb7f607644f1d9af316247d19e510b521bf9fd
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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
21 use strict;
22 use CGI;
23 use C4::Auth;
24 use C4::Output;
25 use C4::AuthoritiesMarc;
26 use C4::Context;
27 use C4::Koha; # XXX subfield_is_koha_internal_p
28 use Date::Calc qw(Today);
29 use MARC::File::USMARC;
30 use MARC::File::XML;
31 use C4::Biblio;
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
41 =cut
43 sub build_authorized_values_list ($$$$$$$) {
44 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
46 my @authorised_values;
47 my %authorised_lib;
49 # builds list, depending on authorised value...
51 #---- branch
52 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
53 my $sth =
54 $dbh->prepare(
55 "select branchcode,branchname from branches order by branchname");
56 $sth->execute;
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;
65 #----- itemtypes
67 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
68 my $sth =
69 $dbh->prepare(
70 "select itemtype,description from itemtypes order by description");
71 $sth->execute;
72 push @authorised_values, ""
73 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
75 my $itemtype;
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
85 else {
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,
100 -default => $value,
101 -labels => \%authorised_lib,
102 -override => 1,
103 -size => 1,
104 -multiple => 0,
105 -tabindex => 1,
106 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
107 -class => "input_marceditor",
112 =item create_input
113 builds the <input ...> entry for a subfield.
114 =cut
116 sub create_input {
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/"/&quot;/g;
123 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
124 my $max_length = 9999;
125 if ($tag eq '000') {
126 $max_length = 24;
127 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
128 $max_length = 40;
131 # if there is no value provided but a default value in parameters, get it
132 unless ($value) {
133 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
135 # get today date & replace YYYY, MM, DD if provided in the default value
136 my ( $year, $month, $day ) = Today();
137 $month = sprintf( "%02d", $month );
138 $day = sprintf( "%02d", $day );
139 $value =~ s/YYYY/$year/g;
140 $value =~ s/MM/$month/g;
141 $value =~ s/DD/$day/g;
143 my $dbh = C4::Context->dbh;
144 my %subfield_data = (
145 tag => $tag,
146 subfield => $subfield,
147 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
148 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
149 tag_mandatory => $tagslib->{$tag}->{mandatory},
150 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
151 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
152 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
153 index => $index_tag,
154 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
155 value => $value,
157 if($subfield eq '@'){
158 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
159 } else {
160 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
163 if(exists $mandatory_z3950->{$tag.$subfield}){
164 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
167 $subfield_data{visibility} = "display:none;"
168 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
169 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
172 # it's an authorised field
173 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
174 $subfield_data{marc_value} =
175 build_authorized_values_list( $tag, $subfield, $value, $dbh,
176 $authorised_values_sth,$index_tag,$index_subfield );
178 # it's a thesaurus / authority field
180 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
181 $subfield_data{marc_value} =
182 "<input type=\"text\"
183 id=\"".$subfield_data{id}."\"
184 name=\"".$subfield_data{id}."\"
185 value=\"$value\"
186 class=\"input_marceditor\"
187 tabindex=\"1\"
188 disabled=\"disabled\"
189 readonly=\"readonly\" \/>
190 <span class=\"buttonDot\"
191 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}')\">...</span>
193 # it's a plugin field
195 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
197 # opening plugin. Just check wether we are on a developper computer on a production one
198 # (the cgidir differs)
199 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
200 unless (-r $cgidir and -d $cgidir) {
201 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
203 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
204 do $plugin || die "Plugin Failed: ".$plugin;
205 my $extended_param;
206 eval{
207 $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
209 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
210 # my ( $function_name, $javascript,$extended_param );
212 $subfield_data{marc_value} =
213 "<input tabindex=\"1\"
214 type=\"text\"
215 id=\"".$subfield_data{id}."\"
216 size=\"67\"
217 maxlength=\"$max_length\"
218 name=\"".$subfield_data{id}."\"
219 value=\"$value\"
220 class=\"input_marceditor\"
221 onfocus=\"Focus$function_name($index_tag)\"
222 onblur=\"Blur$function_name($index_tag); \" \/>
223 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
224 $javascript";
225 # it's an hidden field
227 elsif ( $tag eq '' ) {
228 $subfield_data{marc_value} =
229 "<input tabindex=\"1\"
230 type=\"hidden\"
231 id=\"".$subfield_data{id}."\"
232 name=\"".$subfield_data{id}."\"
233 size=\"67\"
234 maxlength=\"$max_length\"
235 value=\"$value\" \/>
238 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
239 $subfield_data{marc_value} =
240 "<input type=\"text\"
241 id=\"".$subfield_data{id}."\"
242 name=\"".$subfield_data{id}."\"
243 class=\"input_marceditor\"
244 tabindex=\"1\"
245 size=\"67\"
246 maxlength=\"$max_length\"
247 value=\"$value\"
248 \/>";
250 # it's a standard field
252 else {
253 if (
254 length($value) > 100
256 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
257 and $tag < 400 && $subfield eq 'a' )
258 or ( $tag >= 600
259 and $tag < 700
260 && C4::Context->preference("marcflavour") eq "MARC21" )
263 $subfield_data{marc_value} =
264 "<textarea cols=\"70\"
265 rows=\"4\"
266 id=\"".$subfield_data{id}."\"
267 name=\"".$subfield_data{id}."\"
268 class=\"input_marceditor\"
269 tabindex=\"1\"
270 size=\"67\"
271 maxlength=\"$max_length\"
272 >$value</textarea>
275 else {
276 $subfield_data{marc_value} =
277 "<input type=\"text\"
278 id=\"".$subfield_data{id}."\"
279 name=\"".$subfield_data{id}."\"
280 value=\"$value\"
281 tabindex=\"1\"
282 size=\"67\"
283 maxlength=\"$max_length\"
284 class=\"input_marceditor\"
289 $subfield_data{'index_subfield'} = $index_subfield;
290 return \%subfield_data;
293 =item format_indicator
295 Translate indicator value for output form - specifically, map
296 indicator = ' ' to ''. This is for the convenience of a cataloger
297 using a mouse to select an indicator input.
299 =cut
301 sub format_indicator {
302 my $ind_value = shift;
303 return '' if not defined $ind_value;
304 return '' if $ind_value eq ' ';
305 return $ind_value;
308 =item CreateKey
310 Create a random value to set it into the input name
312 =cut
314 sub CreateKey(){
315 return int(rand(1000000));
318 sub build_tabs ($$$$$) {
319 my ( $template, $record, $dbh, $encoding,$input ) = @_;
321 # fill arrays
322 my @loop_data = ();
323 my $tag;
325 my $authorised_values_sth = $dbh->prepare(
326 "SELECT authorised_value,lib
327 FROM authorised_values
328 WHERE category=? ORDER BY lib"
331 # in this array, we will push all the 10 tabs
332 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
333 my @BIG_LOOP;
334 my %seen;
335 my @tab_data; # all tags to display
337 foreach my $used ( keys %$tagslib ){
338 push @tab_data,$used if not $seen{$used};
339 $seen{$used}++;
342 my $max_num_tab=9;
343 # loop through each tab 0 through 9
344 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
345 my @loop_data = (); #innerloop in the template.
346 my $i = 0;
347 foreach my $tag (sort @tab_data) {
348 $i++;
349 next if ! $tag;
350 my ($indicator1, $indicator2);
351 my $index_tag = CreateKey;
353 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
354 # if MARC::Record is empty => use tab as master loop.
355 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
356 my @fields;
357 if ( $tag ne '000' ) {
358 @fields = $record->field($tag);
360 else {
361 push @fields, $record->leader(); # if tag == 000
363 # loop through each field
364 foreach my $field (@fields) {
366 my @subfields_data;
367 if ( $tag < 10 ) {
368 my ( $value, $subfield );
369 if ( $tag ne '000' ) {
370 $value = $field->data();
371 $subfield = "@";
373 else {
374 $value = $field;
375 $subfield = '@';
377 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
378 push(
379 @subfields_data,
380 &create_input(
381 $tag, $subfield, $value, $index_tag, $tabloop, $record,
382 $authorised_values_sth,$input
386 else {
387 my @subfields = $field->subfields();
388 foreach my $subfieldcount ( 0 .. $#subfields ) {
389 my $subfield = $subfields[$subfieldcount][0];
390 my $value = $subfields[$subfieldcount][1];
391 next if ( length $subfield != 1 );
392 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
393 push(
394 @subfields_data,
395 &create_input(
396 $tag, $subfield, $value, $index_tag, $tabloop,
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
410 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
411 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
412 ); #check for visibility flag
413 next if ( defined( $field->subfield($subfield) ) );
414 push(
415 @subfields_data,
416 &create_input(
417 $tag, $subfield, '', $index_tag, $tabloop, $record,
418 $authorised_values_sth,$input
422 if ( $#subfields_data >= 0 ) {
423 # build the tag entry.
424 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
425 # have twice the same "name" value, and cgi->param() will return only one, making
426 # all subfields to be merged in a single field.
427 my %tag_data = (
428 tag => $tag,
429 index => $index_tag,
430 tag_lib => $tagslib->{$tag}->{lib},
431 repeatable => $tagslib->{$tag}->{repeatable},
432 mandatory => $tagslib->{$tag}->{mandatory},
433 subfield_loop => \@subfields_data,
434 fixedfield => ($tag < 10)?(1):(0),
435 random => CreateKey,
437 if ($tag >= 10){ # no indicator for theses tag
438 $tag_data{indicator1} = format_indicator($field->indicator(1)),
439 $tag_data{indicator2} = format_indicator($field->indicator(2)),
441 push( @loop_data, \%tag_data );
443 } # foreach $field end
445 # if breeding is empty
447 else {
448 my @subfields_data;
449 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
450 next if ( length $subfield != 1 );
451 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
452 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
453 ; #check for visibility flag
454 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
455 push(
456 @subfields_data,
457 &create_input(
458 $tag, $subfield, '', $index_tag, $tabloop, $record,
459 $authorised_values_sth,$input
463 if ( $#subfields_data >= 0 ) {
464 my %tag_data = (
465 tag => $tag,
466 index => $index_tag,
467 tag_lib => $tagslib->{$tag}->{lib},
468 repeatable => $tagslib->{$tag}->{repeatable},
469 mandatory => $tagslib->{$tag}->{mandatory},
470 indicator1 => $indicator1,
471 indicator2 => $indicator2,
472 subfield_loop => \@subfields_data,
473 tagfirstsubfield => $subfields_data[0],
474 fixedfield => ($tag < 10)?(1):(0)
477 push @loop_data, \%tag_data ;
481 if ( $#loop_data >= 0 ) {
482 push @BIG_LOOP, {
483 number => $tabloop,
484 innerloop => \@loop_data,
488 $template->param( BIG_LOOP => \@BIG_LOOP );
492 sub build_hidden_data () {
493 # build hidden data =>
494 # we store everything, even if we show only requested subfields.
496 my @loop_data =();
497 my $i=0;
498 foreach my $tag (keys %{$tagslib}) {
499 my $previous_tag = '';
501 # loop through each subfield
502 foreach my $subfield (keys %{$tagslib->{$tag}}) {
503 next if ($subfield eq 'lib');
504 next if ($subfield eq 'tab');
505 next if ($subfield eq 'mandatory');
506 next if ($subfield eq 'repeatable');
507 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
508 my %subfield_data;
509 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
510 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
511 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
512 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
513 push(@loop_data, \%subfield_data);
514 $i++
519 # ========================
520 # MAIN
521 #=========================
522 my $input = new CGI;
523 my $z3950 = $input->param('z3950');
524 my $error = $input->param('error');
525 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
526 my $op = $input->param('op');
527 my $nonav = $input->param('nonav');
528 my $myindex = $input->param('index');
529 my $linkid=$input->param('linkid');
530 my $authtypecode = $input->param('authtypecode');
532 my $dbh = C4::Context->dbh;
533 $authtypecode = &GetAuthTypeCode($authid) if !$authtypecode;
535 my ($template, $loggedinuser, $cookie)
536 = get_template_and_user({template_name => "authorities/authorities.tmpl",
537 query => $input,
538 type => "intranet",
539 authnotrequired => 0,
540 flagsrequired => {editauthorities => 1},
541 debug => 1,
543 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
544 $tagslib = GetTagsLabels(1,$authtypecode);
545 my $record=-1;
546 my $encoding="";
547 $record = GetAuthority($authid) if ($authid);
548 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
549 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
550 $is_a_modif=0;
551 if ($authid) {
552 $is_a_modif=1;
553 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
554 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
557 #------------------------------------------------------------------------------------------------------------------------------
558 if ($op eq "add") {
559 #------------------------------------------------------------------------------------------------------------------------------
560 # rebuild
561 my @tags = $input->param('tag');
562 my @subfields = $input->param('subfield');
563 my @values = $input->param('field_value');
564 # build indicator hash.
565 my @ind_tag = $input->param('ind_tag');
566 my @indicator = $input->param('indicator');
567 my @params = $input->param();
568 my $record = TransformHtmlToMarc(\@params,$input);
569 if (C4::Context->preference("marcflavour") eq "UNIMARC"){
570 unless ($record->field('100')){
571 use POSIX qw(strftime);
572 my $string = strftime( "%Y%m%d", localtime(time) );
573 # set 50 to position 26 is biblios, 13 if authorities
574 my $pos=13;
575 $string = sprintf( "%-*s", 35, $string );
576 substr( $string, $pos , 2, "50" );
577 $record->append_fields(MARC::Field->new('100','','',"a"=>$string));
581 my ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
582 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
583 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
584 if (!$duplicateauthid or $confirm_not_duplicate) {
585 if ($is_a_modif ) {
586 ModAuthority($authid,$record,$authtypecode);
587 } else {
588 ($authid) = AddAuthority($record,$authid,$authtypecode);
590 print $input->redirect("detail.pl?authid=$authid");
591 exit;
592 } else {
593 # it may be a duplicate, warn the user and do nothing
594 build_tabs($template, $record, $dbh, $encoding,$input);
595 build_hidden_data;
596 $template->param(authid =>$authid,
597 duplicateauthid => $duplicateauthid,
598 duplicateauthvalue => $duplicateauthvalue,
601 } elsif ($op eq "delete") {
602 #------------------------------------------------------------------------------------------------------------------------------
603 &DelAuthority($authid);
604 if ($nonav){
605 print $input->redirect("auth_finder.pl");
606 }else{
607 print $input->redirect("authorities-home.pl?authid=0");
609 exit;
610 } else {
611 if ($op eq "duplicate")
613 $authid = "";
615 build_tabs ($template, $record, $dbh,$encoding,$input);
616 build_hidden_data;
617 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
618 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
619 authid => $authid , authtypecode=>$authtypecode, );
622 $template->param(authid => $authid,
623 authtypecode => $authtypecode,
624 linkid=>$linkid,
627 my $authtypes = getauthtypes;
628 my @authtypesloop;
629 foreach my $thisauthtype (keys %$authtypes) {
630 my $selected = 1 if $thisauthtype eq $authtypecode;
631 my %row =(value => $thisauthtype,
632 selected => $selected,
633 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
635 push @authtypesloop, \%row;
638 $template->param(authtypesloop => \@authtypesloop,
639 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
640 hide_marc => C4::Context->preference('hide_marc'),
642 output_html_with_http_headers $input, $cookie, $template->output;