Bug 8230: DBrev 3.15.00.005
[koha.git] / authorities / authorities.pl
blobbb527f61f00e73139f03c2f85e5e4be094aaba79
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} );
97 my $itemtype;
99 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
100 push @authorised_values, $itemtype;
101 $authorised_lib{$itemtype} = $description;
103 $value = $itemtype unless ($value);
105 #---- "true" authorised value
107 else {
108 $authorised_values_sth->execute(
109 $tagslib->{$tag}->{$subfield}->{authorised_value} );
111 push @authorised_values, ""
112 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
114 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
115 push @authorised_values, $value;
116 $authorised_lib{$value} = $lib;
119 return CGI::scrolling_list(
120 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
121 -values => \@authorised_values,
122 -default => $value,
123 -labels => \%authorised_lib,
124 -override => 1,
125 -size => 1,
126 -multiple => 0,
127 -tabindex => 1,
128 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
129 -class => "input_marceditor",
134 =item create_input
136 builds the <input ...> entry for a subfield.
138 =cut
140 sub create_input {
141 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
143 my $index_subfield = CreateKey(); # create a specifique key for each subfield
145 $value =~ s/"/&quot;/g;
147 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
148 my $max_length = 9999;
149 if ($tag eq '000') {
150 $max_length = 24;
151 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
152 $max_length = 40;
155 # if there is no value provided but a default value in parameters, get it
156 if ($value eq '') {
157 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
158 if (!defined $value) {
159 $value = q{};
162 # get today date & replace YYYY, MM, DD if provided in the default value
163 my ( $year, $month, $day ) = Today();
164 $month = sprintf( "%02d", $month );
165 $day = sprintf( "%02d", $day );
166 $value =~ s/YYYY/$year/g;
167 $value =~ s/MM/$month/g;
168 $value =~ s/DD/$day/g;
170 my $dbh = C4::Context->dbh;
172 # map '@' as "subfield" label for fixed fields
173 # to something that's allowed in a div id.
174 my $id_subfield = $subfield;
175 $id_subfield = "00" if $id_subfield eq "@";
177 my %subfield_data = (
178 tag => $tag,
179 subfield => $id_subfield,
180 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
181 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
182 tag_mandatory => $tagslib->{$tag}->{mandatory},
183 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
184 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
185 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
186 index => $index_tag,
187 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
188 value => $value,
189 random => CreateKey(),
192 if(exists $mandatory_z3950->{$tag.$subfield}){
193 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
196 $subfield_data{visibility} = "display:none;"
197 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
198 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
201 # it's an authorised field
202 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
203 $subfield_data{marc_value} =
204 build_authorized_values_list( $tag, $subfield, $value, $dbh,
205 $authorised_values_sth,$index_tag,$index_subfield );
207 # it's a thesaurus / authority field
209 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
210 $subfield_data{marc_value} =
211 "<input type=\"text\"
212 id=\"".$subfield_data{id}."\"
213 name=\"".$subfield_data{id}."\"
214 value=\"$value\"
215 class=\"input_marceditor\"
216 tabindex=\"1\" \/>
217 <a href=\"#\" class=\"buttonDot\"
218 onclick=\"openAuth(this.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode}."','auth'); return false;\" tabindex=\"1\" title=\"Tag Editor\">...</a>
220 # it's a plugin field
222 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
224 # opening plugin. Just check whether we are on a developer computer on a production one
225 # (the cgidir differs)
226 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
227 unless (-r $cgidir and -d $cgidir) {
228 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
230 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
231 do $plugin || die "Plugin Failed: ".$plugin;
232 my $extended_param;
233 eval{
234 $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
236 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
237 # my ( $function_name, $javascript,$extended_param );
239 $subfield_data{marc_value} =
240 "<input tabindex=\"1\"
241 type=\"text\"
242 id=\"".$subfield_data{id}."\"
243 size=\"67\"
244 maxlength=\"$max_length\"
245 name=\"".$subfield_data{id}."\"
246 value=\"$value\"
247 class=\"input_marceditor\"
248 onfocus=\"Focus$function_name($index_tag)\"
249 onblur=\"Blur$function_name($index_tag); \" \/>
250 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
251 $javascript";
252 # it's an hidden field
254 elsif ( $tag eq '' ) {
255 $subfield_data{marc_value} =
256 "<input tabindex=\"1\"
257 type=\"hidden\"
258 id=\"".$subfield_data{id}."\"
259 name=\"".$subfield_data{id}."\"
260 size=\"67\"
261 maxlength=\"$max_length\"
262 value=\"$value\" \/>
265 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
266 $subfield_data{marc_value} =
267 "<input type=\"text\"
268 id=\"".$subfield_data{id}."\"
269 name=\"".$subfield_data{id}."\"
270 class=\"input_marceditor\"
271 tabindex=\"1\"
272 size=\"67\"
273 maxlength=\"$max_length\"
274 value=\"$value\"
275 \/>";
277 # it's a standard field
279 else {
280 if (
281 length($value) > 100
283 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
284 and $tag < 400 && $subfield eq 'a' )
285 or ( $tag >= 600
286 and $tag < 700
287 && C4::Context->preference("marcflavour") eq "MARC21" )
290 $subfield_data{marc_value} =
291 "<textarea cols=\"70\"
292 rows=\"4\"
293 id=\"".$subfield_data{id}."\"
294 name=\"".$subfield_data{id}."\"
295 class=\"input_marceditor\"
296 tabindex=\"1\"
297 size=\"67\"
298 maxlength=\"$max_length\"
299 >$value</textarea>
302 else {
303 $subfield_data{marc_value} =
304 "<input type=\"text\"
305 id=\"".$subfield_data{id}."\"
306 name=\"".$subfield_data{id}."\"
307 value=\"$value\"
308 tabindex=\"1\"
309 size=\"67\"
310 maxlength=\"$max_length\"
311 class=\"input_marceditor\"
316 $subfield_data{'index_subfield'} = $index_subfield;
317 return \%subfield_data;
320 =item format_indicator
322 Translate indicator value for output form - specifically, map
323 indicator = ' ' to ''. This is for the convenience of a cataloger
324 using a mouse to select an indicator input.
326 =cut
328 sub format_indicator {
329 my $ind_value = shift;
330 return '' if not defined $ind_value;
331 return '' if $ind_value eq ' ';
332 return $ind_value;
335 =item CreateKey
337 Create a random value to set it into the input name
339 =cut
341 sub CreateKey {
342 return int(rand(1000000));
345 sub build_tabs {
346 my ( $template, $record, $dbh, $encoding,$input ) = @_;
348 # fill arrays
349 my @loop_data = ();
350 my $tag;
352 my $authorised_values_sth = $dbh->prepare(
353 "SELECT authorised_value,lib
354 FROM authorised_values
355 WHERE category=? ORDER BY lib"
358 # in this array, we will push all the 10 tabs
359 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
360 my @BIG_LOOP;
361 my %seen;
362 my @tab_data; # all tags to display
364 foreach my $used ( keys %$tagslib ){
365 push @tab_data,$used if not $seen{$used};
366 $seen{$used}++;
369 my $max_num_tab=9;
370 # loop through each tab 0 through 9
371 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
372 my @loop_data = (); #innerloop in the template.
373 my $i = 0;
374 foreach my $tag (sort @tab_data) {
375 $i++;
376 next if ! $tag;
377 my ($indicator1, $indicator2);
378 my $index_tag = CreateKey;
380 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
381 # if MARC::Record is empty => use tab as master loop.
382 if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
383 my @fields;
384 if ( $tag ne '000' ) {
385 @fields = $record->field($tag);
387 else {
388 push @fields, $record->leader(); # if tag == 000
390 # loop through each field
391 foreach my $field (@fields) {
393 my @subfields_data;
394 if ( $tag < 10 ) {
395 my ( $value, $subfield );
396 if ( $tag ne '000' ) {
397 $value = $field->data();
398 $subfield = "@";
400 else {
401 $value = $field;
402 $subfield = '@';
404 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
405 push(
406 @subfields_data,
407 &create_input(
408 $tag, $subfield, $value, $index_tag, $tabloop, $record,
409 $authorised_values_sth,$input
413 else {
414 my @subfields = $field->subfields();
415 foreach my $subfieldcount ( 0 .. $#subfields ) {
416 my $subfield = $subfields[$subfieldcount][0];
417 my $value = $subfields[$subfieldcount][1];
418 next if ( length $subfield != 1 );
419 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
420 push(
421 @subfields_data,
422 &create_input(
423 $tag, $subfield, $value, $index_tag, $tabloop,
424 $record, $authorised_values_sth,$input
430 # now, loop again to add parameter subfield that are not in the MARC::Record
431 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
433 next if ( length $subfield != 1 );
434 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
435 next if ( $tag < 10 );
436 next
437 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
438 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
439 ); #check for visibility flag
440 next if ( defined( $field->subfield($subfield) ) );
441 push(
442 @subfields_data,
443 &create_input(
444 $tag, $subfield, '', $index_tag, $tabloop, $record,
445 $authorised_values_sth,$input
449 if ( $#subfields_data >= 0 ) {
450 # build the tag entry.
451 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
452 # have twice the same "name" value, and cgi->param() will return only one, making
453 # all subfields to be merged in a single field.
454 my %tag_data = (
455 tag => $tag,
456 index => $index_tag,
457 tag_lib => $tagslib->{$tag}->{lib},
458 repeatable => $tagslib->{$tag}->{repeatable},
459 mandatory => $tagslib->{$tag}->{mandatory},
460 subfield_loop => \@subfields_data,
461 fixedfield => ($tag < 10)?(1):(0),
462 random => CreateKey,
464 if ($tag >= 10){ # no indicator for theses tag
465 $tag_data{indicator1} = format_indicator($field->indicator(1)),
466 $tag_data{indicator2} = format_indicator($field->indicator(2)),
468 push( @loop_data, \%tag_data );
470 } # foreach $field end
472 # if breeding is empty
474 else {
475 my @subfields_data;
476 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
477 next if ( length $subfield != 1 );
478 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
479 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
480 ; #check for visibility flag
481 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
482 push(
483 @subfields_data,
484 &create_input(
485 $tag, $subfield, '', $index_tag, $tabloop, $record,
486 $authorised_values_sth,$input
490 if ( $#subfields_data >= 0 ) {
491 my %tag_data = (
492 tag => $tag,
493 index => $index_tag,
494 tag_lib => $tagslib->{$tag}->{lib},
495 repeatable => $tagslib->{$tag}->{repeatable},
496 mandatory => $tagslib->{$tag}->{mandatory},
497 indicator1 => $indicator1,
498 indicator2 => $indicator2,
499 subfield_loop => \@subfields_data,
500 tagfirstsubfield => $subfields_data[0],
501 fixedfield => ($tag < 10)?(1):(0)
504 push @loop_data, \%tag_data ;
508 if ( $#loop_data >= 0 ) {
509 push @BIG_LOOP, {
510 number => $tabloop,
511 innerloop => \@loop_data,
515 $template->param( BIG_LOOP => \@BIG_LOOP );
519 sub build_hidden_data {
520 # build hidden data =>
521 # we store everything, even if we show only requested subfields.
523 my @loop_data =();
524 my $i=0;
525 foreach my $tag (keys %{$tagslib}) {
526 my $previous_tag = '';
528 # loop through each subfield
529 foreach my $subfield (keys %{$tagslib->{$tag}}) {
530 next if ($subfield eq 'lib');
531 next if ($subfield eq 'tab');
532 next if ($subfield eq 'mandatory');
533 next if ($subfield eq 'repeatable');
534 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
535 my %subfield_data;
536 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
537 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
538 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
539 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
540 push(@loop_data, \%subfield_data);
541 $i++
546 =back
548 =cut
551 # ========================
552 # MAIN
553 #=========================
554 my $input = new CGI;
555 my $z3950 = $input->param('z3950');
556 my $error = $input->param('error');
557 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
558 my $op = $input->param('op');
559 my $nonav = $input->param('nonav');
560 my $myindex = $input->param('index');
561 my $linkid=$input->param('linkid');
562 my $authtypecode = $input->param('authtypecode');
563 my $breedingid = $input->param('breedingid');
565 my $dbh = C4::Context->dbh;
566 if(!$authtypecode) {
567 $authtypecode = $authid? &GetAuthTypeCode($authid): '';
570 my ($template, $loggedinuser, $cookie)
571 = get_template_and_user({template_name => "authorities/authorities.tmpl",
572 query => $input,
573 type => "intranet",
574 authnotrequired => 0,
575 flagsrequired => {editauthorities => 1},
576 debug => 1,
578 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,breedingid=>$breedingid,);
580 $tagslib = GetTagsLabels(1,$authtypecode);
581 my $record=-1;
582 my $encoding="";
583 if (($authid) && !($breedingid)){
584 $record = GetAuthority($authid);
586 if ($breedingid) {
587 ( $record, $encoding ) = MARCfindbreeding_auth( $breedingid );
590 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
591 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
592 $is_a_modif=0;
593 if ($authid) {
594 $is_a_modif=1;
595 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
596 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
598 $op ||= q{};
599 #------------------------------------------------------------------------------------------------------------------------------
600 if ($op eq "add") {
601 #------------------------------------------------------------------------------------------------------------------------------
602 # rebuild
603 my @tags = $input->param('tag');
604 my @subfields = $input->param('subfield');
605 my @values = $input->param('field_value');
606 # build indicator hash.
607 my @ind_tag = $input->param('ind_tag');
608 my @indicator = $input->param('indicator');
609 my $record = TransformHtmlToMarc($input);
611 my ($duplicateauthid,$duplicateauthvalue);
612 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
613 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
614 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
615 if (!$duplicateauthid or $confirm_not_duplicate) {
616 if ($is_a_modif ) {
617 ModAuthority($authid,$record,$authtypecode);
618 } else {
619 ($authid) = AddAuthority($record,$authid,$authtypecode);
621 if ($myindex) {
622 print $input->redirect("blinddetail-biblio-search.pl?authid=$authid&index=$myindex");
623 } else {
624 print $input->redirect("detail.pl?authid=$authid");
626 exit;
627 } else {
628 # it may be a duplicate, warn the user and do nothing
629 build_tabs($template, $record, $dbh, $encoding,$input);
630 build_hidden_data;
631 $template->param(authid =>$authid,
632 duplicateauthid => $duplicateauthid,
633 duplicateauthvalue => $duplicateauthvalue->{'authorized'}->[0]->{'heading'},
636 } elsif ($op eq "delete") {
637 #------------------------------------------------------------------------------------------------------------------------------
638 &DelAuthority($authid);
639 if ($nonav){
640 print $input->redirect("auth_finder.pl");
641 }else{
642 print $input->redirect("authorities-home.pl?authid=0");
644 exit;
645 } else {
646 if ($op eq "duplicate")
648 $authid = "";
650 build_tabs ($template, $record, $dbh,$encoding,$input);
651 build_hidden_data;
652 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
653 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
654 authid => $authid , authtypecode=>$authtypecode, );
657 $template->param(authid => $authid,
658 authtypecode => $authtypecode,
659 linkid=>$linkid,
662 my $authtypes = getauthtypes;
663 my @authtypesloop;
664 foreach my $thisauthtype (keys %$authtypes) {
665 my %row =(value => $thisauthtype,
666 selected => $thisauthtype eq $authtypecode,
667 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
669 push @authtypesloop, \%row;
672 $template->param(authtypesloop => \@authtypesloop,
673 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
674 hide_marc => C4::Context->preference('hide_marc'),
676 output_html_with_http_headers $input, $cookie, $template->output;