Bug 4085 - Subscription type length wasn't taken into account
[koha.git] / authorities / authorities.pl
blob9eb205de35e5772c5bccd064192f444b9aba04fb
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 readonly=\"readonly\" \/>
189 <a href=\"#\" class=\"buttonDot\"
190 onclick=\"openAuth(this.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode}."'); return false;\" tabindex=\"1\" title=\"Tag Editor\">...</a>
192 # it's a plugin field
194 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
196 # opening plugin. Just check wether we are on a developper computer on a production one
197 # (the cgidir differs)
198 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
199 unless (-r $cgidir and -d $cgidir) {
200 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
202 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
203 do $plugin || die "Plugin Failed: ".$plugin;
204 my $extended_param;
205 eval{
206 $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
208 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
209 # my ( $function_name, $javascript,$extended_param );
211 $subfield_data{marc_value} =
212 "<input tabindex=\"1\"
213 type=\"text\"
214 id=\"".$subfield_data{id}."\"
215 size=\"67\"
216 maxlength=\"$max_length\"
217 name=\"".$subfield_data{id}."\"
218 value=\"$value\"
219 class=\"input_marceditor\"
220 onfocus=\"Focus$function_name($index_tag)\"
221 onblur=\"Blur$function_name($index_tag); \" \/>
222 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
223 $javascript";
224 # it's an hidden field
226 elsif ( $tag eq '' ) {
227 $subfield_data{marc_value} =
228 "<input tabindex=\"1\"
229 type=\"hidden\"
230 id=\"".$subfield_data{id}."\"
231 name=\"".$subfield_data{id}."\"
232 size=\"67\"
233 maxlength=\"$max_length\"
234 value=\"$value\" \/>
237 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
238 $subfield_data{marc_value} =
239 "<input type=\"text\"
240 id=\"".$subfield_data{id}."\"
241 name=\"".$subfield_data{id}."\"
242 class=\"input_marceditor\"
243 tabindex=\"1\"
244 size=\"67\"
245 maxlength=\"$max_length\"
246 value=\"$value\"
247 \/>";
249 # it's a standard field
251 else {
252 if (
253 length($value) > 100
255 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
256 and $tag < 400 && $subfield eq 'a' )
257 or ( $tag >= 600
258 and $tag < 700
259 && C4::Context->preference("marcflavour") eq "MARC21" )
262 $subfield_data{marc_value} =
263 "<textarea cols=\"70\"
264 rows=\"4\"
265 id=\"".$subfield_data{id}."\"
266 name=\"".$subfield_data{id}."\"
267 class=\"input_marceditor\"
268 tabindex=\"1\"
269 size=\"67\"
270 maxlength=\"$max_length\"
271 >$value</textarea>
274 else {
275 $subfield_data{marc_value} =
276 "<input type=\"text\"
277 id=\"".$subfield_data{id}."\"
278 name=\"".$subfield_data{id}."\"
279 value=\"$value\"
280 tabindex=\"1\"
281 size=\"67\"
282 maxlength=\"$max_length\"
283 class=\"input_marceditor\"
288 $subfield_data{'index_subfield'} = $index_subfield;
289 return \%subfield_data;
292 =item format_indicator
294 Translate indicator value for output form - specifically, map
295 indicator = ' ' to ''. This is for the convenience of a cataloger
296 using a mouse to select an indicator input.
298 =cut
300 sub format_indicator {
301 my $ind_value = shift;
302 return '' if not defined $ind_value;
303 return '' if $ind_value eq ' ';
304 return $ind_value;
307 =item CreateKey
309 Create a random value to set it into the input name
311 =cut
313 sub CreateKey(){
314 return int(rand(1000000));
317 sub build_tabs ($$$$$) {
318 my ( $template, $record, $dbh, $encoding,$input ) = @_;
320 # fill arrays
321 my @loop_data = ();
322 my $tag;
324 my $authorised_values_sth = $dbh->prepare(
325 "SELECT authorised_value,lib
326 FROM authorised_values
327 WHERE category=? ORDER BY lib"
330 # in this array, we will push all the 10 tabs
331 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
332 my @BIG_LOOP;
333 my %seen;
334 my @tab_data; # all tags to display
336 foreach my $used ( keys %$tagslib ){
337 push @tab_data,$used if not $seen{$used};
338 $seen{$used}++;
341 my $max_num_tab=9;
342 # loop through each tab 0 through 9
343 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
344 my @loop_data = (); #innerloop in the template.
345 my $i = 0;
346 foreach my $tag (sort @tab_data) {
347 $i++;
348 next if ! $tag;
349 my ($indicator1, $indicator2);
350 my $index_tag = CreateKey;
352 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
353 # if MARC::Record is empty => use tab as master loop.
354 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
355 my @fields;
356 if ( $tag ne '000' ) {
357 @fields = $record->field($tag);
359 else {
360 push @fields, $record->leader(); # if tag == 000
362 # loop through each field
363 foreach my $field (@fields) {
365 my @subfields_data;
366 if ( $tag < 10 ) {
367 my ( $value, $subfield );
368 if ( $tag ne '000' ) {
369 $value = $field->data();
370 $subfield = "@";
372 else {
373 $value = $field;
374 $subfield = '@';
376 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
377 push(
378 @subfields_data,
379 &create_input(
380 $tag, $subfield, $value, $index_tag, $tabloop, $record,
381 $authorised_values_sth,$input
385 else {
386 my @subfields = $field->subfields();
387 foreach my $subfieldcount ( 0 .. $#subfields ) {
388 my $subfield = $subfields[$subfieldcount][0];
389 my $value = $subfields[$subfieldcount][1];
390 next if ( length $subfield != 1 );
391 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
392 push(
393 @subfields_data,
394 &create_input(
395 $tag, $subfield, $value, $index_tag, $tabloop,
396 $record, $authorised_values_sth,$input
402 # now, loop again to add parameter subfield that are not in the MARC::Record
403 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
405 next if ( length $subfield != 1 );
406 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
407 next if ( $tag < 10 );
408 next
409 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
410 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
411 ); #check for visibility flag
412 next if ( defined( $field->subfield($subfield) ) );
413 push(
414 @subfields_data,
415 &create_input(
416 $tag, $subfield, '', $index_tag, $tabloop, $record,
417 $authorised_values_sth,$input
421 if ( $#subfields_data >= 0 ) {
422 # build the tag entry.
423 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
424 # have twice the same "name" value, and cgi->param() will return only one, making
425 # all subfields to be merged in a single field.
426 my %tag_data = (
427 tag => $tag,
428 index => $index_tag,
429 tag_lib => $tagslib->{$tag}->{lib},
430 repeatable => $tagslib->{$tag}->{repeatable},
431 mandatory => $tagslib->{$tag}->{mandatory},
432 subfield_loop => \@subfields_data,
433 fixedfield => ($tag < 10)?(1):(0),
434 random => CreateKey,
436 if ($tag >= 10){ # no indicator for theses tag
437 $tag_data{indicator1} = format_indicator($field->indicator(1)),
438 $tag_data{indicator2} = format_indicator($field->indicator(2)),
440 push( @loop_data, \%tag_data );
442 } # foreach $field end
444 # if breeding is empty
446 else {
447 my @subfields_data;
448 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
449 next if ( length $subfield != 1 );
450 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
451 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
452 ; #check for visibility flag
453 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
454 push(
455 @subfields_data,
456 &create_input(
457 $tag, $subfield, '', $index_tag, $tabloop, $record,
458 $authorised_values_sth,$input
462 if ( $#subfields_data >= 0 ) {
463 my %tag_data = (
464 tag => $tag,
465 index => $index_tag,
466 tag_lib => $tagslib->{$tag}->{lib},
467 repeatable => $tagslib->{$tag}->{repeatable},
468 mandatory => $tagslib->{$tag}->{mandatory},
469 indicator1 => $indicator1,
470 indicator2 => $indicator2,
471 subfield_loop => \@subfields_data,
472 tagfirstsubfield => $subfields_data[0],
473 fixedfield => ($tag < 10)?(1):(0)
476 push @loop_data, \%tag_data ;
480 if ( $#loop_data >= 0 ) {
481 push @BIG_LOOP, {
482 number => $tabloop,
483 innerloop => \@loop_data,
487 $template->param( BIG_LOOP => \@BIG_LOOP );
491 sub build_hidden_data () {
492 # build hidden data =>
493 # we store everything, even if we show only requested subfields.
495 my @loop_data =();
496 my $i=0;
497 foreach my $tag (keys %{$tagslib}) {
498 my $previous_tag = '';
500 # loop through each subfield
501 foreach my $subfield (keys %{$tagslib->{$tag}}) {
502 next if ($subfield eq 'lib');
503 next if ($subfield eq 'tab');
504 next if ($subfield eq 'mandatory');
505 next if ($subfield eq 'repeatable');
506 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
507 my %subfield_data;
508 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
509 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
510 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
511 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
512 push(@loop_data, \%subfield_data);
513 $i++
518 # ========================
519 # MAIN
520 #=========================
521 my $input = new CGI;
522 my $z3950 = $input->param('z3950');
523 my $error = $input->param('error');
524 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
525 my $op = $input->param('op');
526 my $nonav = $input->param('nonav');
527 my $myindex = $input->param('index');
528 my $linkid=$input->param('linkid');
529 my $authtypecode = $input->param('authtypecode');
531 my $dbh = C4::Context->dbh;
532 $authtypecode = &GetAuthTypeCode($authid) if !$authtypecode;
534 my ($template, $loggedinuser, $cookie)
535 = get_template_and_user({template_name => "authorities/authorities.tmpl",
536 query => $input,
537 type => "intranet",
538 authnotrequired => 0,
539 flagsrequired => {editauthorities => 1},
540 debug => 1,
542 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
543 $tagslib = GetTagsLabels(1,$authtypecode);
544 my $record=-1;
545 my $encoding="";
546 $record = GetAuthority($authid) if ($authid);
547 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
548 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
549 $is_a_modif=0;
550 if ($authid) {
551 $is_a_modif=1;
552 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
553 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
556 #------------------------------------------------------------------------------------------------------------------------------
557 if ($op eq "add") {
558 #------------------------------------------------------------------------------------------------------------------------------
559 # rebuild
560 my @tags = $input->param('tag');
561 my @subfields = $input->param('subfield');
562 my @values = $input->param('field_value');
563 # build indicator hash.
564 my @ind_tag = $input->param('ind_tag');
565 my @indicator = $input->param('indicator');
566 my @params = $input->param();
567 my $record = TransformHtmlToMarc(\@params,$input);
568 if (C4::Context->preference("marcflavour") eq "UNIMARC"){
569 unless ($record->field('100')){
570 use POSIX qw(strftime);
571 my $string = strftime( "%Y%m%d", localtime(time) );
572 # set 50 to position 26 is biblios, 13 if authorities
573 my $pos=13;
574 $string = sprintf( "%-*s", 35, $string );
575 substr( $string, $pos , 2, "50" );
576 $record->append_fields(MARC::Field->new('100','','',"a"=>$string));
580 my ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
581 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
582 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
583 if (!$duplicateauthid or $confirm_not_duplicate) {
584 if ($is_a_modif ) {
585 ModAuthority($authid,$record,$authtypecode);
586 } else {
587 ($authid) = AddAuthority($record,$authid,$authtypecode);
589 print $input->redirect("detail.pl?authid=$authid");
590 exit;
591 } else {
592 # it may be a duplicate, warn the user and do nothing
593 build_tabs($template, $record, $dbh, $encoding,$input);
594 build_hidden_data;
595 $template->param(authid =>$authid,
596 duplicateauthid => $duplicateauthid,
597 duplicateauthvalue => $duplicateauthvalue,
600 } elsif ($op eq "delete") {
601 #------------------------------------------------------------------------------------------------------------------------------
602 &DelAuthority($authid);
603 if ($nonav){
604 print $input->redirect("auth_finder.pl");
605 }else{
606 print $input->redirect("authorities-home.pl?authid=0");
608 exit;
609 } else {
610 if ($op eq "duplicate")
612 $authid = "";
614 build_tabs ($template, $record, $dbh,$encoding,$input);
615 build_hidden_data;
616 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
617 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
618 authid => $authid , authtypecode=>$authtypecode, );
621 $template->param(authid => $authid,
622 authtypecode => $authtypecode,
623 linkid=>$linkid,
626 my $authtypes = getauthtypes;
627 my @authtypesloop;
628 foreach my $thisauthtype (keys %$authtypes) {
629 my $selected = 1 if $thisauthtype eq $authtypecode;
630 my %row =(value => $thisauthtype,
631 selected => $selected,
632 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
634 push @authtypesloop, \%row;
637 $template->param(authtypesloop => \@authtypesloop,
638 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
639 hide_marc => C4::Context->preference('hide_marc'),
641 output_html_with_http_headers $input, $cookie, $template->output;