Bug 19873: Add unit tests
[koha.git] / authorities / authorities.pl
blob034027792444bc3db0361121eeeb5e40ade177f4
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 Koha::ItemTypes;
36 use vars qw( $tagslib);
37 use vars qw( $authorised_values_sth);
38 use vars qw( $is_a_modif );
40 my $itemtype; # created here because it can be used in build_authorized_values_list sub
41 our($authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
43 =head1 FUNCTIONS
45 =over
47 =item build_authorized_values_list
49 builds list, depending on authorised value...
51 =cut
53 sub MARCfindbreeding_auth {
54 my ( $id ) = @_;
55 my ($marc, $encoding) = GetImportRecordMarc($id);
56 if ($marc) {
57 my $record = MARC::Record->new_from_usmarc($marc);
58 if ( !defined(ref($record)) ) {
59 return -1;
60 } else {
61 return $record, $encoding;
63 } else {
64 return -1;
68 sub build_authorized_values_list {
69 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
71 my @authorised_values;
72 my %authorised_lib;
75 #---- branch
76 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
77 my $sth =
78 $dbh->prepare(
79 "select branchcode,branchname from branches order by branchname");
80 $sth->execute;
81 push @authorised_values, ""
82 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
84 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
85 push @authorised_values, $branchcode;
86 $authorised_lib{$branchcode} = $branchname;
89 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
90 push @authorised_values, ""
91 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
92 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
94 my $itemtype;
95 my $itemtypes = Koha::ItemTypes->search_with_localization;
96 while ( $itemtype = $itemtypes->next ) {
97 push @authorised_values, $itemtype->itemtype;
98 $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
100 $value = $itemtype unless ($value);
102 #---- "true" authorised value
104 else {
105 $authorised_values_sth->execute(
106 $tagslib->{$tag}->{$subfield}->{authorised_value} );
108 push @authorised_values, ""
109 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
110 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
112 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
113 push @authorised_values, $value;
114 $authorised_lib{$value} = $lib;
117 return {
118 type => 'select',
119 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
120 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
121 values => \@authorised_values,
122 labels => \%authorised_lib,
123 default => $value,
128 =item create_input
130 builds the <input ...> entry for a subfield.
132 =cut
134 sub create_input {
135 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
137 my $index_subfield = CreateKey(); # create a specifique key for each subfield
139 $value =~ s/"/&quot;/g;
141 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
142 my $max_length = 9999;
143 if ($tag eq '000') {
144 $max_length = 24;
145 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
146 $max_length = 40;
149 # if there is no value provided but a default value in parameters, get it
150 if ($value eq '') {
151 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
152 if (!defined $value) {
153 $value = q{};
156 # get today date & replace YYYY, MM, DD if provided in the default value
157 my ( $year, $month, $day ) = Today();
158 $month = sprintf( "%02d", $month );
159 $day = sprintf( "%02d", $day );
160 $value =~ s/YYYY/$year/g;
161 $value =~ s/MM/$month/g;
162 $value =~ s/DD/$day/g;
164 my $dbh = C4::Context->dbh;
166 # map '@' as "subfield" label for fixed fields
167 # to something that's allowed in a div id.
168 my $id_subfield = $subfield;
169 $id_subfield = "00" if $id_subfield eq "@";
171 my %subfield_data = (
172 tag => $tag,
173 subfield => $id_subfield,
174 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
175 tag_mandatory => $tagslib->{$tag}->{mandatory},
176 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
177 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
178 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
179 index => $index_tag,
180 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
181 value => $value,
182 random => CreateKey(),
185 if(exists $mandatory_z3950->{$tag.$subfield}){
186 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
189 $subfield_data{visibility} = "display:none;"
190 if( $tagslib->{$tag}->{$subfield}->{hidden} and $value ne ''
191 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
194 # it's an authorised field
195 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
196 $subfield_data{marc_value} =
197 build_authorized_values_list( $tag, $subfield, $value, $dbh,
198 $authorised_values_sth,$index_tag,$index_subfield );
200 # it's a thesaurus / authority field
202 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
203 $subfield_data{marc_value} = {
204 type => 'text1',
205 id => $subfield_data{id},
206 name => $subfield_data{id},
207 value => $value,
208 authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode},
211 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) { # plugin
212 require Koha::FrameworkPlugin;
213 my $plugin = Koha::FrameworkPlugin->new({
214 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
216 my $pars= { dbh => $dbh, record => $rec, tagslib =>$tagslib,
217 id => $subfield_data{id}, tabloop => $tabloop };
218 $plugin->build( $pars );
219 if( !$plugin->errstr ) {
220 $subfield_data{marc_value} = {
221 type => 'text2',
222 id => $subfield_data{id},
223 name => $subfield_data{id},
224 value => $value,
225 maxlength => $max_length,
226 javascript => $plugin->javascript,
227 noclick => $plugin->noclick,
229 } else { # warn and supply default field
230 warn $plugin->errstr;
231 $subfield_data{marc_value} = {
232 type => 'text',
233 id => $subfield_data{id},
234 name => $subfield_data{id},
235 value => $value,
236 maxlength => $max_length,
240 # it's an hidden field
241 elsif ( $tag eq '' ) {
242 $subfield_data{marc_value} = {
243 type => 'hidden',
244 id => $subfield_data{id},
245 name => $subfield_data{id},
246 value => $value,
247 maxlength => $max_length,
250 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
251 $subfield_data{marc_value} = {
252 type => 'text',
253 id => $subfield_data{id},
254 name => $subfield_data{id},
255 value => $value,
256 maxlength => $max_length,
259 # it's a standard field
261 else {
262 if (
263 length($value) > 100
265 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
266 and $tag < 400 && $subfield eq 'a' )
267 or ( $tag >= 600
268 and $tag < 700
269 && C4::Context->preference("marcflavour") eq "MARC21" )
272 $subfield_data{marc_value} = {
273 type => 'textarea',
274 id => $subfield_data{id},
275 name => $subfield_data{id},
276 value => $value,
277 maxlength => $max_length,
281 else {
282 $subfield_data{marc_value} = {
283 type => 'text',
284 id => $subfield_data{id},
285 name => $subfield_data{id},
286 value => $value,
287 maxlength => $max_length,
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 =item GetMandatoryFieldZ3950
323 This function returns a hashref which contains all mandatory field
324 to search with z3950 server.
326 =cut
328 sub GetMandatoryFieldZ3950 {
329 my $authtypecode = shift;
330 if ( C4::Context->preference('marcflavour') eq 'MARC21' ){
331 return {
332 '100a' => 'authorpersonal',
333 '110a' => 'authorcorp',
334 '111a' => 'authormeetingcon',
335 '130a' => 'uniformtitle',
336 '150a' => 'topic',
338 }else{
339 return {
340 '200a' => 'authorpersonal',
341 '210a' => 'authormeetingcon', #210 in UNIMARC is used for both corporation and meeting
342 '230a' => 'uniformtitle',
347 sub build_tabs {
348 my ( $template, $record, $dbh, $encoding,$input ) = @_;
350 # fill arrays
351 my @loop_data = ();
352 my $tag;
354 my $authorised_values_sth = $dbh->prepare(
355 "SELECT authorised_value,lib
356 FROM authorised_values
357 WHERE category=? ORDER BY lib"
360 # in this array, we will push all the 10 tabs
361 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
362 my @BIG_LOOP;
363 my %seen;
364 my @tab_data; # all tags to display
366 foreach my $used ( keys %$tagslib ){
367 push @tab_data,$used if not $seen{$used};
368 $seen{$used}++;
371 my $max_num_tab=9;
372 # loop through each tab 0 through 9
373 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
374 my @loop_data = (); #innerloop in the template.
375 my $i = 0;
376 foreach my $tag (sort @tab_data) {
377 $i++;
378 next if ! $tag;
379 my ($indicator1, $indicator2);
380 my $index_tag = CreateKey;
382 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
383 # if MARC::Record is empty => use tab as master loop.
384 if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
385 my @fields;
386 if ( $tag ne '000' ) {
387 @fields = $record->field($tag);
389 else {
390 push @fields, $record->leader(); # if tag == 000
392 # loop through each field
393 foreach my $field (@fields) {
395 my @subfields_data;
396 if ( $tag < 10 ) {
397 my ( $value, $subfield );
398 if ( $tag ne '000' ) {
399 $value = $field->data();
400 $subfield = "@";
402 else {
403 $value = $field;
404 $subfield = '@';
406 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
407 next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
408 push(
409 @subfields_data,
410 &create_input(
411 $tag, $subfield, $value, $index_tag, $tabloop, $record,
412 $authorised_values_sth,$input
416 else {
417 my @subfields = $field->subfields();
418 foreach my $subfieldcount ( 0 .. $#subfields ) {
419 my $subfield = $subfields[$subfieldcount][0];
420 my $value = $subfields[$subfieldcount][1];
421 next if ( length $subfield != 1 );
422 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
423 next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
424 push(
425 @subfields_data,
426 &create_input(
427 $tag, $subfield, $value, $index_tag, $tabloop,
428 $record, $authorised_values_sth,$input
434 # now, loop again to add parameter subfield that are not in the MARC::Record
435 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
437 next if ( length $subfield != 1 );
438 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
439 next if ( $tag < 10 );
440 next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
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} && $subfield ne '9';
480 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
481 push(
482 @subfields_data,
483 &create_input(
484 $tag, $subfield, '', $index_tag, $tabloop, $record,
485 $authorised_values_sth,$input
489 if ( $#subfields_data >= 0 ) {
490 my %tag_data = (
491 tag => $tag,
492 index => $index_tag,
493 tag_lib => $tagslib->{$tag}->{lib},
494 repeatable => $tagslib->{$tag}->{repeatable},
495 mandatory => $tagslib->{$tag}->{mandatory},
496 indicator1 => $indicator1,
497 indicator2 => $indicator2,
498 subfield_loop => \@subfields_data,
499 tagfirstsubfield => $subfields_data[0],
500 fixedfield => ($tag < 10)?(1):(0)
503 push @loop_data, \%tag_data ;
507 if ( $#loop_data >= 0 ) {
508 push @BIG_LOOP, {
509 number => $tabloop,
510 innerloop => \@loop_data,
514 $template->param( BIG_LOOP => \@BIG_LOOP );
518 sub build_hidden_data {
519 # build hidden data =>
520 # we store everything, even if we show only requested subfields.
522 my @loop_data =();
523 my $i=0;
524 foreach my $tag (keys %{$tagslib}) {
525 my $previous_tag = '';
527 # loop through each subfield
528 foreach my $subfield (keys %{$tagslib->{$tag}}) {
529 next if ($subfield eq 'lib');
530 next if ($subfield eq 'tab');
531 next if ($subfield eq 'mandatory');
532 next if ($subfield eq 'repeatable');
533 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
534 my %subfield_data;
535 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
536 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
537 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
538 $subfield_data{marc_value} = {
539 type => 'hidden_simple',
540 name => 'field_value[]',
542 push(@loop_data, \%subfield_data);
543 $i++
548 =back
550 =cut
553 # ========================
554 # MAIN
555 #=========================
556 my $input = new CGI;
557 my $z3950 = $input->param('z3950');
558 my $error = $input->param('error');
559 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
560 my $op = $input->param('op');
561 my $nonav = $input->param('nonav');
562 my $myindex = $input->param('index');
563 my $linkid=$input->param('linkid');
564 my $authtypecode = $input->param('authtypecode');
565 my $breedingid = $input->param('breedingid');
567 my $dbh = C4::Context->dbh;
568 if(!$authtypecode) {
569 $authtypecode = $authid ? Koha::Authorities->find($authid)->authtypecode : '';
572 my ($template, $loggedinuser, $cookie)
573 = get_template_and_user({template_name => "authorities/authorities.tt",
574 query => $input,
575 type => "intranet",
576 authnotrequired => 0,
577 flagsrequired => {editauthorities => 1},
578 debug => 1,
580 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,breedingid=>$breedingid,);
582 $tagslib = GetTagsLabels(1,$authtypecode);
583 $mandatory_z3950 = GetMandatoryFieldZ3950($authtypecode);
585 my $record=-1;
586 my $encoding="";
587 if (($authid) && !($breedingid)){
588 $record = GetAuthority($authid);
590 if ($breedingid) {
591 ( $record, $encoding ) = MARCfindbreeding_auth( $breedingid );
594 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
595 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
596 $is_a_modif=0;
597 if ($authid) {
598 $is_a_modif=1;
599 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
600 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
602 $op ||= q{};
603 #------------------------------------------------------------------------------------------------------------------------------
604 if ($op eq "add") {
605 #------------------------------------------------------------------------------------------------------------------------------
606 # rebuild
607 my @tags = $input->multi_param('tag');
608 my @subfields = $input->multi_param('subfield');
609 my @values = $input->multi_param('field_value');
610 # build indicator hash.
611 my @ind_tag = $input->multi_param('ind_tag');
612 my @indicator = $input->multi_param('indicator');
613 my $record = TransformHtmlToMarc($input, 0);
615 my ($duplicateauthid,$duplicateauthvalue);
616 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
617 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
618 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
619 if (!$duplicateauthid or $confirm_not_duplicate) {
620 if ($is_a_modif ) {
621 ModAuthority($authid,$record,$authtypecode);
622 } else {
623 ($authid) = AddAuthority($record,$authid,$authtypecode);
625 if ($myindex) {
626 print $input->redirect("blinddetail-biblio-search.pl?authid=$authid&index=$myindex");
627 } else {
628 print $input->redirect("detail.pl?authid=$authid");
630 exit;
631 } else {
632 # it may be a duplicate, warn the user and do nothing
633 build_tabs($template, $record, $dbh, $encoding,$input);
634 build_hidden_data;
635 $template->param(authid =>$authid,
636 duplicateauthid => $duplicateauthid,
637 duplicateauthvalue => $duplicateauthvalue->{'authorized'}->[0]->{'heading'},
640 } elsif ($op eq "delete") {
641 #------------------------------------------------------------------------------------------------------------------------------
642 DelAuthority({ authid => $authid });
643 if ($nonav){
644 print $input->redirect("auth_finder.pl");
645 }else{
646 print $input->redirect("authorities-home.pl?authid=0");
648 exit;
649 } else {
650 if ($op eq "duplicate")
652 $authid = "";
654 build_tabs ($template, $record, $dbh,$encoding,$input);
655 build_hidden_data;
656 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
657 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
658 authid => $authid , authtypecode=>$authtypecode, );
661 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
663 my $type = $authority_types->find($authtypecode);
664 $template->param(
665 authority_types => $authority_types,
666 authtypecode => $authtypecode,
667 authid => $authid,
668 linkid => $linkid,
669 authtypetext => $type ? $type->authtypetext : "",
670 hide_marc => C4::Context->preference('hide_marc'),
672 output_html_with_http_headers $input, $cookie, $template->output;