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
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
27 use C4::AuthoritiesMarc;
31 use C4::Koha; # XXX subfield_is_koha_internal_p
32 use C4::Branch; # XXX subfield_is_koha_internal_p
36 use Date::Calc qw(Today);
37 use MARC::File::USMARC;
40 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
41 MARC::File::XML->default_record_format('UNIMARC');
44 our($tagslib,$authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
46 =item MARCfindbreeding
48 $record = MARCfindbreeding($breedingid);
50 Look up the import record repository for the record with
51 record with id $breedingid. If found, returns the decoded
52 MARC::Record; otherwise, -1 is returned (FIXME).
53 Returns as second parameter the character encoding.
57 sub MARCfindbreeding {
59 my ($marc, $encoding) = GetImportRecordMarc($id);
60 # remove the - in isbn, koha store isbn without any -
62 my $record = MARC::Record->new_from_usmarc($marc);
63 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
64 if ( $record->field($isbnfield) ) {
65 foreach my $field ( $record->field($isbnfield) ) {
66 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
67 my $newisbn = $field->subfield($isbnsubfield);
69 $field->update( $isbnsubfield => $newisbn );
73 # fix the unimarc 100 coded field (with unicode information)
74 if (C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield(100,'a')) {
75 my $f100a=$record->subfield(100,'a');
76 my $f100 = $record->field(100);
77 my $f100temp = $f100->as_string;
78 $record->delete_field($f100);
79 if ( length($f100temp) > 28 ) {
80 substr( $f100temp, 26, 2, "50" );
81 $f100->update( 'a' => $f100temp );
82 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
83 $record->insert_fields_ordered($f100);
87 if ( ref($record) eq undef ) {
91 # normalize author : probably UNIMARC specific...
92 if ( C4::Context->preference("z3950NormalizeAuthor")
93 and C4::Context->preference("z3950AuthorAuthFields") )
95 my ( $tag, $subfield ) = GetMarcFromKohaField("biblio.author");
97 # my $summary = C4::Context->preference("z3950authortemplate");
99 C4::Context->preference("z3950AuthorAuthFields");
100 my @auth_fields = split /,/, $auth_fields;
103 if ( $record->field($tag) ) {
104 foreach my $tmpfield ( $record->field($tag)->subfields ) {
106 # foreach my $subfieldcode ($tmpfield->subfields){
107 my $subfieldcode = shift @$tmpfield;
108 my $subfieldvalue = shift @$tmpfield;
110 $field->add_subfields(
111 "$subfieldcode" => $subfieldvalue )
112 if ( $subfieldcode ne $subfield );
116 MARC::Field->new( $tag, "", "",
117 $subfieldcode => $subfieldvalue )
118 if ( $subfieldcode ne $subfield );
122 $record->delete_field( $record->field($tag) );
123 foreach my $fieldtag (@auth_fields) {
124 next unless ( $record->field($fieldtag) );
125 my $lastname = $record->field($fieldtag)->subfield('a');
126 my $firstname = $record->field($fieldtag)->subfield('b');
127 my $title = $record->field($fieldtag)->subfield('c');
128 my $number = $record->field($fieldtag)->subfield('d');
131 # $field->add_subfields("$subfield"=>"[ ".ucfirst($title).ucfirst($firstname)." ".$number." ]");
132 $field->add_subfields(
133 "$subfield" => ucfirst($title) . " "
134 . ucfirst($firstname) . " "
139 # $field->add_subfields("$subfield"=>"[ ".ucfirst($firstname).", ".ucfirst($lastname)." ]");
140 $field->add_subfields(
141 "$subfield" => ucfirst($firstname) . ", "
142 . ucfirst($lastname) );
145 $record->insert_fields_ordered($field);
147 return $record, $encoding;
153 =item build_authorized_values_list
157 sub build_authorized_values_list ($$$$$$$) {
158 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
160 my @authorised_values;
163 # builds list, depending on authorised value...
166 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
167 #Use GetBranches($onlymine)
168 my $onlymine=C4::Context->preference('IndependantBranches') &&
169 C4::Context->userenv &&
170 C4::Context->userenv->{flags}!=1 &&
171 C4::Context->userenv->{branch};
172 my $branches = GetBranches($onlymine);
174 foreach my $thisbranch ( sort keys %$branches ) {
175 push @authorised_values, $thisbranch;
176 $authorised_lib{$thisbranch} = $branches->{$thisbranch}->{'branchname'};
181 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
184 "select itemtype,description from itemtypes order by description");
186 push @authorised_values, ""
187 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
191 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
192 push @authorised_values, $itemtype;
193 $authorised_lib{$itemtype} = $description;
195 $value = $itemtype unless ($value);
199 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
200 push @authorised_values, ""
201 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
203 my $class_sources = GetClassSources();
205 my $default_source = C4::Context->preference("DefaultClassificationSource");
207 foreach my $class_source (sort keys %$class_sources) {
208 next unless $class_sources->{$class_source}->{'used'} or
209 ($value and $class_source eq $value) or
210 ($class_source eq $default_source);
211 push @authorised_values, $class_source;
212 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
213 $value = $class_source unless ($value);
214 $value = $default_source unless ($value);
216 #---- "true" authorised value
219 $authorised_values_sth->execute(
220 $tagslib->{$tag}->{$subfield}->{authorised_value} );
222 push @authorised_values, ""
223 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
225 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
226 push @authorised_values, $value;
227 $authorised_lib{$value} = $lib;
230 return CGI::scrolling_list(
231 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
232 -values => \@authorised_values,
234 -labels => \%authorised_lib,
239 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
240 -class => "input_marceditor",
246 Create a random value to set it into the input name
251 return int(rand(1000000));
254 =item GetMandatoryFieldZ3950
256 This function return an hashref which containts all mandatory field
257 to search with z3950 server.
261 sub GetMandatoryFieldZ3950($){
262 my $frameworkcode = shift;
263 my @isbn = GetMarcFromKohaField('biblioitems.isbn',$frameworkcode);
264 my @title = GetMarcFromKohaField('biblio.title',$frameworkcode);
265 my @author = GetMarcFromKohaField('biblio.author',$frameworkcode);
266 my @issn = GetMarcFromKohaField('biblioitems.issn',$frameworkcode);
267 my @lccn = GetMarcFromKohaField('biblioitems.lccn',$frameworkcode);
270 $isbn[0].$isbn[1] => 'isbn',
271 $title[0].$title[1] => 'title',
272 $author[0].$author[1] => 'author',
273 $issn[0].$issn[1] => 'issn',
274 $lccn[0].$lccn[1] => 'lccn',
280 builds the <input ...> entry for a subfield.
285 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
287 my $index_subfield = CreateKey(); # create a specifique key for each subfield
289 $value =~ s/"/"/g;
291 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
292 my $max_length = 9999;
295 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
299 # if there is no value provided but a default value in parameters, get it
301 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
303 # get today date & replace YYYY, MM, DD if provided in the default value
304 my ( $year, $month, $day ) = Today();
305 $month = sprintf( "%02d", $month );
306 $day = sprintf( "%02d", $day );
307 $value =~ s/YYYY/$year/g;
308 $value =~ s/MM/$month/g;
309 $value =~ s/DD/$day/g;
311 my $dbh = C4::Context->dbh;
313 # map '@' as "subfield" label for fixed fields
314 # to something that's allowed in a div id.
315 my $id_subfield = $subfield;
316 $id_subfield = "00" if $id_subfield eq "@";
318 my %subfield_data = (
320 subfield => $id_subfield,
321 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
322 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
323 tag_mandatory => $tagslib->{$tag}->{mandatory},
324 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
325 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
326 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
328 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
330 random => CreateKey(),
333 if(exists $mandatory_z3950->{$tag.$subfield}){
334 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
336 # decide if the subfield must be expanded (visible) by default or not
337 # if it is mandatory, then expand. If it is hidden explicitly by the hidden flag, hidden anyway
338 $subfield_data{visibility} = "display:none;"
339 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
340 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
342 # always expand all subfields of a mandatory field
343 $subfield_data{visibility} = "" if $tagslib->{$tag}->{mandatory};
344 # it's an authorised field
345 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
346 $subfield_data{marc_value} =
347 build_authorized_values_list( $tag, $subfield, $value, $dbh,
348 $authorised_values_sth,$index_tag,$index_subfield );
350 # it's a subfield $9 linking to an authority record - see bug 2206
352 elsif ($subfield eq "9" and
353 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
354 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
355 $tagslib->{$tag}->{'a'}->{authtypecode} ne '') {
357 $subfield_data{marc_value} =
358 "<input type=\"text\"
359 id=\"".$subfield_data{id}."\"
360 name=\"".$subfield_data{id}."\"
362 class=\"input_marceditor\"
365 maxlength=\"$max_length\"
366 readonly=\"readonly\"
369 # it's a thesaurus / authority field
371 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
372 if (C4::Context->preference("BiblioAddsAuthorities")) {
373 $subfield_data{marc_value} =
374 "<input type=\"text\"
375 id=\"".$subfield_data{id}."\"
376 name=\"".$subfield_data{id}."\"
378 class=\"input_marceditor\"
381 maxlength=\"$max_length\"
383 <a href=\"#\" class=\"buttonDot\"
384 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
387 $subfield_data{marc_value} =
388 "<input type=\"text\"
389 id=\"".$subfield_data{id}."\"
390 name=\"".$subfield_data{id}."\"
392 class=\"input_marceditor\"
395 maxlength=\"$max_length\"
396 readonly=\"readonly\"
398 <a href=\"#\" class=\"buttonDot\"
399 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
402 # it's a plugin field
404 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
406 # opening plugin. Just check wether we are on a developper computer on a production one
407 # (the cgidir differs)
408 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
409 unless ( opendir( DIR, "$cgidir" ) ) {
410 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
413 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
415 my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
416 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
418 $subfield_data{marc_value} =
419 "<input tabindex=\"1\"
421 id=\"".$subfield_data{id}."\"
422 name=\"".$subfield_data{id}."\"
424 class=\"input_marceditor\"
425 onfocus=\"Focus$function_name($index_tag)\"
427 maxlength=\"$max_length\"
428 onblur=\"Blur$function_name($index_tag); \" \/>
429 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
432 warn "Plugin Failed: $plugin";
433 # supply default input form
434 $subfield_data{marc_value} =
435 "<input type=\"text\"
436 id=\"".$subfield_data{id}."\"
437 name=\"".$subfield_data{id}."\"
441 maxlength=\"$max_length\"
442 class=\"input_marceditor\"
446 # it's an hidden field
448 elsif ( $tag eq '' ) {
449 $subfield_data{marc_value} =
450 "<input tabindex=\"1\"
452 id=\"".$subfield_data{id}."\"
453 name=\"".$subfield_data{id}."\"
455 maxlength=\"$max_length\"
459 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
460 $subfield_data{marc_value} =
461 "<input type=\"text\"
462 id=\"".$subfield_data{id}."\"
463 name=\"".$subfield_data{id}."\"
464 class=\"input_marceditor\"
467 maxlength=\"$max_length\"
471 # it's a standard field
477 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
478 and $tag < 400 && $subfield eq 'a' )
481 && C4::Context->preference("marcflavour") eq "MARC21" )
484 $subfield_data{marc_value} =
485 "<textarea cols=\"70\"
487 id=\"".$subfield_data{id}."\"
488 name=\"".$subfield_data{id}."\"
489 class=\"input_marceditor\"
495 $subfield_data{marc_value} =
496 "<input type=\"text\"
497 id=\"".$subfield_data{id}."\"
498 name=\"".$subfield_data{id}."\"
502 maxlength=\"$max_length\"
503 class=\"input_marceditor\"
508 $subfield_data{'index_subfield'} = $index_subfield;
509 return \%subfield_data;
513 =item format_indicator
515 Translate indicator value for output form - specifically, map
516 indicator = ' ' to ''. This is for the convenience of a cataloger
517 using a mouse to select an indicator input.
521 sub format_indicator {
522 my $ind_value = shift;
523 return '' if not defined $ind_value;
524 return '' if $ind_value eq ' ';
528 sub build_tabs ($$$$$) {
529 my ( $template, $record, $dbh, $encoding,$input ) = @_;
535 my $authorised_values_sth = $dbh->prepare(
536 "select authorised_value,lib
537 from authorised_values
538 where category=? order by lib"
541 # in this array, we will push all the 10 tabs
542 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
545 my @tab_data; # all tags to display
547 foreach my $used ( @$usedTagsLib ){
548 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
549 $seen{$used->{tagfield}}++;
553 foreach(@$usedTagsLib){
554 if($_->{tab} > -1 && $_->{tab} >= $max_num_tab && $_->{tagfield} != '995'){ # FIXME : MARC21 ?
555 $max_num_tab = $_->{tab};
558 if($max_num_tab >= 9){
561 # loop through each tab 0 through 9
562 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
563 my @loop_data = (); #innerloop in the template.
565 foreach my $tag (@tab_data) {
568 my ($indicator1, $indicator2);
569 my $index_tag = CreateKey;
571 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
572 # if MARC::Record is empty => use tab as master loop.
573 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
575 if ( $tag ne '000' ) {
576 @fields = $record->field($tag);
579 push @fields, $record->leader(); # if tag == 000
581 # loop through each field
582 foreach my $field (@fields) {
586 my ( $value, $subfield );
587 if ( $tag ne '000' ) {
588 $value = $field->data();
595 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
597 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
598 'biblio.biblionumber' );
602 $tag, $subfield, $value, $index_tag, $tabloop, $record,
603 $authorised_values_sth,$input
608 my @subfields = $field->subfields();
609 foreach my $subfieldcount ( 0 .. $#subfields ) {
610 my $subfield = $subfields[$subfieldcount][0];
611 my $value = $subfields[$subfieldcount][1];
612 next if ( length $subfield != 1 );
613 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
617 $tag, $subfield, $value, $index_tag, $tabloop,
618 $record, $authorised_values_sth,$input
624 # now, loop again to add parameter subfield that are not in the MARC::Record
625 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
627 next if ( length $subfield != 1 );
628 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
629 next if ( $tag < 10 );
631 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
632 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
633 and not ( $subfield eq "9" and
634 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
635 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
636 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
638 ; #check for visibility flag
639 # if subfield is $9 in a field whose $a is authority-controlled,
640 # always include in the form regardless of the hidden setting - bug 2206
641 next if ( defined( $field->subfield($subfield) ) );
645 $tag, $subfield, '', $index_tag, $tabloop, $record,
646 $authorised_values_sth,$input
650 if ( $#subfields_data >= 0 ) {
651 # build the tag entry.
652 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
653 # have twice the same "name" value, and cgi->param() will return only one, making
654 # all subfields to be merged in a single field.
658 tag_lib => $tagslib->{$tag}->{lib},
659 repeatable => $tagslib->{$tag}->{repeatable},
660 mandatory => $tagslib->{$tag}->{mandatory},
661 subfield_loop => \@subfields_data,
662 fixedfield => $tag < 10?1:0,
665 if ($tag >= 010){ # no indicator for theses tag
666 $tag_data{indicator1} = format_indicator($field->indicator(1)),
667 $tag_data{indicator2} = format_indicator($field->indicator(2)),
669 push( @loop_data, \%tag_data );
671 } # foreach $field end
673 # if breeding is empty
677 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
678 next if ( length $subfield != 1 );
680 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
681 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
682 and not ( $subfield eq "9" and
683 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
684 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
685 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
687 ; #check for visibility flag
688 # if subfield is $9 in a field whose $a is authority-controlled,
689 # always include in the form regardless of the hidden setting - bug 2206
691 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
695 $tag, $subfield, '', $index_tag, $tabloop, $record,
696 $authorised_values_sth,$input
700 if ( $#subfields_data >= 0 ) {
704 tag_lib => $tagslib->{$tag}->{lib},
705 repeatable => $tagslib->{$tag}->{repeatable},
706 mandatory => $tagslib->{$tag}->{mandatory},
707 indicator1 => $indicator1,
708 indicator2 => $indicator2,
709 subfield_loop => \@subfields_data,
710 tagfirstsubfield => $subfields_data[0],
711 fixedfield => $tag < 10?1:0,
714 push @loop_data, \%tag_data ;
718 if ( $#loop_data >= 0 ) {
721 innerloop => \@loop_data,
725 $template->param( BIG_LOOP => \@BIG_LOOP );
729 # sub that tries to find authorities linked to the biblio
731 # - search in the authority DB for the same authid (in $9 of the biblio)
732 # - search in the authority DB for the same 001 (in $3 of the biblio in UNIMARC)
733 # - search in the authority DB for the same values (exactly) (in all subfields of the biblio)
734 # if the authority is found, the biblio is modified accordingly to be connected to the authority.
735 # if the authority is not found, it's added, and the biblio is then modified to be connected to the authority.
738 sub BiblioAddAuthorities{
739 my ( $record, $frameworkcode ) = @_;
740 my $dbh=C4::Context->dbh;
741 my $query=$dbh->prepare(qq|
742 SELECT authtypecode,tagfield
743 FROM marc_subfield_structure
744 WHERE frameworkcode=?
745 AND (authtypecode IS NOT NULL AND authtypecode<>\"\")|);
746 # SELECT authtypecode,tagfield
747 # FROM marc_subfield_structure
748 # WHERE frameworkcode=?
749 # AND (authtypecode IS NOT NULL OR authtypecode<>\"\")|);
750 $query->execute($frameworkcode);
751 my ($countcreated,$countlinked);
752 while (my $data=$query->fetchrow_hashref){
753 foreach my $field ($record->field($data->{tagfield})){
754 next if ($field->subfield('3')||$field->subfield('9'));
755 # No authorities id in the tag.
756 # Search if there is any authorities to link to.
757 my $query='at='.$data->{authtypecode}.' ';
758 map {$query.= ' and he,ext="'.$_->[1].'"' if ($_->[0]=~/[A-z]/)} $field->subfields();
759 my ($error, $results, $total_hits)=SimpleSearch( $query, undef, undef, [ "authorityserver" ] );
760 # there is only 1 result
762 warn "BIBLIOADDSAUTHORITIES: $error";
765 if ($results && scalar(@$results)==1) {
766 my $marcrecord = MARC::File::USMARC::decode($results->[0]);
767 $field->add_subfields('9'=>$marcrecord->field('001')->data);
769 } elsif (scalar(@$results)>1) {
770 #More than One result
771 #This can comes out of a lack of a subfield.
772 # my $marcrecord = MARC::File::USMARC::decode($results->[0]);
773 # $record->field($data->{tagfield})->add_subfields('9'=>$marcrecord->field('001')->data);
776 #There are no results, build authority record, add it to Authorities, get authid and add it to 9
777 ###NOTICE : This is only valid if a subfield is linked to one and only one authtypecode
778 ###NOTICE : This can be a problem. We should also look into other types and rejected forms.
779 my $authtypedata=GetAuthType($data->{authtypecode});
780 next unless $authtypedata;
781 my $marcrecordauth=MARC::Record->new();
782 my $authfield=MARC::Field->new($authtypedata->{auth_tag_to_report},'','',"a"=>"".$field->subfield('a'));
783 map { $authfield->add_subfields($_->[0]=>$_->[1]) if ($_->[0]=~/[A-z]/ && $_->[0] ne "a" )} $field->subfields();
784 $marcrecordauth->insert_fields_ordered($authfield);
785 # warn "AUTH RECORD ADDED : ".$marcrecordauth->as_formatted;
786 my $authid=AddAuthority($marcrecordauth,'',$data->{authtypecode});
788 $field->add_subfields('9'=>$authid);
792 return ($countlinked,$countcreated);
795 # ========================
797 #=========================
799 my $error = $input->param('error');
800 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
801 my $breedingid = $input->param('breedingid');
802 my $z3950 = $input->param('z3950');
803 my $op = $input->param('op');
804 my $mode = $input->param('mode');
805 my $frameworkcode = $input->param('frameworkcode');
806 my $dbh = C4::Context->dbh;
808 $frameworkcode = &GetFrameworkCode($biblionumber)
809 if ( $biblionumber and not($frameworkcode) );
811 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
812 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
814 template_name => "cataloguing/addbiblio.tmpl",
817 authnotrequired => 0,
818 flagsrequired => { editcatalogue => 1 },
822 # Getting the list of all frameworks
824 my $frameworks = getframeworks;
825 my @frameworkcodeloop;
826 foreach my $thisframeworkcode ( keys %$frameworks ) {
828 value => $thisframeworkcode,
829 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
831 if ($frameworkcode eq $thisframeworkcode){
832 $row{'selected'}="selected=\"selected\"";
834 push @frameworkcodeloop, \%row;
836 $template->param( frameworkcodeloop => \@frameworkcodeloop,
837 breedingid => $breedingid );
840 $tagslib = &GetMarcStructure( 1, $frameworkcode );
841 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
842 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
848 $biblionumbertagfield,
849 $biblionumbertagsubfield,
850 $biblioitemnumtagfield,
851 $biblioitemnumtagsubfield,
856 if (($biblionumber) && !($breedingid)){
857 $record = GetMarcBiblio($biblionumber);
860 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
867 $template->param( title => $record->title(), );
869 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
870 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
871 &GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
872 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
873 &GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
875 # search biblioitems value
876 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
877 $sth->execute($biblionumber);
878 ($biblioitemnumber) = $sth->fetchrow;
881 #-------------------------------------------------------------------------------------
882 if ( $op eq "addbiblio" ) {
883 #-------------------------------------------------------------------------------------
885 my @params = $input->param();
886 $record = TransformHtmlToMarc( \@params , $input );
887 # check for a duplicate
888 my ($duplicatebiblionumber,$duplicatetitle) = FindDuplicate($record) if (!$is_a_modif);
889 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
890 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
891 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
894 if (C4::Context->preference("BiblioAddsAuthorities")){
895 my ($countlinked,$countcreated)=BiblioAddAuthorities($record,$frameworkcode);
898 ModBiblioframework( $biblionumber, $frameworkcode );
899 ModBiblio( $record, $biblionumber, $frameworkcode );
902 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
905 if ($mode ne "popup"){
906 print $input->redirect(
907 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode"
912 biblionumber => $biblionumber,
916 $template->param( title => $record->subfield('200',"a") ) if ($record ne "-1" && C4::Context->preference('marcflavour') =~/unimarc/i);
917 $template->param( title => $record->title() ) if ($record ne "-1" && C4::Context->preference('marcflavour') eq "usmarc");
920 itemtype => $frameworkcode,
922 output_html_with_http_headers $input, $cookie, $template->output;
926 # it may be a duplicate, warn the user and do nothing
927 build_tabs ($template, $record, $dbh,$encoding,$input);
929 biblionumber => $biblionumber,
930 biblioitemnumber => $biblioitemnumber,
931 duplicatebiblionumber => $duplicatebiblionumber,
932 duplicatebibid => $duplicatebiblionumber,
933 duplicatetitle => $duplicatetitle,
937 elsif ( $op eq "delete" ) {
939 my $error = &DelBiblio($biblionumber);
941 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
942 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
946 print $input->redirect('/cgi-bin/koha/catalogue/search.pl');
950 #----------------------------------------------------------------------------
951 # If we're in a duplication case, we have to set to "" the biblionumber
952 # as we'll save the biblio as a new one.
953 if ( $op eq "duplicate" ) {
957 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
959 my $uxml = $record->as_xml;
960 MARC::Record::default_record_format("UNIMARC")
961 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
962 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
965 build_tabs( $template, $record, $dbh, $encoding,$input );
967 biblionumber => $biblionumber,
968 biblionumbertagfield => $biblionumbertagfield,
969 biblionumbertagsubfield => $biblionumbertagsubfield,
970 biblioitemnumtagfield => $biblioitemnumtagfield,
971 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
972 biblioitemnumber => $biblioitemnumber,
976 $template->param( title => $record->title() ) if ( $record ne "-1" );
979 frameworkcode => $frameworkcode,
980 itemtype => $frameworkcode,
983 output_html_with_http_headers $input, $cookie, $template->output;