Bug 13428: Fix regression on deleting facets
[koha.git] / authorities / authorities.pl
blob89049fedf724a2a76de0e56888c35544b1be601d
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}
96 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
98 my $itemtype;
100 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
101 push @authorised_values, $itemtype;
102 $authorised_lib{$itemtype} = $description;
104 $value = $itemtype unless ($value);
106 #---- "true" authorised value
108 else {
109 $authorised_values_sth->execute(
110 $tagslib->{$tag}->{$subfield}->{authorised_value} );
112 push @authorised_values, ""
113 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
114 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
116 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
117 push @authorised_values, $value;
118 $authorised_lib{$value} = $lib;
121 return {
122 type => 'select',
123 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
124 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
125 values => \@authorised_values,
126 labels => \%authorised_lib,
127 default => $value,
132 =item create_input
134 builds the <input ...> entry for a subfield.
136 =cut
138 sub create_input {
139 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
141 my $index_subfield = CreateKey(); # create a specifique key for each subfield
143 $value =~ s/"/&quot;/g;
145 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
146 my $max_length = 9999;
147 if ($tag eq '000') {
148 $max_length = 24;
149 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
150 $max_length = 40;
153 # if there is no value provided but a default value in parameters, get it
154 if ($value eq '') {
155 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
156 if (!defined $value) {
157 $value = q{};
160 # get today date & replace YYYY, MM, DD if provided in the default value
161 my ( $year, $month, $day ) = Today();
162 $month = sprintf( "%02d", $month );
163 $day = sprintf( "%02d", $day );
164 $value =~ s/YYYY/$year/g;
165 $value =~ s/MM/$month/g;
166 $value =~ s/DD/$day/g;
168 my $dbh = C4::Context->dbh;
170 # map '@' as "subfield" label for fixed fields
171 # to something that's allowed in a div id.
172 my $id_subfield = $subfield;
173 $id_subfield = "00" if $id_subfield eq "@";
175 my %subfield_data = (
176 tag => $tag,
177 subfield => $id_subfield,
178 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
179 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
180 tag_mandatory => $tagslib->{$tag}->{mandatory},
181 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
182 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
183 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
184 index => $index_tag,
185 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
186 value => $value,
187 random => CreateKey(),
190 if(exists $mandatory_z3950->{$tag.$subfield}){
191 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
194 $subfield_data{visibility} = "display:none;"
195 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
196 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
199 # it's an authorised field
200 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
201 $subfield_data{marc_value} =
202 build_authorized_values_list( $tag, $subfield, $value, $dbh,
203 $authorised_values_sth,$index_tag,$index_subfield );
205 # it's a thesaurus / authority field
207 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
208 $subfield_data{marc_value} = {
209 type => 'text1',
210 id => $subfield_data{id},
211 name => $subfield_data{id},
212 value => $value,
213 authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode},
215 # it's a plugin field
217 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
219 # opening plugin. Just check whether we are on a developer computer on a production one
220 # (the cgidir differs)
221 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
222 unless (-r $cgidir and -d $cgidir) {
223 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
225 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
226 do $plugin || die "Plugin Failed: ".$plugin;
227 my $extended_param;
228 eval{
229 $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
231 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
232 # my ( $function_name, $javascript,$extended_param );
234 $subfield_data{marc_value} = {
235 type => 'text2',
236 id => $subfield_data{id},
237 name => $subfield_data{id},
238 value => $value,
239 maxlength => $max_length,
240 function => $function_name,
241 index_tag => $index_tag,
242 javascript => $javascript,
244 # it's an hidden field
246 elsif ( $tag eq '' ) {
247 $subfield_data{marc_value} = {
248 type => 'hidden',
249 id => $subfield_data{id},
250 name => $subfield_data{id},
251 value => $value,
252 maxlength => $max_length,
255 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
256 $subfield_data{marc_value} = {
257 type => 'text',
258 id => $subfield_data{id},
259 name => $subfield_data{id},
260 value => $value,
261 maxlength => $max_length,
264 # it's a standard field
266 else {
267 if (
268 length($value) > 100
270 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
271 and $tag < 400 && $subfield eq 'a' )
272 or ( $tag >= 600
273 and $tag < 700
274 && C4::Context->preference("marcflavour") eq "MARC21" )
277 $subfield_data{marc_value} = {
278 type => 'textarea',
279 id => $subfield_data{id},
280 name => $subfield_data{id},
281 value => $value,
282 maxlength => $max_length,
286 else {
287 $subfield_data{marc_value} = {
288 type => 'text',
289 id => $subfield_data{id},
290 name => $subfield_data{id},
291 value => $value,
292 maxlength => $max_length,
297 $subfield_data{'index_subfield'} = $index_subfield;
298 return \%subfield_data;
301 =item format_indicator
303 Translate indicator value for output form - specifically, map
304 indicator = ' ' to ''. This is for the convenience of a cataloger
305 using a mouse to select an indicator input.
307 =cut
309 sub format_indicator {
310 my $ind_value = shift;
311 return '' if not defined $ind_value;
312 return '' if $ind_value eq ' ';
313 return $ind_value;
316 =item CreateKey
318 Create a random value to set it into the input name
320 =cut
322 sub CreateKey {
323 return int(rand(1000000));
326 sub build_tabs {
327 my ( $template, $record, $dbh, $encoding,$input ) = @_;
329 # fill arrays
330 my @loop_data = ();
331 my $tag;
333 my $authorised_values_sth = $dbh->prepare(
334 "SELECT authorised_value,lib
335 FROM authorised_values
336 WHERE category=? ORDER BY lib"
339 # in this array, we will push all the 10 tabs
340 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
341 my @BIG_LOOP;
342 my %seen;
343 my @tab_data; # all tags to display
345 foreach my $used ( keys %$tagslib ){
346 push @tab_data,$used if not $seen{$used};
347 $seen{$used}++;
350 my $max_num_tab=9;
351 # loop through each tab 0 through 9
352 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
353 my @loop_data = (); #innerloop in the template.
354 my $i = 0;
355 foreach my $tag (sort @tab_data) {
356 $i++;
357 next if ! $tag;
358 my ($indicator1, $indicator2);
359 my $index_tag = CreateKey;
361 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
362 # if MARC::Record is empty => use tab as master loop.
363 if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
364 my @fields;
365 if ( $tag ne '000' ) {
366 @fields = $record->field($tag);
368 else {
369 push @fields, $record->leader(); # if tag == 000
371 # loop through each field
372 foreach my $field (@fields) {
374 my @subfields_data;
375 if ( $tag < 10 ) {
376 my ( $value, $subfield );
377 if ( $tag ne '000' ) {
378 $value = $field->data();
379 $subfield = "@";
381 else {
382 $value = $field;
383 $subfield = '@';
385 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
386 push(
387 @subfields_data,
388 &create_input(
389 $tag, $subfield, $value, $index_tag, $tabloop, $record,
390 $authorised_values_sth,$input
394 else {
395 my @subfields = $field->subfields();
396 foreach my $subfieldcount ( 0 .. $#subfields ) {
397 my $subfield = $subfields[$subfieldcount][0];
398 my $value = $subfields[$subfieldcount][1];
399 next if ( length $subfield != 1 );
400 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
401 push(
402 @subfields_data,
403 &create_input(
404 $tag, $subfield, $value, $index_tag, $tabloop,
405 $record, $authorised_values_sth,$input
411 # now, loop again to add parameter subfield that are not in the MARC::Record
412 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
414 next if ( length $subfield != 1 );
415 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
416 next if ( $tag < 10 );
417 next
418 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
419 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
420 ); #check for visibility flag
421 next if ( defined( $field->subfield($subfield) ) );
422 push(
423 @subfields_data,
424 &create_input(
425 $tag, $subfield, '', $index_tag, $tabloop, $record,
426 $authorised_values_sth,$input
430 if ( $#subfields_data >= 0 ) {
431 # build the tag entry.
432 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
433 # have twice the same "name" value, and cgi->param() will return only one, making
434 # all subfields to be merged in a single field.
435 my %tag_data = (
436 tag => $tag,
437 index => $index_tag,
438 tag_lib => $tagslib->{$tag}->{lib},
439 repeatable => $tagslib->{$tag}->{repeatable},
440 mandatory => $tagslib->{$tag}->{mandatory},
441 subfield_loop => \@subfields_data,
442 fixedfield => ($tag < 10)?(1):(0),
443 random => CreateKey,
445 if ($tag >= 10){ # no indicator for theses tag
446 $tag_data{indicator1} = format_indicator($field->indicator(1)),
447 $tag_data{indicator2} = format_indicator($field->indicator(2)),
449 push( @loop_data, \%tag_data );
451 } # foreach $field end
453 # if breeding is empty
455 else {
456 my @subfields_data;
457 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
458 next if ( length $subfield != 1 );
459 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
460 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
461 ; #check for visibility flag
462 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
463 push(
464 @subfields_data,
465 &create_input(
466 $tag, $subfield, '', $index_tag, $tabloop, $record,
467 $authorised_values_sth,$input
471 if ( $#subfields_data >= 0 ) {
472 my %tag_data = (
473 tag => $tag,
474 index => $index_tag,
475 tag_lib => $tagslib->{$tag}->{lib},
476 repeatable => $tagslib->{$tag}->{repeatable},
477 mandatory => $tagslib->{$tag}->{mandatory},
478 indicator1 => $indicator1,
479 indicator2 => $indicator2,
480 subfield_loop => \@subfields_data,
481 tagfirstsubfield => $subfields_data[0],
482 fixedfield => ($tag < 10)?(1):(0)
485 push @loop_data, \%tag_data ;
489 if ( $#loop_data >= 0 ) {
490 push @BIG_LOOP, {
491 number => $tabloop,
492 innerloop => \@loop_data,
496 $template->param( BIG_LOOP => \@BIG_LOOP );
500 sub build_hidden_data {
501 # build hidden data =>
502 # we store everything, even if we show only requested subfields.
504 my @loop_data =();
505 my $i=0;
506 foreach my $tag (keys %{$tagslib}) {
507 my $previous_tag = '';
509 # loop through each subfield
510 foreach my $subfield (keys %{$tagslib->{$tag}}) {
511 next if ($subfield eq 'lib');
512 next if ($subfield eq 'tab');
513 next if ($subfield eq 'mandatory');
514 next if ($subfield eq 'repeatable');
515 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
516 my %subfield_data;
517 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
518 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
519 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
520 $subfield_data{marc_value} = {
521 type => 'hidden_simple',
522 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');
547 my $breedingid = $input->param('breedingid');
549 my $dbh = C4::Context->dbh;
550 if(!$authtypecode) {
551 $authtypecode = $authid? &GetAuthTypeCode($authid): '';
554 my ($template, $loggedinuser, $cookie)
555 = get_template_and_user({template_name => "authorities/authorities.tt",
556 query => $input,
557 type => "intranet",
558 authnotrequired => 0,
559 flagsrequired => {editauthorities => 1},
560 debug => 1,
562 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,breedingid=>$breedingid,);
564 $tagslib = GetTagsLabels(1,$authtypecode);
565 my $record=-1;
566 my $encoding="";
567 if (($authid) && !($breedingid)){
568 $record = GetAuthority($authid);
570 if ($breedingid) {
571 ( $record, $encoding ) = MARCfindbreeding_auth( $breedingid );
574 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
575 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
576 $is_a_modif=0;
577 if ($authid) {
578 $is_a_modif=1;
579 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
580 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
582 $op ||= q{};
583 #------------------------------------------------------------------------------------------------------------------------------
584 if ($op eq "add") {
585 #------------------------------------------------------------------------------------------------------------------------------
586 # rebuild
587 my @tags = $input->param('tag');
588 my @subfields = $input->param('subfield');
589 my @values = $input->param('field_value');
590 # build indicator hash.
591 my @ind_tag = $input->param('ind_tag');
592 my @indicator = $input->param('indicator');
593 my $record = TransformHtmlToMarc($input);
595 my ($duplicateauthid,$duplicateauthvalue);
596 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
597 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
598 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
599 if (!$duplicateauthid or $confirm_not_duplicate) {
600 if ($is_a_modif ) {
601 ModAuthority($authid,$record,$authtypecode);
602 } else {
603 ($authid) = AddAuthority($record,$authid,$authtypecode);
605 if ($myindex) {
606 print $input->redirect("blinddetail-biblio-search.pl?authid=$authid&index=$myindex");
607 } else {
608 print $input->redirect("detail.pl?authid=$authid");
610 exit;
611 } else {
612 # it may be a duplicate, warn the user and do nothing
613 build_tabs($template, $record, $dbh, $encoding,$input);
614 build_hidden_data;
615 $template->param(authid =>$authid,
616 duplicateauthid => $duplicateauthid,
617 duplicateauthvalue => $duplicateauthvalue->{'authorized'}->[0]->{'heading'},
620 } elsif ($op eq "delete") {
621 #------------------------------------------------------------------------------------------------------------------------------
622 &DelAuthority($authid);
623 if ($nonav){
624 print $input->redirect("auth_finder.pl");
625 }else{
626 print $input->redirect("authorities-home.pl?authid=0");
628 exit;
629 } else {
630 if ($op eq "duplicate")
632 $authid = "";
634 build_tabs ($template, $record, $dbh,$encoding,$input);
635 build_hidden_data;
636 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
637 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
638 authid => $authid , authtypecode=>$authtypecode, );
641 $template->param(authid => $authid,
642 authtypecode => $authtypecode,
643 linkid=>$linkid,
646 my $authtypes = getauthtypes;
647 my @authtypesloop;
648 foreach my $thisauthtype (keys %$authtypes) {
649 my %row =(value => $thisauthtype,
650 selected => $thisauthtype eq $authtypecode,
651 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
653 push @authtypesloop, \%row;
656 $template->param(authtypesloop => \@authtypesloop,
657 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
658 hide_marc => C4::Context->preference('hide_marc'),
660 output_html_with_http_headers $input, $cookie, $template->output;