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
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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;
44 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
45 MARC::File::XML->default_record_format('UNIMARC');
48 our($tagslib,$authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
52 =head2 MARCfindbreeding
54 $record = MARCfindbreeding($breedingid);
56 Look up the import record repository for the record with
57 record with id $breedingid. If found, returns the decoded
58 MARC::Record; otherwise, -1 is returned (FIXME).
59 Returns as second parameter the character encoding.
63 sub MARCfindbreeding {
65 my ($marc, $encoding) = GetImportRecordMarc($id);
66 # remove the - in isbn, koha store isbn without any -
68 my $record = MARC::Record->new_from_usmarc($marc);
69 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
70 if ( $record->field($isbnfield) ) {
71 foreach my $field ( $record->field($isbnfield) ) {
72 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
73 my $newisbn = $field->subfield($isbnsubfield);
75 $field->update( $isbnsubfield => $newisbn );
79 # fix the unimarc 100 coded field (with unicode information)
80 if (C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield(100,'a')) {
81 my $f100a=$record->subfield(100,'a');
82 my $f100 = $record->field(100);
83 my $f100temp = $f100->as_string;
84 $record->delete_field($f100);
85 if ( length($f100temp) > 28 ) {
86 substr( $f100temp, 26, 2, "50" );
87 $f100->update( 'a' => $f100temp );
88 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
89 $record->insert_fields_ordered($f100);
93 if ( !defined(ref($record)) ) {
97 # normalize author : UNIMARC specific...
98 if ( C4::Context->preference("z3950NormalizeAuthor")
99 and C4::Context->preference("z3950AuthorAuthFields")
100 and C4::Context->preference("marcflavour") eq 'UNIMARC' )
102 my ( $tag, $subfield ) = GetMarcFromKohaField("biblio.author", '');
104 # my $summary = C4::Context->preference("z3950authortemplate");
106 C4::Context->preference("z3950AuthorAuthFields");
107 my @auth_fields = split /,/, $auth_fields;
110 if ( $record->field($tag) ) {
111 foreach my $tmpfield ( $record->field($tag)->subfields ) {
113 # foreach my $subfieldcode ($tmpfield->subfields){
114 my $subfieldcode = shift @$tmpfield;
115 my $subfieldvalue = shift @$tmpfield;
117 $field->add_subfields(
118 "$subfieldcode" => $subfieldvalue )
119 if ( $subfieldcode ne $subfield );
123 MARC::Field->new( $tag, "", "",
124 $subfieldcode => $subfieldvalue )
125 if ( $subfieldcode ne $subfield );
129 $record->delete_field( $record->field($tag) );
130 foreach my $fieldtag (@auth_fields) {
131 next unless ( $record->field($fieldtag) );
132 my $lastname = $record->field($fieldtag)->subfield('a');
133 my $firstname = $record->field($fieldtag)->subfield('b');
134 my $title = $record->field($fieldtag)->subfield('c');
135 my $number = $record->field($fieldtag)->subfield('d');
138 # $field->add_subfields("$subfield"=>"[ ".ucfirst($title).ucfirst($firstname)." ".$number." ]");
139 $field->add_subfields(
140 "$subfield" => ucfirst($title) . " "
141 . ucfirst($firstname) . " "
146 # $field->add_subfields("$subfield"=>"[ ".ucfirst($firstname).", ".ucfirst($lastname)." ]");
147 $field->add_subfields(
148 "$subfield" => ucfirst($firstname) . ", "
149 . ucfirst($lastname) );
152 $record->insert_fields_ordered($field);
154 return $record, $encoding;
160 =head2 build_authorized_values_list
164 sub build_authorized_values_list {
165 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
167 my @authorised_values;
170 # builds list, depending on authorised value...
173 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
174 #Use GetBranches($onlymine)
176 C4::Context->preference('IndependentBranches')
177 && C4::Context->userenv
178 && !C4::Context->IsSuperLibrarian()
179 && C4::Context->userenv->{branch};
180 my $branches = GetBranches($onlymine);
182 foreach my $thisbranch ( sort keys %$branches ) {
183 push @authorised_values, $thisbranch;
184 $authorised_lib{$thisbranch} = $branches->{$thisbranch}->{'branchname'};
189 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
192 "select itemtype,description from itemtypes order by description");
194 push @authorised_values, ""
195 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
196 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
200 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
201 push @authorised_values, $itemtype;
202 $authorised_lib{$itemtype} = $description;
204 $value = $itemtype unless ($value);
208 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
209 push @authorised_values, ""
210 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
212 my $class_sources = GetClassSources();
214 my $default_source = C4::Context->preference("DefaultClassificationSource");
216 foreach my $class_source (sort keys %$class_sources) {
217 next unless $class_sources->{$class_source}->{'used'} or
218 ($value and $class_source eq $value) or
219 ($class_source eq $default_source);
220 push @authorised_values, $class_source;
221 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
223 $value = $default_source unless $value;
226 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
227 $authorised_values_sth->execute(
228 $tagslib->{$tag}->{$subfield}->{authorised_value},
229 $branch_limit ? $branch_limit : (),
232 push @authorised_values, ""
233 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
234 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
236 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
237 push @authorised_values, $value;
238 $authorised_lib{$value} = $lib;
241 $authorised_values_sth->finish;
244 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
245 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
247 values => \@authorised_values,
248 labels => \%authorised_lib,
255 Create a random value to set it into the input name
260 return int(rand(1000000));
263 =head2 GetMandatoryFieldZ3950
265 This function return an hashref which containts all mandatory field
266 to search with z3950 server.
270 sub GetMandatoryFieldZ3950 {
271 my $frameworkcode = shift;
272 my @isbn = GetMarcFromKohaField('biblioitems.isbn',$frameworkcode);
273 my @title = GetMarcFromKohaField('biblio.title',$frameworkcode);
274 my @author = GetMarcFromKohaField('biblio.author',$frameworkcode);
275 my @issn = GetMarcFromKohaField('biblioitems.issn',$frameworkcode);
276 my @lccn = GetMarcFromKohaField('biblioitems.lccn',$frameworkcode);
279 $isbn[0].$isbn[1] => 'isbn',
280 $title[0].$title[1] => 'title',
281 $author[0].$author[1] => 'author',
282 $issn[0].$issn[1] => 'issn',
283 $lccn[0].$lccn[1] => 'lccn',
289 builds the <input ...> entry for a subfield.
294 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
296 my $index_subfield = CreateKey(); # create a specifique key for each subfield
298 $value =~ s/"/"/g;
300 # if there is no value provided but a default value in parameters, get it
301 if ( $value eq '' ) {
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 = (
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},
332 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
334 maxlength => $tagslib->{$tag}->{$subfield}->{maxlength},
335 random => CreateKey(),
338 if(exists $mandatory_z3950->{$tag.$subfield}){
339 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
341 # Subfield is hidden depending of hidden and mandatory flag, and is always
342 # shown if it contains anything or if its field is mandatory.
343 my $tdef = $tagslib->{$tag};
344 $subfield_data{visibility} = "display:none;"
345 if $tdef->{$subfield}->{hidden} % 2 == 1 &&
347 !$tdef->{$subfield}->{mandatory} &&
349 # expand all subfields of 773 if there is a host item provided in the input
350 $subfield_data{visibility} ="" if ($tag eq 773 and $cgi->param('hostitemnumber'));
353 # it's an authorised field
354 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
355 $subfield_data{marc_value} =
356 build_authorized_values_list( $tag, $subfield, $value, $dbh,
357 $authorised_values_sth,$index_tag,$index_subfield );
359 # it's a subfield $9 linking to an authority record - see bug 2206
361 elsif ($subfield eq "9" and
362 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
363 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
364 $tagslib->{$tag}->{'a'}->{authtypecode} ne '') {
366 $subfield_data{marc_value} = {
368 id => $subfield_data{id},
369 name => $subfield_data{id},
372 maxlength => $subfield_data{maxlength},
376 # it's a thesaurus / authority field
378 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
379 # when authorities auto-creation is allowed, do not set readonly
380 my $is_readonly = !C4::Context->preference("BiblioAddsAuthorities");
382 $subfield_data{marc_value} = {
384 id => $subfield_data{id},
385 name => $subfield_data{id},
388 maxlength => $subfield_data{maxlength},
389 readonly => ($is_readonly) ? 1 : 0,
390 authtype => $tagslib->{$tag}->{$subfield}->{authtypecode},
393 # it's a plugin field
394 } elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
395 require Koha::FrameworkPlugin;
396 my $plugin = Koha::FrameworkPlugin->new( {
397 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
399 my $pars= { dbh => $dbh, record => $rec, tagslib => $tagslib,
400 id => $subfield_data{id}, tabloop => $tabloop };
401 $plugin->build( $pars );
402 if( !$plugin->errstr ) {
403 $subfield_data{marc_value} = {
404 type => 'text_complex',
405 id => $subfield_data{id},
406 name => $subfield_data{id},
409 maxlength => $subfield_data{maxlength},
410 javascript => $plugin->javascript,
411 noclick => $plugin->noclick,
414 warn $plugin->errstr;
415 # supply default input form
416 $subfield_data{marc_value} = {
418 id => $subfield_data{id},
419 name => $subfield_data{id},
422 maxlength => $subfield_data{maxlength},
427 # it's an hidden field
428 } elsif ( $tag eq '' ) {
429 $subfield_data{marc_value} = {
431 id => $subfield_data{id},
432 name => $subfield_data{id},
435 maxlength => $subfield_data{maxlength},
440 # it's a standard field
444 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
445 and $tag < 400 && $subfield eq 'a' )
448 && C4::Context->preference("marcflavour") eq "MARC21" )
451 $subfield_data{marc_value} = {
453 id => $subfield_data{id},
454 name => $subfield_data{id},
460 $subfield_data{marc_value} = {
462 id => $subfield_data{id},
463 name => $subfield_data{id},
466 maxlength => $subfield_data{maxlength},
472 $subfield_data{'index_subfield'} = $index_subfield;
473 return \%subfield_data;
477 =head2 format_indicator
479 Translate indicator value for output form - specifically, map
480 indicator = ' ' to ''. This is for the convenience of a cataloger
481 using a mouse to select an indicator input.
485 sub format_indicator {
486 my $ind_value = shift;
487 return '' if not defined $ind_value;
488 return '' if $ind_value eq ' ';
493 my ( $template, $record, $dbh, $encoding,$input ) = @_;
499 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
500 my $query = "SELECT authorised_value, lib
501 FROM authorised_values";
502 $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id )} if $branch_limit;
503 $query .= " WHERE category = ?";
504 $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
505 $query .= " GROUP BY lib ORDER BY lib, lib_opac";
506 my $authorised_values_sth = $dbh->prepare( $query );
508 # in this array, we will push all the 10 tabs
509 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
512 my @tab_data; # all tags to display
514 foreach my $used ( @$usedTagsLib ){
515 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
516 $seen{$used->{tagfield}}++;
520 foreach(@$usedTagsLib){
521 if($_->{tab} > -1 && $_->{tab} >= $max_num_tab && $_->{tagfield} != '995'){ # FIXME : MARC21 ?
522 $max_num_tab = $_->{tab};
525 if($max_num_tab >= 9){
528 # loop through each tab 0 through 9
529 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
530 my @loop_data = (); #innerloop in the template.
532 foreach my $tag (@tab_data) {
535 my ($indicator1, $indicator2);
536 my $index_tag = CreateKey;
538 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
539 # if MARC::Record is empty => use tab as master loop.
540 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
542 if ( $tag ne '000' ) {
543 @fields = $record->field($tag);
546 push @fields, $record->leader(); # if tag == 000
548 # loop through each field
549 foreach my $field (@fields) {
553 my ( $value, $subfield );
554 if ( $tag ne '000' ) {
555 $value = $field->data();
562 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
564 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
565 'biblio.biblionumber' );
569 $tag, $subfield, $value, $index_tag, $tabloop, $record,
570 $authorised_values_sth,$input
575 my @subfields = $field->subfields();
576 foreach my $subfieldcount ( 0 .. $#subfields ) {
577 my $subfield = $subfields[$subfieldcount][0];
578 my $value = $subfields[$subfieldcount][1];
579 next if ( length $subfield != 1 );
580 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
584 $tag, $subfield, $value, $index_tag, $tabloop,
585 $record, $authorised_values_sth,$input
591 # now, loop again to add parameter subfield that are not in the MARC::Record
592 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
594 next if ( length $subfield != 1 );
595 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
596 next if ( $tag < 10 );
598 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
599 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
600 and not ( $subfield eq "9" and
601 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
602 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
603 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
605 ; #check for visibility flag
606 # if subfield is $9 in a field whose $a is authority-controlled,
607 # always include in the form regardless of the hidden setting - bug 2206
608 next if ( defined( $field->subfield($subfield) ) );
612 $tag, $subfield, '', $index_tag, $tabloop, $record,
613 $authorised_values_sth,$input
617 if ( $#subfields_data >= 0 ) {
618 # build the tag entry.
619 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
620 # have twice the same "name" value, and cgi->param() will return only one, making
621 # all subfields to be merged in a single field.
625 tag_lib => $tagslib->{$tag}->{lib},
626 repeatable => $tagslib->{$tag}->{repeatable},
627 mandatory => $tagslib->{$tag}->{mandatory},
628 subfield_loop => \@subfields_data,
629 fixedfield => $tag < 10?1:0,
632 if ($tag >= 10){ # no indicator for 00x tags
633 $tag_data{indicator1} = format_indicator($field->indicator(1)),
634 $tag_data{indicator2} = format_indicator($field->indicator(2)),
636 push( @loop_data, \%tag_data );
638 } # foreach $field end
640 # if breeding is empty
644 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
645 next if ( length $subfield != 1 );
647 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
648 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
649 and not ( $subfield eq "9" and
650 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
651 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
652 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
654 ; #check for visibility flag
655 # if subfield is $9 in a field whose $a is authority-controlled,
656 # always include in the form regardless of the hidden setting - bug 2206
658 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
662 $tag, $subfield, '', $index_tag, $tabloop, $record,
663 $authorised_values_sth,$input
667 if ( $#subfields_data >= 0 ) {
671 tag_lib => $tagslib->{$tag}->{lib},
672 repeatable => $tagslib->{$tag}->{repeatable},
673 mandatory => $tagslib->{$tag}->{mandatory},
674 indicator1 => $indicator1,
675 indicator2 => $indicator2,
676 subfield_loop => \@subfields_data,
677 tagfirstsubfield => $subfields_data[0],
678 fixedfield => $tag < 10?1:0,
681 push @loop_data, \%tag_data ;
685 if ( $#loop_data >= 0 ) {
688 innerloop => \@loop_data,
692 $authorised_values_sth->finish;
693 $template->param( BIG_LOOP => \@BIG_LOOP );
696 # ========================
698 #=========================
700 my $error = $input->param('error');
701 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
702 my $parentbiblio = $input->param('parentbiblionumber');
703 my $breedingid = $input->param('breedingid');
704 my $z3950 = $input->param('z3950');
705 my $op = $input->param('op');
706 my $mode = $input->param('mode');
707 my $frameworkcode = $input->param('frameworkcode');
708 my $redirect = $input->param('redirect');
709 my $searchid = $input->param('searchid');
710 my $dbh = C4::Context->dbh;
711 my $hostbiblionumber = $input->param('hostbiblionumber');
712 my $hostitemnumber = $input->param('hostitemnumber');
713 # fast cataloguing datas in transit
714 my $fa_circborrowernumber = $input->param('circborrowernumber');
715 my $fa_barcode = $input->param('barcode');
716 my $fa_branch = $input->param('branch');
717 my $fa_stickyduedate = $input->param('stickyduedate');
718 my $fa_duedatespec = $input->param('duedatespec');
720 my $userflags = 'edit_catalogue';
722 my $changed_framework = $input->param('changed_framework');
723 $frameworkcode = &GetFrameworkCode($biblionumber)
724 if ( $biblionumber and not($frameworkcode) and $op ne 'addbiblio' );
726 if ($frameworkcode eq 'FA'){
727 $userflags = 'fast_cataloging';
730 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
731 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
733 template_name => "cataloguing/addbiblio.tt",
736 authnotrequired => 0,
737 flagsrequired => { editcatalogue => $userflags },
741 if ($frameworkcode eq 'FA'){
742 # We need to grab and set some variables in the template for use on the additems screen
744 'circborrowernumber' => $fa_circborrowernumber,
745 'barcode' => $fa_barcode,
746 'branch' => $fa_branch,
747 'stickyduedate' => $fa_stickyduedate,
748 'duedatespec' => $fa_duedatespec,
752 # Getting the list of all frameworks
754 my $frameworks = getframeworks;
755 my @frameworkcodeloop;
756 foreach my $thisframeworkcode ( keys %$frameworks ) {
758 value => $thisframeworkcode,
759 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
761 if ($frameworkcode eq $thisframeworkcode){
762 $row{'selected'} = 1;
764 push @frameworkcodeloop, \%row;
766 $template->param( frameworkcodeloop => \@frameworkcodeloop,
767 breedingid => $breedingid );
770 $tagslib = &GetMarcStructure( 1, $frameworkcode );
771 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
772 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
778 $biblionumbertagfield,
779 $biblionumbertagsubfield,
780 $biblioitemnumtagfield,
781 $biblioitemnumtagsubfield,
786 if (($biblionumber) && !($breedingid)){
787 $record = GetMarcBiblio($biblionumber);
790 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
793 #populate hostfield if hostbiblionumber is available
794 if ($hostbiblionumber) {
795 my $marcflavour = C4::Context->preference("marcflavour");
796 $record = MARC::Record->new();
799 PrepHostMarcField( $hostbiblionumber, $hostitemnumber, $marcflavour );
800 $record->append_fields($field);
803 # This is a child record
805 my $marcflavour = C4::Context->preference('marcflavour');
806 $record = MARC::Record->new();
807 SetMarcUnicodeFlag($record, $marcflavour);
808 my $hostfield = prepare_host_field($parentbiblio,$marcflavour);
810 $record->append_fields($hostfield);
818 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
819 $template->param( title => $title );
821 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
822 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
823 &GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
824 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
825 &GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
827 # search biblioitems value
828 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
829 $sth->execute($biblionumber);
830 ($biblioitemnumber) = $sth->fetchrow;
833 #-------------------------------------------------------------------------------------
834 if ( $op eq "addbiblio" ) {
835 #-------------------------------------------------------------------------------------
837 biblionumberdata => $biblionumber,
840 my @params = $input->param();
841 $record = TransformHtmlToMarc( $input );
842 # check for a duplicate
843 my ( $duplicatebiblionumber, $duplicatetitle );
844 if ( !$is_a_modif ) {
845 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
847 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
848 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
849 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
852 if (C4::Context->preference("BiblioAddsAuthorities")){
853 BiblioAutoLink( $record, $frameworkcode );
856 ModBiblioframework( $biblionumber, $frameworkcode );
857 ModBiblio( $record, $biblionumber, $frameworkcode );
860 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
862 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view" && $redirect ne "just_save")){
863 if ($frameworkcode eq 'FA'){
864 print $input->redirect(
865 '/cgi-bin/koha/cataloguing/additem.pl?'
866 .'biblionumber='.$biblionumber
867 .'&frameworkcode='.$frameworkcode
868 .'&circborrowernumber='.$fa_circborrowernumber
869 .'&branch='.$fa_branch
870 .'&barcode='.uri_escape_utf8($fa_barcode)
871 .'&stickyduedate='.$fa_stickyduedate
872 .'&duedatespec='.$fa_duedatespec
877 print $input->redirect(
878 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid"
883 elsif(($is_a_modif || $redirect eq "view") && $redirect ne "just_save"){
884 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
885 my $views = { C4::Search::enabled_staff_search_views };
886 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
887 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
888 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
889 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
890 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
891 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
893 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
898 elsif ($redirect eq "just_save"){
899 my $tab = $input->param('current_tab');
900 print $input->redirect("/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber&framework=$frameworkcode&tab=$tab&searchid=$searchid");
904 biblionumber => $biblionumber,
908 if ( $record ne '-1' ) {
909 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
910 $template->param( title => $title );
914 itemtype => $frameworkcode,
916 output_html_with_http_headers $input, $cookie, $template->output;
920 # it may be a duplicate, warn the user and do nothing
921 build_tabs ($template, $record, $dbh,$encoding,$input);
923 biblionumber => $biblionumber,
924 biblioitemnumber => $biblioitemnumber,
925 duplicatebiblionumber => $duplicatebiblionumber,
926 duplicatebibid => $duplicatebiblionumber,
927 duplicatetitle => $duplicatetitle,
931 elsif ( $op eq "delete" ) {
933 my $error = &DelBiblio($biblionumber);
935 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
936 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
940 print $input->redirect('/cgi-bin/koha/catalogue/search.pl');
944 #----------------------------------------------------------------------------
945 # If we're in a duplication case, we have to set to "" the biblionumber
946 # as we'll save the biblio as a new one.
948 biblionumberdata => $biblionumber,
951 if ( $op eq "duplicate" ) {
955 if($changed_framework eq "changed"){
956 $record = TransformHtmlToMarc( $input );
958 elsif( $record ne -1 ) {
959 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
961 my $uxml = $record->as_xml;
962 MARC::Record::default_record_format("UNIMARC")
963 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
964 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
968 build_tabs( $template, $record, $dbh, $encoding,$input );
970 biblionumber => $biblionumber,
971 biblionumbertagfield => $biblionumbertagfield,
972 biblionumbertagsubfield => $biblionumbertagsubfield,
973 biblioitemnumtagfield => $biblioitemnumtagfield,
974 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
975 biblioitemnumber => $biblioitemnumber,
976 hostbiblionumber => $hostbiblionumber,
977 hostitemnumber => $hostitemnumber
981 if ( $record ne '-1' ) {
982 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
983 $template->param( title => $title );
987 frameworkcode => $frameworkcode,
988 itemtype => $frameworkcode,
989 borrowernumber => $loggedinuser,
990 tab => $input->param('tab')
992 $template->{'VARS'}->{'searchid'} = $searchid;
994 output_html_with_http_headers $input, $cookie, $template->output;