Bug 19827: Removing &checkuniquemember from the export in C4/Members.pm
[koha.git] / cataloguing / addbiblio.pl
blob3d14f83e666f1ffbe25aee888bb5c501bcdb03f4
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::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 $summary = C4::Context->preference("z3950authortemplate");
111 my $auth_fields =
112 C4::Context->preference("z3950AuthorAuthFields");
113 my @auth_fields = split /,/, $auth_fields;
114 my $field;
116 if ( $record->field($tag) ) {
117 foreach my $tmpfield ( $record->field($tag)->subfields ) {
119 # foreach my $subfieldcode ($tmpfield->subfields){
120 my $subfieldcode = shift @$tmpfield;
121 my $subfieldvalue = shift @$tmpfield;
122 if ($field) {
123 $field->add_subfields(
124 "$subfieldcode" => $subfieldvalue )
125 if ( $subfieldcode ne $subfield );
127 else {
128 $field =
129 MARC::Field->new( $tag, "", "",
130 $subfieldcode => $subfieldvalue )
131 if ( $subfieldcode ne $subfield );
135 $record->delete_field( $record->field($tag) );
136 foreach my $fieldtag (@auth_fields) {
137 next unless ( $record->field($fieldtag) );
138 my $lastname = $record->field($fieldtag)->subfield('a');
139 my $firstname = $record->field($fieldtag)->subfield('b');
140 my $title = $record->field($fieldtag)->subfield('c');
141 my $number = $record->field($fieldtag)->subfield('d');
142 if ($title) {
144 # $field->add_subfields("$subfield"=>"[ ".ucfirst($title).ucfirst($firstname)." ".$number." ]");
145 $field->add_subfields(
146 "$subfield" => ucfirst($title) . " "
147 . ucfirst($firstname) . " "
148 . $number );
150 else {
152 # $field->add_subfields("$subfield"=>"[ ".ucfirst($firstname).", ".ucfirst($lastname)." ]");
153 $field->add_subfields(
154 "$subfield" => ucfirst($firstname) . ", "
155 . ucfirst($lastname) );
158 $record->insert_fields_ordered($field);
160 return $record, $encoding;
163 return -1;
166 =head2 build_authorized_values_list
168 =cut
170 sub build_authorized_values_list {
171 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
173 my @authorised_values;
174 my %authorised_lib;
176 # builds list, depending on authorised value...
178 #---- branch
179 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
180 my $libraries = Koha::Libraries->search_filtered({}, {order_by => ['branchname']});
181 while ( my $l = $libraries->next ) {
182 push @authorised_values, $l->branchcode;;
183 $authorised_lib{$l->branchcode} = $l->branchname;
186 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
187 push @authorised_values, ""
188 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
189 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
191 my $itemtype;
192 my $itemtypes = Koha::ItemTypes->search_with_localization;
193 while ( $itemtype = $itemtypes->next ) {
194 push @authorised_values, $itemtype->itemtype;
195 $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
197 $value = $itemtype unless ($value);
199 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
200 push @authorised_values, ""
201 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
203 my $class_sources = GetClassSources();
205 my $default_source = C4::Context->preference("DefaultClassificationSource");
207 foreach my $class_source (sort keys %$class_sources) {
208 next unless $class_sources->{$class_source}->{'used'} or
209 ($value and $class_source eq $value) or
210 ($class_source eq $default_source);
211 push @authorised_values, $class_source;
212 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
214 $value = $default_source unless $value;
216 else {
217 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
218 $authorised_values_sth->execute(
219 $tagslib->{$tag}->{$subfield}->{authorised_value},
220 $branch_limit ? $branch_limit : (),
223 push @authorised_values, ""
224 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
225 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
227 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
228 push @authorised_values, $value;
229 $authorised_lib{$value} = $lib;
232 $authorised_values_sth->finish;
233 return {
234 type => 'select',
235 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
236 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
237 default => $value,
238 values => \@authorised_values,
239 labels => \%authorised_lib,
244 =head2 CreateKey
246 Create a random value to set it into the input name
248 =cut
250 sub CreateKey {
251 return int(rand(1000000));
254 =head2 GetMandatoryFieldZ3950
256 This function returns a hashref which contains all mandatory field
257 to search with z3950 server.
259 =cut
261 sub GetMandatoryFieldZ3950 {
262 my $frameworkcode = shift;
263 my @isbn = GetMarcFromKohaField('biblioitems.isbn',$frameworkcode);
264 my @title = GetMarcFromKohaField('biblio.title',$frameworkcode);
265 my @author = GetMarcFromKohaField('biblio.author',$frameworkcode);
266 my @issn = GetMarcFromKohaField('biblioitems.issn',$frameworkcode);
267 my @lccn = GetMarcFromKohaField('biblioitems.lccn',$frameworkcode);
269 return {
270 $isbn[0].$isbn[1] => 'isbn',
271 $title[0].$title[1] => 'title',
272 $author[0].$author[1] => 'author',
273 $issn[0].$issn[1] => 'issn',
274 $lccn[0].$lccn[1] => 'lccn',
278 =head2 create_input
280 builds the <input ...> entry for a subfield.
282 =cut
284 sub create_input {
285 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
287 my $index_subfield = CreateKey(); # create a specifique key for each subfield
289 $value =~ s/"/&quot;/g;
291 # if there is no value provided but a default value in parameters, get it
292 if ( $value eq '' ) {
293 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
295 # get today date & replace <<YYYY>>, <<MM>>, <<DD>> if provided in the default value
296 my $today_dt = dt_from_string;
297 my $year = $today_dt->strftime('%Y');
298 my $month = $today_dt->strftime('%m');
299 my $day = $today_dt->strftime('%d');
300 $value =~ s/<<YYYY>>/$year/g;
301 $value =~ s/<<MM>>/$month/g;
302 $value =~ s/<<DD>>/$day/g;
303 # And <<USER>> with surname (?)
304 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
305 $value=~s/<<USER>>/$username/g;
308 my $dbh = C4::Context->dbh;
310 # map '@' as "subfield" label for fixed fields
311 # to something that's allowed in a div id.
312 my $id_subfield = $subfield;
313 $id_subfield = "00" if $id_subfield eq "@";
315 my %subfield_data = (
316 tag => $tag,
317 subfield => $id_subfield,
318 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
319 tag_mandatory => $tagslib->{$tag}->{mandatory},
320 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
321 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
322 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
323 index => $index_tag,
324 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
325 value => $value,
326 maxlength => $tagslib->{$tag}->{$subfield}->{maxlength},
327 random => CreateKey(),
330 if(exists $mandatory_z3950->{$tag.$subfield}){
331 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
333 # Subfield is hidden depending of hidden and mandatory flag, and is always
334 # shown if it contains anything or if its field is mandatory.
335 my $tdef = $tagslib->{$tag};
336 $subfield_data{visibility} = "display:none;"
337 if $tdef->{$subfield}->{hidden} % 2 == 1 &&
338 $value eq '' &&
339 !$tdef->{$subfield}->{mandatory} &&
340 !$tdef->{mandatory};
341 # expand all subfields of 773 if there is a host item provided in the input
342 $subfield_data{visibility} ="" if ($tag eq 773 and $cgi->param('hostitemnumber'));
345 # it's an authorised field
346 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
347 $subfield_data{marc_value} =
348 build_authorized_values_list( $tag, $subfield, $value, $dbh,
349 $authorised_values_sth,$index_tag,$index_subfield );
351 # it's a subfield $9 linking to an authority record - see bug 2206
353 elsif ($subfield eq "9" and
354 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
355 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
356 $tagslib->{$tag}->{'a'}->{authtypecode} ne '') {
358 $subfield_data{marc_value} = {
359 type => 'text',
360 id => $subfield_data{id},
361 name => $subfield_data{id},
362 value => $value,
363 size => 5,
364 maxlength => $subfield_data{maxlength},
365 readonly => 1,
368 # it's a thesaurus / authority field
370 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
371 # when authorities auto-creation is allowed, do not set readonly
372 my $is_readonly = !C4::Context->preference("BiblioAddsAuthorities");
374 $subfield_data{marc_value} = {
375 type => 'text',
376 id => $subfield_data{id},
377 name => $subfield_data{id},
378 value => $value,
379 size => 67,
380 maxlength => $subfield_data{maxlength},
381 readonly => ($is_readonly) ? 1 : 0,
382 authtype => $tagslib->{$tag}->{$subfield}->{authtypecode},
385 # it's a plugin field
386 } elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
387 require Koha::FrameworkPlugin;
388 my $plugin = Koha::FrameworkPlugin->new( {
389 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
391 my $pars= { dbh => $dbh, record => $rec, tagslib => $tagslib,
392 id => $subfield_data{id}, tabloop => $tabloop };
393 $plugin->build( $pars );
394 if( !$plugin->errstr ) {
395 $subfield_data{marc_value} = {
396 type => 'text_complex',
397 id => $subfield_data{id},
398 name => $subfield_data{id},
399 value => $value,
400 size => 67,
401 maxlength => $subfield_data{maxlength},
402 javascript => $plugin->javascript,
403 noclick => $plugin->noclick,
405 } else {
406 warn $plugin->errstr;
407 # supply default input form
408 $subfield_data{marc_value} = {
409 type => 'text',
410 id => $subfield_data{id},
411 name => $subfield_data{id},
412 value => $value,
413 size => 67,
414 maxlength => $subfield_data{maxlength},
415 readonly => 0,
419 # it's an hidden field
420 } elsif ( $tag eq '' ) {
421 $subfield_data{marc_value} = {
422 type => 'hidden',
423 id => $subfield_data{id},
424 name => $subfield_data{id},
425 value => $value,
426 size => 67,
427 maxlength => $subfield_data{maxlength},
431 else {
432 # it's a standard field
433 if (
434 length($value) > 100
436 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
437 and $tag < 400 && $subfield eq 'a' )
438 or ( $tag >= 500
439 and $tag < 600
440 && C4::Context->preference("marcflavour") eq "MARC21" )
443 $subfield_data{marc_value} = {
444 type => 'textarea',
445 id => $subfield_data{id},
446 name => $subfield_data{id},
447 value => $value,
451 else {
452 $subfield_data{marc_value} = {
453 type => 'text',
454 id => $subfield_data{id},
455 name => $subfield_data{id},
456 value => $value,
457 size => 67,
458 maxlength => $subfield_data{maxlength},
459 readonly => 0,
464 $subfield_data{'index_subfield'} = $index_subfield;
465 return \%subfield_data;
469 =head2 format_indicator
471 Translate indicator value for output form - specifically, map
472 indicator = ' ' to ''. This is for the convenience of a cataloger
473 using a mouse to select an indicator input.
475 =cut
477 sub format_indicator {
478 my $ind_value = shift;
479 return '' if not defined $ind_value;
480 return '' if $ind_value eq ' ';
481 return $ind_value;
484 sub build_tabs {
485 my ( $template, $record, $dbh, $encoding,$input ) = @_;
487 # fill arrays
488 my @loop_data = ();
489 my $tag;
491 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
492 my $query = "SELECT authorised_value, lib
493 FROM authorised_values";
494 $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id )} if $branch_limit;
495 $query .= " WHERE category = ?";
496 $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
497 $query .= " GROUP BY lib ORDER BY lib, lib_opac";
498 my $authorised_values_sth = $dbh->prepare( $query );
500 # in this array, we will push all the 10 tabs
501 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
502 my @BIG_LOOP;
503 my %seen;
504 my @tab_data; # all tags to display
506 foreach my $used ( @$usedTagsLib ){
507 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
508 $seen{$used->{tagfield}}++;
511 my $max_num_tab=-1;
512 foreach(@$usedTagsLib){
513 if($_->{tab} > -1 && $_->{tab} >= $max_num_tab && $_->{tagfield} != '995'){ # FIXME : MARC21 ?
514 $max_num_tab = $_->{tab};
517 if($max_num_tab >= 9){
518 $max_num_tab = 9;
520 # loop through each tab 0 through 9
521 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
522 my @loop_data = (); #innerloop in the template.
523 my $i = 0;
524 foreach my $tag (@tab_data) {
525 $i++;
526 next if ! $tag;
527 my ($indicator1, $indicator2);
528 my $index_tag = CreateKey;
530 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
531 # if MARC::Record is empty => use tab as master loop.
532 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
533 my @fields;
534 if ( $tag ne '000' ) {
535 @fields = $record->field($tag);
537 else {
538 push @fields, $record->leader(); # if tag == 000
540 # loop through each field
541 foreach my $field (@fields) {
543 my @subfields_data;
544 if ( $tag < 10 ) {
545 my ( $value, $subfield );
546 if ( $tag ne '000' ) {
547 $value = $field->data();
548 $subfield = "@";
550 else {
551 $value = $field;
552 $subfield = '@';
554 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
555 next
556 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
557 'biblio.biblionumber' );
558 push(
559 @subfields_data,
560 &create_input(
561 $tag, $subfield, $value, $index_tag, $tabloop, $record,
562 $authorised_values_sth,$input
566 else {
567 my @subfields = $field->subfields();
568 foreach my $subfieldcount ( 0 .. $#subfields ) {
569 my $subfield = $subfields[$subfieldcount][0];
570 my $value = $subfields[$subfieldcount][1];
571 next if ( length $subfield != 1 );
572 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
573 push(
574 @subfields_data,
575 &create_input(
576 $tag, $subfield, $value, $index_tag, $tabloop,
577 $record, $authorised_values_sth,$input
583 # now, loop again to add parameter subfield that are not in the MARC::Record
584 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
586 next if ( length $subfield != 1 );
587 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
588 next if ( $tag < 10 );
589 next
590 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
591 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
592 and not ( $subfield eq "9" and
593 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
594 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
595 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
597 ; #check for visibility flag
598 # if subfield is $9 in a field whose $a is authority-controlled,
599 # always include in the form regardless of the hidden setting - bug 2206
600 next if ( defined( $field->subfield($subfield) ) );
601 push(
602 @subfields_data,
603 &create_input(
604 $tag, $subfield, '', $index_tag, $tabloop, $record,
605 $authorised_values_sth,$input
609 if ( $#subfields_data >= 0 ) {
610 # build the tag entry.
611 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
612 # have twice the same "name" value, and cgi->param() will return only one, making
613 # all subfields to be merged in a single field.
614 my %tag_data = (
615 tag => $tag,
616 index => $index_tag,
617 tag_lib => $tagslib->{$tag}->{lib},
618 repeatable => $tagslib->{$tag}->{repeatable},
619 mandatory => $tagslib->{$tag}->{mandatory},
620 subfield_loop => \@subfields_data,
621 fixedfield => $tag < 10?1:0,
622 random => CreateKey,
624 if ($tag >= 10){ # no indicator for 00x tags
625 $tag_data{indicator1} = format_indicator($field->indicator(1)),
626 $tag_data{indicator2} = format_indicator($field->indicator(2)),
628 push( @loop_data, \%tag_data );
630 } # foreach $field end
632 # if breeding is empty
634 else {
635 my @subfields_data;
636 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
637 next if ( length $subfield != 1 );
638 next
639 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
640 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
641 and not ( $subfield eq "9" and
642 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
643 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
644 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
646 ; #check for visibility flag
647 # if subfield is $9 in a field whose $a is authority-controlled,
648 # always include in the form regardless of the hidden setting - bug 2206
649 next
650 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
651 push(
652 @subfields_data,
653 &create_input(
654 $tag, $subfield, '', $index_tag, $tabloop, $record,
655 $authorised_values_sth,$input
659 if ( $#subfields_data >= 0 ) {
660 my %tag_data = (
661 tag => $tag,
662 index => $index_tag,
663 tag_lib => $tagslib->{$tag}->{lib},
664 repeatable => $tagslib->{$tag}->{repeatable},
665 mandatory => $tagslib->{$tag}->{mandatory},
666 indicator1 => $indicator1,
667 indicator2 => $indicator2,
668 subfield_loop => \@subfields_data,
669 tagfirstsubfield => $subfields_data[0],
670 fixedfield => $tag < 10?1:0,
673 push @loop_data, \%tag_data ;
677 if ( $#loop_data >= 0 ) {
678 push @BIG_LOOP, {
679 number => $tabloop,
680 innerloop => \@loop_data,
684 $authorised_values_sth->finish;
685 $template->param( BIG_LOOP => \@BIG_LOOP );
688 # ========================
689 # MAIN
690 #=========================
691 my $input = new CGI;
692 my $error = $input->param('error');
693 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
694 my $parentbiblio = $input->param('parentbiblionumber');
695 my $breedingid = $input->param('breedingid');
696 my $z3950 = $input->param('z3950');
697 my $op = $input->param('op');
698 my $mode = $input->param('mode');
699 my $frameworkcode = $input->param('frameworkcode');
700 my $redirect = $input->param('redirect');
701 my $searchid = $input->param('searchid');
702 my $dbh = C4::Context->dbh;
703 my $hostbiblionumber = $input->param('hostbiblionumber');
704 my $hostitemnumber = $input->param('hostitemnumber');
705 # fast cataloguing datas in transit
706 my $fa_circborrowernumber = $input->param('circborrowernumber');
707 my $fa_barcode = $input->param('barcode');
708 my $fa_branch = $input->param('branch');
709 my $fa_stickyduedate = $input->param('stickyduedate');
710 my $fa_duedatespec = $input->param('duedatespec');
712 my $userflags = 'edit_catalogue';
714 my $changed_framework = $input->param('changed_framework');
715 $frameworkcode = &GetFrameworkCode($biblionumber)
716 if ( $biblionumber and not( defined $frameworkcode) and $op ne 'addbiblio' );
718 if ($frameworkcode eq 'FA'){
719 $userflags = 'fast_cataloging';
722 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
723 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
725 template_name => "cataloguing/addbiblio.tt",
726 query => $input,
727 type => "intranet",
728 authnotrequired => 0,
729 flagsrequired => { editcatalogue => $userflags },
733 if ($biblionumber){
734 my $does_bib_exist = Koha::Biblios->find($biblionumber);
735 if (!defined $does_bib_exist){
736 $biblionumber = undef;
737 $template->param( bib_doesnt_exist => 1 );
741 if ($frameworkcode eq 'FA'){
742 # We need to grab and set some variables in the template for use on the additems screen
743 $template->param(
744 'circborrowernumber' => $fa_circborrowernumber,
745 'barcode' => $fa_barcode,
746 'branch' => $fa_branch,
747 'stickyduedate' => $fa_stickyduedate,
748 'duedatespec' => $fa_duedatespec,
750 } elsif ( $op ne "delete" && C4::Context->preference('EnableAdvancedCatalogingEditor') && $input->cookie( 'catalogue_editor_' . $loggedinuser ) eq 'advanced' && !$breedingid ) {
751 # Only use the advanced editor for non-fast-cataloging.
752 # breedingid is not handled because those would only come off a Z39.50
753 # search initiated by the basic editor.
754 print $input->redirect( '/cgi-bin/koha/cataloguing/editor.pl' . ( $biblionumber ? ( '#catalog/' . $biblionumber ) : '' ) );
755 exit;
758 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
759 $template->param(
760 frameworks => $frameworks,
761 breedingid => $breedingid,
764 # ++ Global
765 $tagslib = &GetMarcStructure( 1, $frameworkcode );
766 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
767 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
768 # -- Global
770 my $record = -1;
771 my $encoding = "";
772 my (
773 $biblionumbertagfield,
774 $biblionumbertagsubfield,
775 $biblioitemnumtagfield,
776 $biblioitemnumtagsubfield,
777 $biblioitemnumber
780 if (($biblionumber) && !($breedingid)){
781 $record = GetMarcBiblio({ biblionumber => $biblionumber });
783 if ($breedingid) {
784 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
787 #populate hostfield if hostbiblionumber is available
788 if ($hostbiblionumber) {
789 my $marcflavour = C4::Context->preference("marcflavour");
790 $record = MARC::Record->new();
791 $record->leader('');
792 my $field =
793 PrepHostMarcField( $hostbiblionumber, $hostitemnumber, $marcflavour );
794 $record->append_fields($field);
797 # This is a child record
798 if ($parentbiblio) {
799 my $marcflavour = C4::Context->preference('marcflavour');
800 $record = MARC::Record->new();
801 SetMarcUnicodeFlag($record, $marcflavour);
802 my $hostfield = prepare_host_field($parentbiblio,$marcflavour);
803 if ($hostfield) {
804 $record->append_fields($hostfield);
808 $is_a_modif = 0;
810 if ($biblionumber) {
811 $is_a_modif = 1;
812 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
813 $template->param( title => $title );
815 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
816 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
817 &GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
818 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
819 &GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
821 # search biblioitems value
822 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
823 $sth->execute($biblionumber);
824 ($biblioitemnumber) = $sth->fetchrow;
827 #-------------------------------------------------------------------------------------
828 if ( $op eq "addbiblio" ) {
829 #-------------------------------------------------------------------------------------
830 $template->param(
831 biblionumberdata => $biblionumber,
833 # getting html input
834 my @params = $input->multi_param();
835 $record = TransformHtmlToMarc( $input, 1 );
836 # check for a duplicate
837 my ( $duplicatebiblionumber, $duplicatetitle );
838 if ( !$is_a_modif ) {
839 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
841 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
842 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
843 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
844 my $oldbibitemnum;
845 if (C4::Context->preference("BiblioAddsAuthorities")){
846 BiblioAutoLink( $record, $frameworkcode );
848 if ( $is_a_modif ) {
849 ModBiblio( $record, $biblionumber, $frameworkcode );
851 else {
852 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
854 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view" && $redirect ne "just_save")){
855 if ($frameworkcode eq 'FA'){
856 print $input->redirect(
857 '/cgi-bin/koha/cataloguing/additem.pl?'
858 .'biblionumber='.$biblionumber
859 .'&frameworkcode='.$frameworkcode
860 .'&circborrowernumber='.$fa_circborrowernumber
861 .'&branch='.$fa_branch
862 .'&barcode='.uri_escape_utf8($fa_barcode)
863 .'&stickyduedate='.$fa_stickyduedate
864 .'&duedatespec='.$fa_duedatespec
866 exit;
868 else {
869 print $input->redirect(
870 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid"
872 exit;
875 elsif(($is_a_modif || $redirect eq "view") && $redirect ne "just_save"){
876 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
877 my $views = { C4::Search::enabled_staff_search_views };
878 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
879 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
880 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
881 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
882 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
883 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
884 } else {
885 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
887 exit;
890 elsif ($redirect eq "just_save"){
891 my $tab = $input->param('current_tab');
892 print $input->redirect("/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber&framework=$frameworkcode&tab=$tab&searchid=$searchid");
894 else {
895 $template->param(
896 biblionumber => $biblionumber,
897 done =>1,
898 popup =>1
900 if ( $record ne '-1' ) {
901 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
902 $template->param( title => $title );
904 $template->param(
905 popup => $mode,
906 itemtype => $frameworkcode,
908 output_html_with_http_headers $input, $cookie, $template->output;
909 exit;
911 } else {
912 # it may be a duplicate, warn the user and do nothing
913 build_tabs ($template, $record, $dbh,$encoding,$input);
914 $template->param(
915 biblionumber => $biblionumber,
916 biblioitemnumber => $biblioitemnumber,
917 duplicatebiblionumber => $duplicatebiblionumber,
918 duplicatebibid => $duplicatebiblionumber,
919 duplicatetitle => $duplicatetitle,
923 elsif ( $op eq "delete" ) {
925 my $error = &DelBiblio($biblionumber);
926 if ($error) {
927 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
928 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
929 exit;
932 print $input->redirect('/cgi-bin/koha/catalogue/search.pl');
933 exit;
935 } else {
936 #----------------------------------------------------------------------------
937 # If we're in a duplication case, we have to set to "" the biblionumber
938 # as we'll save the biblio as a new one.
939 $template->param(
940 biblionumberdata => $biblionumber,
941 op => $op,
943 if ( $op eq "duplicate" ) {
944 $biblionumber = "";
947 if($changed_framework eq "changed"){
948 $record = TransformHtmlToMarc( $input, 1 );
950 elsif( $record ne -1 ) {
951 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
952 eval {
953 my $uxml = $record->as_xml;
954 MARC::Record::default_record_format("UNIMARC")
955 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
956 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
957 $record = $urecord;
960 build_tabs( $template, $record, $dbh, $encoding,$input );
961 $template->param(
962 biblionumber => $biblionumber,
963 biblionumbertagfield => $biblionumbertagfield,
964 biblionumbertagsubfield => $biblionumbertagsubfield,
965 biblioitemnumtagfield => $biblioitemnumtagfield,
966 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
967 biblioitemnumber => $biblioitemnumber,
968 hostbiblionumber => $hostbiblionumber,
969 hostitemnumber => $hostitemnumber
973 if ( $record ne '-1' ) {
974 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
975 $template->param( title => $title );
977 $template->param(
978 popup => $mode,
979 frameworkcode => $frameworkcode,
980 itemtype => $frameworkcode,
981 borrowernumber => $loggedinuser,
982 tab => scalar $input->param('tab')
984 $template->{'VARS'}->{'searchid'} = $searchid;
986 output_html_with_http_headers $input, $cookie, $template->output;