Bug 19794: DBRev 17.12.00.032
[koha.git] / cataloguing / addbiblio.pl
blob0f40ecd18f8b4e9a8d576f85c1fd23c77f944d68
1 #!/usr/bin/perl
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>.
22 use Modern::Perl;
24 use CGI q(-utf8);
25 use C4::Output;
26 use C4::Auth;
27 use C4::Biblio;
28 use C4::Search;
29 use C4::AuthoritiesMarc;
30 use C4::Context;
31 use MARC::Record;
32 use C4::Log;
33 use C4::Koha;
34 use C4::ClassSource;
35 use C4::ImportBatch;
36 use C4::Charset;
37 use Koha::BiblioFrameworks;
38 use Koha::DateUtils;
40 use Koha::ItemTypes;
41 use Koha::Libraries;
43 use Koha::BiblioFrameworks;
45 use Date::Calc qw(Today);
46 use MARC::File::USMARC;
47 use MARC::File::XML;
48 use URI::Escape;
50 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
51 MARC::File::XML->default_record_format('UNIMARC');
54 our($tagslib,$authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
56 =head1 FUNCTIONS
58 =head2 MARCfindbreeding
60 $record = MARCfindbreeding($breedingid);
62 Look up the import record repository for the record with
63 record with id $breedingid. If found, returns the decoded
64 MARC::Record; otherwise, -1 is returned (FIXME).
65 Returns as second parameter the character encoding.
67 =cut
69 sub MARCfindbreeding {
70 my ( $id ) = @_;
71 my ($marc, $encoding) = GetImportRecordMarc($id);
72 # remove the - in isbn, koha store isbn without any -
73 if ($marc) {
74 my $record = MARC::Record->new_from_usmarc($marc);
75 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
76 if ( $record->field($isbnfield) ) {
77 foreach my $field ( $record->field($isbnfield) ) {
78 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
79 my $newisbn = $field->subfield($isbnsubfield);
80 $newisbn =~ s/-//g;
81 $field->update( $isbnsubfield => $newisbn );
85 # fix the unimarc 100 coded field (with unicode information)
86 if (C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield(100,'a')) {
87 my $f100a=$record->subfield(100,'a');
88 my $f100 = $record->field(100);
89 my $f100temp = $f100->as_string;
90 $record->delete_field($f100);
91 if ( length($f100temp) > 28 ) {
92 substr( $f100temp, 26, 2, "50" );
93 $f100->update( 'a' => $f100temp );
94 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
95 $record->insert_fields_ordered($f100);
99 if ( !defined(ref($record)) ) {
100 return -1;
102 else {
103 # normalize author : UNIMARC specific...
104 if ( C4::Context->preference("z3950NormalizeAuthor")
105 and C4::Context->preference("z3950AuthorAuthFields")
106 and C4::Context->preference("marcflavour") eq 'UNIMARC' )
108 my ( $tag, $subfield ) = GetMarcFromKohaField("biblio.author", '');
110 my $auth_fields =
111 C4::Context->preference("z3950AuthorAuthFields");
112 my @auth_fields = split /,/, $auth_fields;
113 my $field;
115 if ( $record->field($tag) ) {
116 foreach my $tmpfield ( $record->field($tag)->subfields ) {
118 my $subfieldcode = shift @$tmpfield;
119 my $subfieldvalue = shift @$tmpfield;
120 if ($field) {
121 $field->add_subfields(
122 "$subfieldcode" => $subfieldvalue )
123 if ( $subfieldcode ne $subfield );
125 else {
126 $field =
127 MARC::Field->new( $tag, "", "",
128 $subfieldcode => $subfieldvalue )
129 if ( $subfieldcode ne $subfield );
133 $record->delete_field( $record->field($tag) );
134 foreach my $fieldtag (@auth_fields) {
135 next unless ( $record->field($fieldtag) );
136 my $lastname = $record->field($fieldtag)->subfield('a');
137 my $firstname = $record->field($fieldtag)->subfield('b');
138 my $title = $record->field($fieldtag)->subfield('c');
139 my $number = $record->field($fieldtag)->subfield('d');
140 if ($title) {
141 $field->add_subfields(
142 "$subfield" => ucfirst($title) . " "
143 . ucfirst($firstname) . " "
144 . $number );
146 else {
147 $field->add_subfields(
148 "$subfield" => ucfirst($firstname) . ", "
149 . ucfirst($lastname) );
152 $record->insert_fields_ordered($field);
154 return $record, $encoding;
157 return -1;
160 =head2 build_authorized_values_list
162 =cut
164 sub build_authorized_values_list {
165 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
167 my @authorised_values;
168 my %authorised_lib;
170 # builds list, depending on authorised value...
172 #---- branch
173 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
174 my $libraries = Koha::Libraries->search_filtered({}, {order_by => ['branchname']});
175 while ( my $l = $libraries->next ) {
176 push @authorised_values, $l->branchcode;;
177 $authorised_lib{$l->branchcode} = $l->branchname;
180 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
181 push @authorised_values, ""
182 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
183 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
185 my $itemtype;
186 my $itemtypes = Koha::ItemTypes->search_with_localization;
187 while ( $itemtype = $itemtypes->next ) {
188 push @authorised_values, $itemtype->itemtype;
189 $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
191 $value = $itemtype unless ($value);
193 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
194 push @authorised_values, ""
195 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
197 my $class_sources = GetClassSources();
199 my $default_source = C4::Context->preference("DefaultClassificationSource");
201 foreach my $class_source (sort keys %$class_sources) {
202 next unless $class_sources->{$class_source}->{'used'} or
203 ($value and $class_source eq $value) or
204 ($class_source eq $default_source);
205 push @authorised_values, $class_source;
206 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
208 $value = $default_source unless $value;
210 else {
211 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
212 $authorised_values_sth->execute(
213 $tagslib->{$tag}->{$subfield}->{authorised_value},
214 $branch_limit ? $branch_limit : (),
217 push @authorised_values, ""
218 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
219 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
221 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
222 push @authorised_values, $value;
223 $authorised_lib{$value} = $lib;
226 $authorised_values_sth->finish;
227 return {
228 type => 'select',
229 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
230 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
231 default => $value,
232 values => \@authorised_values,
233 labels => \%authorised_lib,
238 =head2 CreateKey
240 Create a random value to set it into the input name
242 =cut
244 sub CreateKey {
245 return int(rand(1000000));
248 =head2 GetMandatoryFieldZ3950
250 This function returns a hashref which contains all mandatory field
251 to search with z3950 server.
253 =cut
255 sub GetMandatoryFieldZ3950 {
256 my $frameworkcode = shift;
257 my @isbn = GetMarcFromKohaField('biblioitems.isbn',$frameworkcode);
258 my @title = GetMarcFromKohaField('biblio.title',$frameworkcode);
259 my @author = GetMarcFromKohaField('biblio.author',$frameworkcode);
260 my @issn = GetMarcFromKohaField('biblioitems.issn',$frameworkcode);
261 my @lccn = GetMarcFromKohaField('biblioitems.lccn',$frameworkcode);
263 return {
264 $isbn[0].$isbn[1] => 'isbn',
265 $title[0].$title[1] => 'title',
266 $author[0].$author[1] => 'author',
267 $issn[0].$issn[1] => 'issn',
268 $lccn[0].$lccn[1] => 'lccn',
272 =head2 create_input
274 builds the <input ...> entry for a subfield.
276 =cut
278 sub create_input {
279 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
281 my $index_subfield = CreateKey(); # create a specifique key for each subfield
283 $value =~ s/"/&quot;/g;
285 # if there is no value provided but a default value in parameters, get it
286 if ( $value eq '' ) {
287 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
289 # get today date & replace <<YYYY>>, <<MM>>, <<DD>> if provided in the default value
290 my $today_dt = dt_from_string;
291 my $year = $today_dt->strftime('%Y');
292 my $month = $today_dt->strftime('%m');
293 my $day = $today_dt->strftime('%d');
294 $value =~ s/<<YYYY>>/$year/g;
295 $value =~ s/<<MM>>/$month/g;
296 $value =~ s/<<DD>>/$day/g;
297 # And <<USER>> with surname (?)
298 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
299 $value=~s/<<USER>>/$username/g;
302 my $dbh = C4::Context->dbh;
304 # map '@' as "subfield" label for fixed fields
305 # to something that's allowed in a div id.
306 my $id_subfield = $subfield;
307 $id_subfield = "00" if $id_subfield eq "@";
309 my %subfield_data = (
310 tag => $tag,
311 subfield => $id_subfield,
312 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
313 tag_mandatory => $tagslib->{$tag}->{mandatory},
314 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
315 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
316 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
317 index => $index_tag,
318 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
319 value => $value,
320 maxlength => $tagslib->{$tag}->{$subfield}->{maxlength},
321 random => CreateKey(),
324 if(exists $mandatory_z3950->{$tag.$subfield}){
325 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
327 # Subfield is hidden depending of hidden and mandatory flag, and is always
328 # shown if it contains anything or if its field is mandatory.
329 my $tdef = $tagslib->{$tag};
330 $subfield_data{visibility} = "display:none;"
331 if $tdef->{$subfield}->{hidden} % 2 == 1 &&
332 $value eq '' &&
333 !$tdef->{$subfield}->{mandatory} &&
334 !$tdef->{mandatory};
335 # expand all subfields of 773 if there is a host item provided in the input
336 $subfield_data{visibility} ="" if ($tag eq 773 and $cgi->param('hostitemnumber'));
339 # it's an authorised field
340 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
341 $subfield_data{marc_value} =
342 build_authorized_values_list( $tag, $subfield, $value, $dbh,
343 $authorised_values_sth,$index_tag,$index_subfield );
345 # it's a subfield $9 linking to an authority record - see bug 2206
347 elsif ($subfield eq "9" and
348 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
349 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
350 $tagslib->{$tag}->{'a'}->{authtypecode} ne '') {
352 $subfield_data{marc_value} = {
353 type => 'text',
354 id => $subfield_data{id},
355 name => $subfield_data{id},
356 value => $value,
357 size => 5,
358 maxlength => $subfield_data{maxlength},
359 readonly => 1,
362 # it's a thesaurus / authority field
364 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
365 # when authorities auto-creation is allowed, do not set readonly
366 my $is_readonly = !C4::Context->preference("BiblioAddsAuthorities");
368 $subfield_data{marc_value} = {
369 type => 'text',
370 id => $subfield_data{id},
371 name => $subfield_data{id},
372 value => $value,
373 size => 67,
374 maxlength => $subfield_data{maxlength},
375 readonly => ($is_readonly) ? 1 : 0,
376 authtype => $tagslib->{$tag}->{$subfield}->{authtypecode},
379 # it's a plugin field
380 } elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
381 require Koha::FrameworkPlugin;
382 my $plugin = Koha::FrameworkPlugin->new( {
383 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
385 my $pars= { dbh => $dbh, record => $rec, tagslib => $tagslib,
386 id => $subfield_data{id}, tabloop => $tabloop };
387 $plugin->build( $pars );
388 if( !$plugin->errstr ) {
389 $subfield_data{marc_value} = {
390 type => 'text_complex',
391 id => $subfield_data{id},
392 name => $subfield_data{id},
393 value => $value,
394 size => 67,
395 maxlength => $subfield_data{maxlength},
396 javascript => $plugin->javascript,
397 noclick => $plugin->noclick,
399 } else {
400 warn $plugin->errstr;
401 # supply default input form
402 $subfield_data{marc_value} = {
403 type => 'text',
404 id => $subfield_data{id},
405 name => $subfield_data{id},
406 value => $value,
407 size => 67,
408 maxlength => $subfield_data{maxlength},
409 readonly => 0,
413 # it's an hidden field
414 } elsif ( $tag eq '' ) {
415 $subfield_data{marc_value} = {
416 type => 'hidden',
417 id => $subfield_data{id},
418 name => $subfield_data{id},
419 value => $value,
420 size => 67,
421 maxlength => $subfield_data{maxlength},
425 else {
426 # it's a standard field
427 if (
428 length($value) > 100
430 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
431 and $tag < 400 && $subfield eq 'a' )
432 or ( $tag >= 500
433 and $tag < 600
434 && C4::Context->preference("marcflavour") eq "MARC21" )
437 $subfield_data{marc_value} = {
438 type => 'textarea',
439 id => $subfield_data{id},
440 name => $subfield_data{id},
441 value => $value,
445 else {
446 $subfield_data{marc_value} = {
447 type => 'text',
448 id => $subfield_data{id},
449 name => $subfield_data{id},
450 value => $value,
451 size => 67,
452 maxlength => $subfield_data{maxlength},
453 readonly => 0,
458 $subfield_data{'index_subfield'} = $index_subfield;
459 return \%subfield_data;
463 =head2 format_indicator
465 Translate indicator value for output form - specifically, map
466 indicator = ' ' to ''. This is for the convenience of a cataloger
467 using a mouse to select an indicator input.
469 =cut
471 sub format_indicator {
472 my $ind_value = shift;
473 return '' if not defined $ind_value;
474 return '' if $ind_value eq ' ';
475 return $ind_value;
478 sub build_tabs {
479 my ( $template, $record, $dbh, $encoding,$input ) = @_;
481 # fill arrays
482 my @loop_data = ();
483 my $tag;
485 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
486 my $query = "SELECT authorised_value, lib
487 FROM authorised_values";
488 $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id )} if $branch_limit;
489 $query .= " WHERE category = ?";
490 $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
491 $query .= " GROUP BY lib ORDER BY lib, lib_opac";
492 my $authorised_values_sth = $dbh->prepare( $query );
494 # in this array, we will push all the 10 tabs
495 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
496 my @BIG_LOOP;
497 my %seen;
498 my @tab_data; # all tags to display
500 foreach my $used ( @$usedTagsLib ){
501 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
502 $seen{$used->{tagfield}}++;
505 my $max_num_tab=-1;
506 foreach(@$usedTagsLib){
507 if($_->{tab} > -1 && $_->{tab} >= $max_num_tab && $_->{tagfield} != '995'){ # FIXME : MARC21 ?
508 $max_num_tab = $_->{tab};
511 if($max_num_tab >= 9){
512 $max_num_tab = 9;
514 # loop through each tab 0 through 9
515 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
516 my @loop_data = (); #innerloop in the template.
517 my $i = 0;
518 foreach my $tag (@tab_data) {
519 $i++;
520 next if ! $tag;
521 my ($indicator1, $indicator2);
522 my $index_tag = CreateKey;
524 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
525 # if MARC::Record is empty => use tab as master loop.
526 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
527 my @fields;
528 if ( $tag ne '000' ) {
529 @fields = $record->field($tag);
531 else {
532 push @fields, $record->leader(); # if tag == 000
534 # loop through each field
535 foreach my $field (@fields) {
537 my @subfields_data;
538 if ( $tag < 10 ) {
539 my ( $value, $subfield );
540 if ( $tag ne '000' ) {
541 $value = $field->data();
542 $subfield = "@";
544 else {
545 $value = $field;
546 $subfield = '@';
548 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
549 next
550 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
551 'biblio.biblionumber' );
552 push(
553 @subfields_data,
554 &create_input(
555 $tag, $subfield, $value, $index_tag, $tabloop, $record,
556 $authorised_values_sth,$input
560 else {
561 my @subfields = $field->subfields();
562 foreach my $subfieldcount ( 0 .. $#subfields ) {
563 my $subfield = $subfields[$subfieldcount][0];
564 my $value = $subfields[$subfieldcount][1];
565 next if ( length $subfield != 1 );
566 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
567 push(
568 @subfields_data,
569 &create_input(
570 $tag, $subfield, $value, $index_tag, $tabloop,
571 $record, $authorised_values_sth,$input
577 # now, loop again to add parameter subfield that are not in the MARC::Record
578 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
580 next if ( length $subfield != 1 );
581 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
582 next if ( $tag < 10 );
583 next
584 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
585 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
586 and not ( $subfield eq "9" and
587 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
588 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
589 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
591 ; #check for visibility flag
592 # if subfield is $9 in a field whose $a is authority-controlled,
593 # always include in the form regardless of the hidden setting - bug 2206
594 next if ( defined( $field->subfield($subfield) ) );
595 push(
596 @subfields_data,
597 &create_input(
598 $tag, $subfield, '', $index_tag, $tabloop, $record,
599 $authorised_values_sth,$input
603 if ( $#subfields_data >= 0 ) {
604 # build the tag entry.
605 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
606 # have twice the same "name" value, and cgi->param() will return only one, making
607 # all subfields to be merged in a single field.
608 my %tag_data = (
609 tag => $tag,
610 index => $index_tag,
611 tag_lib => $tagslib->{$tag}->{lib},
612 repeatable => $tagslib->{$tag}->{repeatable},
613 mandatory => $tagslib->{$tag}->{mandatory},
614 subfield_loop => \@subfields_data,
615 fixedfield => $tag < 10?1:0,
616 random => CreateKey,
618 if ($tag >= 10){ # no indicator for 00x tags
619 $tag_data{indicator1} = format_indicator($field->indicator(1)),
620 $tag_data{indicator2} = format_indicator($field->indicator(2)),
622 push( @loop_data, \%tag_data );
624 } # foreach $field end
626 # if breeding is empty
628 else {
629 my @subfields_data;
630 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
631 next if ( length $subfield != 1 );
632 next
633 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
634 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
635 and not ( $subfield eq "9" and
636 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
637 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
638 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
640 ; #check for visibility flag
641 # if subfield is $9 in a field whose $a is authority-controlled,
642 # always include in the form regardless of the hidden setting - bug 2206
643 next
644 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
645 push(
646 @subfields_data,
647 &create_input(
648 $tag, $subfield, '', $index_tag, $tabloop, $record,
649 $authorised_values_sth,$input
653 if ( $#subfields_data >= 0 ) {
654 my %tag_data = (
655 tag => $tag,
656 index => $index_tag,
657 tag_lib => $tagslib->{$tag}->{lib},
658 repeatable => $tagslib->{$tag}->{repeatable},
659 mandatory => $tagslib->{$tag}->{mandatory},
660 indicator1 => ( $indicator1 || $tagslib->{$tag}->{ind1_defaultvalue} ), #if not set, try to load the default value
661 indicator2 => ( $indicator2 || $tagslib->{$tag}->{ind2_defaultvalue} ), #use short-circuit operator for efficiency
662 subfield_loop => \@subfields_data,
663 tagfirstsubfield => $subfields_data[0],
664 fixedfield => $tag < 10?1:0,
667 push @loop_data, \%tag_data ;
671 if ( $#loop_data >= 0 ) {
672 push @BIG_LOOP, {
673 number => $tabloop,
674 innerloop => \@loop_data,
678 $authorised_values_sth->finish;
679 $template->param( BIG_LOOP => \@BIG_LOOP );
682 # ========================
683 # MAIN
684 #=========================
685 my $input = new CGI;
686 my $error = $input->param('error');
687 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
688 my $parentbiblio = $input->param('parentbiblionumber');
689 my $breedingid = $input->param('breedingid');
690 my $z3950 = $input->param('z3950');
691 my $op = $input->param('op');
692 my $mode = $input->param('mode');
693 my $frameworkcode = $input->param('frameworkcode');
694 my $redirect = $input->param('redirect');
695 my $searchid = $input->param('searchid');
696 my $dbh = C4::Context->dbh;
697 my $hostbiblionumber = $input->param('hostbiblionumber');
698 my $hostitemnumber = $input->param('hostitemnumber');
699 # fast cataloguing datas in transit
700 my $fa_circborrowernumber = $input->param('circborrowernumber');
701 my $fa_barcode = $input->param('barcode');
702 my $fa_branch = $input->param('branch');
703 my $fa_stickyduedate = $input->param('stickyduedate');
704 my $fa_duedatespec = $input->param('duedatespec');
706 my $userflags = 'edit_catalogue';
708 my $changed_framework = $input->param('changed_framework');
709 $frameworkcode = &GetFrameworkCode($biblionumber)
710 if ( $biblionumber and not( defined $frameworkcode) and $op ne 'addbiblio' );
712 if ($frameworkcode eq 'FA'){
713 $userflags = 'fast_cataloging';
716 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
717 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
719 template_name => "cataloguing/addbiblio.tt",
720 query => $input,
721 type => "intranet",
722 authnotrequired => 0,
723 flagsrequired => { editcatalogue => $userflags },
727 if ($biblionumber){
728 my $does_bib_exist = Koha::Biblios->find($biblionumber);
729 if (!defined $does_bib_exist){
730 $biblionumber = undef;
731 $template->param( bib_doesnt_exist => 1 );
735 if ($frameworkcode eq 'FA'){
736 # We need to grab and set some variables in the template for use on the additems screen
737 $template->param(
738 'circborrowernumber' => $fa_circborrowernumber,
739 'barcode' => $fa_barcode,
740 'branch' => $fa_branch,
741 'stickyduedate' => $fa_stickyduedate,
742 'duedatespec' => $fa_duedatespec,
744 } elsif ( $op ne "delete" && C4::Context->preference('EnableAdvancedCatalogingEditor') && $input->cookie( 'catalogue_editor_' . $loggedinuser ) eq 'advanced' && !$breedingid ) {
745 # Only use the advanced editor for non-fast-cataloging.
746 # breedingid is not handled because those would only come off a Z39.50
747 # search initiated by the basic editor.
748 print $input->redirect( '/cgi-bin/koha/cataloguing/editor.pl' . ( $biblionumber ? ( '#catalog/' . $biblionumber ) : '' ) );
749 exit;
752 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
753 $template->param(
754 frameworks => $frameworks,
755 breedingid => $breedingid,
758 # ++ Global
759 $tagslib = &GetMarcStructure( 1, $frameworkcode );
760 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
761 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
762 # -- Global
764 my $record = -1;
765 my $encoding = "";
766 my (
767 $biblionumbertagfield,
768 $biblionumbertagsubfield,
769 $biblioitemnumtagfield,
770 $biblioitemnumtagsubfield,
771 $biblioitemnumber
774 if (($biblionumber) && !($breedingid)){
775 $record = GetMarcBiblio({ biblionumber => $biblionumber });
777 if ($breedingid) {
778 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
781 #populate hostfield if hostbiblionumber is available
782 if ($hostbiblionumber) {
783 my $marcflavour = C4::Context->preference("marcflavour");
784 $record = MARC::Record->new();
785 $record->leader('');
786 my $field =
787 PrepHostMarcField( $hostbiblionumber, $hostitemnumber, $marcflavour );
788 $record->append_fields($field);
791 # This is a child record
792 if ($parentbiblio) {
793 my $marcflavour = C4::Context->preference('marcflavour');
794 $record = MARC::Record->new();
795 SetMarcUnicodeFlag($record, $marcflavour);
796 my $hostfield = prepare_host_field($parentbiblio,$marcflavour);
797 if ($hostfield) {
798 $record->append_fields($hostfield);
802 $is_a_modif = 0;
804 if ($biblionumber) {
805 $is_a_modif = 1;
806 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
807 $template->param( title => $title );
809 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
810 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
811 &GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
812 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
813 &GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
815 # search biblioitems value
816 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
817 $sth->execute($biblionumber);
818 ($biblioitemnumber) = $sth->fetchrow;
821 #-------------------------------------------------------------------------------------
822 if ( $op eq "addbiblio" ) {
823 #-------------------------------------------------------------------------------------
824 $template->param(
825 biblionumberdata => $biblionumber,
827 # getting html input
828 my @params = $input->multi_param();
829 $record = TransformHtmlToMarc( $input, 1 );
830 # check for a duplicate
831 my ( $duplicatebiblionumber, $duplicatetitle );
832 if ( !$is_a_modif ) {
833 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
835 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
836 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
837 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
838 my $oldbibitemnum;
839 if (C4::Context->preference("BiblioAddsAuthorities")){
840 BiblioAutoLink( $record, $frameworkcode );
842 if ( $is_a_modif ) {
843 ModBiblio( $record, $biblionumber, $frameworkcode );
845 else {
846 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
848 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view" && $redirect ne "just_save")){
849 if ($frameworkcode eq 'FA'){
850 print $input->redirect(
851 '/cgi-bin/koha/cataloguing/additem.pl?'
852 .'biblionumber='.$biblionumber
853 .'&frameworkcode='.$frameworkcode
854 .'&circborrowernumber='.$fa_circborrowernumber
855 .'&branch='.$fa_branch
856 .'&barcode='.uri_escape_utf8($fa_barcode)
857 .'&stickyduedate='.$fa_stickyduedate
858 .'&duedatespec='.$fa_duedatespec
860 exit;
862 else {
863 print $input->redirect(
864 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid"
866 exit;
869 elsif(($is_a_modif || $redirect eq "view") && $redirect ne "just_save"){
870 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
871 my $views = { C4::Search::enabled_staff_search_views };
872 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
873 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
874 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
875 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
876 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
877 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
878 } else {
879 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
881 exit;
884 elsif ($redirect eq "just_save"){
885 my $tab = $input->param('current_tab');
886 print $input->redirect("/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber&framework=$frameworkcode&tab=$tab&searchid=$searchid");
888 else {
889 $template->param(
890 biblionumber => $biblionumber,
891 done =>1,
892 popup =>1
894 if ( $record ne '-1' ) {
895 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
896 $template->param( title => $title );
898 $template->param(
899 popup => $mode,
900 itemtype => $frameworkcode,
902 output_html_with_http_headers $input, $cookie, $template->output;
903 exit;
905 } else {
906 # it may be a duplicate, warn the user and do nothing
907 build_tabs ($template, $record, $dbh,$encoding,$input);
908 $template->param(
909 biblionumber => $biblionumber,
910 biblioitemnumber => $biblioitemnumber,
911 duplicatebiblionumber => $duplicatebiblionumber,
912 duplicatebibid => $duplicatebiblionumber,
913 duplicatetitle => $duplicatetitle,
917 elsif ( $op eq "delete" ) {
919 my $error = &DelBiblio($biblionumber);
920 if ($error) {
921 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
922 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
923 exit;
926 print $input->redirect('/cgi-bin/koha/catalogue/search.pl');
927 exit;
929 } else {
930 #----------------------------------------------------------------------------
931 # If we're in a duplication case, we have to set to "" the biblionumber
932 # as we'll save the biblio as a new one.
933 $template->param(
934 biblionumberdata => $biblionumber,
935 op => $op,
937 if ( $op eq "duplicate" ) {
938 $biblionumber = "";
941 if($changed_framework eq "changed"){
942 $record = TransformHtmlToMarc( $input, 1 );
944 elsif( $record ne -1 ) {
945 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
946 eval {
947 my $uxml = $record->as_xml;
948 MARC::Record::default_record_format("UNIMARC")
949 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
950 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
951 $record = $urecord;
954 build_tabs( $template, $record, $dbh, $encoding,$input );
955 $template->param(
956 biblionumber => $biblionumber,
957 biblionumbertagfield => $biblionumbertagfield,
958 biblionumbertagsubfield => $biblionumbertagsubfield,
959 biblioitemnumtagfield => $biblioitemnumtagfield,
960 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
961 biblioitemnumber => $biblioitemnumber,
962 hostbiblionumber => $hostbiblionumber,
963 hostitemnumber => $hostitemnumber
967 if ( $record ne '-1' ) {
968 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
969 $template->param( title => $title );
971 $template->param(
972 popup => $mode,
973 frameworkcode => $frameworkcode,
974 itemtype => $frameworkcode,
975 borrowernumber => $loggedinuser,
976 tab => scalar $input->param('tab')
978 $template->{'VARS'}->{'searchid'} = $searchid;
980 output_html_with_http_headers $input, $cookie, $template->output;