bug 3902: item class source can now be set during serials receiving
[koha.git] / authorities / authorities.pl
blob297369730e086fbd21b242abf86b6aada6358d29
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 =item build_authorized_values_list
42 =cut
44 sub build_authorized_values_list ($$$$$$$) {
45 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
47 my @authorised_values;
48 my %authorised_lib;
50 # builds list, depending on authorised value...
52 #---- branch
53 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
54 my $sth =
55 $dbh->prepare(
56 "select branchcode,branchname from branches order by branchname");
57 $sth->execute;
58 push @authorised_values, ""
59 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
61 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
62 push @authorised_values, $branchcode;
63 $authorised_lib{$branchcode} = $branchname;
66 #----- itemtypes
68 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
69 my $sth =
70 $dbh->prepare(
71 "select itemtype,description from itemtypes order by description");
72 $sth->execute;
73 push @authorised_values, ""
74 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
76 my $itemtype;
78 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
79 push @authorised_values, $itemtype;
80 $authorised_lib{$itemtype} = $description;
82 $value = $itemtype unless ($value);
84 #---- "true" authorised value
86 else {
87 $authorised_values_sth->execute(
88 $tagslib->{$tag}->{$subfield}->{authorised_value} );
90 push @authorised_values, ""
91 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
93 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
94 push @authorised_values, $value;
95 $authorised_lib{$value} = $lib;
98 return CGI::scrolling_list(
99 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
100 -values => \@authorised_values,
101 -default => $value,
102 -labels => \%authorised_lib,
103 -override => 1,
104 -size => 1,
105 -multiple => 0,
106 -tabindex => 1,
107 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
108 -class => "input_marceditor",
113 =item create_input
114 builds the <input ...> entry for a subfield.
115 =cut
117 sub create_input {
118 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
120 my $index_subfield = CreateKey(); # create a specifique key for each subfield
122 $value =~ s/"/&quot;/g;
124 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
125 my $max_length = 9999;
126 if ($tag eq '000') {
127 $max_length = 24;
128 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
129 $max_length = 40;
132 # if there is no value provided but a default value in parameters, get it
133 unless ($value) {
134 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
135 if (!defined $value) {
136 $value = q{};
139 # get today date & replace YYYY, MM, DD if provided in the default value
140 my ( $year, $month, $day ) = Today();
141 $month = sprintf( "%02d", $month );
142 $day = sprintf( "%02d", $day );
143 $value =~ s/YYYY/$year/g;
144 $value =~ s/MM/$month/g;
145 $value =~ s/DD/$day/g;
147 my $dbh = C4::Context->dbh;
148 my %subfield_data = (
149 tag => $tag,
150 subfield => $subfield,
151 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
152 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
153 tag_mandatory => $tagslib->{$tag}->{mandatory},
154 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
155 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
156 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
157 index => $index_tag,
158 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
159 value => $value,
161 if($subfield eq '@'){
162 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
163 } else {
164 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
167 if(exists $mandatory_z3950->{$tag.$subfield}){
168 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
171 $subfield_data{visibility} = "display:none;"
172 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
173 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
176 # it's an authorised field
177 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
178 $subfield_data{marc_value} =
179 build_authorized_values_list( $tag, $subfield, $value, $dbh,
180 $authorised_values_sth,$index_tag,$index_subfield );
182 # it's a thesaurus / authority field
184 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
185 $subfield_data{marc_value} =
186 "<input type=\"text\"
187 id=\"".$subfield_data{id}."\"
188 name=\"".$subfield_data{id}."\"
189 value=\"$value\"
190 class=\"input_marceditor\"
191 tabindex=\"1\"
192 readonly=\"readonly\" \/>
193 <a href=\"#\" class=\"buttonDot\"
194 onclick=\"openAuth(this.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode}."'); return false;\" tabindex=\"1\" title=\"Tag Editor\">...</a>
196 # it's a plugin field
198 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
200 # opening plugin. Just check wether we are on a developper computer on a production one
201 # (the cgidir differs)
202 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
203 unless (-r $cgidir and -d $cgidir) {
204 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
206 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
207 do $plugin || die "Plugin Failed: ".$plugin;
208 my $extended_param;
209 eval{
210 $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
212 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
213 # my ( $function_name, $javascript,$extended_param );
215 $subfield_data{marc_value} =
216 "<input tabindex=\"1\"
217 type=\"text\"
218 id=\"".$subfield_data{id}."\"
219 size=\"67\"
220 maxlength=\"$max_length\"
221 name=\"".$subfield_data{id}."\"
222 value=\"$value\"
223 class=\"input_marceditor\"
224 onfocus=\"Focus$function_name($index_tag)\"
225 onblur=\"Blur$function_name($index_tag); \" \/>
226 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
227 $javascript";
228 # it's an hidden field
230 elsif ( $tag eq '' ) {
231 $subfield_data{marc_value} =
232 "<input tabindex=\"1\"
233 type=\"hidden\"
234 id=\"".$subfield_data{id}."\"
235 name=\"".$subfield_data{id}."\"
236 size=\"67\"
237 maxlength=\"$max_length\"
238 value=\"$value\" \/>
241 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
242 $subfield_data{marc_value} =
243 "<input type=\"text\"
244 id=\"".$subfield_data{id}."\"
245 name=\"".$subfield_data{id}."\"
246 class=\"input_marceditor\"
247 tabindex=\"1\"
248 size=\"67\"
249 maxlength=\"$max_length\"
250 value=\"$value\"
251 \/>";
253 # it's a standard field
255 else {
256 if (
257 length($value) > 100
259 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
260 and $tag < 400 && $subfield eq 'a' )
261 or ( $tag >= 600
262 and $tag < 700
263 && C4::Context->preference("marcflavour") eq "MARC21" )
266 $subfield_data{marc_value} =
267 "<textarea cols=\"70\"
268 rows=\"4\"
269 id=\"".$subfield_data{id}."\"
270 name=\"".$subfield_data{id}."\"
271 class=\"input_marceditor\"
272 tabindex=\"1\"
273 size=\"67\"
274 maxlength=\"$max_length\"
275 >$value</textarea>
278 else {
279 $subfield_data{marc_value} =
280 "<input type=\"text\"
281 id=\"".$subfield_data{id}."\"
282 name=\"".$subfield_data{id}."\"
283 value=\"$value\"
284 tabindex=\"1\"
285 size=\"67\"
286 maxlength=\"$max_length\"
287 class=\"input_marceditor\"
292 $subfield_data{'index_subfield'} = $index_subfield;
293 return \%subfield_data;
296 =item format_indicator
298 Translate indicator value for output form - specifically, map
299 indicator = ' ' to ''. This is for the convenience of a cataloger
300 using a mouse to select an indicator input.
302 =cut
304 sub format_indicator {
305 my $ind_value = shift;
306 return '' if not defined $ind_value;
307 return '' if $ind_value eq ' ';
308 return $ind_value;
311 =item CreateKey
313 Create a random value to set it into the input name
315 =cut
317 sub CreateKey(){
318 return int(rand(1000000));
321 sub build_tabs ($$$$$) {
322 my ( $template, $record, $dbh, $encoding,$input ) = @_;
324 # fill arrays
325 my @loop_data = ();
326 my $tag;
328 my $authorised_values_sth = $dbh->prepare(
329 "SELECT authorised_value,lib
330 FROM authorised_values
331 WHERE category=? ORDER BY lib"
334 # in this array, we will push all the 10 tabs
335 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
336 my @BIG_LOOP;
337 my %seen;
338 my @tab_data; # all tags to display
340 foreach my $used ( keys %$tagslib ){
341 push @tab_data,$used if not $seen{$used};
342 $seen{$used}++;
345 my $max_num_tab=9;
346 # loop through each tab 0 through 9
347 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
348 my @loop_data = (); #innerloop in the template.
349 my $i = 0;
350 foreach my $tag (sort @tab_data) {
351 $i++;
352 next if ! $tag;
353 my ($indicator1, $indicator2);
354 my $index_tag = CreateKey;
356 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
357 # if MARC::Record is empty => use tab as master loop.
358 if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
359 my @fields;
360 if ( $tag ne '000' ) {
361 @fields = $record->field($tag);
363 else {
364 push @fields, $record->leader(); # if tag == 000
366 # loop through each field
367 foreach my $field (@fields) {
369 my @subfields_data;
370 if ( $tag < 10 ) {
371 my ( $value, $subfield );
372 if ( $tag ne '000' ) {
373 $value = $field->data();
374 $subfield = "@";
376 else {
377 $value = $field;
378 $subfield = '@';
380 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
381 push(
382 @subfields_data,
383 &create_input(
384 $tag, $subfield, $value, $index_tag, $tabloop, $record,
385 $authorised_values_sth,$input
389 else {
390 my @subfields = $field->subfields();
391 foreach my $subfieldcount ( 0 .. $#subfields ) {
392 my $subfield = $subfields[$subfieldcount][0];
393 my $value = $subfields[$subfieldcount][1];
394 next if ( length $subfield != 1 );
395 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
396 push(
397 @subfields_data,
398 &create_input(
399 $tag, $subfield, $value, $index_tag, $tabloop,
400 $record, $authorised_values_sth,$input
406 # now, loop again to add parameter subfield that are not in the MARC::Record
407 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
409 next if ( length $subfield != 1 );
410 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
411 next if ( $tag < 10 );
412 next
413 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
414 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
415 ); #check for visibility flag
416 next if ( defined( $field->subfield($subfield) ) );
417 push(
418 @subfields_data,
419 &create_input(
420 $tag, $subfield, '', $index_tag, $tabloop, $record,
421 $authorised_values_sth,$input
425 if ( $#subfields_data >= 0 ) {
426 # build the tag entry.
427 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
428 # have twice the same "name" value, and cgi->param() will return only one, making
429 # all subfields to be merged in a single field.
430 my %tag_data = (
431 tag => $tag,
432 index => $index_tag,
433 tag_lib => $tagslib->{$tag}->{lib},
434 repeatable => $tagslib->{$tag}->{repeatable},
435 mandatory => $tagslib->{$tag}->{mandatory},
436 subfield_loop => \@subfields_data,
437 fixedfield => ($tag < 10)?(1):(0),
438 random => CreateKey,
440 if ($tag >= 10){ # no indicator for theses tag
441 $tag_data{indicator1} = format_indicator($field->indicator(1)),
442 $tag_data{indicator2} = format_indicator($field->indicator(2)),
444 push( @loop_data, \%tag_data );
446 } # foreach $field end
448 # if breeding is empty
450 else {
451 my @subfields_data;
452 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
453 next if ( length $subfield != 1 );
454 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
455 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
456 ; #check for visibility flag
457 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
458 push(
459 @subfields_data,
460 &create_input(
461 $tag, $subfield, '', $index_tag, $tabloop, $record,
462 $authorised_values_sth,$input
466 if ( $#subfields_data >= 0 ) {
467 my %tag_data = (
468 tag => $tag,
469 index => $index_tag,
470 tag_lib => $tagslib->{$tag}->{lib},
471 repeatable => $tagslib->{$tag}->{repeatable},
472 mandatory => $tagslib->{$tag}->{mandatory},
473 indicator1 => $indicator1,
474 indicator2 => $indicator2,
475 subfield_loop => \@subfields_data,
476 tagfirstsubfield => $subfields_data[0],
477 fixedfield => ($tag < 10)?(1):(0)
480 push @loop_data, \%tag_data ;
484 if ( $#loop_data >= 0 ) {
485 push @BIG_LOOP, {
486 number => $tabloop,
487 innerloop => \@loop_data,
491 $template->param( BIG_LOOP => \@BIG_LOOP );
495 sub build_hidden_data () {
496 # build hidden data =>
497 # we store everything, even if we show only requested subfields.
499 my @loop_data =();
500 my $i=0;
501 foreach my $tag (keys %{$tagslib}) {
502 my $previous_tag = '';
504 # loop through each subfield
505 foreach my $subfield (keys %{$tagslib->{$tag}}) {
506 next if ($subfield eq 'lib');
507 next if ($subfield eq 'tab');
508 next if ($subfield eq 'mandatory');
509 next if ($subfield eq 'repeatable');
510 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
511 my %subfield_data;
512 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
513 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
514 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
515 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
516 push(@loop_data, \%subfield_data);
517 $i++
522 # ========================
523 # MAIN
524 #=========================
525 my $input = new CGI;
526 my $z3950 = $input->param('z3950');
527 my $error = $input->param('error');
528 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
529 my $op = $input->param('op');
530 my $nonav = $input->param('nonav');
531 my $myindex = $input->param('index');
532 my $linkid=$input->param('linkid');
533 my $authtypecode = $input->param('authtypecode');
535 my $dbh = C4::Context->dbh;
536 $authtypecode = &GetAuthTypeCode($authid) if !$authtypecode;
538 my ($template, $loggedinuser, $cookie)
539 = get_template_and_user({template_name => "authorities/authorities.tmpl",
540 query => $input,
541 type => "intranet",
542 authnotrequired => 0,
543 flagsrequired => {editauthorities => 1},
544 debug => 1,
546 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
547 $tagslib = GetTagsLabels(1,$authtypecode);
548 my $record=-1;
549 my $encoding="";
550 $record = GetAuthority($authid) if ($authid);
551 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
552 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
553 $is_a_modif=0;
554 if ($authid) {
555 $is_a_modif=1;
556 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
557 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
559 $op ||= q{};
560 #------------------------------------------------------------------------------------------------------------------------------
561 if ($op eq "add") {
562 #------------------------------------------------------------------------------------------------------------------------------
563 # rebuild
564 my @tags = $input->param('tag');
565 my @subfields = $input->param('subfield');
566 my @values = $input->param('field_value');
567 # build indicator hash.
568 my @ind_tag = $input->param('ind_tag');
569 my @indicator = $input->param('indicator');
570 my @params = $input->param();
571 my $record = TransformHtmlToMarc(\@params,$input);
572 if (C4::Context->preference("marcflavour") eq "UNIMARC"){
573 unless ($record->field('100')){
574 use POSIX qw(strftime);
575 my $string = strftime( "%Y%m%d", localtime(time) );
576 # set 50 to position 26 is biblios, 13 if authorities
577 my $pos=13;
578 $string = sprintf( "%-*s", 35, $string );
579 substr( $string, $pos , 2, "50" );
580 $record->append_fields(MARC::Field->new('100','','',"a"=>$string));
584 my ($duplicateauthid,$duplicateauthvalue);
585 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
586 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
587 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
588 if (!$duplicateauthid or $confirm_not_duplicate) {
589 if ($is_a_modif ) {
590 ModAuthority($authid,$record,$authtypecode);
591 } else {
592 ($authid) = AddAuthority($record,$authid,$authtypecode);
594 print $input->redirect("detail.pl?authid=$authid");
595 exit;
596 } else {
597 # it may be a duplicate, warn the user and do nothing
598 build_tabs($template, $record, $dbh, $encoding,$input);
599 build_hidden_data;
600 $template->param(authid =>$authid,
601 duplicateauthid => $duplicateauthid,
602 duplicateauthvalue => $duplicateauthvalue,
605 } elsif ($op eq "delete") {
606 #------------------------------------------------------------------------------------------------------------------------------
607 &DelAuthority($authid);
608 if ($nonav){
609 print $input->redirect("auth_finder.pl");
610 }else{
611 print $input->redirect("authorities-home.pl?authid=0");
613 exit;
614 } else {
615 if ($op eq "duplicate")
617 $authid = "";
619 build_tabs ($template, $record, $dbh,$encoding,$input);
620 build_hidden_data;
621 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
622 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
623 authid => $authid , authtypecode=>$authtypecode, );
626 $template->param(authid => $authid,
627 authtypecode => $authtypecode,
628 linkid=>$linkid,
631 my $authtypes = getauthtypes;
632 my @authtypesloop;
633 foreach my $thisauthtype (keys %$authtypes) {
634 my %row =(value => $thisauthtype,
635 selected => $thisauthtype eq $authtypecode,
636 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
638 push @authtypesloop, \%row;
641 $template->param(authtypesloop => \@authtypesloop,
642 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
643 hide_marc => C4::Context->preference('hide_marc'),
645 output_html_with_http_headers $input, $cookie, $template->output;