4 # Copyright 2000-2002 Katipo Communications
5 # Copyright 2004-2010 BibLibre
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #use warnings; FIXME - Bug 2505
29 use C4::AuthoritiesMarc;
33 use C4::Koha; # XXX subfield_is_koha_internal_p
34 use C4::Branch; # XXX subfield_is_koha_internal_p
39 use Date::Calc qw(Today);
40 use MARC::File::USMARC;
43 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
44 MARC::File::XML->default_record_format('UNIMARC');
47 our($tagslib,$authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
51 =head2 MARCfindbreeding
53 $record = MARCfindbreeding($breedingid);
55 Look up the import record repository for the record with
56 record with id $breedingid. If found, returns the decoded
57 MARC::Record; otherwise, -1 is returned (FIXME).
58 Returns as second parameter the character encoding.
62 sub MARCfindbreeding {
64 my ($marc, $encoding) = GetImportRecordMarc($id);
65 # remove the - in isbn, koha store isbn without any -
67 my $record = MARC::Record->new_from_usmarc($marc);
68 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
69 if ( $record->field($isbnfield) ) {
70 foreach my $field ( $record->field($isbnfield) ) {
71 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
72 my $newisbn = $field->subfield($isbnsubfield);
74 $field->update( $isbnsubfield => $newisbn );
78 # fix the unimarc 100 coded field (with unicode information)
79 if (C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield(100,'a')) {
80 my $f100a=$record->subfield(100,'a');
81 my $f100 = $record->field(100);
82 my $f100temp = $f100->as_string;
83 $record->delete_field($f100);
84 if ( length($f100temp) > 28 ) {
85 substr( $f100temp, 26, 2, "50" );
86 $f100->update( 'a' => $f100temp );
87 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
88 $record->insert_fields_ordered($f100);
92 if ( !defined(ref($record)) ) {
96 # normalize author : probably UNIMARC specific...
97 if ( C4::Context->preference("z3950NormalizeAuthor")
98 and C4::Context->preference("z3950AuthorAuthFields") )
100 my ( $tag, $subfield ) = GetMarcFromKohaField("biblio.author", '');
102 # my $summary = C4::Context->preference("z3950authortemplate");
104 C4::Context->preference("z3950AuthorAuthFields");
105 my @auth_fields = split /,/, $auth_fields;
108 if ( $record->field($tag) ) {
109 foreach my $tmpfield ( $record->field($tag)->subfields ) {
111 # foreach my $subfieldcode ($tmpfield->subfields){
112 my $subfieldcode = shift @$tmpfield;
113 my $subfieldvalue = shift @$tmpfield;
115 $field->add_subfields(
116 "$subfieldcode" => $subfieldvalue )
117 if ( $subfieldcode ne $subfield );
121 MARC::Field->new( $tag, "", "",
122 $subfieldcode => $subfieldvalue )
123 if ( $subfieldcode ne $subfield );
127 $record->delete_field( $record->field($tag) );
128 foreach my $fieldtag (@auth_fields) {
129 next unless ( $record->field($fieldtag) );
130 my $lastname = $record->field($fieldtag)->subfield('a');
131 my $firstname = $record->field($fieldtag)->subfield('b');
132 my $title = $record->field($fieldtag)->subfield('c');
133 my $number = $record->field($fieldtag)->subfield('d');
136 # $field->add_subfields("$subfield"=>"[ ".ucfirst($title).ucfirst($firstname)." ".$number." ]");
137 $field->add_subfields(
138 "$subfield" => ucfirst($title) . " "
139 . ucfirst($firstname) . " "
144 # $field->add_subfields("$subfield"=>"[ ".ucfirst($firstname).", ".ucfirst($lastname)." ]");
145 $field->add_subfields(
146 "$subfield" => ucfirst($firstname) . ", "
147 . ucfirst($lastname) );
150 $record->insert_fields_ordered($field);
152 return $record, $encoding;
158 =head2 build_authorized_values_list
162 sub build_authorized_values_list {
163 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
165 my @authorised_values;
168 # builds list, depending on authorised value...
171 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
172 #Use GetBranches($onlymine)
173 my $onlymine=C4::Context->preference('IndependantBranches') &&
174 C4::Context->userenv &&
175 C4::Context->userenv->{flags} % 2 == 0 &&
176 C4::Context->userenv->{branch};
177 my $branches = GetBranches($onlymine);
179 foreach my $thisbranch ( sort keys %$branches ) {
180 push @authorised_values, $thisbranch;
181 $authorised_lib{$thisbranch} = $branches->{$thisbranch}->{'branchname'};
186 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
189 "select itemtype,description from itemtypes order by description");
191 push @authorised_values, ""
192 unless ( $tagslib->{$tag}->{$subfield}->{defaultvalue} and $tagslib->{$tag}->{$subfield}->{mandatory} );
196 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
197 push @authorised_values, $itemtype;
198 $authorised_lib{$itemtype} = $description;
200 $value = $itemtype unless ($value);
204 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
205 push @authorised_values, ""
206 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
208 my $class_sources = GetClassSources();
210 my $default_source = C4::Context->preference("DefaultClassificationSource");
212 foreach my $class_source (sort keys %$class_sources) {
213 next unless $class_sources->{$class_source}->{'used'} or
214 ($value and $class_source eq $value) or
215 ($class_source eq $default_source);
216 push @authorised_values, $class_source;
217 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
218 $value = $class_source unless ($value);
219 $value = $default_source unless ($value);
221 #---- "true" authorised value
224 $authorised_values_sth->execute(
225 $tagslib->{$tag}->{$subfield}->{authorised_value} );
227 push @authorised_values, ""
228 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
230 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
231 push @authorised_values, $value;
232 $authorised_lib{$value} = $lib;
235 return CGI::scrolling_list(
236 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
237 -values => \@authorised_values,
239 -labels => \%authorised_lib,
244 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
245 -class => "input_marceditor",
251 Create a random value to set it into the input name
256 return int(rand(1000000));
259 =head2 GetMandatoryFieldZ3950
261 This function return an hashref which containts all mandatory field
262 to search with z3950 server.
266 sub GetMandatoryFieldZ3950 {
267 my $frameworkcode = shift;
268 my @isbn = GetMarcFromKohaField('biblioitems.isbn',$frameworkcode);
269 my @title = GetMarcFromKohaField('biblio.title',$frameworkcode);
270 my @author = GetMarcFromKohaField('biblio.author',$frameworkcode);
271 my @issn = GetMarcFromKohaField('biblioitems.issn',$frameworkcode);
272 my @lccn = GetMarcFromKohaField('biblioitems.lccn',$frameworkcode);
275 $isbn[0].$isbn[1] => 'isbn',
276 $title[0].$title[1] => 'title',
277 $author[0].$author[1] => 'author',
278 $issn[0].$issn[1] => 'issn',
279 $lccn[0].$lccn[1] => 'lccn',
285 builds the <input ...> entry for a subfield.
290 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
292 my $index_subfield = CreateKey(); # create a specifique key for each subfield
294 $value =~ s/"/"/g;
296 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
297 my $max_length = 9999;
300 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
304 # if there is no value provided but a default value in parameters, get it
305 if ( $value eq '' ) {
306 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
308 # get today date & replace YYYY, MM, DD if provided in the default value
309 my ( $year, $month, $day ) = Today();
310 $month = sprintf( "%02d", $month );
311 $day = sprintf( "%02d", $day );
312 $value =~ s/YYYY/$year/g;
313 $value =~ s/MM/$month/g;
314 $value =~ s/DD/$day/g;
315 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
316 $value=~s/user/$username/g;
319 my $dbh = C4::Context->dbh;
321 # map '@' as "subfield" label for fixed fields
322 # to something that's allowed in a div id.
323 my $id_subfield = $subfield;
324 $id_subfield = "00" if $id_subfield eq "@";
326 my %subfield_data = (
328 subfield => $id_subfield,
329 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
330 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
331 tag_mandatory => $tagslib->{$tag}->{mandatory},
332 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
333 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
334 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
336 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
338 random => CreateKey(),
341 if(exists $mandatory_z3950->{$tag.$subfield}){
342 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
344 # Subfield is hidden depending of hidden and mandatory flag, and is always
345 # shown if it contains anything or if its field is mandatory.
346 my $tdef = $tagslib->{$tag};
347 $subfield_data{visibility} = "display:none;"
348 if $tdef->{$subfield}->{hidden} % 2 == 1 &&
350 !$tdef->{$subfield}->{mandatory} &&
352 # expand all subfields of 773 if there is a host item provided in the input
353 $subfield_data{visibility} ="" if ($tag eq 773 and $cgi->param('hostitemnumber'));
356 # it's an authorised field
357 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
358 $subfield_data{marc_value} =
359 build_authorized_values_list( $tag, $subfield, $value, $dbh,
360 $authorised_values_sth,$index_tag,$index_subfield );
362 # it's a subfield $9 linking to an authority record - see bug 2206
364 elsif ($subfield eq "9" and
365 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
366 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
367 $tagslib->{$tag}->{'a'}->{authtypecode} ne '') {
369 $subfield_data{marc_value} =
370 "<input type=\"text\"
371 id=\"".$subfield_data{id}."\"
372 name=\"".$subfield_data{id}."\"
374 class=\"input_marceditor readonly\"
377 maxlength=\"$max_length\"
378 readonly=\"readonly\"
381 # it's a thesaurus / authority field
383 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
384 if (C4::Context->preference("BiblioAddsAuthorities")) {
385 $subfield_data{marc_value} =
386 "<input type=\"text\"
387 id=\"".$subfield_data{id}."\"
388 name=\"".$subfield_data{id}."\"
390 class=\"input_marceditor readonly\"
393 maxlength=\"$max_length\"
395 <span class=\"subfield_controls\"><a href=\"#\" class=\"buttonDot\"
396 onclick=\"openAuth(this.parentNode.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode}."'); return false;\" tabindex=\"1\" title=\"Tag Editor\"><img src=\"/intranet-tmpl/prog/img/edit-tag.png\" alt=\"Tag Editor\" /></a></span>
399 $subfield_data{marc_value} =
400 "<input type=\"text\"
401 id=\"".$subfield_data{id}."\"
402 name=\"".$subfield_data{id}."\"
404 class=\"input_marceditor readonly\"
407 maxlength=\"$max_length\"
408 readonly=\"readonly\"
409 \/><span class=\"subfield_controls\"><a href=\"#\" class=\"buttonDot\"
410 onclick=\"openAuth(this.parentNode.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode}."'); return false;\" tabindex=\"1\" title=\"Tag Editor\"><img src=\"/intranet-tmpl/prog/img/edit-tag.png\" alt=\"Tag Editor\" /></a></span>
413 # it's a plugin field
415 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
417 # opening plugin. Just check whether we are on a developer computer on a production one
418 # (the cgidir differs)
419 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
420 unless ( opendir( DIR, "$cgidir" ) ) {
421 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
424 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
426 my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
427 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
429 $subfield_data{marc_value} =
430 "<input tabindex=\"1\"
432 id=\"".$subfield_data{id}."\"
433 name=\"".$subfield_data{id}."\"
435 class=\"input_marceditor\"
436 onfocus=\"Focus$function_name($index_tag)\"
438 maxlength=\"$max_length\"
439 onblur=\"Blur$function_name($index_tag); \" \/>
440 <span class=\"subfield_controls\"><a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" tabindex=\"1\" title=\"Tag Editor\"><img src=\"/intranet-tmpl/prog/img/edit-tag.png\" alt=\"Tag Editor\" /></a></span>
443 warn "Plugin Failed: $plugin";
444 # supply default input form
445 $subfield_data{marc_value} =
446 "<input type=\"text\"
447 id=\"".$subfield_data{id}."\"
448 name=\"".$subfield_data{id}."\"
452 maxlength=\"$max_length\"
453 class=\"input_marceditor\"
457 # it's an hidden field
459 elsif ( $tag eq '' ) {
460 $subfield_data{marc_value} =
461 "<input tabindex=\"1\"
463 id=\"".$subfield_data{id}."\"
464 name=\"".$subfield_data{id}."\"
466 maxlength=\"$max_length\"
470 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
471 $subfield_data{marc_value} =
472 "<input type=\"text\"
473 id=\"".$subfield_data{id}."\"
474 name=\"".$subfield_data{id}."\"
475 class=\"input_marceditor\"
478 maxlength=\"$max_length\"
482 # it's a standard field
488 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
489 and $tag < 400 && $subfield eq 'a' )
492 && C4::Context->preference("marcflavour") eq "MARC21" )
495 $subfield_data{marc_value} =
496 "<textarea cols=\"70\"
498 id=\"".$subfield_data{id}."\"
499 name=\"".$subfield_data{id}."\"
500 class=\"input_marceditor\"
506 $subfield_data{marc_value} =
507 "<input type=\"text\"
508 id=\"".$subfield_data{id}."\"
509 name=\"".$subfield_data{id}."\"
513 maxlength=\"$max_length\"
514 class=\"input_marceditor\"
519 $subfield_data{'index_subfield'} = $index_subfield;
520 return \%subfield_data;
524 =head2 format_indicator
526 Translate indicator value for output form - specifically, map
527 indicator = ' ' to ''. This is for the convenience of a cataloger
528 using a mouse to select an indicator input.
532 sub format_indicator {
533 my $ind_value = shift;
534 return '' if not defined $ind_value;
535 return '' if $ind_value eq ' ';
540 my ( $template, $record, $dbh, $encoding,$input ) = @_;
546 my $authorised_values_sth = $dbh->prepare(
547 "select authorised_value,lib
548 from authorised_values
549 where category=? order by lib"
552 # in this array, we will push all the 10 tabs
553 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
556 my @tab_data; # all tags to display
558 foreach my $used ( @$usedTagsLib ){
559 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
560 $seen{$used->{tagfield}}++;
564 foreach(@$usedTagsLib){
565 if($_->{tab} > -1 && $_->{tab} >= $max_num_tab && $_->{tagfield} != '995'){ # FIXME : MARC21 ?
566 $max_num_tab = $_->{tab};
569 if($max_num_tab >= 9){
572 # loop through each tab 0 through 9
573 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
574 my @loop_data = (); #innerloop in the template.
576 foreach my $tag (@tab_data) {
579 my ($indicator1, $indicator2);
580 my $index_tag = CreateKey;
582 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
583 # if MARC::Record is empty => use tab as master loop.
584 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
586 if ( $tag ne '000' ) {
587 @fields = $record->field($tag);
590 push @fields, $record->leader(); # if tag == 000
592 # loop through each field
593 foreach my $field (@fields) {
597 my ( $value, $subfield );
598 if ( $tag ne '000' ) {
599 $value = $field->data();
606 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
608 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
609 'biblio.biblionumber' );
613 $tag, $subfield, $value, $index_tag, $tabloop, $record,
614 $authorised_values_sth,$input
619 my @subfields = $field->subfields();
620 foreach my $subfieldcount ( 0 .. $#subfields ) {
621 my $subfield = $subfields[$subfieldcount][0];
622 my $value = $subfields[$subfieldcount][1];
623 next if ( length $subfield != 1 );
624 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
628 $tag, $subfield, $value, $index_tag, $tabloop,
629 $record, $authorised_values_sth,$input
635 # now, loop again to add parameter subfield that are not in the MARC::Record
636 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
638 next if ( length $subfield != 1 );
639 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
640 next if ( $tag < 10 );
642 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
643 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
644 and not ( $subfield eq "9" and
645 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
646 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
647 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
649 ; #check for visibility flag
650 # if subfield is $9 in a field whose $a is authority-controlled,
651 # always include in the form regardless of the hidden setting - bug 2206
652 next if ( defined( $field->subfield($subfield) ) );
656 $tag, $subfield, '', $index_tag, $tabloop, $record,
657 $authorised_values_sth,$input
661 if ( $#subfields_data >= 0 ) {
662 # build the tag entry.
663 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
664 # have twice the same "name" value, and cgi->param() will return only one, making
665 # all subfields to be merged in a single field.
669 tag_lib => $tagslib->{$tag}->{lib},
670 repeatable => $tagslib->{$tag}->{repeatable},
671 mandatory => $tagslib->{$tag}->{mandatory},
672 subfield_loop => \@subfields_data,
673 fixedfield => $tag < 10?1:0,
676 if ($tag >= 10){ # no indicator for 00x tags
677 $tag_data{indicator1} = format_indicator($field->indicator(1)),
678 $tag_data{indicator2} = format_indicator($field->indicator(2)),
680 push( @loop_data, \%tag_data );
682 } # foreach $field end
684 # if breeding is empty
688 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
689 next if ( length $subfield != 1 );
691 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
692 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
693 and not ( $subfield eq "9" and
694 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
695 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
696 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
698 ; #check for visibility flag
699 # if subfield is $9 in a field whose $a is authority-controlled,
700 # always include in the form regardless of the hidden setting - bug 2206
702 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
706 $tag, $subfield, '', $index_tag, $tabloop, $record,
707 $authorised_values_sth,$input
711 if ( $#subfields_data >= 0 ) {
715 tag_lib => $tagslib->{$tag}->{lib},
716 repeatable => $tagslib->{$tag}->{repeatable},
717 mandatory => $tagslib->{$tag}->{mandatory},
718 indicator1 => $indicator1,
719 indicator2 => $indicator2,
720 subfield_loop => \@subfields_data,
721 tagfirstsubfield => $subfields_data[0],
722 fixedfield => $tag < 10?1:0,
725 push @loop_data, \%tag_data ;
729 if ( $#loop_data >= 0 ) {
732 innerloop => \@loop_data,
736 $template->param( BIG_LOOP => \@BIG_LOOP );
740 # sub that tries to find authorities linked to the biblio
742 # - search in the authority DB for the same authid (in $9 of the biblio)
743 # - search in the authority DB for the same 001 (in $3 of the biblio in UNIMARC)
744 # - search in the authority DB for the same values (exactly) (in all subfields of the biblio)
745 # if the authority is found, the biblio is modified accordingly to be connected to the authority.
746 # if the authority is not found, it's added, and the biblio is then modified to be connected to the authority.
749 sub BiblioAddAuthorities{
750 my ( $record, $frameworkcode ) = @_;
751 my $dbh=C4::Context->dbh;
752 my $query=$dbh->prepare(qq|
753 SELECT authtypecode,tagfield
754 FROM marc_subfield_structure
755 WHERE frameworkcode=?
756 AND (authtypecode IS NOT NULL AND authtypecode<>\"\")|);
757 # SELECT authtypecode,tagfield
758 # FROM marc_subfield_structure
759 # WHERE frameworkcode=?
760 # AND (authtypecode IS NOT NULL OR authtypecode<>\"\")|);
761 $query->execute($frameworkcode);
762 my ($countcreated,$countlinked);
763 while (my $data=$query->fetchrow_hashref){
764 foreach my $field ($record->field($data->{tagfield})){
765 next if ($field->subfield('3') || $field->subfield('9'));
766 # No authorities id in the tag.
767 # Search if there is any authorities to link to.
768 my $query='at='.$data->{authtypecode}.' ';
769 map {$query.= ' and he,ext="'.$_->[1].'"' if ($_->[0]=~/[A-z]/)} $field->subfields();
770 my ($error, $results, $total_hits)=SimpleSearch( $query, undef, undef, [ "authorityserver" ] );
771 # there is only 1 result
773 warn "BIBLIOADDSAUTHORITIES: $error";
776 if ( @{$results} == 1) {
777 my $marcrecord = MARC::File::USMARC::decode($results->[0]);
778 $field->add_subfields('9'=>$marcrecord->field('001')->data);
780 } elsif (@{$results} > 1) {
781 #More than One result
782 #This can comes out of a lack of a subfield.
783 # my $marcrecord = MARC::File::USMARC::decode($results->[0]);
784 # $record->field($data->{tagfield})->add_subfields('9'=>$marcrecord->field('001')->data);
787 #There are no results, build authority record, add it to Authorities, get authid and add it to 9
788 ###NOTICE : This is only valid if a subfield is linked to one and only one authtypecode
789 ###NOTICE : This can be a problem. We should also look into other types and rejected forms.
790 my $authtypedata=GetAuthType($data->{authtypecode});
791 next unless $authtypedata;
792 my $marcrecordauth=MARC::Record->new();
793 if (C4::Context->preference('marcflavour') eq 'MARC21') {
794 $marcrecordauth->leader(' nz a22 o 4500');
795 SetMarcUnicodeFlag($marcrecordauth, 'MARC21');
797 my $authfield=MARC::Field->new($authtypedata->{auth_tag_to_report},'','',"a"=>"".$field->subfield('a'));
798 map { $authfield->add_subfields($_->[0]=>$_->[1]) if ($_->[0]=~/[A-z]/ && $_->[0] ne "a" )} $field->subfields();
799 $marcrecordauth->insert_fields_ordered($authfield);
801 # bug 2317: ensure new authority knows it's using UTF-8; currently
802 # only need to do this for MARC21, as MARC::Record->as_xml_record() handles
803 # automatically for UNIMARC (by not transcoding)
804 # FIXME: AddAuthority() instead should simply explicitly require that the MARC::Record
805 # use UTF-8, but as of 2008-08-05, did not want to introduce that kind
806 # of change to a core API just before the 3.0 release.
808 if (C4::Context->preference('marcflavour') eq 'MARC21') {
809 $marcrecordauth->insert_fields_ordered(MARC::Field->new('667','','','a'=>"Machine generated authority record."));
810 my $cite = $record->author() . ", " . $record->title_proper() . ", " . $record->publication_date() . " ";
811 $cite =~ s/^[\s\,]*//;
812 $cite =~ s/[\s\,]*$//;
813 $cite = "Work cat.: (" . C4::Context->preference('MARCOrgCode') . ")". $record->subfield('999','c') . ": " . $cite;
814 $marcrecordauth->insert_fields_ordered(MARC::Field->new('670','','','a'=>$cite));
817 # warn "AUTH RECORD ADDED : ".$marcrecordauth->as_formatted;
819 my $authid=AddAuthority($marcrecordauth,'',$data->{authtypecode});
821 $field->add_subfields('9'=>$authid);
825 return ($countlinked,$countcreated);
828 # ========================
830 #=========================
832 my $error = $input->param('error');
833 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
834 my $breedingid = $input->param('breedingid');
835 my $z3950 = $input->param('z3950');
836 my $op = $input->param('op');
837 my $mode = $input->param('mode');
838 my $frameworkcode = $input->param('frameworkcode');
839 my $redirect = $input->param('redirect');
840 my $dbh = C4::Context->dbh;
841 my $hostbiblionumber = $input->param('hostbiblionumber');
842 my $hostitemnumber = $input->param('hostitemnumber');
845 my $userflags = 'edit_catalogue';
846 if ($frameworkcode eq 'FA'){
847 $userflags = 'fast_cataloging';
850 $frameworkcode = &GetFrameworkCode($biblionumber)
851 if ( $biblionumber and not($frameworkcode) and $op ne 'addbiblio' );
853 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
854 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
856 template_name => "cataloguing/addbiblio.tt",
859 authnotrequired => 0,
860 flagsrequired => { editcatalogue => $userflags },
864 if ($frameworkcode eq 'FA'){
865 # We need to grab and set some variables in the template for use on the additems screen
866 $template->{VARS}->{'circborrowernumber'} = $input->param('borrowernumber');
867 $template->{VARS}->{'barcode'} = $input->param('barcode');
868 $template->{VARS}->{'branch'} = $input->param('branch');
869 $template->{VARS}->{'stickyduedate'} = $input->param('stickyduedate');
870 $template->{VARS}->{'duedatespec'} = $input->param('duedatespec');
873 # Getting the list of all frameworks
875 my $frameworks = getframeworks;
876 my @frameworkcodeloop;
877 foreach my $thisframeworkcode ( keys %$frameworks ) {
879 value => $thisframeworkcode,
880 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
882 if ($frameworkcode eq $thisframeworkcode){
883 $row{'selected'} = 1;
885 push @frameworkcodeloop, \%row;
887 $template->param( frameworkcodeloop => \@frameworkcodeloop,
888 breedingid => $breedingid );
891 $tagslib = &GetMarcStructure( 1, $frameworkcode );
892 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
893 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
899 $biblionumbertagfield,
900 $biblionumbertagsubfield,
901 $biblioitemnumtagfield,
902 $biblioitemnumtagsubfield,
907 if (($biblionumber) && !($breedingid)){
908 $record = GetMarcBiblio($biblionumber);
911 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
913 #populate hostfield if hostbiblionumber is available
914 if ($hostbiblionumber){
915 my $marcflavour = C4::Context->preference("marcflavour");
916 $record=MARC::Record->new();
918 my $field = PrepHostMarcField($hostbiblionumber, $hostitemnumber,$marcflavour);
919 $record->append_fields($field);
926 $template->param( title => $record->title(), );
928 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
929 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
930 &GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
931 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
932 &GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
934 # search biblioitems value
935 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
936 $sth->execute($biblionumber);
937 ($biblioitemnumber) = $sth->fetchrow;
940 #-------------------------------------------------------------------------------------
941 if ( $op eq "addbiblio" ) {
942 #-------------------------------------------------------------------------------------
944 biblionumberdata => $biblionumber,
947 my @params = $input->param();
948 $record = TransformHtmlToMarc( $input );
949 # check for a duplicate
950 my ( $duplicatebiblionumber, $duplicatetitle );
951 if ( !$is_a_modif ) {
952 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
954 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
955 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
956 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
959 if (C4::Context->preference("BiblioAddsAuthorities")){
960 my ($countlinked,$countcreated)=BiblioAddAuthorities($record,$frameworkcode);
963 ModBiblioframework( $biblionumber, $frameworkcode );
964 ModBiblio( $record, $biblionumber, $frameworkcode );
967 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
969 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view")){
970 if ($frameworkcode eq 'FA'){
971 my $borrowernumber = $input->param('circborrowernumber');
972 my $barcode = $input->param('barcode');
973 my $branch = $input->param('branch');
974 my $stickyduedate = $input->param('stickyduedate');
975 my $duedatespec = $input->param('duedatespec');
976 print $input->redirect(
977 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&borrowernumber=$borrowernumber&branch=$branch&barcode=$barcode&stickyduedate=$stickyduedate&duedatespec=$duedatespec"
982 print $input->redirect(
983 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode"
988 elsif($is_a_modif || $redirect eq "view"){
989 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
990 my $views = { C4::Search::enabled_staff_search_views };
991 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
992 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber");
993 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
994 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
995 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
996 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber");
998 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber");
1005 biblionumber => $biblionumber,
1009 $template->param( title => $record->subfield('200',"a") ) if ($record ne "-1" && C4::Context->preference('marcflavour') =~/unimarc/i);
1010 $template->param( title => $record->title() ) if ($record ne "-1" && C4::Context->preference('marcflavour') eq "usmarc");
1013 itemtype => $frameworkcode,
1015 output_html_with_http_headers $input, $cookie, $template->output;
1019 # it may be a duplicate, warn the user and do nothing
1020 build_tabs ($template, $record, $dbh,$encoding,$input);
1022 biblionumber => $biblionumber,
1023 biblioitemnumber => $biblioitemnumber,
1024 duplicatebiblionumber => $duplicatebiblionumber,
1025 duplicatebibid => $duplicatebiblionumber,
1026 duplicatetitle => $duplicatetitle,
1030 elsif ( $op eq "delete" ) {
1032 my $error = &DelBiblio($biblionumber);
1034 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
1035 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
1039 print $input->redirect('/cgi-bin/koha/catalogue/search.pl');
1043 #----------------------------------------------------------------------------
1044 # If we're in a duplication case, we have to set to "" the biblionumber
1045 # as we'll save the biblio as a new one.
1047 biblionumberdata => $biblionumber,
1050 if ( $op eq "duplicate" ) {
1054 if ( $record ne -1 ) {
1055 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
1057 my $uxml = $record->as_xml;
1058 MARC::Record::default_record_format("UNIMARC")
1059 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
1060 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
1064 build_tabs( $template, $record, $dbh, $encoding,$input );
1066 biblionumber => $biblionumber,
1067 biblionumbertagfield => $biblionumbertagfield,
1068 biblionumbertagsubfield => $biblionumbertagsubfield,
1069 biblioitemnumtagfield => $biblioitemnumtagfield,
1070 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
1071 biblioitemnumber => $biblioitemnumber,
1072 hostbiblionumber => $hostbiblionumber,
1073 hostitemnumber => $hostitemnumber
1077 $template->param( title => $record->title() ) if ( $record ne "-1" );
1080 frameworkcode => $frameworkcode,
1081 itemtype => $frameworkcode,
1082 borrowernumber => $loggedinuser,
1083 marcflavour => C4::Context->preference("marcflavour"),
1086 output_html_with_http_headers $input, $cookie, $template->output;