Bug 3962: Fixes XHTML errors in aqbudgets.tmpl.
[koha.git] / cataloguing / addbiblio.pl
blob38289501aba8807c8a09771fb8bc3209379515f0
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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
21 use strict;
22 use CGI;
23 use C4::Output;
24 use C4::Auth;
25 use C4::Biblio;
26 use C4::Search;
27 use C4::AuthoritiesMarc;
28 use C4::Context;
29 use MARC::Record;
30 use C4::Log;
31 use C4::Koha; # XXX subfield_is_koha_internal_p
32 use C4::Branch; # XXX subfield_is_koha_internal_p
33 use C4::ClassSource;
34 use C4::ImportBatch;
35 use C4::Charset;
37 use Date::Calc qw(Today);
38 use MARC::File::USMARC;
39 use MARC::File::XML;
41 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
42 MARC::File::XML->default_record_format('UNIMARC');
45 our($tagslib,$authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
47 =item MARCfindbreeding
49 $record = MARCfindbreeding($breedingid);
51 Look up the import record repository for the record with
52 record with id $breedingid. If found, returns the decoded
53 MARC::Record; otherwise, -1 is returned (FIXME).
54 Returns as second parameter the character encoding.
56 =cut
58 sub MARCfindbreeding {
59 my ( $id ) = @_;
60 my ($marc, $encoding) = GetImportRecordMarc($id);
61 # remove the - in isbn, koha store isbn without any -
62 if ($marc) {
63 my $record = MARC::Record->new_from_usmarc($marc);
64 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
65 if ( $record->field($isbnfield) ) {
66 foreach my $field ( $record->field($isbnfield) ) {
67 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
68 my $newisbn = $field->subfield($isbnsubfield);
69 $newisbn =~ s/-//g;
70 $field->update( $isbnsubfield => $newisbn );
74 # fix the unimarc 100 coded field (with unicode information)
75 if (C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield(100,'a')) {
76 my $f100a=$record->subfield(100,'a');
77 my $f100 = $record->field(100);
78 my $f100temp = $f100->as_string;
79 $record->delete_field($f100);
80 if ( length($f100temp) > 28 ) {
81 substr( $f100temp, 26, 2, "50" );
82 $f100->update( 'a' => $f100temp );
83 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
84 $record->insert_fields_ordered($f100);
88 if ( !defined(ref($record)) ) {
89 return -1;
91 else {
92 # normalize author : probably UNIMARC specific...
93 if ( C4::Context->preference("z3950NormalizeAuthor")
94 and C4::Context->preference("z3950AuthorAuthFields") )
96 my ( $tag, $subfield ) = GetMarcFromKohaField("biblio.author", '');
98 # my $summary = C4::Context->preference("z3950authortemplate");
99 my $auth_fields =
100 C4::Context->preference("z3950AuthorAuthFields");
101 my @auth_fields = split /,/, $auth_fields;
102 my $field;
104 if ( $record->field($tag) ) {
105 foreach my $tmpfield ( $record->field($tag)->subfields ) {
107 # foreach my $subfieldcode ($tmpfield->subfields){
108 my $subfieldcode = shift @$tmpfield;
109 my $subfieldvalue = shift @$tmpfield;
110 if ($field) {
111 $field->add_subfields(
112 "$subfieldcode" => $subfieldvalue )
113 if ( $subfieldcode ne $subfield );
115 else {
116 $field =
117 MARC::Field->new( $tag, "", "",
118 $subfieldcode => $subfieldvalue )
119 if ( $subfieldcode ne $subfield );
123 $record->delete_field( $record->field($tag) );
124 foreach my $fieldtag (@auth_fields) {
125 next unless ( $record->field($fieldtag) );
126 my $lastname = $record->field($fieldtag)->subfield('a');
127 my $firstname = $record->field($fieldtag)->subfield('b');
128 my $title = $record->field($fieldtag)->subfield('c');
129 my $number = $record->field($fieldtag)->subfield('d');
130 if ($title) {
132 # $field->add_subfields("$subfield"=>"[ ".ucfirst($title).ucfirst($firstname)." ".$number." ]");
133 $field->add_subfields(
134 "$subfield" => ucfirst($title) . " "
135 . ucfirst($firstname) . " "
136 . $number );
138 else {
140 # $field->add_subfields("$subfield"=>"[ ".ucfirst($firstname).", ".ucfirst($lastname)." ]");
141 $field->add_subfields(
142 "$subfield" => ucfirst($firstname) . ", "
143 . ucfirst($lastname) );
146 $record->insert_fields_ordered($field);
148 return $record, $encoding;
151 return -1;
154 =item build_authorized_values_list
156 =cut
158 sub build_authorized_values_list ($$$$$$$) {
159 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
161 my @authorised_values;
162 my %authorised_lib;
164 # builds list, depending on authorised value...
166 #---- branch
167 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
168 #Use GetBranches($onlymine)
169 my $onlymine=C4::Context->preference('IndependantBranches') &&
170 C4::Context->userenv &&
171 C4::Context->userenv->{flags} % 2 == 0 &&
172 C4::Context->userenv->{branch};
173 my $branches = GetBranches($onlymine);
174 my @branchloop;
175 foreach my $thisbranch ( sort keys %$branches ) {
176 push @authorised_values, $thisbranch;
177 $authorised_lib{$thisbranch} = $branches->{$thisbranch}->{'branchname'};
180 #----- itemtypes
182 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
183 my $sth =
184 $dbh->prepare(
185 "select itemtype,description from itemtypes order by description");
186 $sth->execute;
187 push @authorised_values, ""
188 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
190 my $itemtype;
192 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
193 push @authorised_values, $itemtype;
194 $authorised_lib{$itemtype} = $description;
196 $value = $itemtype unless ($value);
198 #---- class_sources
200 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
201 push @authorised_values, ""
202 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
204 my $class_sources = GetClassSources();
206 my $default_source = C4::Context->preference("DefaultClassificationSource");
208 foreach my $class_source (sort keys %$class_sources) {
209 next unless $class_sources->{$class_source}->{'used'} or
210 ($value and $class_source eq $value) or
211 ($class_source eq $default_source);
212 push @authorised_values, $class_source;
213 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
214 $value = $class_source unless ($value);
215 $value = $default_source unless ($value);
217 #---- "true" authorised value
219 else {
220 $authorised_values_sth->execute(
221 $tagslib->{$tag}->{$subfield}->{authorised_value} );
223 push @authorised_values, ""
224 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
226 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
227 push @authorised_values, $value;
228 $authorised_lib{$value} = $lib;
231 return CGI::scrolling_list(
232 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
233 -values => \@authorised_values,
234 -default => $value,
235 -labels => \%authorised_lib,
236 -override => 1,
237 -size => 1,
238 -multiple => 0,
239 -tabindex => 1,
240 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
241 -class => "input_marceditor",
245 =item CreateKey
247 Create a random value to set it into the input name
249 =cut
251 sub CreateKey(){
252 return int(rand(1000000));
255 =item GetMandatoryFieldZ3950
257 This function return an hashref which containts all mandatory field
258 to search with z3950 server.
260 =cut
262 sub GetMandatoryFieldZ3950($){
263 my $frameworkcode = shift;
264 my @isbn = GetMarcFromKohaField('biblioitems.isbn',$frameworkcode);
265 my @title = GetMarcFromKohaField('biblio.title',$frameworkcode);
266 my @author = GetMarcFromKohaField('biblio.author',$frameworkcode);
267 my @issn = GetMarcFromKohaField('biblioitems.issn',$frameworkcode);
268 my @lccn = GetMarcFromKohaField('biblioitems.lccn',$frameworkcode);
270 return {
271 $isbn[0].$isbn[1] => 'isbn',
272 $title[0].$title[1] => 'title',
273 $author[0].$author[1] => 'author',
274 $issn[0].$issn[1] => 'issn',
275 $lccn[0].$lccn[1] => 'lccn',
279 =item create_input
281 builds the <input ...> entry for a subfield.
283 =cut
285 sub create_input {
286 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
288 my $index_subfield = CreateKey(); # create a specifique key for each subfield
290 $value =~ s/"/&quot;/g;
292 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
293 my $max_length = 9999;
294 if ($tag eq '000') {
295 $max_length = 24;
296 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
297 $max_length = 40;
300 # if there is no value provided but a default value in parameters, get it
301 unless ($value) {
302 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
304 # get today date & replace YYYY, MM, DD if provided in the default value
305 my ( $year, $month, $day ) = Today();
306 $month = sprintf( "%02d", $month );
307 $day = sprintf( "%02d", $day );
308 $value =~ s/YYYY/$year/g;
309 $value =~ s/MM/$month/g;
310 $value =~ s/DD/$day/g;
311 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
312 $value=~s/user/$username/g;
315 my $dbh = C4::Context->dbh;
317 # map '@' as "subfield" label for fixed fields
318 # to something that's allowed in a div id.
319 my $id_subfield = $subfield;
320 $id_subfield = "00" if $id_subfield eq "@";
322 my %subfield_data = (
323 tag => $tag,
324 subfield => $id_subfield,
325 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
326 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
327 tag_mandatory => $tagslib->{$tag}->{mandatory},
328 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
329 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
330 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
331 index => $index_tag,
332 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
333 value => $value,
334 random => CreateKey(),
337 if(exists $mandatory_z3950->{$tag.$subfield}){
338 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
340 # decide if the subfield must be expanded (visible) by default or not
341 # if it is mandatory, then expand. If it is hidden explicitly by the hidden flag, hidden anyway
342 $subfield_data{visibility} = "display:none;"
343 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
344 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
346 # always expand all subfields of a mandatory field
347 $subfield_data{visibility} = "" if $tagslib->{$tag}->{mandatory};
348 # it's an authorised field
349 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
350 $subfield_data{marc_value} =
351 build_authorized_values_list( $tag, $subfield, $value, $dbh,
352 $authorised_values_sth,$index_tag,$index_subfield );
354 # it's a subfield $9 linking to an authority record - see bug 2206
356 elsif ($subfield eq "9" and
357 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
358 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
359 $tagslib->{$tag}->{'a'}->{authtypecode} ne '') {
361 $subfield_data{marc_value} =
362 "<input type=\"text\"
363 id=\"".$subfield_data{id}."\"
364 name=\"".$subfield_data{id}."\"
365 value=\"$value\"
366 class=\"input_marceditor\"
367 tabindex=\"1\"
368 size=\"5\"
369 maxlength=\"$max_length\"
370 readonly=\"readonly\"
371 \/>";
373 # it's a thesaurus / authority field
375 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
376 if (C4::Context->preference("BiblioAddsAuthorities")) {
377 $subfield_data{marc_value} =
378 "<input type=\"text\"
379 id=\"".$subfield_data{id}."\"
380 name=\"".$subfield_data{id}."\"
381 value=\"$value\"
382 class=\"input_marceditor\"
383 tabindex=\"1\"
384 size=\"67\"
385 maxlength=\"$max_length\"
387 <a href=\"#\" class=\"buttonDot\"
388 onclick=\"openAuth(this.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode}."'); return false;\" tabindex=\"1\" title=\"Tag Editor\">...</a>
390 } else {
391 $subfield_data{marc_value} =
392 "<input type=\"text\"
393 id=\"".$subfield_data{id}."\"
394 name=\"".$subfield_data{id}."\"
395 value=\"$value\"
396 class=\"input_marceditor\"
397 tabindex=\"1\"
398 size=\"67\"
399 maxlength=\"$max_length\"
400 readonly=\"readonly\"
401 \/><a href=\"#\" class=\"buttonDot\"
402 onclick=\"openAuth(this.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode}."'); return false;\" tabindex=\"1\" title=\"Tag Editor\">...</a>
405 # it's a plugin field
407 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
409 # opening plugin. Just check wether we are on a developper computer on a production one
410 # (the cgidir differs)
411 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
412 unless ( opendir( DIR, "$cgidir" ) ) {
413 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
414 closedir( DIR );
416 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
417 if (do $plugin) {
418 my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
419 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
421 $subfield_data{marc_value} =
422 "<input tabindex=\"1\"
423 type=\"text\"
424 id=\"".$subfield_data{id}."\"
425 name=\"".$subfield_data{id}."\"
426 value=\"$value\"
427 class=\"input_marceditor\"
428 onfocus=\"Focus$function_name($index_tag)\"
429 size=\"67\"
430 maxlength=\"$max_length\"
431 onblur=\"Blur$function_name($index_tag); \" \/>
432 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" tabindex=\"1\" title=\"Tag Editor\">...</a>
433 $javascript";
434 } else {
435 warn "Plugin Failed: $plugin";
436 # supply default input form
437 $subfield_data{marc_value} =
438 "<input type=\"text\"
439 id=\"".$subfield_data{id}."\"
440 name=\"".$subfield_data{id}."\"
441 value=\"$value\"
442 tabindex=\"1\"
443 size=\"67\"
444 maxlength=\"$max_length\"
445 class=\"input_marceditor\"
449 # it's an hidden field
451 elsif ( $tag eq '' ) {
452 $subfield_data{marc_value} =
453 "<input tabindex=\"1\"
454 type=\"hidden\"
455 id=\"".$subfield_data{id}."\"
456 name=\"".$subfield_data{id}."\"
457 size=\"67\"
458 maxlength=\"$max_length\"
459 value=\"$value\" \/>
462 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
463 $subfield_data{marc_value} =
464 "<input type=\"text\"
465 id=\"".$subfield_data{id}."\"
466 name=\"".$subfield_data{id}."\"
467 class=\"input_marceditor\"
468 tabindex=\"1\"
469 size=\"67\"
470 maxlength=\"$max_length\"
471 value=\"$value\"
472 \/>";
474 # it's a standard field
476 else {
477 if (
478 length($value) > 100
480 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
481 and $tag < 400 && $subfield eq 'a' )
482 or ( $tag >= 500
483 and $tag < 600
484 && C4::Context->preference("marcflavour") eq "MARC21" )
487 $subfield_data{marc_value} =
488 "<textarea cols=\"70\"
489 rows=\"4\"
490 id=\"".$subfield_data{id}."\"
491 name=\"".$subfield_data{id}."\"
492 class=\"input_marceditor\"
493 tabindex=\"1\"
494 >$value</textarea>
497 else {
498 $subfield_data{marc_value} =
499 "<input type=\"text\"
500 id=\"".$subfield_data{id}."\"
501 name=\"".$subfield_data{id}."\"
502 value=\"$value\"
503 tabindex=\"1\"
504 size=\"67\"
505 maxlength=\"$max_length\"
506 class=\"input_marceditor\"
511 $subfield_data{'index_subfield'} = $index_subfield;
512 return \%subfield_data;
516 =item format_indicator
518 Translate indicator value for output form - specifically, map
519 indicator = ' ' to ''. This is for the convenience of a cataloger
520 using a mouse to select an indicator input.
522 =cut
524 sub format_indicator {
525 my $ind_value = shift;
526 return '' if not defined $ind_value;
527 return '' if $ind_value eq ' ';
528 return $ind_value;
531 sub build_tabs ($$$$$) {
532 my ( $template, $record, $dbh, $encoding,$input ) = @_;
534 # fill arrays
535 my @loop_data = ();
536 my $tag;
538 my $authorised_values_sth = $dbh->prepare(
539 "select authorised_value,lib
540 from authorised_values
541 where category=? order by lib"
544 # in this array, we will push all the 10 tabs
545 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
546 my @BIG_LOOP;
547 my %seen;
548 my @tab_data; # all tags to display
550 foreach my $used ( @$usedTagsLib ){
551 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
552 $seen{$used->{tagfield}}++;
555 my $max_num_tab=-1;
556 foreach(@$usedTagsLib){
557 if($_->{tab} > -1 && $_->{tab} >= $max_num_tab && $_->{tagfield} != '995'){ # FIXME : MARC21 ?
558 $max_num_tab = $_->{tab};
561 if($max_num_tab >= 9){
562 $max_num_tab = 9;
564 # loop through each tab 0 through 9
565 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
566 my @loop_data = (); #innerloop in the template.
567 my $i = 0;
568 foreach my $tag (@tab_data) {
569 $i++;
570 next if ! $tag;
571 my ($indicator1, $indicator2);
572 my $index_tag = CreateKey;
574 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
575 # if MARC::Record is empty => use tab as master loop.
576 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
577 my @fields;
578 if ( $tag ne '000' ) {
579 @fields = $record->field($tag);
581 else {
582 push @fields, $record->leader(); # if tag == 000
584 # loop through each field
585 foreach my $field (@fields) {
587 my @subfields_data;
588 if ( $tag < 10 ) {
589 my ( $value, $subfield );
590 if ( $tag ne '000' ) {
591 $value = $field->data();
592 $subfield = "@";
594 else {
595 $value = $field;
596 $subfield = '@';
598 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
599 next
600 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
601 'biblio.biblionumber' );
602 push(
603 @subfields_data,
604 &create_input(
605 $tag, $subfield, $value, $index_tag, $tabloop, $record,
606 $authorised_values_sth,$input
610 else {
611 my @subfields = $field->subfields();
612 foreach my $subfieldcount ( 0 .. $#subfields ) {
613 my $subfield = $subfields[$subfieldcount][0];
614 my $value = $subfields[$subfieldcount][1];
615 next if ( length $subfield != 1 );
616 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
617 push(
618 @subfields_data,
619 &create_input(
620 $tag, $subfield, $value, $index_tag, $tabloop,
621 $record, $authorised_values_sth,$input
627 # now, loop again to add parameter subfield that are not in the MARC::Record
628 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
630 next if ( length $subfield != 1 );
631 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
632 next if ( $tag < 10 );
633 next
634 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
635 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
636 and not ( $subfield eq "9" and
637 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
638 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
639 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
641 ; #check for visibility flag
642 # if subfield is $9 in a field whose $a is authority-controlled,
643 # always include in the form regardless of the hidden setting - bug 2206
644 next if ( defined( $field->subfield($subfield) ) );
645 push(
646 @subfields_data,
647 &create_input(
648 $tag, $subfield, '', $index_tag, $tabloop, $record,
649 $authorised_values_sth,$input
653 if ( $#subfields_data >= 0 ) {
654 # build the tag entry.
655 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
656 # have twice the same "name" value, and cgi->param() will return only one, making
657 # all subfields to be merged in a single field.
658 my %tag_data = (
659 tag => $tag,
660 index => $index_tag,
661 tag_lib => $tagslib->{$tag}->{lib},
662 repeatable => $tagslib->{$tag}->{repeatable},
663 mandatory => $tagslib->{$tag}->{mandatory},
664 subfield_loop => \@subfields_data,
665 fixedfield => $tag < 10?1:0,
666 random => CreateKey,
668 if ($tag >= 10){ # no indicator for 00x tags
669 $tag_data{indicator1} = format_indicator($field->indicator(1)),
670 $tag_data{indicator2} = format_indicator($field->indicator(2)),
672 push( @loop_data, \%tag_data );
674 } # foreach $field end
676 # if breeding is empty
678 else {
679 my @subfields_data;
680 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
681 next if ( length $subfield != 1 );
682 next
683 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
684 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
685 and not ( $subfield eq "9" and
686 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
687 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
688 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
690 ; #check for visibility flag
691 # if subfield is $9 in a field whose $a is authority-controlled,
692 # always include in the form regardless of the hidden setting - bug 2206
693 next
694 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
695 push(
696 @subfields_data,
697 &create_input(
698 $tag, $subfield, '', $index_tag, $tabloop, $record,
699 $authorised_values_sth,$input
703 if ( $#subfields_data >= 0 ) {
704 my %tag_data = (
705 tag => $tag,
706 index => $index_tag,
707 tag_lib => $tagslib->{$tag}->{lib},
708 repeatable => $tagslib->{$tag}->{repeatable},
709 mandatory => $tagslib->{$tag}->{mandatory},
710 indicator1 => $indicator1,
711 indicator2 => $indicator2,
712 subfield_loop => \@subfields_data,
713 tagfirstsubfield => $subfields_data[0],
714 fixedfield => $tag < 10?1:0,
717 push @loop_data, \%tag_data ;
721 if ( $#loop_data >= 0 ) {
722 push @BIG_LOOP, {
723 number => $tabloop,
724 innerloop => \@loop_data,
728 $template->param( BIG_LOOP => \@BIG_LOOP );
732 # sub that tries to find authorities linked to the biblio
733 # the sub :
734 # - search in the authority DB for the same authid (in $9 of the biblio)
735 # - search in the authority DB for the same 001 (in $3 of the biblio in UNIMARC)
736 # - search in the authority DB for the same values (exactly) (in all subfields of the biblio)
737 # if the authority is found, the biblio is modified accordingly to be connected to the authority.
738 # if the authority is not found, it's added, and the biblio is then modified to be connected to the authority.
741 sub BiblioAddAuthorities{
742 my ( $record, $frameworkcode ) = @_;
743 my $dbh=C4::Context->dbh;
744 my $query=$dbh->prepare(qq|
745 SELECT authtypecode,tagfield
746 FROM marc_subfield_structure
747 WHERE frameworkcode=?
748 AND (authtypecode IS NOT NULL AND authtypecode<>\"\")|);
749 # SELECT authtypecode,tagfield
750 # FROM marc_subfield_structure
751 # WHERE frameworkcode=?
752 # AND (authtypecode IS NOT NULL OR authtypecode<>\"\")|);
753 $query->execute($frameworkcode);
754 my ($countcreated,$countlinked);
755 while (my $data=$query->fetchrow_hashref){
756 foreach my $field ($record->field($data->{tagfield})){
757 next if ($field->subfield('3')||$field->subfield('9'));
758 # No authorities id in the tag.
759 # Search if there is any authorities to link to.
760 my $query='at='.$data->{authtypecode}.' ';
761 map {$query.= ' and he,ext="'.$_->[1].'"' if ($_->[0]=~/[A-z]/)} $field->subfields();
762 my ($error, $results, $total_hits)=SimpleSearch( $query, undef, undef, [ "authorityserver" ] );
763 # there is only 1 result
764 if ( $error ) {
765 warn "BIBLIOADDSAUTHORITIES: $error";
766 return (0,0) ;
768 if ($results && scalar(@$results)==1) {
769 my $marcrecord = MARC::File::USMARC::decode($results->[0]);
770 $field->add_subfields('9'=>$marcrecord->field('001')->data);
771 $countlinked++;
772 } elsif (scalar(@$results)>1) {
773 #More than One result
774 #This can comes out of a lack of a subfield.
775 # my $marcrecord = MARC::File::USMARC::decode($results->[0]);
776 # $record->field($data->{tagfield})->add_subfields('9'=>$marcrecord->field('001')->data);
777 $countlinked++;
778 } else {
779 #There are no results, build authority record, add it to Authorities, get authid and add it to 9
780 ###NOTICE : This is only valid if a subfield is linked to one and only one authtypecode
781 ###NOTICE : This can be a problem. We should also look into other types and rejected forms.
782 my $authtypedata=GetAuthType($data->{authtypecode});
783 next unless $authtypedata;
784 my $marcrecordauth=MARC::Record->new();
785 if (C4::Context->preference('marcflavour') eq 'MARC21') {
786 $marcrecordauth->leader(' nz a22 o 4500');
787 SetMarcUnicodeFlag($marcrecordauth, 'MARC21');
789 my $authfield=MARC::Field->new($authtypedata->{auth_tag_to_report},'','',"a"=>"".$field->subfield('a'));
790 map { $authfield->add_subfields($_->[0]=>$_->[1]) if ($_->[0]=~/[A-z]/ && $_->[0] ne "a" )} $field->subfields();
791 $marcrecordauth->insert_fields_ordered($authfield);
793 # bug 2317: ensure new authority knows it's using UTF-8; currently
794 # only need to do this for MARC21, as MARC::Record->as_xml_record() handles
795 # automatically for UNIMARC (by not transcoding)
796 # FIXME: AddAuthority() instead should simply explicitly require that the MARC::Record
797 # use UTF-8, but as of 2008-08-05, did not want to introduce that kind
798 # of change to a core API just before the 3.0 release.
800 if (C4::Context->preference('marcflavour') eq 'MARC21') {
801 $marcrecordauth->insert_fields_ordered(MARC::Field->new('667','','','a'=>"Machine generated authority record."));
802 my $cite = $record->author() . ", " . $record->title_proper() . ", " . $record->publication_date() . " ";
803 $cite =~ s/^[\s\,]*//;
804 $cite =~ s/[\s\,]*$//;
805 $cite = "Work cat.: (" . C4::Context->preference('MARCOrgCode') . ")". $record->subfield('999','c') . ": " . $cite;
806 $marcrecordauth->insert_fields_ordered(MARC::Field->new('670','','','a'=>$cite));
809 # warn "AUTH RECORD ADDED : ".$marcrecordauth->as_formatted;
811 my $authid=AddAuthority($marcrecordauth,'',$data->{authtypecode});
812 $countcreated++;
813 $field->add_subfields('9'=>$authid);
817 return ($countlinked,$countcreated);
820 # ========================
821 # MAIN
822 #=========================
823 my $input = new CGI;
824 my $error = $input->param('error');
825 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
826 my $breedingid = $input->param('breedingid');
827 my $z3950 = $input->param('z3950');
828 my $op = $input->param('op');
829 my $mode = $input->param('mode');
830 my $frameworkcode = $input->param('frameworkcode');
831 my $dbh = C4::Context->dbh;
833 my $userflags = ($frameworkcode eq 'FA') ? "fast_cataloging" : "edit_catalogue";
835 $frameworkcode = &GetFrameworkCode($biblionumber)
836 if ( $biblionumber and not($frameworkcode) );
838 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
839 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
841 template_name => "cataloguing/addbiblio.tmpl",
842 query => $input,
843 type => "intranet",
844 authnotrequired => 0,
845 flagsrequired => { editcatalogue => $userflags },
849 # Getting the list of all frameworks
850 # get framework list
851 my $frameworks = getframeworks;
852 my @frameworkcodeloop;
853 foreach my $thisframeworkcode ( keys %$frameworks ) {
854 my %row = (
855 value => $thisframeworkcode,
856 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
858 if ($frameworkcode eq $thisframeworkcode){
859 $row{'selected'}="selected=\"selected\"";
861 push @frameworkcodeloop, \%row;
863 $template->param( frameworkcodeloop => \@frameworkcodeloop,
864 breedingid => $breedingid );
866 # ++ Global
867 $tagslib = &GetMarcStructure( 1, $frameworkcode );
868 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
869 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
870 # -- Global
872 my $record = -1;
873 my $encoding = "";
874 my (
875 $biblionumbertagfield,
876 $biblionumbertagsubfield,
877 $biblioitemnumtagfield,
878 $biblioitemnumtagsubfield,
879 $bibitem,
880 $biblioitemnumber
883 if (($biblionumber) && !($breedingid)){
884 $record = GetMarcBiblio($biblionumber);
886 if ($breedingid) {
887 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
890 $is_a_modif = 0;
892 if ($biblionumber) {
893 $is_a_modif = 1;
894 $template->param( title => $record->title(), );
896 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
897 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
898 &GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
899 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
900 &GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
902 # search biblioitems value
903 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
904 $sth->execute($biblionumber);
905 ($biblioitemnumber) = $sth->fetchrow;
908 #-------------------------------------------------------------------------------------
909 if ( $op eq "addbiblio" ) {
910 #-------------------------------------------------------------------------------------
911 # getting html input
912 my @params = $input->param();
913 $record = TransformHtmlToMarc( \@params , $input );
914 # check for a duplicate
915 my ($duplicatebiblionumber,$duplicatetitle) = FindDuplicate($record) if (!$is_a_modif);
916 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
917 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
918 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
919 my $oldbibnum;
920 my $oldbibitemnum;
921 if (C4::Context->preference("BiblioAddsAuthorities")){
922 my ($countlinked,$countcreated)=BiblioAddAuthorities($record,$frameworkcode);
924 if ( $is_a_modif ) {
925 ModBiblioframework( $biblionumber, $frameworkcode );
926 ModBiblio( $record, $biblionumber, $frameworkcode );
928 else {
929 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
932 if ($mode ne "popup" && !$is_a_modif){
933 print $input->redirect(
934 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode"
936 exit;
938 elsif($is_a_modif){
939 print $input->redirect(
940 "/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode"
942 exit;
944 }else {
945 $template->param(
946 biblionumber => $biblionumber,
947 done =>1,
948 popup =>1
950 $template->param( title => $record->subfield('200',"a") ) if ($record ne "-1" && C4::Context->preference('marcflavour') =~/unimarc/i);
951 $template->param( title => $record->title() ) if ($record ne "-1" && C4::Context->preference('marcflavour') eq "usmarc");
952 $template->param(
953 popup => $mode,
954 itemtype => $frameworkcode,
956 output_html_with_http_headers $input, $cookie, $template->output;
957 exit;
959 } else {
960 # it may be a duplicate, warn the user and do nothing
961 build_tabs ($template, $record, $dbh,$encoding,$input);
962 $template->param(
963 biblionumber => $biblionumber,
964 biblioitemnumber => $biblioitemnumber,
965 duplicatebiblionumber => $duplicatebiblionumber,
966 duplicatebibid => $duplicatebiblionumber,
967 duplicatetitle => $duplicatetitle,
971 elsif ( $op eq "delete" ) {
973 my $error = &DelBiblio($biblionumber);
974 if ($error) {
975 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
976 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
977 exit;
980 print $input->redirect('/cgi-bin/koha/catalogue/search.pl');
981 exit;
983 } else {
984 #----------------------------------------------------------------------------
985 # If we're in a duplication case, we have to set to "" the biblionumber
986 # as we'll save the biblio as a new one.
987 if ( $op eq "duplicate" ) {
988 $biblionumber = "";
991 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
992 eval {
993 my $uxml = $record->as_xml;
994 MARC::Record::default_record_format("UNIMARC")
995 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
996 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
997 $record = $urecord;
999 build_tabs( $template, $record, $dbh, $encoding,$input );
1000 $template->param(
1001 biblionumber => $biblionumber,
1002 biblionumbertagfield => $biblionumbertagfield,
1003 biblionumbertagsubfield => $biblionumbertagsubfield,
1004 biblioitemnumtagfield => $biblioitemnumtagfield,
1005 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
1006 biblioitemnumber => $biblioitemnumber,
1010 $template->param( title => $record->title() ) if ( $record ne "-1" );
1011 $template->param(
1012 popup => $mode,
1013 frameworkcode => $frameworkcode,
1014 itemtype => $frameworkcode,
1017 output_html_with_http_headers $input, $cookie, $template->output;