Bug 17318: Make C4::Matcher::_get_match_keys handle 'norms' param
[koha.git] / authorities / authorities.pl
blob1e49d9609eaac8cee01f5ce9af741f02973c3b6c
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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use strict;
22 use warnings;
23 use CGI qw ( -utf8 );
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;
30 use Date::Calc qw(Today);
31 use MARC::File::USMARC;
32 use MARC::File::XML;
33 use C4::Biblio;
34 use Koha::Authority::Types;
35 use vars qw( $tagslib);
36 use vars qw( $authorised_values_sth);
37 use vars qw( $is_a_modif );
39 my $itemtype; # created here because it can be used in build_authorized_values_list sub
40 our($authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
42 =head1 FUNCTIONS
44 =over
46 =item build_authorized_values_list
48 builds list, depending on authorised value...
50 =cut
52 sub MARCfindbreeding_auth {
53 my ( $id ) = @_;
54 my ($marc, $encoding) = GetImportRecordMarc($id);
55 if ($marc) {
56 my $record = MARC::Record->new_from_usmarc($marc);
57 if ( !defined(ref($record)) ) {
58 return -1;
59 } else {
60 return $record, $encoding;
62 } else {
63 return -1;
67 sub build_authorized_values_list {
68 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
70 my @authorised_values;
71 my %authorised_lib;
74 #---- branch
75 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
76 my $sth =
77 $dbh->prepare(
78 "select branchcode,branchname from branches order by branchname");
79 $sth->execute;
80 push @authorised_values, ""
81 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
83 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
84 push @authorised_values, $branchcode;
85 $authorised_lib{$branchcode} = $branchname;
88 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
89 push @authorised_values, ""
90 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
91 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
93 my $itemtype;
94 my $itemtypes = GetItemTypes( style => 'array' );
95 for my $itemtype ( @$itemtypes ) {
96 push @authorised_values, $itemtype->{itemtype};
97 $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description};
99 $value = $itemtype unless ($value);
101 #---- "true" authorised value
103 else {
104 $authorised_values_sth->execute(
105 $tagslib->{$tag}->{$subfield}->{authorised_value} );
107 push @authorised_values, ""
108 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
109 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
111 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
112 push @authorised_values, $value;
113 $authorised_lib{$value} = $lib;
116 return {
117 type => 'select',
118 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
119 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
120 values => \@authorised_values,
121 labels => \%authorised_lib,
122 default => $value,
127 =item create_input
129 builds the <input ...> entry for a subfield.
131 =cut
133 sub create_input {
134 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
136 my $index_subfield = CreateKey(); # create a specifique key for each subfield
138 $value =~ s/"/&quot;/g;
140 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
141 my $max_length = 9999;
142 if ($tag eq '000') {
143 $max_length = 24;
144 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
145 $max_length = 40;
148 # if there is no value provided but a default value in parameters, get it
149 if ($value eq '') {
150 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
151 if (!defined $value) {
152 $value = q{};
155 # get today date & replace YYYY, MM, DD if provided in the default value
156 my ( $year, $month, $day ) = Today();
157 $month = sprintf( "%02d", $month );
158 $day = sprintf( "%02d", $day );
159 $value =~ s/YYYY/$year/g;
160 $value =~ s/MM/$month/g;
161 $value =~ s/DD/$day/g;
163 my $dbh = C4::Context->dbh;
165 # map '@' as "subfield" label for fixed fields
166 # to something that's allowed in a div id.
167 my $id_subfield = $subfield;
168 $id_subfield = "00" if $id_subfield eq "@";
170 my %subfield_data = (
171 tag => $tag,
172 subfield => $id_subfield,
173 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
174 tag_mandatory => $tagslib->{$tag}->{mandatory},
175 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
176 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
177 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
178 index => $index_tag,
179 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
180 value => $value,
181 random => CreateKey(),
184 if(exists $mandatory_z3950->{$tag.$subfield}){
185 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
188 $subfield_data{visibility} = "display:none;"
189 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
190 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
193 # it's an authorised field
194 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
195 $subfield_data{marc_value} =
196 build_authorized_values_list( $tag, $subfield, $value, $dbh,
197 $authorised_values_sth,$index_tag,$index_subfield );
199 # it's a thesaurus / authority field
201 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
202 $subfield_data{marc_value} = {
203 type => 'text1',
204 id => $subfield_data{id},
205 name => $subfield_data{id},
206 value => $value,
207 authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode},
210 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) { # plugin
211 require Koha::FrameworkPlugin;
212 my $plugin = Koha::FrameworkPlugin->new({
213 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
215 my $pars= { dbh => $dbh, record => $rec, tagslib =>$tagslib,
216 id => $subfield_data{id}, tabloop => $tabloop };
217 $plugin->build( $pars );
218 if( !$plugin->errstr ) {
219 $subfield_data{marc_value} = {
220 type => 'text2',
221 id => $subfield_data{id},
222 name => $subfield_data{id},
223 value => $value,
224 maxlength => $max_length,
225 javascript => $plugin->javascript,
226 noclick => $plugin->noclick,
228 } else { # warn and supply default field
229 warn $plugin->errstr;
230 $subfield_data{marc_value} = {
231 type => 'text',
232 id => $subfield_data{id},
233 name => $subfield_data{id},
234 value => $value,
235 maxlength => $max_length,
239 # it's an hidden field
240 elsif ( $tag eq '' ) {
241 $subfield_data{marc_value} = {
242 type => 'hidden',
243 id => $subfield_data{id},
244 name => $subfield_data{id},
245 value => $value,
246 maxlength => $max_length,
249 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
250 $subfield_data{marc_value} = {
251 type => 'text',
252 id => $subfield_data{id},
253 name => $subfield_data{id},
254 value => $value,
255 maxlength => $max_length,
258 # it's a standard field
260 else {
261 if (
262 length($value) > 100
264 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
265 and $tag < 400 && $subfield eq 'a' )
266 or ( $tag >= 600
267 and $tag < 700
268 && C4::Context->preference("marcflavour") eq "MARC21" )
271 $subfield_data{marc_value} = {
272 type => 'textarea',
273 id => $subfield_data{id},
274 name => $subfield_data{id},
275 value => $value,
276 maxlength => $max_length,
280 else {
281 $subfield_data{marc_value} = {
282 type => 'text',
283 id => $subfield_data{id},
284 name => $subfield_data{id},
285 value => $value,
286 maxlength => $max_length,
291 $subfield_data{'index_subfield'} = $index_subfield;
292 return \%subfield_data;
295 =item format_indicator
297 Translate indicator value for output form - specifically, map
298 indicator = ' ' to ''. This is for the convenience of a cataloger
299 using a mouse to select an indicator input.
301 =cut
303 sub format_indicator {
304 my $ind_value = shift;
305 return '' if not defined $ind_value;
306 return '' if $ind_value eq ' ';
307 return $ind_value;
310 =item CreateKey
312 Create a random value to set it into the input name
314 =cut
316 sub CreateKey {
317 return int(rand(1000000));
320 =item GetMandatoryFieldZ3950
322 This function return an hashref which containts all mandatory field
323 to search with z3950 server.
325 =cut
327 sub GetMandatoryFieldZ3950 {
328 my $authtypecode = shift;
329 if ( C4::Context->preference('marcflavour') eq 'MARC21' ){
330 return {
331 '100a' => 'authorpersonal',
332 '110a' => 'authorcorp',
333 '111a' => 'authormeetingcon',
334 '130a' => 'uniformtitle',
335 '150a' => 'topic',
337 }else{
338 return {
339 '200a' => 'authorpersonal',
340 '210a' => 'authormeetingcon', #210 in UNIMARC is used for both corporation and meeting
341 '230a' => 'uniformtitle',
346 sub build_tabs {
347 my ( $template, $record, $dbh, $encoding,$input ) = @_;
349 # fill arrays
350 my @loop_data = ();
351 my $tag;
353 my $authorised_values_sth = $dbh->prepare(
354 "SELECT authorised_value,lib
355 FROM authorised_values
356 WHERE category=? ORDER BY lib"
359 # in this array, we will push all the 10 tabs
360 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
361 my @BIG_LOOP;
362 my %seen;
363 my @tab_data; # all tags to display
365 foreach my $used ( keys %$tagslib ){
366 push @tab_data,$used if not $seen{$used};
367 $seen{$used}++;
370 my $max_num_tab=9;
371 # loop through each tab 0 through 9
372 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
373 my @loop_data = (); #innerloop in the template.
374 my $i = 0;
375 foreach my $tag (sort @tab_data) {
376 $i++;
377 next if ! $tag;
378 my ($indicator1, $indicator2);
379 my $index_tag = CreateKey;
381 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
382 # if MARC::Record is empty => use tab as master loop.
383 if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
384 my @fields;
385 if ( $tag ne '000' ) {
386 @fields = $record->field($tag);
388 else {
389 push @fields, $record->leader(); # if tag == 000
391 # loop through each field
392 foreach my $field (@fields) {
394 my @subfields_data;
395 if ( $tag < 10 ) {
396 my ( $value, $subfield );
397 if ( $tag ne '000' ) {
398 $value = $field->data();
399 $subfield = "@";
401 else {
402 $value = $field;
403 $subfield = '@';
405 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
406 push(
407 @subfields_data,
408 &create_input(
409 $tag, $subfield, $value, $index_tag, $tabloop, $record,
410 $authorised_values_sth,$input
414 else {
415 my @subfields = $field->subfields();
416 foreach my $subfieldcount ( 0 .. $#subfields ) {
417 my $subfield = $subfields[$subfieldcount][0];
418 my $value = $subfields[$subfieldcount][1];
419 next if ( length $subfield != 1 );
420 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
421 push(
422 @subfields_data,
423 &create_input(
424 $tag, $subfield, $value, $index_tag, $tabloop,
425 $record, $authorised_values_sth,$input
431 # now, loop again to add parameter subfield that are not in the MARC::Record
432 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
434 next if ( length $subfield != 1 );
435 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
436 next if ( $tag < 10 );
437 next
438 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
439 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
440 ); #check for visibility flag
441 next if ( defined( $field->subfield($subfield) ) );
442 push(
443 @subfields_data,
444 &create_input(
445 $tag, $subfield, '', $index_tag, $tabloop, $record,
446 $authorised_values_sth,$input
450 if ( $#subfields_data >= 0 ) {
451 # build the tag entry.
452 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
453 # have twice the same "name" value, and cgi->param() will return only one, making
454 # all subfields to be merged in a single field.
455 my %tag_data = (
456 tag => $tag,
457 index => $index_tag,
458 tag_lib => $tagslib->{$tag}->{lib},
459 repeatable => $tagslib->{$tag}->{repeatable},
460 mandatory => $tagslib->{$tag}->{mandatory},
461 subfield_loop => \@subfields_data,
462 fixedfield => ($tag < 10)?(1):(0),
463 random => CreateKey,
465 if ($tag >= 10){ # no indicator for theses tag
466 $tag_data{indicator1} = format_indicator($field->indicator(1)),
467 $tag_data{indicator2} = format_indicator($field->indicator(2)),
469 push( @loop_data, \%tag_data );
471 } # foreach $field end
473 # if breeding is empty
475 else {
476 my @subfields_data;
477 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
478 next if ( length $subfield != 1 );
479 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
480 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
481 ; #check for visibility flag
482 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
483 push(
484 @subfields_data,
485 &create_input(
486 $tag, $subfield, '', $index_tag, $tabloop, $record,
487 $authorised_values_sth,$input
491 if ( $#subfields_data >= 0 ) {
492 my %tag_data = (
493 tag => $tag,
494 index => $index_tag,
495 tag_lib => $tagslib->{$tag}->{lib},
496 repeatable => $tagslib->{$tag}->{repeatable},
497 mandatory => $tagslib->{$tag}->{mandatory},
498 indicator1 => $indicator1,
499 indicator2 => $indicator2,
500 subfield_loop => \@subfields_data,
501 tagfirstsubfield => $subfields_data[0],
502 fixedfield => ($tag < 10)?(1):(0)
505 push @loop_data, \%tag_data ;
509 if ( $#loop_data >= 0 ) {
510 push @BIG_LOOP, {
511 number => $tabloop,
512 innerloop => \@loop_data,
516 $template->param( BIG_LOOP => \@BIG_LOOP );
520 sub build_hidden_data {
521 # build hidden data =>
522 # we store everything, even if we show only requested subfields.
524 my @loop_data =();
525 my $i=0;
526 foreach my $tag (keys %{$tagslib}) {
527 my $previous_tag = '';
529 # loop through each subfield
530 foreach my $subfield (keys %{$tagslib->{$tag}}) {
531 next if ($subfield eq 'lib');
532 next if ($subfield eq 'tab');
533 next if ($subfield eq 'mandatory');
534 next if ($subfield eq 'repeatable');
535 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
536 my %subfield_data;
537 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
538 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
539 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
540 $subfield_data{marc_value} = {
541 type => 'hidden_simple',
542 name => 'field_value[]',
544 push(@loop_data, \%subfield_data);
545 $i++
550 =back
552 =cut
555 # ========================
556 # MAIN
557 #=========================
558 my $input = new CGI;
559 my $z3950 = $input->param('z3950');
560 my $error = $input->param('error');
561 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
562 my $op = $input->param('op');
563 my $nonav = $input->param('nonav');
564 my $myindex = $input->param('index');
565 my $linkid=$input->param('linkid');
566 my $authtypecode = $input->param('authtypecode');
567 my $breedingid = $input->param('breedingid');
569 my $dbh = C4::Context->dbh;
570 if(!$authtypecode) {
571 $authtypecode = $authid ? Koha::Authorities->find($authid)->authtypecode : '';
574 my ($template, $loggedinuser, $cookie)
575 = get_template_and_user({template_name => "authorities/authorities.tt",
576 query => $input,
577 type => "intranet",
578 authnotrequired => 0,
579 flagsrequired => {editauthorities => 1},
580 debug => 1,
582 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,breedingid=>$breedingid,);
584 $tagslib = GetTagsLabels(1,$authtypecode);
585 $mandatory_z3950 = GetMandatoryFieldZ3950($authtypecode);
587 my $record=-1;
588 my $encoding="";
589 if (($authid) && !($breedingid)){
590 $record = GetAuthority($authid);
592 if ($breedingid) {
593 ( $record, $encoding ) = MARCfindbreeding_auth( $breedingid );
596 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
597 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
598 $is_a_modif=0;
599 if ($authid) {
600 $is_a_modif=1;
601 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
602 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
604 $op ||= q{};
605 #------------------------------------------------------------------------------------------------------------------------------
606 if ($op eq "add") {
607 #------------------------------------------------------------------------------------------------------------------------------
608 # rebuild
609 my @tags = $input->multi_param('tag');
610 my @subfields = $input->multi_param('subfield');
611 my @values = $input->multi_param('field_value');
612 # build indicator hash.
613 my @ind_tag = $input->multi_param('ind_tag');
614 my @indicator = $input->multi_param('indicator');
615 my $record = TransformHtmlToMarc($input, 0);
617 my ($duplicateauthid,$duplicateauthvalue);
618 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
619 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
620 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
621 if (!$duplicateauthid or $confirm_not_duplicate) {
622 if ($is_a_modif ) {
623 ModAuthority($authid,$record,$authtypecode);
624 } else {
625 ($authid) = AddAuthority($record,$authid,$authtypecode);
627 if ($myindex) {
628 print $input->redirect("blinddetail-biblio-search.pl?authid=$authid&index=$myindex");
629 } else {
630 print $input->redirect("detail.pl?authid=$authid");
632 exit;
633 } else {
634 # it may be a duplicate, warn the user and do nothing
635 build_tabs($template, $record, $dbh, $encoding,$input);
636 build_hidden_data;
637 $template->param(authid =>$authid,
638 duplicateauthid => $duplicateauthid,
639 duplicateauthvalue => $duplicateauthvalue->{'authorized'}->[0]->{'heading'},
642 } elsif ($op eq "delete") {
643 #------------------------------------------------------------------------------------------------------------------------------
644 &DelAuthority($authid);
645 if ($nonav){
646 print $input->redirect("auth_finder.pl");
647 }else{
648 print $input->redirect("authorities-home.pl?authid=0");
650 exit;
651 } else {
652 if ($op eq "duplicate")
654 $authid = "";
656 build_tabs ($template, $record, $dbh,$encoding,$input);
657 build_hidden_data;
658 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
659 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
660 authid => $authid , authtypecode=>$authtypecode, );
663 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
665 $template->param(
666 authority_types => $authority_types,
667 authtypecode => $authtypecode,
668 authid => $authid,
669 linkid => $linkid,
670 authtypetext => $authority_types->find($authtypecode)->authtypetext,
671 hide_marc => C4::Context->preference('hide_marc'),
673 output_html_with_http_headers $input, $cookie, $template->output;