Bug 18501: (follow-up) Do not rely on userenv->{branchcode}
[koha.git] / cataloguing / addbiblio.pl
blob4895564216ffa81397f07aca4e1f2768e241a117
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, "";
183 my $itemtype;
184 my $itemtypes = Koha::ItemTypes->search_with_localization;
185 while ( $itemtype = $itemtypes->next ) {
186 push @authorised_values, $itemtype->itemtype;
187 $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
189 $value = $itemtype unless ($value);
191 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
192 push @authorised_values, "";
194 my $class_sources = GetClassSources();
196 my $default_source = C4::Context->preference("DefaultClassificationSource");
198 foreach my $class_source (sort keys %$class_sources) {
199 next unless $class_sources->{$class_source}->{'used'} or
200 ($value and $class_source eq $value) or
201 ($class_source eq $default_source);
202 push @authorised_values, $class_source;
203 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
205 $value = $default_source unless $value;
207 else {
208 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
209 $authorised_values_sth->execute(
210 $tagslib->{$tag}->{$subfield}->{authorised_value},
211 $branch_limit ? $branch_limit : (),
214 push @authorised_values, "";
216 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
217 push @authorised_values, $value;
218 $authorised_lib{$value} = $lib;
221 $authorised_values_sth->finish;
222 return {
223 type => 'select',
224 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
225 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
226 default => $value,
227 values => \@authorised_values,
228 labels => \%authorised_lib,
233 =head2 CreateKey
235 Create a random value to set it into the input name
237 =cut
239 sub CreateKey {
240 return int(rand(1000000));
243 =head2 GetMandatoryFieldZ3950
245 This function returns a hashref which contains all mandatory field
246 to search with z3950 server.
248 =cut
250 sub GetMandatoryFieldZ3950 {
251 my $frameworkcode = shift;
252 my @isbn = GetMarcFromKohaField( 'biblioitems.isbn' );
253 my @title = GetMarcFromKohaField( 'biblio.title' );
254 my @author = GetMarcFromKohaField( 'biblio.author' );
255 my @issn = GetMarcFromKohaField( 'biblioitems.issn' );
256 my @lccn = GetMarcFromKohaField( 'biblioitems.lccn' );
258 return {
259 $isbn[0].$isbn[1] => 'isbn',
260 $title[0].$title[1] => 'title',
261 $author[0].$author[1] => 'author',
262 $issn[0].$issn[1] => 'issn',
263 $lccn[0].$lccn[1] => 'lccn',
267 =head2 create_input
269 builds the <input ...> entry for a subfield.
271 =cut
273 sub create_input {
274 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
276 my $index_subfield = CreateKey(); # create a specifique key for each subfield
278 # if there is no value provided but a default value in parameters, get it
279 if ( $value eq '' ) {
280 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue} // q{};
282 # get today date & replace <<YYYY>>, <<YY>>, <<MM>>, <<DD>> if provided in the default value
283 my $today_dt = dt_from_string;
284 my $year = $today_dt->strftime('%Y');
285 my $shortyear = $today_dt->strftime('%y');
286 my $month = $today_dt->strftime('%m');
287 my $day = $today_dt->strftime('%d');
288 $value =~ s/<<YYYY>>/$year/g;
289 $value =~ s/<<YY>>/$shortyear/g;
290 $value =~ s/<<MM>>/$month/g;
291 $value =~ s/<<DD>>/$day/g;
292 # And <<USER>> with surname (?)
293 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
294 $value=~s/<<USER>>/$username/g;
297 my $dbh = C4::Context->dbh;
299 # map '@' as "subfield" label for fixed fields
300 # to something that's allowed in a div id.
301 my $id_subfield = $subfield;
302 $id_subfield = "00" if $id_subfield eq "@";
304 my %subfield_data = (
305 tag => $tag,
306 subfield => $id_subfield,
307 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
308 tag_mandatory => $tagslib->{$tag}->{mandatory},
309 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
310 important => $tagslib->{$tag}->{$subfield}->{important},
311 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
312 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
313 index => $index_tag,
314 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
315 value => $value,
316 maxlength => $tagslib->{$tag}->{$subfield}->{maxlength},
317 random => CreateKey(),
320 if(exists $mandatory_z3950->{$tag.$subfield}){
321 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
323 # Subfield is hidden depending of hidden and mandatory flag, and is always
324 # shown if it contains anything or if its field is mandatory or important.
325 my $tdef = $tagslib->{$tag};
326 $subfield_data{visibility} = "display:none;"
327 if $tdef->{$subfield}->{hidden} % 2 == 1 &&
328 $value eq '' &&
329 !$tdef->{$subfield}->{mandatory} &&
330 !$tdef->{mandatory} &&
331 !$tdef->{$subfield}->{important} &&
332 !$tdef->{important};
333 # expand all subfields of 773 if there is a host item provided in the input
334 $subfield_data{visibility} ="" if ($tag eq 773 and $cgi->param('hostitemnumber'));
337 # it's an authorised field
338 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
339 $subfield_data{marc_value} =
340 build_authorized_values_list( $tag, $subfield, $value, $dbh,
341 $authorised_values_sth,$index_tag,$index_subfield );
343 # it's a subfield $9 linking to an authority record - see bug 2206
345 elsif ($subfield eq "9" and
346 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
347 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
348 $tagslib->{$tag}->{'a'}->{authtypecode} ne '') {
350 $subfield_data{marc_value} = {
351 type => 'text',
352 id => $subfield_data{id},
353 name => $subfield_data{id},
354 value => $value,
355 size => 5,
356 maxlength => $subfield_data{maxlength},
357 readonly => 1,
360 # it's a thesaurus / authority field
362 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
363 # when authorities auto-creation is allowed, do not set readonly
364 my $is_readonly = !C4::Context->preference("BiblioAddsAuthorities");
366 $subfield_data{marc_value} = {
367 type => 'text',
368 id => $subfield_data{id},
369 name => $subfield_data{id},
370 value => $value,
371 size => 67,
372 maxlength => $subfield_data{maxlength},
373 readonly => ($is_readonly) ? 1 : 0,
374 authtype => $tagslib->{$tag}->{$subfield}->{authtypecode},
377 # it's a plugin field
378 } elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
379 require Koha::FrameworkPlugin;
380 my $plugin = Koha::FrameworkPlugin->new( {
381 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
383 my $pars= { dbh => $dbh, record => $rec, tagslib => $tagslib,
384 id => $subfield_data{id}, tabloop => $tabloop };
385 $plugin->build( $pars );
386 if( !$plugin->errstr ) {
387 $subfield_data{marc_value} = {
388 type => 'text_complex',
389 id => $subfield_data{id},
390 name => $subfield_data{id},
391 value => $value,
392 size => 67,
393 maxlength => $subfield_data{maxlength},
394 javascript => $plugin->javascript,
395 noclick => $plugin->noclick,
397 } else {
398 warn $plugin->errstr;
399 # supply default input form
400 $subfield_data{marc_value} = {
401 type => 'text',
402 id => $subfield_data{id},
403 name => $subfield_data{id},
404 value => $value,
405 size => 67,
406 maxlength => $subfield_data{maxlength},
407 readonly => 0,
411 # it's an hidden field
412 } elsif ( $tag eq '' ) {
413 $subfield_data{marc_value} = {
414 type => 'hidden',
415 id => $subfield_data{id},
416 name => $subfield_data{id},
417 value => $value,
418 size => 67,
419 maxlength => $subfield_data{maxlength},
423 else {
424 # it's a standard field
425 if (
426 length($value) > 100
428 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
429 and $tag < 400 && $subfield eq 'a' )
430 or ( $tag >= 500
431 and $tag < 600
432 && C4::Context->preference("marcflavour") eq "MARC21" )
435 $subfield_data{marc_value} = {
436 type => 'textarea',
437 id => $subfield_data{id},
438 name => $subfield_data{id},
439 value => $value,
443 else {
444 $subfield_data{marc_value} = {
445 type => 'text',
446 id => $subfield_data{id},
447 name => $subfield_data{id},
448 value => $value,
449 size => 67,
450 maxlength => $subfield_data{maxlength},
451 readonly => 0,
456 $subfield_data{'index_subfield'} = $index_subfield;
457 return \%subfield_data;
461 =head2 format_indicator
463 Translate indicator value for output form - specifically, map
464 indicator = ' ' to ''. This is for the convenience of a cataloger
465 using a mouse to select an indicator input.
467 =cut
469 sub format_indicator {
470 my $ind_value = shift;
471 return '' if not defined $ind_value;
472 return '' if $ind_value eq ' ';
473 return $ind_value;
476 sub build_tabs {
477 my ( $template, $record, $dbh, $encoding,$input ) = @_;
479 # fill arrays
480 my @loop_data = ();
481 my $tag;
483 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
484 my $query = "SELECT authorised_value, lib
485 FROM authorised_values";
486 $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id )} if $branch_limit;
487 $query .= " WHERE category = ?";
488 $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
489 $query .= " GROUP BY authorised_value,lib ORDER BY lib, lib_opac";
490 my $authorised_values_sth = $dbh->prepare( $query );
492 # in this array, we will push all the 10 tabs
493 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
494 my @BIG_LOOP;
495 my %seen;
496 my @tab_data; # all tags to display
498 my $max_num_tab=-1;
499 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" );
500 foreach my $used ( @$usedTagsLib ){
502 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
503 $seen{$used->{tagfield}}++;
505 if ( $used->{tab} > -1
506 && $used->{tab} >= $max_num_tab
507 && $used->{tagfield} ne $itemtag )
509 $max_num_tab = $used->{tab};
512 if($max_num_tab >= 9){
513 $max_num_tab = 9;
515 # loop through each tab 0 through 9
516 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
517 my @loop_data = (); #innerloop in the template.
518 my $i = 0;
519 foreach my $tag (sort @tab_data) {
520 $i++;
521 next if ! $tag;
522 my ($indicator1, $indicator2);
523 my $index_tag = CreateKey;
525 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
526 # if MARC::Record is empty => use tab as master loop.
527 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
528 my @fields;
529 if ( $tag ne '000' ) {
530 @fields = $record->field($tag);
532 else {
533 push @fields, $record->leader(); # if tag == 000
535 # loop through each field
536 foreach my $field (@fields) {
538 my @subfields_data;
539 if ( $tag < 10 ) {
540 my ( $value, $subfield );
541 if ( $tag ne '000' ) {
542 $value = $field->data();
543 $subfield = "@";
545 else {
546 $value = $field;
547 $subfield = '@';
549 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
550 next
551 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
552 'biblio.biblionumber' );
553 push(
554 @subfields_data,
555 &create_input(
556 $tag, $subfield, $value, $index_tag, $tabloop, $record,
557 $authorised_values_sth,$input
561 else {
562 my @subfields = $field->subfields();
563 foreach my $subfieldcount ( 0 .. $#subfields ) {
564 my $subfield = $subfields[$subfieldcount][0];
565 my $value = $subfields[$subfieldcount][1];
566 next if ( length $subfield != 1 );
567 next if ( !defined $tagslib->{$tag}->{$subfield} || $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
568 push(
569 @subfields_data,
570 &create_input(
571 $tag, $subfield, $value, $index_tag, $tabloop,
572 $record, $authorised_values_sth,$input
578 # now, loop again to add parameter subfield that are not in the MARC::Record
579 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
581 next if ( length $subfield != 1 );
582 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
583 next if ( $tag < 10 );
584 next
585 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
586 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
587 and not ( $subfield eq "9" and
588 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
589 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
590 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
592 ; #check for visibility flag
593 # if subfield is $9 in a field whose $a is authority-controlled,
594 # always include in the form regardless of the hidden setting - bug 2206
595 next if ( defined( $field->subfield($subfield) ) );
596 push(
597 @subfields_data,
598 &create_input(
599 $tag, $subfield, '', $index_tag, $tabloop, $record,
600 $authorised_values_sth,$input
604 if ( $#subfields_data >= 0 ) {
605 # build the tag entry.
606 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
607 # have twice the same "name" value, and cgi->param() will return only one, making
608 # all subfields to be merged in a single field.
609 my %tag_data = (
610 tag => $tag,
611 index => $index_tag,
612 tag_lib => $tagslib->{$tag}->{lib},
613 repeatable => $tagslib->{$tag}->{repeatable},
614 mandatory => $tagslib->{$tag}->{mandatory},
615 important => $tagslib->{$tag}->{important},
616 subfield_loop => \@subfields_data,
617 fixedfield => $tag < 10?1:0,
618 random => CreateKey,
620 if ($tag >= 10){ # no indicator for 00x tags
621 $tag_data{indicator1} = format_indicator($field->indicator(1)),
622 $tag_data{indicator2} = format_indicator($field->indicator(2)),
624 push( @loop_data, \%tag_data );
626 } # foreach $field end
628 # if breeding is empty
630 else {
631 my @subfields_data;
632 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
633 next if ( length $subfield != 1 );
634 next
635 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
636 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
637 and not ( $subfield eq "9" and
638 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
639 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
640 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
642 ; #check for visibility flag
643 # if subfield is $9 in a field whose $a is authority-controlled,
644 # always include in the form regardless of the hidden setting - bug 2206
645 next
646 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
647 push(
648 @subfields_data,
649 &create_input(
650 $tag, $subfield, '', $index_tag, $tabloop, $record,
651 $authorised_values_sth,$input
655 if ( $#subfields_data >= 0 ) {
656 my %tag_data = (
657 tag => $tag,
658 index => $index_tag,
659 tag_lib => $tagslib->{$tag}->{lib},
660 repeatable => $tagslib->{$tag}->{repeatable},
661 mandatory => $tagslib->{$tag}->{mandatory},
662 important => $tagslib->{$tag}->{important},
663 indicator1 => ( $indicator1 || $tagslib->{$tag}->{ind1_defaultvalue} ), #if not set, try to load the default value
664 indicator2 => ( $indicator2 || $tagslib->{$tag}->{ind2_defaultvalue} ), #use short-circuit operator for efficiency
665 subfield_loop => \@subfields_data,
666 tagfirstsubfield => $subfields_data[0],
667 fixedfield => $tag < 10?1:0,
670 push @loop_data, \%tag_data ;
674 if ( $#loop_data >= 0 ) {
675 push @BIG_LOOP, {
676 number => $tabloop,
677 innerloop => \@loop_data,
681 $authorised_values_sth->finish;
682 $template->param( BIG_LOOP => \@BIG_LOOP );
685 # ========================
686 # MAIN
687 #=========================
688 my $input = new CGI;
689 my $error = $input->param('error');
690 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
691 my $parentbiblio = $input->param('parentbiblionumber');
692 my $breedingid = $input->param('breedingid');
693 my $z3950 = $input->param('z3950');
694 my $op = $input->param('op') // q{};
695 my $mode = $input->param('mode');
696 my $frameworkcode = $input->param('frameworkcode');
697 my $redirect = $input->param('redirect');
698 my $searchid = $input->param('searchid');
699 my $dbh = C4::Context->dbh;
700 my $hostbiblionumber = $input->param('hostbiblionumber');
701 my $hostitemnumber = $input->param('hostitemnumber');
702 # fast cataloguing datas in transit
703 my $fa_circborrowernumber = $input->param('circborrowernumber');
704 my $fa_barcode = $input->param('barcode');
705 my $fa_branch = $input->param('branch');
706 my $fa_stickyduedate = $input->param('stickyduedate');
707 my $fa_duedatespec = $input->param('duedatespec');
709 my $userflags = 'edit_catalogue';
711 my $changed_framework = $input->param('changed_framework') // q{};
712 $frameworkcode = &GetFrameworkCode($biblionumber)
713 if ( $biblionumber and not( defined $frameworkcode) and $op ne 'addbiblio' );
715 if ($frameworkcode eq 'FA'){
716 $userflags = 'fast_cataloging';
719 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
720 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
722 template_name => "cataloguing/addbiblio.tt",
723 query => $input,
724 type => "intranet",
725 authnotrequired => 0,
726 flagsrequired => { editcatalogue => $userflags },
730 if ($biblionumber){
731 my $does_bib_exist = Koha::Biblios->find($biblionumber);
732 if (!defined $does_bib_exist){
733 $biblionumber = undef;
734 $template->param( bib_doesnt_exist => 1 );
738 if ($frameworkcode eq 'FA'){
739 # We need to grab and set some variables in the template for use on the additems screen
740 $template->param(
741 'circborrowernumber' => $fa_circborrowernumber,
742 'barcode' => $fa_barcode,
743 'branch' => $fa_branch,
744 'stickyduedate' => $fa_stickyduedate,
745 'duedatespec' => $fa_duedatespec,
747 } elsif ( $op ne "delete" &&
748 C4::Context->preference('EnableAdvancedCatalogingEditor') &&
749 C4::Auth::haspermission(C4::Context->userenv->{id},{'editcatalogue'=>'advanced_editor'}) &&
750 $input->cookie( 'catalogue_editor_' . $loggedinuser ) eq 'advanced' &&
751 !$breedingid ) {
752 # Only use the advanced editor for non-fast-cataloging.
753 # breedingid is not handled because those would only come off a Z39.50
754 # search initiated by the basic editor.
755 print $input->redirect( '/cgi-bin/koha/cataloguing/editor.pl' . ( $biblionumber ? ( ($op eq 'duplicate'?'#duplicate/':'#catalog/') . $biblionumber ) : '' ) );
756 exit;
759 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
760 $template->param(
761 frameworks => $frameworks,
762 breedingid => $breedingid,
765 # ++ Global
766 $tagslib = &GetMarcStructure( 1, $frameworkcode );
767 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
768 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
769 # -- Global
771 my $record = -1;
772 my $encoding = "";
773 my (
774 $biblionumbertagfield,
775 $biblionumbertagsubfield,
776 $biblioitemnumtagfield,
777 $biblioitemnumtagsubfield,
778 $biblioitemnumber
781 if (($biblionumber) && !($breedingid)){
782 $record = GetMarcBiblio({ biblionumber => $biblionumber });
784 if ($breedingid) {
785 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
788 #populate hostfield if hostbiblionumber is available
789 if ($hostbiblionumber) {
790 my $marcflavour = C4::Context->preference("marcflavour");
791 $record = MARC::Record->new();
792 $record->leader('');
793 my $field =
794 PrepHostMarcField( $hostbiblionumber, $hostitemnumber, $marcflavour );
795 $record->append_fields($field);
798 # This is a child record
799 if ($parentbiblio) {
800 my $marcflavour = C4::Context->preference('marcflavour');
801 $record = MARC::Record->new();
802 SetMarcUnicodeFlag($record, $marcflavour);
803 my $hostfield = prepare_host_field($parentbiblio,$marcflavour);
804 if ($hostfield) {
805 $record->append_fields($hostfield);
809 $is_a_modif = 0;
811 if ($biblionumber) {
812 $is_a_modif = 1;
813 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
814 $template->param( title => $title );
816 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
817 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
818 &GetMarcFromKohaField( "biblio.biblionumber" );
819 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
820 &GetMarcFromKohaField( "biblioitems.biblioitemnumber" );
822 # search biblioitems value
823 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
824 $sth->execute($biblionumber);
825 ($biblioitemnumber) = $sth->fetchrow;
828 #-------------------------------------------------------------------------------------
829 if ( $op eq "addbiblio" ) {
830 #-------------------------------------------------------------------------------------
831 $template->param(
832 biblionumberdata => $biblionumber,
834 # getting html input
835 my @params = $input->multi_param();
836 $record = TransformHtmlToMarc( $input, 1 );
837 # check for a duplicate
838 my ( $duplicatebiblionumber, $duplicatetitle );
839 if ( !$is_a_modif ) {
840 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
842 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
843 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
844 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
845 my $oldbibitemnum;
846 if ( $is_a_modif ) {
847 ModBiblio( $record, $biblionumber, $frameworkcode );
849 else {
850 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
852 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view" && $redirect ne "just_save")){
853 if ($frameworkcode eq 'FA'){
854 print $input->redirect(
855 '/cgi-bin/koha/cataloguing/additem.pl?'
856 .'biblionumber='.$biblionumber
857 .'&frameworkcode='.$frameworkcode
858 .'&circborrowernumber='.$fa_circborrowernumber
859 .'&branch='.$fa_branch
860 .'&barcode='.uri_escape_utf8($fa_barcode)
861 .'&stickyduedate='.$fa_stickyduedate
862 .'&duedatespec='.$fa_duedatespec
864 exit;
866 else {
867 print $input->redirect(
868 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid"
870 exit;
873 elsif(($is_a_modif || $redirect eq "view") && $redirect ne "just_save"){
874 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
875 my $views = { C4::Search::enabled_staff_search_views };
876 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
877 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
878 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
879 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
880 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
881 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
882 } else {
883 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
885 exit;
888 elsif ($redirect eq "just_save"){
889 my $tab = $input->param('current_tab');
890 print $input->redirect("/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber&framework=$frameworkcode&tab=$tab&searchid=$searchid");
892 else {
893 $template->param(
894 biblionumber => $biblionumber,
895 done =>1,
896 popup =>1
898 if ( $record ne '-1' ) {
899 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
900 $template->param( title => $title );
902 $template->param(
903 popup => $mode,
904 itemtype => $frameworkcode,
906 output_html_with_http_headers $input, $cookie, $template->output;
907 exit;
909 } else {
910 # it may be a duplicate, warn the user and do nothing
911 build_tabs ($template, $record, $dbh,$encoding,$input);
912 $template->param(
913 biblionumber => $biblionumber,
914 biblioitemnumber => $biblioitemnumber,
915 duplicatebiblionumber => $duplicatebiblionumber,
916 duplicatebibid => $duplicatebiblionumber,
917 duplicatetitle => $duplicatetitle,
921 elsif ( $op eq "delete" ) {
923 my $error = &DelBiblio($biblionumber);
924 if ($error) {
925 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
926 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
927 exit;
930 print $input->redirect('/cgi-bin/koha/catalogue/search.pl' . ($searchid ? "?searchid=$searchid" : ""));
931 exit;
933 } else {
934 #----------------------------------------------------------------------------
935 # If we're in a duplication case, we have to set to "" the biblionumber
936 # as we'll save the biblio as a new one.
937 $template->param(
938 biblionumberdata => $biblionumber,
939 op => $op,
941 if ( $op eq "duplicate" ) {
942 $biblionumber = "";
945 if($changed_framework eq "changed"){
946 $record = TransformHtmlToMarc( $input, 1 );
948 elsif( $record ne -1 ) {
949 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
950 eval {
951 my $uxml = $record->as_xml;
952 MARC::Record::default_record_format("UNIMARC")
953 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
954 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
955 $record = $urecord;
958 build_tabs( $template, $record, $dbh, $encoding,$input );
959 $template->param(
960 biblionumber => $biblionumber,
961 biblionumbertagfield => $biblionumbertagfield,
962 biblionumbertagsubfield => $biblionumbertagsubfield,
963 biblioitemnumtagfield => $biblioitemnumtagfield,
964 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
965 biblioitemnumber => $biblioitemnumber,
966 hostbiblionumber => $hostbiblionumber,
967 hostitemnumber => $hostitemnumber
971 if ( $record ne '-1' ) {
972 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
973 $template->param( title => $title );
975 $template->param(
976 popup => $mode,
977 frameworkcode => $frameworkcode,
978 itemtype => $frameworkcode,
979 borrowernumber => $loggedinuser,
980 tab => scalar $input->param('tab')
982 $template->{'VARS'}->{'searchid'} = $searchid;
984 output_html_with_http_headers $input, $cookie, $template->output;