Bug 14739: Add several patrons to a new batch will create a new batch per patron...
[koha.git] / authorities / authorities.pl
blob16910c8e2d3c93722732bf2241f19935d5532eab
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; # 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 => $tagslib->{$tag}->{$subfield}->{lib},
179 tag_mandatory => $tagslib->{$tag}->{mandatory},
180 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
181 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
182 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
183 index => $index_tag,
184 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
185 value => $value,
186 random => CreateKey(),
189 if(exists $mandatory_z3950->{$tag.$subfield}){
190 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
193 $subfield_data{visibility} = "display:none;"
194 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
195 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
198 # it's an authorised field
199 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
200 $subfield_data{marc_value} =
201 build_authorized_values_list( $tag, $subfield, $value, $dbh,
202 $authorised_values_sth,$index_tag,$index_subfield );
204 # it's a thesaurus / authority field
206 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
207 $subfield_data{marc_value} = {
208 type => 'text1',
209 id => $subfield_data{id},
210 name => $subfield_data{id},
211 value => $value,
212 authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode},
215 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) { # plugin
216 require Koha::FrameworkPlugin;
217 my $plugin = Koha::FrameworkPlugin->new({
218 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
220 my $pars= { dbh => $dbh, record => $rec, tagslib =>$tagslib,
221 id => $subfield_data{id}, tabloop => $tabloop };
222 $plugin->build( $pars );
223 if( !$plugin->errstr ) {
224 $subfield_data{marc_value} = {
225 type => 'text2',
226 id => $subfield_data{id},
227 name => $subfield_data{id},
228 value => $value,
229 maxlength => $max_length,
230 javascript => $plugin->javascript,
231 noclick => $plugin->noclick,
233 } else { # warn and supply default field
234 warn $plugin->errstr;
235 $subfield_data{marc_value} = {
236 type => 'text',
237 id => $subfield_data{id},
238 name => $subfield_data{id},
239 value => $value,
240 maxlength => $max_length,
244 # it's an hidden field
245 elsif ( $tag eq '' ) {
246 $subfield_data{marc_value} = {
247 type => 'hidden',
248 id => $subfield_data{id},
249 name => $subfield_data{id},
250 value => $value,
251 maxlength => $max_length,
254 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
255 $subfield_data{marc_value} = {
256 type => 'text',
257 id => $subfield_data{id},
258 name => $subfield_data{id},
259 value => $value,
260 maxlength => $max_length,
263 # it's a standard field
265 else {
266 if (
267 length($value) > 100
269 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
270 and $tag < 400 && $subfield eq 'a' )
271 or ( $tag >= 600
272 and $tag < 700
273 && C4::Context->preference("marcflavour") eq "MARC21" )
276 $subfield_data{marc_value} = {
277 type => 'textarea',
278 id => $subfield_data{id},
279 name => $subfield_data{id},
280 value => $value,
281 maxlength => $max_length,
285 else {
286 $subfield_data{marc_value} = {
287 type => 'text',
288 id => $subfield_data{id},
289 name => $subfield_data{id},
290 value => $value,
291 maxlength => $max_length,
296 $subfield_data{'index_subfield'} = $index_subfield;
297 return \%subfield_data;
300 =item format_indicator
302 Translate indicator value for output form - specifically, map
303 indicator = ' ' to ''. This is for the convenience of a cataloger
304 using a mouse to select an indicator input.
306 =cut
308 sub format_indicator {
309 my $ind_value = shift;
310 return '' if not defined $ind_value;
311 return '' if $ind_value eq ' ';
312 return $ind_value;
315 =item CreateKey
317 Create a random value to set it into the input name
319 =cut
321 sub CreateKey {
322 return int(rand(1000000));
325 =item GetMandatoryFieldZ3950
327 This function return an hashref which containts all mandatory field
328 to search with z3950 server.
330 =cut
332 sub GetMandatoryFieldZ3950 {
333 my $authtypecode = shift;
334 if ( C4::Context->preference('marcflavour') eq 'MARC21' ){
335 return {
336 '100a' => 'authorpersonal',
337 '110a' => 'authorcorp',
338 '111a' => 'authormeetingcon',
339 '130a' => 'uniformtitle',
340 '150a' => 'topic',
342 }else{
343 return {
344 '200a' => 'authorpersonal',
345 '210a' => 'authormeetingcon', #210 in UNIMARC is used for both corporation and meeting
346 '230a' => 'uniformtitle',
351 sub build_tabs {
352 my ( $template, $record, $dbh, $encoding,$input ) = @_;
354 # fill arrays
355 my @loop_data = ();
356 my $tag;
358 my $authorised_values_sth = $dbh->prepare(
359 "SELECT authorised_value,lib
360 FROM authorised_values
361 WHERE category=? ORDER BY lib"
364 # in this array, we will push all the 10 tabs
365 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
366 my @BIG_LOOP;
367 my %seen;
368 my @tab_data; # all tags to display
370 foreach my $used ( keys %$tagslib ){
371 push @tab_data,$used if not $seen{$used};
372 $seen{$used}++;
375 my $max_num_tab=9;
376 # loop through each tab 0 through 9
377 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
378 my @loop_data = (); #innerloop in the template.
379 my $i = 0;
380 foreach my $tag (sort @tab_data) {
381 $i++;
382 next if ! $tag;
383 my ($indicator1, $indicator2);
384 my $index_tag = CreateKey;
386 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
387 # if MARC::Record is empty => use tab as master loop.
388 if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
389 my @fields;
390 if ( $tag ne '000' ) {
391 @fields = $record->field($tag);
393 else {
394 push @fields, $record->leader(); # if tag == 000
396 # loop through each field
397 foreach my $field (@fields) {
399 my @subfields_data;
400 if ( $tag < 10 ) {
401 my ( $value, $subfield );
402 if ( $tag ne '000' ) {
403 $value = $field->data();
404 $subfield = "@";
406 else {
407 $value = $field;
408 $subfield = '@';
410 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
411 push(
412 @subfields_data,
413 &create_input(
414 $tag, $subfield, $value, $index_tag, $tabloop, $record,
415 $authorised_values_sth,$input
419 else {
420 my @subfields = $field->subfields();
421 foreach my $subfieldcount ( 0 .. $#subfields ) {
422 my $subfield = $subfields[$subfieldcount][0];
423 my $value = $subfields[$subfieldcount][1];
424 next if ( length $subfield != 1 );
425 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
426 push(
427 @subfields_data,
428 &create_input(
429 $tag, $subfield, $value, $index_tag, $tabloop,
430 $record, $authorised_values_sth,$input
436 # now, loop again to add parameter subfield that are not in the MARC::Record
437 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
439 next if ( length $subfield != 1 );
440 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
441 next if ( $tag < 10 );
442 next
443 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
444 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
445 ); #check for visibility flag
446 next if ( defined( $field->subfield($subfield) ) );
447 push(
448 @subfields_data,
449 &create_input(
450 $tag, $subfield, '', $index_tag, $tabloop, $record,
451 $authorised_values_sth,$input
455 if ( $#subfields_data >= 0 ) {
456 # build the tag entry.
457 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
458 # have twice the same "name" value, and cgi->param() will return only one, making
459 # all subfields to be merged in a single field.
460 my %tag_data = (
461 tag => $tag,
462 index => $index_tag,
463 tag_lib => $tagslib->{$tag}->{lib},
464 repeatable => $tagslib->{$tag}->{repeatable},
465 mandatory => $tagslib->{$tag}->{mandatory},
466 subfield_loop => \@subfields_data,
467 fixedfield => ($tag < 10)?(1):(0),
468 random => CreateKey,
470 if ($tag >= 10){ # no indicator for theses tag
471 $tag_data{indicator1} = format_indicator($field->indicator(1)),
472 $tag_data{indicator2} = format_indicator($field->indicator(2)),
474 push( @loop_data, \%tag_data );
476 } # foreach $field end
478 # if breeding is empty
480 else {
481 my @subfields_data;
482 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
483 next if ( length $subfield != 1 );
484 next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
485 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
486 ; #check for visibility flag
487 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
488 push(
489 @subfields_data,
490 &create_input(
491 $tag, $subfield, '', $index_tag, $tabloop, $record,
492 $authorised_values_sth,$input
496 if ( $#subfields_data >= 0 ) {
497 my %tag_data = (
498 tag => $tag,
499 index => $index_tag,
500 tag_lib => $tagslib->{$tag}->{lib},
501 repeatable => $tagslib->{$tag}->{repeatable},
502 mandatory => $tagslib->{$tag}->{mandatory},
503 indicator1 => $indicator1,
504 indicator2 => $indicator2,
505 subfield_loop => \@subfields_data,
506 tagfirstsubfield => $subfields_data[0],
507 fixedfield => ($tag < 10)?(1):(0)
510 push @loop_data, \%tag_data ;
514 if ( $#loop_data >= 0 ) {
515 push @BIG_LOOP, {
516 number => $tabloop,
517 innerloop => \@loop_data,
521 $template->param( BIG_LOOP => \@BIG_LOOP );
525 sub build_hidden_data {
526 # build hidden data =>
527 # we store everything, even if we show only requested subfields.
529 my @loop_data =();
530 my $i=0;
531 foreach my $tag (keys %{$tagslib}) {
532 my $previous_tag = '';
534 # loop through each subfield
535 foreach my $subfield (keys %{$tagslib->{$tag}}) {
536 next if ($subfield eq 'lib');
537 next if ($subfield eq 'tab');
538 next if ($subfield eq 'mandatory');
539 next if ($subfield eq 'repeatable');
540 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
541 my %subfield_data;
542 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
543 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
544 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
545 $subfield_data{marc_value} = {
546 type => 'hidden_simple',
547 name => 'field_value[]',
549 push(@loop_data, \%subfield_data);
550 $i++
555 =back
557 =cut
560 # ========================
561 # MAIN
562 #=========================
563 my $input = new CGI;
564 my $z3950 = $input->param('z3950');
565 my $error = $input->param('error');
566 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
567 my $op = $input->param('op');
568 my $nonav = $input->param('nonav');
569 my $myindex = $input->param('index');
570 my $linkid=$input->param('linkid');
571 my $authtypecode = $input->param('authtypecode');
572 my $breedingid = $input->param('breedingid');
574 my $dbh = C4::Context->dbh;
575 if(!$authtypecode) {
576 $authtypecode = $authid? &GetAuthTypeCode($authid): '';
579 my ($template, $loggedinuser, $cookie)
580 = get_template_and_user({template_name => "authorities/authorities.tt",
581 query => $input,
582 type => "intranet",
583 authnotrequired => 0,
584 flagsrequired => {editauthorities => 1},
585 debug => 1,
587 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,breedingid=>$breedingid,);
589 $tagslib = GetTagsLabels(1,$authtypecode);
590 $mandatory_z3950 = GetMandatoryFieldZ3950($authtypecode);
592 my $record=-1;
593 my $encoding="";
594 if (($authid) && !($breedingid)){
595 $record = GetAuthority($authid);
597 if ($breedingid) {
598 ( $record, $encoding ) = MARCfindbreeding_auth( $breedingid );
601 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
602 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
603 $is_a_modif=0;
604 if ($authid) {
605 $is_a_modif=1;
606 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
607 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
609 $op ||= q{};
610 #------------------------------------------------------------------------------------------------------------------------------
611 if ($op eq "add") {
612 #------------------------------------------------------------------------------------------------------------------------------
613 # rebuild
614 my @tags = $input->param('tag');
615 my @subfields = $input->param('subfield');
616 my @values = $input->param('field_value');
617 # build indicator hash.
618 my @ind_tag = $input->param('ind_tag');
619 my @indicator = $input->param('indicator');
620 my $record = TransformHtmlToMarc($input);
622 my ($duplicateauthid,$duplicateauthvalue);
623 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
624 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
625 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
626 if (!$duplicateauthid or $confirm_not_duplicate) {
627 if ($is_a_modif ) {
628 ModAuthority($authid,$record,$authtypecode);
629 } else {
630 ($authid) = AddAuthority($record,$authid,$authtypecode);
632 if ($myindex) {
633 print $input->redirect("blinddetail-biblio-search.pl?authid=$authid&index=$myindex");
634 } else {
635 print $input->redirect("detail.pl?authid=$authid");
637 exit;
638 } else {
639 # it may be a duplicate, warn the user and do nothing
640 build_tabs($template, $record, $dbh, $encoding,$input);
641 build_hidden_data;
642 $template->param(authid =>$authid,
643 duplicateauthid => $duplicateauthid,
644 duplicateauthvalue => $duplicateauthvalue->{'authorized'}->[0]->{'heading'},
647 } elsif ($op eq "delete") {
648 #------------------------------------------------------------------------------------------------------------------------------
649 &DelAuthority($authid);
650 if ($nonav){
651 print $input->redirect("auth_finder.pl");
652 }else{
653 print $input->redirect("authorities-home.pl?authid=0");
655 exit;
656 } else {
657 if ($op eq "duplicate")
659 $authid = "";
661 build_tabs ($template, $record, $dbh,$encoding,$input);
662 build_hidden_data;
663 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
664 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
665 authid => $authid , authtypecode=>$authtypecode, );
668 $template->param(authid => $authid,
669 authtypecode => $authtypecode,
670 linkid=>$linkid,
673 my $authtypes = getauthtypes;
674 my @authtypesloop;
675 foreach my $thisauthtype (keys %$authtypes) {
676 my %row =(value => $thisauthtype,
677 selected => $thisauthtype eq $authtypecode,
678 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
680 push @authtypesloop, \%row;
683 $template->param(authtypesloop => \@authtypesloop,
684 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
685 hide_marc => C4::Context->preference('hide_marc'),
687 output_html_with_http_headers $input, $cookie, $template->output;