Follow up to Bug 6680: tiny up download lists formats
[koha.git] / authorities / authorities.pl
blobee842e02666369406237fa6d8e40e7653b5b4daa
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;
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 = (
162 tag => $tag,
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},
170 index => $index_tag,
171 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
172 value => $value,
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}."\"
198 value=\"$value\"
199 class=\"input_marceditor\"
200 tabindex=\"1\" \/>
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;
216 my $extended_param;
217 eval{
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\"
225 type=\"text\"
226 id=\"".$subfield_data{id}."\"
227 size=\"67\"
228 maxlength=\"$max_length\"
229 name=\"".$subfield_data{id}."\"
230 value=\"$value\"
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>
235 $javascript";
236 # it's an hidden field
238 elsif ( $tag eq '' ) {
239 $subfield_data{marc_value} =
240 "<input tabindex=\"1\"
241 type=\"hidden\"
242 id=\"".$subfield_data{id}."\"
243 name=\"".$subfield_data{id}."\"
244 size=\"67\"
245 maxlength=\"$max_length\"
246 value=\"$value\" \/>
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\"
255 tabindex=\"1\"
256 size=\"67\"
257 maxlength=\"$max_length\"
258 value=\"$value\"
259 \/>";
261 # it's a standard field
263 else {
264 if (
265 length($value) > 100
267 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
268 and $tag < 400 && $subfield eq 'a' )
269 or ( $tag >= 600
270 and $tag < 700
271 && C4::Context->preference("marcflavour") eq "MARC21" )
274 $subfield_data{marc_value} =
275 "<textarea cols=\"70\"
276 rows=\"4\"
277 id=\"".$subfield_data{id}."\"
278 name=\"".$subfield_data{id}."\"
279 class=\"input_marceditor\"
280 tabindex=\"1\"
281 size=\"67\"
282 maxlength=\"$max_length\"
283 >$value</textarea>
286 else {
287 $subfield_data{marc_value} =
288 "<input type=\"text\"
289 id=\"".$subfield_data{id}."\"
290 name=\"".$subfield_data{id}."\"
291 value=\"$value\"
292 tabindex=\"1\"
293 size=\"67\"
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.
310 =cut
312 sub format_indicator {
313 my $ind_value = shift;
314 return '' if not defined $ind_value;
315 return '' if $ind_value eq ' ';
316 return $ind_value;
319 =item CreateKey
321 Create a random value to set it into the input name
323 =cut
325 sub CreateKey {
326 return int(rand(1000000));
329 sub build_tabs {
330 my ( $template, $record, $dbh, $encoding,$input ) = @_;
332 # fill arrays
333 my @loop_data = ();
334 my $tag;
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
344 my @BIG_LOOP;
345 my %seen;
346 my @tab_data; # all tags to display
348 foreach my $used ( keys %$tagslib ){
349 push @tab_data,$used if not $seen{$used};
350 $seen{$used}++;
353 my $max_num_tab=9;
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.
357 my $i = 0;
358 foreach my $tag (sort @tab_data) {
359 $i++;
360 next if ! $tag;
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' ) ) {
367 my @fields;
368 if ( $tag ne '000' ) {
369 @fields = $record->field($tag);
371 else {
372 push @fields, $record->leader(); # if tag == 000
374 # loop through each field
375 foreach my $field (@fields) {
377 my @subfields_data;
378 if ( $tag < 10 ) {
379 my ( $value, $subfield );
380 if ( $tag ne '000' ) {
381 $value = $field->data();
382 $subfield = "@";
384 else {
385 $value = $field;
386 $subfield = '@';
388 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
389 push(
390 @subfields_data,
391 &create_input(
392 $tag, $subfield, $value, $index_tag, $tabloop, $record,
393 $authorised_values_sth,$input
397 else {
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 );
404 push(
405 @subfields_data,
406 &create_input(
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 );
420 next
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) ) );
425 push(
426 @subfields_data,
427 &create_input(
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.
438 my %tag_data = (
439 tag => $tag,
440 index => $index_tag,
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),
446 random => CreateKey,
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
458 else {
459 my @subfields_data;
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 );
466 push(
467 @subfields_data,
468 &create_input(
469 $tag, $subfield, '', $index_tag, $tabloop, $record,
470 $authorised_values_sth,$input
474 if ( $#subfields_data >= 0 ) {
475 my %tag_data = (
476 tag => $tag,
477 index => $index_tag,
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 ) {
493 push @BIG_LOOP, {
494 number => $tabloop,
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.
507 my @loop_data =();
508 my $i=0;
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");
519 my %subfield_data;
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);
525 $i++
530 =back
532 =cut
535 # ========================
536 # MAIN
537 #=========================
538 my $input = new CGI;
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;
549 if(!$authtypecode) {
550 $authtypecode = $authid? &GetAuthTypeCode($authid): '';
553 my ($template, $loggedinuser, $cookie)
554 = get_template_and_user({template_name => "authorities/authorities.tmpl",
555 query => $input,
556 type => "intranet",
557 authnotrequired => 0,
558 flagsrequired => {editauthorities => 1},
559 debug => 1,
561 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
562 $tagslib = GetTagsLabels(1,$authtypecode);
563 my $record=-1;
564 my $encoding="";
565 $record = GetAuthority($authid) if ($authid);
566 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
567 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
568 $is_a_modif=0;
569 if ($authid) {
570 $is_a_modif=1;
571 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
572 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
574 $op ||= q{};
575 #------------------------------------------------------------------------------------------------------------------------------
576 if ($op eq "add") {
577 #------------------------------------------------------------------------------------------------------------------------------
578 # rebuild
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);
586 if (C4::Context->preference("marcflavour") eq "UNIMARC"){
587 unless ($record->field('100')){
588 use POSIX qw(strftime);
589 my $string = strftime( "%Y%m%d", localtime(time) );
590 # set 50 to position 26 is biblios, 13 if authorities
591 my $pos=13;
592 $string = sprintf( "%-*s", 35, $string );
593 substr( $string, $pos , 2, "50" );
594 $record->append_fields(MARC::Field->new('100','','',"a"=>$string));
598 my ($duplicateauthid,$duplicateauthvalue);
599 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
600 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
601 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
602 if (!$duplicateauthid or $confirm_not_duplicate) {
603 if ($is_a_modif ) {
604 ModAuthority($authid,$record,$authtypecode);
605 } else {
606 ($authid) = AddAuthority($record,$authid,$authtypecode);
608 if ($myindex) {
609 print $input->redirect("blinddetail-biblio-search.pl?authid=$authid&index=$myindex");
610 } else {
611 print $input->redirect("detail.pl?authid=$authid");
613 exit;
614 } else {
615 # it may be a duplicate, warn the user and do nothing
616 build_tabs($template, $record, $dbh, $encoding,$input);
617 build_hidden_data;
618 $template->param(authid =>$authid,
619 duplicateauthid => $duplicateauthid,
620 duplicateauthvalue => $duplicateauthvalue->{'authorized'}->[0]->{'heading'},
623 } elsif ($op eq "delete") {
624 #------------------------------------------------------------------------------------------------------------------------------
625 &DelAuthority($authid);
626 if ($nonav){
627 print $input->redirect("auth_finder.pl");
628 }else{
629 print $input->redirect("authorities-home.pl?authid=0");
631 exit;
632 } else {
633 if ($op eq "duplicate")
635 $authid = "";
637 build_tabs ($template, $record, $dbh,$encoding,$input);
638 build_hidden_data;
639 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
640 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
641 authid => $authid , authtypecode=>$authtypecode, );
644 $template->param(authid => $authid,
645 authtypecode => $authtypecode,
646 linkid=>$linkid,
649 my $authtypes = getauthtypes;
650 my @authtypesloop;
651 foreach my $thisauthtype (keys %$authtypes) {
652 my %row =(value => $thisauthtype,
653 selected => $thisauthtype eq $authtypecode,
654 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
656 push @authtypesloop, \%row;
659 $template->param(authtypesloop => \@authtypesloop,
660 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
661 hide_marc => C4::Context->preference('hide_marc'),
663 output_html_with_http_headers $input, $cookie, $template->output;