Bug 17670: Grammar mistakes - effect vs affect
[koha.git] / cataloguing / addbiblio.pl
blob461599800039e54d11b7ff00083da1222e50806d
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 strict;
23 #use warnings; FIXME - Bug 2505
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::Libraries;
42 use Koha::BiblioFrameworks;
44 use Date::Calc qw(Today);
45 use MARC::File::USMARC;
46 use MARC::File::XML;
47 use URI::Escape;
49 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
50 MARC::File::XML->default_record_format('UNIMARC');
53 our($tagslib,$authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
55 =head1 FUNCTIONS
57 =head2 MARCfindbreeding
59 $record = MARCfindbreeding($breedingid);
61 Look up the import record repository for the record with
62 record with id $breedingid. If found, returns the decoded
63 MARC::Record; otherwise, -1 is returned (FIXME).
64 Returns as second parameter the character encoding.
66 =cut
68 sub MARCfindbreeding {
69 my ( $id ) = @_;
70 my ($marc, $encoding) = GetImportRecordMarc($id);
71 # remove the - in isbn, koha store isbn without any -
72 if ($marc) {
73 my $record = MARC::Record->new_from_usmarc($marc);
74 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
75 if ( $record->field($isbnfield) ) {
76 foreach my $field ( $record->field($isbnfield) ) {
77 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
78 my $newisbn = $field->subfield($isbnsubfield);
79 $newisbn =~ s/-//g;
80 $field->update( $isbnsubfield => $newisbn );
84 # fix the unimarc 100 coded field (with unicode information)
85 if (C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield(100,'a')) {
86 my $f100a=$record->subfield(100,'a');
87 my $f100 = $record->field(100);
88 my $f100temp = $f100->as_string;
89 $record->delete_field($f100);
90 if ( length($f100temp) > 28 ) {
91 substr( $f100temp, 26, 2, "50" );
92 $f100->update( 'a' => $f100temp );
93 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
94 $record->insert_fields_ordered($f100);
98 if ( !defined(ref($record)) ) {
99 return -1;
101 else {
102 # normalize author : UNIMARC specific...
103 if ( C4::Context->preference("z3950NormalizeAuthor")
104 and C4::Context->preference("z3950AuthorAuthFields")
105 and C4::Context->preference("marcflavour") eq 'UNIMARC' )
107 my ( $tag, $subfield ) = GetMarcFromKohaField("biblio.author", '');
109 # my $summary = C4::Context->preference("z3950authortemplate");
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 # foreach my $subfieldcode ($tmpfield->subfields){
119 my $subfieldcode = shift @$tmpfield;
120 my $subfieldvalue = shift @$tmpfield;
121 if ($field) {
122 $field->add_subfields(
123 "$subfieldcode" => $subfieldvalue )
124 if ( $subfieldcode ne $subfield );
126 else {
127 $field =
128 MARC::Field->new( $tag, "", "",
129 $subfieldcode => $subfieldvalue )
130 if ( $subfieldcode ne $subfield );
134 $record->delete_field( $record->field($tag) );
135 foreach my $fieldtag (@auth_fields) {
136 next unless ( $record->field($fieldtag) );
137 my $lastname = $record->field($fieldtag)->subfield('a');
138 my $firstname = $record->field($fieldtag)->subfield('b');
139 my $title = $record->field($fieldtag)->subfield('c');
140 my $number = $record->field($fieldtag)->subfield('d');
141 if ($title) {
143 # $field->add_subfields("$subfield"=>"[ ".ucfirst($title).ucfirst($firstname)." ".$number." ]");
144 $field->add_subfields(
145 "$subfield" => ucfirst($title) . " "
146 . ucfirst($firstname) . " "
147 . $number );
149 else {
151 # $field->add_subfields("$subfield"=>"[ ".ucfirst($firstname).", ".ucfirst($lastname)." ]");
152 $field->add_subfields(
153 "$subfield" => ucfirst($firstname) . ", "
154 . ucfirst($lastname) );
157 $record->insert_fields_ordered($field);
159 return $record, $encoding;
162 return -1;
165 =head2 build_authorized_values_list
167 =cut
169 sub build_authorized_values_list {
170 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
172 my @authorised_values;
173 my %authorised_lib;
175 # builds list, depending on authorised value...
177 #---- branch
178 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
179 my $libraries = Koha::Libraries->search_filtered({}, {order_by => ['branchname']});
180 while ( my $l = $libraries->next ) {
181 push @authorised_values, $l->branchcode;;
182 $authorised_lib{$l->branchcode} = $l->branchname;
185 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
186 push @authorised_values, ""
187 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
188 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
190 my $itemtype;
191 my $itemtypes = GetItemTypes( style => 'array' );
192 for my $itemtype ( @$itemtypes ) {
193 push @authorised_values, $itemtype->{itemtype};
194 $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description};
196 $value = $itemtype unless ($value);
198 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
199 push @authorised_values, ""
200 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
202 my $class_sources = GetClassSources();
204 my $default_source = C4::Context->preference("DefaultClassificationSource");
206 foreach my $class_source (sort keys %$class_sources) {
207 next unless $class_sources->{$class_source}->{'used'} or
208 ($value and $class_source eq $value) or
209 ($class_source eq $default_source);
210 push @authorised_values, $class_source;
211 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
213 $value = $default_source unless $value;
215 else {
216 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
217 $authorised_values_sth->execute(
218 $tagslib->{$tag}->{$subfield}->{authorised_value},
219 $branch_limit ? $branch_limit : (),
222 push @authorised_values, ""
223 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
224 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
226 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
227 push @authorised_values, $value;
228 $authorised_lib{$value} = $lib;
231 $authorised_values_sth->finish;
232 return {
233 type => 'select',
234 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
235 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
236 default => $value,
237 values => \@authorised_values,
238 labels => \%authorised_lib,
243 =head2 CreateKey
245 Create a random value to set it into the input name
247 =cut
249 sub CreateKey {
250 return int(rand(1000000));
253 =head2 GetMandatoryFieldZ3950
255 This function return an hashref which containts all mandatory field
256 to search with z3950 server.
258 =cut
260 sub GetMandatoryFieldZ3950 {
261 my $frameworkcode = shift;
262 my @isbn = GetMarcFromKohaField('biblioitems.isbn',$frameworkcode);
263 my @title = GetMarcFromKohaField('biblio.title',$frameworkcode);
264 my @author = GetMarcFromKohaField('biblio.author',$frameworkcode);
265 my @issn = GetMarcFromKohaField('biblioitems.issn',$frameworkcode);
266 my @lccn = GetMarcFromKohaField('biblioitems.lccn',$frameworkcode);
268 return {
269 $isbn[0].$isbn[1] => 'isbn',
270 $title[0].$title[1] => 'title',
271 $author[0].$author[1] => 'author',
272 $issn[0].$issn[1] => 'issn',
273 $lccn[0].$lccn[1] => 'lccn',
277 =head2 create_input
279 builds the <input ...> entry for a subfield.
281 =cut
283 sub create_input {
284 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
286 my $index_subfield = CreateKey(); # create a specifique key for each subfield
288 $value =~ s/"/&quot;/g;
290 # if there is no value provided but a default value in parameters, get it
291 if ( $value eq '' ) {
292 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
294 # get today date & replace <<YYYY>>, <<MM>>, <<DD>> if provided in the default value
295 my $today_dt = dt_from_string;
296 my $year = $today_dt->year;
297 my $month = $today_dt->month;
298 my $day = $today_dt->day;
299 $value =~ s/<<YYYY>>/$year/g;
300 $value =~ s/<<MM>>/$month/g;
301 $value =~ s/<<DD>>/$day/g;
302 # And <<USER>> with surname (?)
303 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
304 $value=~s/<<USER>>/$username/g;
307 my $dbh = C4::Context->dbh;
309 # map '@' as "subfield" label for fixed fields
310 # to something that's allowed in a div id.
311 my $id_subfield = $subfield;
312 $id_subfield = "00" if $id_subfield eq "@";
314 my %subfield_data = (
315 tag => $tag,
316 subfield => $id_subfield,
317 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
318 tag_mandatory => $tagslib->{$tag}->{mandatory},
319 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
320 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
321 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
322 index => $index_tag,
323 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
324 value => $value,
325 maxlength => $tagslib->{$tag}->{$subfield}->{maxlength},
326 random => CreateKey(),
329 if(exists $mandatory_z3950->{$tag.$subfield}){
330 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
332 # Subfield is hidden depending of hidden and mandatory flag, and is always
333 # shown if it contains anything or if its field is mandatory.
334 my $tdef = $tagslib->{$tag};
335 $subfield_data{visibility} = "display:none;"
336 if $tdef->{$subfield}->{hidden} % 2 == 1 &&
337 $value eq '' &&
338 !$tdef->{$subfield}->{mandatory} &&
339 !$tdef->{mandatory};
340 # expand all subfields of 773 if there is a host item provided in the input
341 $subfield_data{visibility} ="" if ($tag eq 773 and $cgi->param('hostitemnumber'));
344 # it's an authorised field
345 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
346 $subfield_data{marc_value} =
347 build_authorized_values_list( $tag, $subfield, $value, $dbh,
348 $authorised_values_sth,$index_tag,$index_subfield );
350 # it's a subfield $9 linking to an authority record - see bug 2206
352 elsif ($subfield eq "9" and
353 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
354 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
355 $tagslib->{$tag}->{'a'}->{authtypecode} ne '') {
357 $subfield_data{marc_value} = {
358 type => 'text',
359 id => $subfield_data{id},
360 name => $subfield_data{id},
361 value => $value,
362 size => 5,
363 maxlength => $subfield_data{maxlength},
364 readonly => 1,
367 # it's a thesaurus / authority field
369 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
370 # when authorities auto-creation is allowed, do not set readonly
371 my $is_readonly = !C4::Context->preference("BiblioAddsAuthorities");
373 $subfield_data{marc_value} = {
374 type => 'text',
375 id => $subfield_data{id},
376 name => $subfield_data{id},
377 value => $value,
378 size => 67,
379 maxlength => $subfield_data{maxlength},
380 readonly => ($is_readonly) ? 1 : 0,
381 authtype => $tagslib->{$tag}->{$subfield}->{authtypecode},
384 # it's a plugin field
385 } elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
386 require Koha::FrameworkPlugin;
387 my $plugin = Koha::FrameworkPlugin->new( {
388 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
390 my $pars= { dbh => $dbh, record => $rec, tagslib => $tagslib,
391 id => $subfield_data{id}, tabloop => $tabloop };
392 $plugin->build( $pars );
393 if( !$plugin->errstr ) {
394 $subfield_data{marc_value} = {
395 type => 'text_complex',
396 id => $subfield_data{id},
397 name => $subfield_data{id},
398 value => $value,
399 size => 67,
400 maxlength => $subfield_data{maxlength},
401 javascript => $plugin->javascript,
402 noclick => $plugin->noclick,
404 } else {
405 warn $plugin->errstr;
406 # supply default input form
407 $subfield_data{marc_value} = {
408 type => 'text',
409 id => $subfield_data{id},
410 name => $subfield_data{id},
411 value => $value,
412 size => 67,
413 maxlength => $subfield_data{maxlength},
414 readonly => 0,
418 # it's an hidden field
419 } elsif ( $tag eq '' ) {
420 $subfield_data{marc_value} = {
421 type => 'hidden',
422 id => $subfield_data{id},
423 name => $subfield_data{id},
424 value => $value,
425 size => 67,
426 maxlength => $subfield_data{maxlength},
430 else {
431 # it's a standard field
432 if (
433 length($value) > 100
435 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
436 and $tag < 400 && $subfield eq 'a' )
437 or ( $tag >= 500
438 and $tag < 600
439 && C4::Context->preference("marcflavour") eq "MARC21" )
442 $subfield_data{marc_value} = {
443 type => 'textarea',
444 id => $subfield_data{id},
445 name => $subfield_data{id},
446 value => $value,
450 else {
451 $subfield_data{marc_value} = {
452 type => 'text',
453 id => $subfield_data{id},
454 name => $subfield_data{id},
455 value => $value,
456 size => 67,
457 maxlength => $subfield_data{maxlength},
458 readonly => 0,
463 $subfield_data{'index_subfield'} = $index_subfield;
464 return \%subfield_data;
468 =head2 format_indicator
470 Translate indicator value for output form - specifically, map
471 indicator = ' ' to ''. This is for the convenience of a cataloger
472 using a mouse to select an indicator input.
474 =cut
476 sub format_indicator {
477 my $ind_value = shift;
478 return '' if not defined $ind_value;
479 return '' if $ind_value eq ' ';
480 return $ind_value;
483 sub build_tabs {
484 my ( $template, $record, $dbh, $encoding,$input ) = @_;
486 # fill arrays
487 my @loop_data = ();
488 my $tag;
490 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
491 my $query = "SELECT authorised_value, lib
492 FROM authorised_values";
493 $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id )} if $branch_limit;
494 $query .= " WHERE category = ?";
495 $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
496 $query .= " GROUP BY lib ORDER BY lib, lib_opac";
497 my $authorised_values_sth = $dbh->prepare( $query );
499 # in this array, we will push all the 10 tabs
500 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
501 my @BIG_LOOP;
502 my %seen;
503 my @tab_data; # all tags to display
505 foreach my $used ( @$usedTagsLib ){
506 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
507 $seen{$used->{tagfield}}++;
510 my $max_num_tab=-1;
511 foreach(@$usedTagsLib){
512 if($_->{tab} > -1 && $_->{tab} >= $max_num_tab && $_->{tagfield} != '995'){ # FIXME : MARC21 ?
513 $max_num_tab = $_->{tab};
516 if($max_num_tab >= 9){
517 $max_num_tab = 9;
519 # loop through each tab 0 through 9
520 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
521 my @loop_data = (); #innerloop in the template.
522 my $i = 0;
523 foreach my $tag (@tab_data) {
524 $i++;
525 next if ! $tag;
526 my ($indicator1, $indicator2);
527 my $index_tag = CreateKey;
529 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
530 # if MARC::Record is empty => use tab as master loop.
531 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
532 my @fields;
533 if ( $tag ne '000' ) {
534 @fields = $record->field($tag);
536 else {
537 push @fields, $record->leader(); # if tag == 000
539 # loop through each field
540 foreach my $field (@fields) {
542 my @subfields_data;
543 if ( $tag < 10 ) {
544 my ( $value, $subfield );
545 if ( $tag ne '000' ) {
546 $value = $field->data();
547 $subfield = "@";
549 else {
550 $value = $field;
551 $subfield = '@';
553 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
554 next
555 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
556 'biblio.biblionumber' );
557 push(
558 @subfields_data,
559 &create_input(
560 $tag, $subfield, $value, $index_tag, $tabloop, $record,
561 $authorised_values_sth,$input
565 else {
566 my @subfields = $field->subfields();
567 foreach my $subfieldcount ( 0 .. $#subfields ) {
568 my $subfield = $subfields[$subfieldcount][0];
569 my $value = $subfields[$subfieldcount][1];
570 next if ( length $subfield != 1 );
571 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
572 push(
573 @subfields_data,
574 &create_input(
575 $tag, $subfield, $value, $index_tag, $tabloop,
576 $record, $authorised_values_sth,$input
582 # now, loop again to add parameter subfield that are not in the MARC::Record
583 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
585 next if ( length $subfield != 1 );
586 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
587 next if ( $tag < 10 );
588 next
589 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
590 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
591 and not ( $subfield eq "9" and
592 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
593 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
594 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
596 ; #check for visibility flag
597 # if subfield is $9 in a field whose $a is authority-controlled,
598 # always include in the form regardless of the hidden setting - bug 2206
599 next if ( defined( $field->subfield($subfield) ) );
600 push(
601 @subfields_data,
602 &create_input(
603 $tag, $subfield, '', $index_tag, $tabloop, $record,
604 $authorised_values_sth,$input
608 if ( $#subfields_data >= 0 ) {
609 # build the tag entry.
610 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
611 # have twice the same "name" value, and cgi->param() will return only one, making
612 # all subfields to be merged in a single field.
613 my %tag_data = (
614 tag => $tag,
615 index => $index_tag,
616 tag_lib => $tagslib->{$tag}->{lib},
617 repeatable => $tagslib->{$tag}->{repeatable},
618 mandatory => $tagslib->{$tag}->{mandatory},
619 subfield_loop => \@subfields_data,
620 fixedfield => $tag < 10?1:0,
621 random => CreateKey,
623 if ($tag >= 10){ # no indicator for 00x tags
624 $tag_data{indicator1} = format_indicator($field->indicator(1)),
625 $tag_data{indicator2} = format_indicator($field->indicator(2)),
627 push( @loop_data, \%tag_data );
629 } # foreach $field end
631 # if breeding is empty
633 else {
634 my @subfields_data;
635 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
636 next if ( length $subfield != 1 );
637 next
638 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
639 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
640 and not ( $subfield eq "9" and
641 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
642 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
643 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
645 ; #check for visibility flag
646 # if subfield is $9 in a field whose $a is authority-controlled,
647 # always include in the form regardless of the hidden setting - bug 2206
648 next
649 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
650 push(
651 @subfields_data,
652 &create_input(
653 $tag, $subfield, '', $index_tag, $tabloop, $record,
654 $authorised_values_sth,$input
658 if ( $#subfields_data >= 0 ) {
659 my %tag_data = (
660 tag => $tag,
661 index => $index_tag,
662 tag_lib => $tagslib->{$tag}->{lib},
663 repeatable => $tagslib->{$tag}->{repeatable},
664 mandatory => $tagslib->{$tag}->{mandatory},
665 indicator1 => $indicator1,
666 indicator2 => $indicator2,
667 subfield_loop => \@subfields_data,
668 tagfirstsubfield => $subfields_data[0],
669 fixedfield => $tag < 10?1:0,
672 push @loop_data, \%tag_data ;
676 if ( $#loop_data >= 0 ) {
677 push @BIG_LOOP, {
678 number => $tabloop,
679 innerloop => \@loop_data,
683 $authorised_values_sth->finish;
684 $template->param( BIG_LOOP => \@BIG_LOOP );
687 # ========================
688 # MAIN
689 #=========================
690 my $input = new CGI;
691 my $error = $input->param('error');
692 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
693 my $parentbiblio = $input->param('parentbiblionumber');
694 my $breedingid = $input->param('breedingid');
695 my $z3950 = $input->param('z3950');
696 my $op = $input->param('op');
697 my $mode = $input->param('mode');
698 my $frameworkcode = $input->param('frameworkcode');
699 my $redirect = $input->param('redirect');
700 my $searchid = $input->param('searchid');
701 my $dbh = C4::Context->dbh;
702 my $hostbiblionumber = $input->param('hostbiblionumber');
703 my $hostitemnumber = $input->param('hostitemnumber');
704 # fast cataloguing datas in transit
705 my $fa_circborrowernumber = $input->param('circborrowernumber');
706 my $fa_barcode = $input->param('barcode');
707 my $fa_branch = $input->param('branch');
708 my $fa_stickyduedate = $input->param('stickyduedate');
709 my $fa_duedatespec = $input->param('duedatespec');
711 my $userflags = 'edit_catalogue';
713 my $changed_framework = $input->param('changed_framework');
714 $frameworkcode = &GetFrameworkCode($biblionumber)
715 if ( $biblionumber and not( defined $frameworkcode) and $op ne 'addbiblio' );
717 if ($frameworkcode eq 'FA'){
718 $userflags = 'fast_cataloging';
721 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
722 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
724 template_name => "cataloguing/addbiblio.tt",
725 query => $input,
726 type => "intranet",
727 authnotrequired => 0,
728 flagsrequired => { editcatalogue => $userflags },
732 if ($frameworkcode eq 'FA'){
733 # We need to grab and set some variables in the template for use on the additems screen
734 $template->param(
735 'circborrowernumber' => $fa_circborrowernumber,
736 'barcode' => $fa_barcode,
737 'branch' => $fa_branch,
738 'stickyduedate' => $fa_stickyduedate,
739 'duedatespec' => $fa_duedatespec,
741 } elsif ( $op ne "delete" && C4::Context->preference('EnableAdvancedCatalogingEditor') && $input->cookie( 'catalogue_editor_' . $loggedinuser ) eq 'advanced' && !$breedingid ) {
742 # Only use the advanced editor for non-fast-cataloging.
743 # breedingid is not handled because those would only come off a Z39.50
744 # search initiated by the basic editor.
745 print $input->redirect( '/cgi-bin/koha/cataloguing/editor.pl' . ( $biblionumber ? ( '#catalog/' . $biblionumber ) : '' ) );
746 exit;
749 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
750 $template->param(
751 frameworks => $frameworks,
752 breedingid => $breedingid,
755 # ++ Global
756 $tagslib = &GetMarcStructure( 1, $frameworkcode );
757 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
758 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
759 # -- Global
761 my $record = -1;
762 my $encoding = "";
763 my (
764 $biblionumbertagfield,
765 $biblionumbertagsubfield,
766 $biblioitemnumtagfield,
767 $biblioitemnumtagsubfield,
768 $biblioitemnumber
771 if (($biblionumber) && !($breedingid)){
772 $record = GetMarcBiblio($biblionumber);
774 if ($breedingid) {
775 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
778 #populate hostfield if hostbiblionumber is available
779 if ($hostbiblionumber) {
780 my $marcflavour = C4::Context->preference("marcflavour");
781 $record = MARC::Record->new();
782 $record->leader('');
783 my $field =
784 PrepHostMarcField( $hostbiblionumber, $hostitemnumber, $marcflavour );
785 $record->append_fields($field);
788 # This is a child record
789 if ($parentbiblio) {
790 my $marcflavour = C4::Context->preference('marcflavour');
791 $record = MARC::Record->new();
792 SetMarcUnicodeFlag($record, $marcflavour);
793 my $hostfield = prepare_host_field($parentbiblio,$marcflavour);
794 if ($hostfield) {
795 $record->append_fields($hostfield);
799 $is_a_modif = 0;
801 if ($biblionumber) {
802 $is_a_modif = 1;
803 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
804 $template->param( title => $title );
806 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
807 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
808 &GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
809 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
810 &GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
812 # search biblioitems value
813 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
814 $sth->execute($biblionumber);
815 ($biblioitemnumber) = $sth->fetchrow;
818 #-------------------------------------------------------------------------------------
819 if ( $op eq "addbiblio" ) {
820 #-------------------------------------------------------------------------------------
821 $template->param(
822 biblionumberdata => $biblionumber,
824 # getting html input
825 my @params = $input->multi_param();
826 $record = TransformHtmlToMarc( $input, 1 );
827 # check for a duplicate
828 my ( $duplicatebiblionumber, $duplicatetitle );
829 if ( !$is_a_modif ) {
830 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
832 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
833 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
834 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
835 my $oldbibitemnum;
836 if (C4::Context->preference("BiblioAddsAuthorities")){
837 BiblioAutoLink( $record, $frameworkcode );
839 if ( $is_a_modif ) {
840 ModBiblioframework( $biblionumber, $frameworkcode );
841 ModBiblio( $record, $biblionumber, $frameworkcode );
843 else {
844 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
846 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view" && $redirect ne "just_save")){
847 if ($frameworkcode eq 'FA'){
848 print $input->redirect(
849 '/cgi-bin/koha/cataloguing/additem.pl?'
850 .'biblionumber='.$biblionumber
851 .'&frameworkcode='.$frameworkcode
852 .'&circborrowernumber='.$fa_circborrowernumber
853 .'&branch='.$fa_branch
854 .'&barcode='.uri_escape_utf8($fa_barcode)
855 .'&stickyduedate='.$fa_stickyduedate
856 .'&duedatespec='.$fa_duedatespec
858 exit;
860 else {
861 print $input->redirect(
862 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid"
864 exit;
867 elsif(($is_a_modif || $redirect eq "view") && $redirect ne "just_save"){
868 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
869 my $views = { C4::Search::enabled_staff_search_views };
870 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
871 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
872 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
873 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
874 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
875 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
876 } else {
877 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
879 exit;
882 elsif ($redirect eq "just_save"){
883 my $tab = $input->param('current_tab');
884 print $input->redirect("/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber&framework=$frameworkcode&tab=$tab&searchid=$searchid");
886 else {
887 $template->param(
888 biblionumber => $biblionumber,
889 done =>1,
890 popup =>1
892 if ( $record ne '-1' ) {
893 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
894 $template->param( title => $title );
896 $template->param(
897 popup => $mode,
898 itemtype => $frameworkcode,
900 output_html_with_http_headers $input, $cookie, $template->output;
901 exit;
903 } else {
904 # it may be a duplicate, warn the user and do nothing
905 build_tabs ($template, $record, $dbh,$encoding,$input);
906 $template->param(
907 biblionumber => $biblionumber,
908 biblioitemnumber => $biblioitemnumber,
909 duplicatebiblionumber => $duplicatebiblionumber,
910 duplicatebibid => $duplicatebiblionumber,
911 duplicatetitle => $duplicatetitle,
915 elsif ( $op eq "delete" ) {
917 my $error = &DelBiblio($biblionumber);
918 if ($error) {
919 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
920 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
921 exit;
924 print $input->redirect('/cgi-bin/koha/catalogue/search.pl');
925 exit;
927 } else {
928 #----------------------------------------------------------------------------
929 # If we're in a duplication case, we have to set to "" the biblionumber
930 # as we'll save the biblio as a new one.
931 $template->param(
932 biblionumberdata => $biblionumber,
933 op => $op,
935 if ( $op eq "duplicate" ) {
936 $biblionumber = "";
939 if($changed_framework eq "changed"){
940 $record = TransformHtmlToMarc( $input, 1 );
942 elsif( $record ne -1 ) {
943 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
944 eval {
945 my $uxml = $record->as_xml;
946 MARC::Record::default_record_format("UNIMARC")
947 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
948 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
949 $record = $urecord;
952 build_tabs( $template, $record, $dbh, $encoding,$input );
953 $template->param(
954 biblionumber => $biblionumber,
955 biblionumbertagfield => $biblionumbertagfield,
956 biblionumbertagsubfield => $biblionumbertagsubfield,
957 biblioitemnumtagfield => $biblioitemnumtagfield,
958 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
959 biblioitemnumber => $biblioitemnumber,
960 hostbiblionumber => $hostbiblionumber,
961 hostitemnumber => $hostitemnumber
965 if ( $record ne '-1' ) {
966 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
967 $template->param( title => $title );
969 $template->param(
970 popup => $mode,
971 frameworkcode => $frameworkcode,
972 itemtype => $frameworkcode,
973 borrowernumber => $loggedinuser,
974 tab => scalar $input->param('tab')
976 $template->{'VARS'}->{'searchid'} = $searchid;
978 output_html_with_http_headers $input, $cookie, $template->output;