Bug 36908: Additional unit tests to identify flaw when two branches have same IP
[koha.git] / cataloguing / addbiblio.pl
1 #!/usr/bin/perl 
2
3
4 # Copyright 2000-2002 Katipo Communications
5 # Copyright 2004-2010 BibLibre
6 #
7 # This file is part of Koha.
8 #
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.
13 #
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.
18 #
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>.
21
22 use Modern::Perl;
23 use CGI;
24 use POSIX     qw( strftime );
25 use Try::Tiny qw(catch try);
26
27 use C4::Output qw( output_html_with_http_headers );
28 use C4::Auth qw( get_template_and_user haspermission );
29 use C4::Biblio qw(
30     AddBiblio
31     DelBiblio
32     GetFrameworkCode
33     GetMarcFromKohaField
34     GetMarcStructure
35     GetUsedMarcStructure
36     ModBiblio
37     prepare_host_field
38     PrepHostMarcField
39     TransformHtmlToMarc
40     ApplyMarcOverlayRules
41 );
42 use C4::Search qw( FindDuplicate enabled_staff_search_views );
43 use C4::Auth qw( get_template_and_user haspermission );
44 use C4::Context;
45 use MARC::Record;
46 use C4::ClassSource qw( GetClassSources );
47 use C4::ImportBatch qw( GetImportRecordMarc );
48 use C4::Charset qw( SetMarcUnicodeFlag );
49 use Koha::BiblioFrameworks;
50 use Koha::DateUtils qw( dt_from_string );
51
52 use Koha::Acquisition::Orders;
53 use Koha::Biblios;
54 use Koha::ItemTypes;
55 use Koha::Libraries;
56
57 use Koha::BiblioFrameworks;
58 use Koha::Patrons;
59 use Koha::UI::Form::Builder::Biblio;
60
61 use MARC::File::USMARC;
62 use MARC::File::XML;
63 use URI::Escape qw( uri_escape_utf8 );
64
65 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
66     MARC::File::XML->default_record_format('UNIMARC');
67 }
68
69 our($tagslib,$authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950,$op,$changed_framework);
70
71 =head1 FUNCTIONS
72
73 =head2 MARCfindbreeding
74
75     $record = MARCfindbreeding($breedingid);
76
77 Look up the import record repository for the record with
78 record with id $breedingid.  If found, returns the decoded
79 MARC::Record; otherwise, -1 is returned (FIXME).
80 Returns as second parameter the character encoding.
81
82 =cut
83
84 sub MARCfindbreeding {
85     my ( $id ) = @_;
86     my ($marc, $encoding) = GetImportRecordMarc($id);
87     # remove the - in isbn, koha store isbn without any -
88     if ($marc) {
89         my $record = MARC::Record->new_from_usmarc($marc);
90         if(C4::Context->preference('autoControlNumber') eq 'biblionumber'){
91             my @control_num = $record->field('001');
92             $record->delete_fields(@control_num);
93         }
94         my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField( 'biblioitems.isbn' );
95         if ( $record->field($isbnfield) ) {
96             foreach my $field ( $record->field($isbnfield) ) {
97                 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
98                     my $newisbn = $field->subfield($isbnsubfield);
99                     $newisbn =~ s/-//g;
100                     $field->update( $isbnsubfield => $newisbn );
101                 }
102             }
103         }
104         # fix the unimarc 100 coded field (with unicode information)
105         if (C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield(100,'a')) {
106             my $f100a=$record->subfield(100,'a');
107             my $f100 = $record->field(100);
108             my $f100temp = $f100->as_string;
109             $record->delete_field($f100);
110             if ( length($f100temp) > 28 ) {
111                 substr( $f100temp, 26, 2, "50" );
112                 $f100->update( 'a' => $f100temp );
113                 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
114                 $record->insert_fields_ordered($f100);
115             }
116         }
117                 
118         if ( !defined(ref($record)) ) {
119             return -1;
120         }
121         else {
122             # normalize author : UNIMARC specific...
123             if (    C4::Context->preference("z3950NormalizeAuthor")
124                 and C4::Context->preference("z3950AuthorAuthFields")
125                 and C4::Context->preference("marcflavour") eq 'UNIMARC' )
126             {
127                 my ( $tag, $subfield ) = GetMarcFromKohaField( "biblio.author" );
128
129                 my $auth_fields =
130                   C4::Context->preference("z3950AuthorAuthFields");
131                 my @auth_fields = split /,/, $auth_fields;
132                 my $field;
133
134                 if ( $record->field($tag) ) {
135                     foreach my $tmpfield ( $record->field($tag)->subfields ) {
136
137                         my $subfieldcode  = shift @$tmpfield;
138                         my $subfieldvalue = shift @$tmpfield;
139                         if ($field) {
140                             $field->add_subfields(
141                                 "$subfieldcode" => $subfieldvalue )
142                               if ( $subfieldcode ne $subfield );
143                         }
144                         else {
145                             $field =
146                               MARC::Field->new( $tag, "", "",
147                                 $subfieldcode => $subfieldvalue )
148                               if ( $subfieldcode ne $subfield );
149                         }
150                     }
151                 }
152                 $record->delete_field( $record->field($tag) );
153                 foreach my $fieldtag (@auth_fields) {
154                     next unless ( $record->field($fieldtag) );
155                     my $lastname  = $record->field($fieldtag)->subfield('a');
156                     my $firstname = $record->field($fieldtag)->subfield('b');
157                     my $title     = $record->field($fieldtag)->subfield('c');
158                     my $number    = $record->field($fieldtag)->subfield('d');
159                     if ($title) {
160                         $field->add_subfields(
161                                 "$subfield" => ucfirst($title) . " "
162                               . ucfirst($firstname) . " "
163                               . $number );
164                     }
165                     else {
166                         $field->add_subfields(
167                             "$subfield" => ucfirst($firstname) . ", "
168                               . ucfirst($lastname) );
169                     }
170                 }
171                 $record->insert_fields_ordered($field);
172             }
173             return $record, $encoding;
174         }
175     }
176     return -1;
177 }
178
179 =head2 CreateKey
180
181     Create a random value to set it into the input name
182
183 =cut
184
185 sub CreateKey {
186     return int(rand(1000000));
187 }
188
189 =head2 GetMandatoryFieldZ3950
190
191     This function returns a hashref which contains all mandatory field
192     to search with z3950 server.
193
194 =cut
195
196 sub GetMandatoryFieldZ3950 {
197     my $frameworkcode = shift;
198     my @isbn   = GetMarcFromKohaField( 'biblioitems.isbn' );
199     my @title  = GetMarcFromKohaField( 'biblio.title' );
200     my @author = GetMarcFromKohaField( 'biblio.author' );
201     my @issn   = GetMarcFromKohaField( 'biblioitems.issn' );
202     my @lccn   = GetMarcFromKohaField( 'biblioitems.lccn' );
203     
204     return {
205         $isbn[0].$isbn[1]     => 'isbn',
206         $title[0].$title[1]   => 'title',
207         $author[0].$author[1] => 'author',
208         $issn[0].$issn[1]     => 'issn',
209         $lccn[0].$lccn[1]     => 'lccn',
210     };
211 }
212
213 =head2 format_indicator
214
215 Translate indicator value for output form - specifically, map
216 indicator = ' ' to ''.  This is for the convenience of a cataloger
217 using a mouse to select an indicator input.
218
219 =cut
220
221 sub format_indicator {
222     my $ind_value = shift;
223     return '' if not defined $ind_value;
224     return '' if $ind_value eq ' ';
225     return $ind_value;
226 }
227
228 sub build_tabs {
229     my ( $template, $record, $dbh, $encoding,$input ) = @_;
230
231     # fill arrays
232     my @loop_data = ();
233     my $tag;
234
235     my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
236     my $query = "SELECT authorised_value, lib
237                 FROM authorised_values";
238     $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id )} if $branch_limit;
239     $query .= " WHERE category = ?";
240     $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
241     $query .= " GROUP BY authorised_value,lib ORDER BY lib, lib_opac";
242     my $authorised_values_sth = $dbh->prepare( $query );
243
244     # in this array, we will push all the 10 tabs
245     # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
246     my @BIG_LOOP;
247     my %seen;
248     my @tab_data; # all tags to display
249
250     my $max_num_tab=-1;
251     my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" );
252     foreach my $used ( @$usedTagsLib ){
253
254         push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
255         $seen{$used->{tagfield}}++;
256
257         if (   $used->{tab} > -1
258             && $used->{tab} >= $max_num_tab
259             && $used->{tagfield} ne $itemtag )
260         {
261             $max_num_tab = $used->{tab};
262         }
263     }
264     if($max_num_tab >= 9){
265         $max_num_tab = 9;
266     }
267
268     my $biblio_form_builder = Koha::UI::Form::Builder::Biblio->new(
269         {
270             biblionumber => scalar $input->param('biblionumber'),
271         }
272     );
273
274     # loop through each tab 0 through 9
275     for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
276         my @loop_data = (); #innerloop in the template.
277         my $i = 0;
278         foreach my $tag (sort @tab_data) {
279             $i++;
280             next if ! $tag;
281             my ($indicator1, $indicator2);
282             my $index_tag = CreateKey;
283
284             # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
285             # if MARC::Record is empty => use tab as master loop.
286             if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
287                 my @fields;
288                 if ( $tag ne '000' ) {
289                     @fields = $record->field($tag);
290                 }
291                 else {
292                    push @fields, $record->leader(); # if tag == 000
293                 }
294                 # loop through each field
295                 foreach my $field (@fields) {
296                     
297                     my @subfields_data;
298                     if ( $tag < 10 ) {
299                         my ( $value, $subfield );
300                         if ( $tag ne '000' ) {
301                             $value    = $field->data();
302                             $subfield = "@";
303                         }
304                         else {
305                             $value    = $field;
306                             $subfield = '@';
307                         }
308                         next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
309                         next
310                           if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
311                             'biblio.biblionumber' );
312                         push(
313                             @subfields_data,
314                             $biblio_form_builder->generate_subfield_form(
315                                 {
316                                     tag               => $tag,
317                                     subfield          => $subfield,
318                                     value             => $value,
319                                     index_tag         => $index_tag,
320                                     record            => $record,
321                                     hostitemnumber    => scalar $input->param('hostitemnumber'),
322                                     op                => $op,
323                                     changed_framework => $changed_framework,
324                                     breedingid        => scalar $input->param('breedingid'),
325                                     tagslib           => $tagslib,
326                                     mandatory_z3950   => $mandatory_z3950,
327                                 }
328                             )
329                         );
330                     }
331                     else {
332                         my @subfields = $field->subfields();
333                         foreach my $subfieldcount ( 0 .. $#subfields ) {
334                             my $subfield = $subfields[$subfieldcount][0];
335                             my $value    = $subfields[$subfieldcount][1];
336                             next if ( length $subfield != 1 );
337                             next if ( !defined $tagslib->{$tag}->{$subfield} || $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
338                             push(
339                                 @subfields_data,
340                                 $biblio_form_builder->generate_subfield_form(
341                                     {
342                                         tag               => $tag,
343                                         subfield          => $subfield,
344                                         value             => $value,
345                                         index_tag         => $index_tag,
346                                         record            => $record,
347                                         hostitemnumber    => scalar $input->param('hostitemnumber'),
348                                         op                => $op,
349                                         changed_framework => $changed_framework,
350                                         breedingid        => scalar $input->param('breedingid'),
351                                         tagslib           => $tagslib,
352                                         mandatory_z3950   => $mandatory_z3950,
353                                     }
354                                 )
355                             );
356                         }
357                     }
358
359                     # now, loop again to add parameter subfield that are not in the MARC::Record
360                     foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
361                     {
362                         next if ( length $subfield != 1 );
363                         next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
364                         next if ( $tag < 10 );
365                         next
366                           if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
367                             or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
368                             and not ( $subfield eq "9" and
369                                       exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
370                                       defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
371                                       $tagslib->{$tag}->{'a'}->{authtypecode} ne "" and
372                                       $tagslib->{$tag}->{'a'}->{hidden} > -4 and
373                                       $tagslib->{$tag}->{'a'}->{hidden} < 5
374                                     )
375                           ;    #check for visibility flag
376                                # if subfield is $9 in a field whose $a is authority-controlled,
377                                # always include in the form regardless of the hidden setting - bug 2206 and 28022
378                         next if ( defined( $field->subfield($subfield) ) );
379                         push(
380                             @subfields_data,
381                             $biblio_form_builder->generate_subfield_form(
382                                 {
383                                     tag               => $tag,
384                                     subfield          => $subfield,
385                                     value             => '',
386                                     index_tag         => $index_tag,
387                                     record            => $record,
388                                     hostitemnumber    => scalar $input->param('hostitemnumber'),
389                                     op                => $op,
390                                     changed_framework => $changed_framework,
391                                     breedingid        => scalar $input->param('breedingid'),
392                                     tagslib           => $tagslib,
393                                     mandatory_z3950   => $mandatory_z3950,
394                                 }
395                             )
396                         );
397                     }
398                     if ( $#subfields_data >= 0 ) {
399                         # build the tag entry.
400                         # note that the random() field is mandatory. Otherwise, on repeated fields, you'll 
401                         # have twice the same "name" value, and cgi->param() will return only one, making
402                         # all subfields to be merged in a single field.
403                         my %tag_data = (
404                             tag           => $tag,
405                             index         => $index_tag,
406                             tag_lib       => $tagslib->{$tag}->{lib},
407                             repeatable       => $tagslib->{$tag}->{repeatable},
408                             mandatory       => $tagslib->{$tag}->{mandatory},
409                             important       => $tagslib->{$tag}->{important},
410                             subfield_loop => \@subfields_data,
411                             fixedfield    => $tag < 10?1:0,
412                             random        => CreateKey,
413                         );
414                         if ($tag >= 10){ # no indicator for 00x tags
415                            $tag_data{indicator1} = format_indicator($field->indicator(1)),
416                            $tag_data{indicator2} = format_indicator($field->indicator(2)),
417                         }
418                         push( @loop_data, \%tag_data );
419                     }
420                  } # foreach $field end
421
422             # if breeding is empty
423             }
424             else {
425                 my @subfields_data;
426                 foreach my $subfield (
427                     sort { $a->{display_order} <=> $b->{display_order} || $a->{subfield} cmp $b->{subfield} }
428                     grep { ref($_) && %$_ } # Not a subfield (values for "important", "lib", "mandatory", etc.) or empty
429                     values %{ $tagslib->{$tag} } )
430                 {
431                     next
432                       if ( ( $subfield->{hidden} <= -4 )
433                         or ( $subfield->{hidden} >= 5 ) )
434                       and not ( $subfield->{subfield} eq "9" and
435                                 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
436                                 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
437                                 $tagslib->{$tag}->{'a'}->{authtypecode} ne "" and
438                                 $tagslib->{$tag}->{'a'}->{hidden} > -4 and
439                                 $tagslib->{$tag}->{'a'}->{hidden} < 5
440                               )
441                       ;    #check for visibility flag
442                            # if subfield is $9 in a field whose $a is authority-controlled,
443                            # always include in the form regardless of the hidden setting - bug 2206 and 28022
444                     next
445                       if ( $subfield->{tab} ne $tabloop );
446                         push(
447                             @subfields_data,
448                             $biblio_form_builder->generate_subfield_form(
449                                 {
450                                     tag               => $tag,
451                                     subfield          => $subfield->{subfield},
452                                     value             => '',
453                                     index_tag         => $index_tag,
454                                     record            => $record,
455                                     hostitemnumber    => scalar $input->param('hostitemnumber'),
456                                     op                => $op,
457                                     changed_framework => $changed_framework,
458                                     breedingid        => scalar $input->param('breedingid'),
459                                     tagslib           => $tagslib,
460                                     mandatory_z3950   => $mandatory_z3950,
461                                 }
462                             )
463                         );
464                 }
465                 if ( $#subfields_data >= 0 ) {
466                     my %tag_data = (
467                         tag              => $tag,
468                         index            => $index_tag,
469                         tag_lib          => $tagslib->{$tag}->{lib},
470                         repeatable       => $tagslib->{$tag}->{repeatable},
471                         mandatory       => $tagslib->{$tag}->{mandatory},
472                         important       => $tagslib->{$tag}->{important},
473                         indicator1       => ( $indicator1 || $tagslib->{$tag}->{ind1_defaultvalue} ), #if not set, try to load the default value
474                         indicator2       => ( $indicator2 || $tagslib->{$tag}->{ind2_defaultvalue} ), #use short-circuit operator for efficiency
475                         subfield_loop    => \@subfields_data,
476                         tagfirstsubfield => $subfields_data[0],
477                         fixedfield       => $tag < 10?1:0,
478                     );
479                     
480                     push @loop_data, \%tag_data ;
481                 }
482             }
483         }
484         if ( $#loop_data >= 0 ) {
485             push @BIG_LOOP, {
486                 number    => $tabloop,
487                 innerloop => \@loop_data,
488             };
489         }
490     }
491     $authorised_values_sth->finish;
492     $template->param( BIG_LOOP => \@BIG_LOOP );
493 }
494
495 # ========================
496 #          MAIN
497 #=========================
498 my $input = CGI->new;
499 my $error = $input->param('error');
500 my $biblionumber  = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
501 my $parentbiblio  = $input->param('parentbiblionumber');
502 my $breedingid    = $input->param('breedingid');
503 my $z3950         = $input->param('z3950');
504 $op               = $input->param('op') // q{};
505 my $mode          = $input->param('mode') // q{};
506 my $frameworkcode = $input->param('frameworkcode');
507 my $redirect      = $input->param('redirect');
508 my $searchid      = $input->param('searchid') // "";
509 my $dbh           = C4::Context->dbh;
510 my $hostbiblionumber = $input->param('hostbiblionumber');
511 my $hostitemnumber = $input->param('hostitemnumber');
512 # fast cataloguing datas in transit
513 my $fa_circborrowernumber = $input->param('circborrowernumber');
514 my $fa_barcode            = $input->param('barcode');
515 my $fa_branch             = $input->param('branch');
516 my $fa_stickyduedate      = $input->param('stickyduedate');
517 my $fa_duedatespec        = $input->param('duedatespec');
518
519 my $userflags = 'edit_catalogue';
520
521 if ( $op eq 'cud-change-framework' ) {
522     $op = $input->param('original_op');
523     $changed_framework = 1;
524 }
525
526 $frameworkcode = &GetFrameworkCode($biblionumber)
527   if ( $biblionumber and not( defined $frameworkcode) and $op ne 'cud-addbiblio' );
528
529 if ($frameworkcode eq 'FA'){
530     $userflags = 'fast_cataloging';
531 }
532
533 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
534 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
535     {
536         template_name   => "cataloguing/addbiblio.tt",
537         query           => $input,
538         type            => "intranet",
539         flagsrequired   => { editcatalogue => $userflags },
540     }
541 );
542
543 my $logged_in_patron = Koha::Patrons->find($loggedinuser);
544 my $biblio;
545
546 if ($biblionumber) {
547
548     $biblio = Koha::Biblios->find($biblionumber);
549
550     # just in case $biblionumber obtained from CGI contains weird characters like spaces
551     $biblionumber = $biblio->biblionumber if $biblio;
552     if ($biblio) {
553         unless ( $biblio->can_be_edited($logged_in_patron) ) {
554             print $input->redirect("/cgi-bin/koha/errors/403.pl");    # escape early
555             exit;
556         }
557     } else {
558         $biblionumber = undef;
559         $template->param( bib_doesnt_exist => 1 );
560     }
561 }
562
563 if ($frameworkcode eq 'FA'){
564     # We need to grab and set some variables in the template for use on the additems screen
565     $template->param(
566         'circborrowernumber' => $fa_circborrowernumber,
567         'barcode'            => $fa_barcode,
568         'branch'             => $fa_branch,
569         'stickyduedate'      => $fa_stickyduedate,
570         'duedatespec'        => $fa_duedatespec,
571     );
572 } elsif ( $op ne "cud-delete" &&
573             C4::Context->preference('EnableAdvancedCatalogingEditor') &&
574             C4::Auth::haspermission(C4::Context->userenv->{id},{'editcatalogue'=>'advanced_editor'}) &&
575             $input->cookie( 'catalogue_editor_' . $loggedinuser ) eq 'advanced' &&
576             !$breedingid ) {
577     # Only use the advanced editor for non-fast-cataloging.
578     # breedingid is not handled because those would only come off a Z39.50
579     # search initiated by the basic editor.
580     print $input->redirect( '/cgi-bin/koha/cataloguing/editor.pl' . ( $biblionumber ? ( ($op eq 'duplicate'?'#duplicate/':'#catalog/') . $biblionumber ) : '' ) );
581     exit;
582 }
583
584 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
585 $template->param(
586     frameworks => $frameworks,
587     breedingid => $breedingid,
588 );
589
590 # ++ Global
591 $tagslib         = &GetMarcStructure( 1, $frameworkcode );
592 $usedTagsLib     = &GetUsedMarcStructure( $frameworkcode );
593 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
594 # -- Global
595
596 my $record   = -1;
597 my $encoding = "";
598 my (
599         $biblionumbertagfield,
600         $biblionumbertagsubfield,
601         $biblioitemnumtagfield,
602         $biblioitemnumtagsubfield,
603         $biblioitemnumber
604 );
605
606 if ( $biblio && !$breedingid ) {
607     eval { $record = $biblio->metadata->record };
608     if ($@) {
609         my $exception = $@;
610         $exception->rethrow unless ( $exception->isa('Koha::Exceptions::Metadata::Invalid') );
611         $record = $biblio->metadata->record_strip_nonxml;
612         $template->param( INVALID_METADATA => $exception );
613     }
614 }
615 if ($breedingid) {
616     ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
617 }
618 if ( $record && $op eq 'duplicate' &&
619      C4::Context->preference('autoControlNumber') eq 'biblionumber' ){
620     my @control_num = $record->field('001');
621     $record->delete_fields(@control_num);
622 }
623 if ( $record && $op eq 'duplicate' ) {
624     if ( C4::Context->preference('marcflavour') eq 'MARC21' && $record->field('008') ) {
625         my $s008 = $record->field('008')->data;
626         my $date = POSIX::strftime( "%y%m%d", localtime );
627         substr( $s008, 0, 6, $date );
628         $record->field('008')->update($s008);
629     } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield( '100', 'a' ) ) {
630         my $s100a = $record->subfield( '100', 'a' );
631         my $date  = POSIX::strftime( "%Y%m%d", localtime );
632         substr( $s100a, 0, 8, $date );
633         $record->field('100')->update( a => $s100a );
634     }
635 }
636 #populate hostfield if hostbiblionumber is available
637 if ($hostbiblionumber) {
638     my $marcflavour = C4::Context->preference("marcflavour");
639     $record = MARC::Record->new();
640     $record->leader('');
641     my $field =
642       PrepHostMarcField( $hostbiblionumber, $hostitemnumber, $marcflavour );
643     $record->append_fields($field);
644 }
645
646 # This is  a child record
647 if ($parentbiblio) {
648     my $marcflavour = C4::Context->preference('marcflavour');
649     $record = MARC::Record->new();
650     SetMarcUnicodeFlag($record, $marcflavour);
651     my $hostfield = prepare_host_field($parentbiblio,$marcflavour);
652     if ($hostfield) {
653         $record->append_fields($hostfield);
654     }
655 }
656
657 $is_a_modif = 0;
658
659 if ($biblionumber) {
660     $is_a_modif = 1;
661     my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
662     $template->param( title => $title );
663
664     # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
665     ( $biblionumbertagfield, $biblionumbertagsubfield ) =
666         &GetMarcFromKohaField( "biblio.biblionumber" );
667     ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
668         &GetMarcFromKohaField( "biblioitems.biblioitemnumber" );
669
670     # search biblioitems value
671     my $sth =  $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
672     $sth->execute($biblionumber);
673     ($biblioitemnumber) = $sth->fetchrow;
674     if (C4::Context->preference('MARCOverlayRules')) {
675         $record = ApplyMarcOverlayRules(
676             {
677                 biblionumber    => $biblionumber,
678                 record          => $record,
679                 overlay_context =>  {
680                         source       => $z3950 ? 'z3950' : 'intranet',
681                         categorycode => $logged_in_patron->categorycode,
682                         userid       => $logged_in_patron->userid,
683                 }
684             }
685         );
686     }
687 }
688
689 #-------------------------------------------------------------------------------------
690 if ( $op eq "cud-addbiblio" ) {
691 #-------------------------------------------------------------------------------------
692     $template->param(
693         biblionumberdata => $biblionumber,
694     );
695     # getting html input
696     my @params = $input->multi_param();
697     $record = TransformHtmlToMarc( $input, 1 );
698     # check for a duplicate
699     my ( $duplicatebiblionumber, $duplicatetitle );
700     if ( !$is_a_modif ) {
701         ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
702     }
703     my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
704     # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
705     if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
706         my $oldbibitemnum;
707         if ( $is_a_modif ) {
708             ModBiblio(
709                 $record,
710                 $biblionumber,
711                 $frameworkcode,
712                 {
713                     overlay_context => {
714                         source       => $z3950 ? 'z3950' : 'intranet',
715                         categorycode => $logged_in_patron->categorycode,
716                         userid       => $logged_in_patron->userid,
717                     }
718                 }
719             );
720         }
721         else {
722             ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
723         }
724         if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view" && $redirect ne "just_save")){
725             if ($frameworkcode eq 'FA'){
726                 print $input->redirect(
727             '/cgi-bin/koha/cataloguing/additem.pl?'
728             .'biblionumber='.$biblionumber
729             .'&frameworkcode='.$frameworkcode
730             .'&circborrowernumber='.$fa_circborrowernumber
731             .'&branch='.$fa_branch
732             .'&barcode='.uri_escape_utf8($fa_barcode)
733             .'&stickyduedate='.$fa_stickyduedate
734             .'&duedatespec='.$fa_duedatespec
735                 );
736                 exit;
737             }
738             else {
739                 print $input->redirect(
740                 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid"
741                 );
742                 exit;
743             }
744         }
745     elsif(($is_a_modif || $redirect eq "view") && $redirect ne "just_save"){
746             my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
747             my $views = { C4::Search::enabled_staff_search_views };
748             if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
749                 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
750             } elsif  ($defaultview eq 'marc' && $views->{can_view_MARC}) {
751                 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
752             } elsif  ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
753                 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
754             } else {
755                 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
756             }
757             exit;
758
759     }
760     elsif ($redirect eq "just_save"){
761         my $tab = $input->param('current_tab');
762         print $input->redirect("/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber&framework=$frameworkcode&tab=$tab&searchid=$searchid");
763     }
764     else {
765           $template->param(
766             biblionumber => $biblionumber,
767             done         =>1,
768             popup        =>1
769           );
770           if ( $record ne '-1' ) {
771               my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
772               $template->param( title => $title );
773           }
774           $template->param(
775             popup => $mode,
776             itemtype => $frameworkcode,
777           );
778           output_html_with_http_headers $input, $cookie, $template->output;
779           exit;     
780         }
781     } else {
782     # it may be a duplicate, warn the user and do nothing
783         build_tabs ($template, $record, $dbh,$encoding,$input);
784         $template->param(
785             biblionumber             => $biblionumber,
786             biblioitemnumber         => $biblioitemnumber,
787             duplicatebiblionumber    => $duplicatebiblionumber,
788             duplicatebibid           => $duplicatebiblionumber,
789             duplicatetitle           => $duplicatetitle,
790         );
791     }
792 }
793     
794 elsif ( $op eq "cud-delete" ) {
795
796     # Cancel attached order lines first before deleting biblio !
797     my $error;
798     try {
799         my @result = Koha::Acquisition::Orders->search( { biblionumber => $biblionumber } )->cancel;
800         my $warns  = @{ $result[1] };
801         if ($warns) {    # warnings about order lines not cancelled
802             warn sprintf( "%d order lines were cancelled, but %d lines gave a warning\n", $result[0], $warns );
803         }
804         $error = &DelBiblio($biblionumber);
805     } catch {
806         $error = ref($_) ? 'Exception raised - ' . $_->error : $_;
807     };
808
809     if ($error) {
810         #FIXME This should be handled in template alert
811         warn "ERROR when DELETING BIBLIO $biblionumber : $error";
812         print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
813         exit;
814     }
815
816     print $input->redirect('/cgi-bin/koha/catalogue/search.pl' . ($searchid ? "?searchid=$searchid" : ""));
817     exit;
818     
819 } else {
820    #----------------------------------------------------------------------------
821    # If we're in a duplication case, we have to set to "" the biblionumber
822    # as we'll save the biblio as a new one.
823     $template->param(
824         biblionumberdata => $biblionumber,
825         op               => $op,
826         z3950            => $z3950
827     );
828     if ( $op eq "duplicate" ) {
829         $biblionumber = "";
830     }
831
832     if($changed_framework){
833         $record = TransformHtmlToMarc( $input, 1 );
834     }
835     elsif( $record ne -1 ) {
836 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
837         eval {
838             my $uxml = $record->as_xml;
839             MARC::Record::default_record_format("UNIMARC")
840             if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
841             my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
842             $record = $urecord;
843         };
844     }
845     build_tabs( $template, $record, $dbh, $encoding,$input );
846     $template->param(
847         biblionumber             => $biblionumber,
848         biblionumbertagfield        => $biblionumbertagfield,
849         biblionumbertagsubfield     => $biblionumbertagsubfield,
850         biblioitemnumtagfield    => $biblioitemnumtagfield,
851         biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
852         biblioitemnumber         => $biblioitemnumber,
853         hostbiblionumber        => $hostbiblionumber,
854         hostitemnumber          => $hostitemnumber
855     );
856 }
857
858 if ( $record ne '-1' ) {
859     my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
860     $template->param( title => $title );
861 }
862 $template->param(
863     popup => $mode,
864     frameworkcode => $frameworkcode,
865     itemtype => $frameworkcode,
866     borrowernumber => $loggedinuser,
867     tab => scalar $input->param('tab')
868 );
869 $template->{'VARS'}->{'searchid'} = $searchid;
870
871 output_html_with_http_headers $input, $cookie, $template->output;