Bug 14685: Fix set guarantor for a child or professional accounts
[koha.git] / cataloguing / addbiblio.pl
blob890db0f946097310a21a87dc9f87c0a10c5979b9
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; # XXX subfield_is_koha_internal_p
34 use C4::Branch; # XXX subfield_is_koha_internal_p
35 use C4::ClassSource;
36 use C4::ImportBatch;
37 use C4::Charset;
39 use Date::Calc qw(Today);
40 use MARC::File::USMARC;
41 use MARC::File::XML;
42 use URI::Escape;
44 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
45 MARC::File::XML->default_record_format('UNIMARC');
48 our($tagslib,$authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
50 =head1 FUNCTIONS
52 =head2 MARCfindbreeding
54 $record = MARCfindbreeding($breedingid);
56 Look up the import record repository for the record with
57 record with id $breedingid. If found, returns the decoded
58 MARC::Record; otherwise, -1 is returned (FIXME).
59 Returns as second parameter the character encoding.
61 =cut
63 sub MARCfindbreeding {
64 my ( $id ) = @_;
65 my ($marc, $encoding) = GetImportRecordMarc($id);
66 # remove the - in isbn, koha store isbn without any -
67 if ($marc) {
68 my $record = MARC::Record->new_from_usmarc($marc);
69 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
70 if ( $record->field($isbnfield) ) {
71 foreach my $field ( $record->field($isbnfield) ) {
72 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
73 my $newisbn = $field->subfield($isbnsubfield);
74 $newisbn =~ s/-//g;
75 $field->update( $isbnsubfield => $newisbn );
79 # fix the unimarc 100 coded field (with unicode information)
80 if (C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield(100,'a')) {
81 my $f100a=$record->subfield(100,'a');
82 my $f100 = $record->field(100);
83 my $f100temp = $f100->as_string;
84 $record->delete_field($f100);
85 if ( length($f100temp) > 28 ) {
86 substr( $f100temp, 26, 2, "50" );
87 $f100->update( 'a' => $f100temp );
88 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
89 $record->insert_fields_ordered($f100);
93 if ( !defined(ref($record)) ) {
94 return -1;
96 else {
97 # normalize author : UNIMARC specific...
98 if ( C4::Context->preference("z3950NormalizeAuthor")
99 and C4::Context->preference("z3950AuthorAuthFields")
100 and C4::Context->preference("marcflavour") eq 'UNIMARC' )
102 my ( $tag, $subfield ) = GetMarcFromKohaField("biblio.author", '');
104 # my $summary = C4::Context->preference("z3950authortemplate");
105 my $auth_fields =
106 C4::Context->preference("z3950AuthorAuthFields");
107 my @auth_fields = split /,/, $auth_fields;
108 my $field;
110 if ( $record->field($tag) ) {
111 foreach my $tmpfield ( $record->field($tag)->subfields ) {
113 # foreach my $subfieldcode ($tmpfield->subfields){
114 my $subfieldcode = shift @$tmpfield;
115 my $subfieldvalue = shift @$tmpfield;
116 if ($field) {
117 $field->add_subfields(
118 "$subfieldcode" => $subfieldvalue )
119 if ( $subfieldcode ne $subfield );
121 else {
122 $field =
123 MARC::Field->new( $tag, "", "",
124 $subfieldcode => $subfieldvalue )
125 if ( $subfieldcode ne $subfield );
129 $record->delete_field( $record->field($tag) );
130 foreach my $fieldtag (@auth_fields) {
131 next unless ( $record->field($fieldtag) );
132 my $lastname = $record->field($fieldtag)->subfield('a');
133 my $firstname = $record->field($fieldtag)->subfield('b');
134 my $title = $record->field($fieldtag)->subfield('c');
135 my $number = $record->field($fieldtag)->subfield('d');
136 if ($title) {
138 # $field->add_subfields("$subfield"=>"[ ".ucfirst($title).ucfirst($firstname)." ".$number." ]");
139 $field->add_subfields(
140 "$subfield" => ucfirst($title) . " "
141 . ucfirst($firstname) . " "
142 . $number );
144 else {
146 # $field->add_subfields("$subfield"=>"[ ".ucfirst($firstname).", ".ucfirst($lastname)." ]");
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 #Use GetBranches($onlymine)
175 my $onlymine =
176 C4::Context->preference('IndependentBranches')
177 && C4::Context->userenv
178 && !C4::Context->IsSuperLibrarian()
179 && C4::Context->userenv->{branch};
180 my $branches = GetBranches($onlymine);
181 my @branchloop;
182 foreach my $thisbranch ( sort keys %$branches ) {
183 push @authorised_values, $thisbranch;
184 $authorised_lib{$thisbranch} = $branches->{$thisbranch}->{'branchname'};
187 #----- itemtypes
189 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
190 my $sth =
191 $dbh->prepare(
192 "select itemtype,description from itemtypes order by description");
193 $sth->execute;
194 push @authorised_values, ""
195 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
196 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
198 my $itemtype;
200 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
201 push @authorised_values, $itemtype;
202 $authorised_lib{$itemtype} = $description;
204 $value = $itemtype unless ($value);
206 #---- class_sources
208 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
209 push @authorised_values, ""
210 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
212 my $class_sources = GetClassSources();
214 my $default_source = C4::Context->preference("DefaultClassificationSource");
216 foreach my $class_source (sort keys %$class_sources) {
217 next unless $class_sources->{$class_source}->{'used'} or
218 ($value and $class_source eq $value) or
219 ($class_source eq $default_source);
220 push @authorised_values, $class_source;
221 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
223 $value = $default_source unless $value;
225 else {
226 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
227 $authorised_values_sth->execute(
228 $tagslib->{$tag}->{$subfield}->{authorised_value},
229 $branch_limit ? $branch_limit : (),
232 push @authorised_values, ""
233 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
234 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
236 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
237 push @authorised_values, $value;
238 $authorised_lib{$value} = $lib;
241 $authorised_values_sth->finish;
242 return {
243 type => 'select',
244 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
245 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
246 default => $value,
247 values => \@authorised_values,
248 labels => \%authorised_lib,
253 =head2 CreateKey
255 Create a random value to set it into the input name
257 =cut
259 sub CreateKey {
260 return int(rand(1000000));
263 =head2 GetMandatoryFieldZ3950
265 This function return an hashref which containts all mandatory field
266 to search with z3950 server.
268 =cut
270 sub GetMandatoryFieldZ3950 {
271 my $frameworkcode = shift;
272 my @isbn = GetMarcFromKohaField('biblioitems.isbn',$frameworkcode);
273 my @title = GetMarcFromKohaField('biblio.title',$frameworkcode);
274 my @author = GetMarcFromKohaField('biblio.author',$frameworkcode);
275 my @issn = GetMarcFromKohaField('biblioitems.issn',$frameworkcode);
276 my @lccn = GetMarcFromKohaField('biblioitems.lccn',$frameworkcode);
278 return {
279 $isbn[0].$isbn[1] => 'isbn',
280 $title[0].$title[1] => 'title',
281 $author[0].$author[1] => 'author',
282 $issn[0].$issn[1] => 'issn',
283 $lccn[0].$lccn[1] => 'lccn',
287 =head2 create_input
289 builds the <input ...> entry for a subfield.
291 =cut
293 sub create_input {
294 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
296 my $index_subfield = CreateKey(); # create a specifique key for each subfield
298 $value =~ s/"/&quot;/g;
300 # if there is no value provided but a default value in parameters, get it
301 if ( $value eq '' ) {
302 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
304 # get today date & replace YYYY, MM, DD if provided in the default value
305 my ( $year, $month, $day ) = Today();
306 $month = sprintf( "%02d", $month );
307 $day = sprintf( "%02d", $day );
308 $value =~ s/YYYY/$year/g;
309 $value =~ s/MM/$month/g;
310 $value =~ s/DD/$day/g;
311 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
312 $value=~s/user/$username/g;
315 my $dbh = C4::Context->dbh;
317 # map '@' as "subfield" label for fixed fields
318 # to something that's allowed in a div id.
319 my $id_subfield = $subfield;
320 $id_subfield = "00" if $id_subfield eq "@";
322 my %subfield_data = (
323 tag => $tag,
324 subfield => $id_subfield,
325 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
326 tag_mandatory => $tagslib->{$tag}->{mandatory},
327 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
328 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
329 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
330 index => $index_tag,
331 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
332 value => $value,
333 maxlength => $tagslib->{$tag}->{$subfield}->{maxlength},
334 random => CreateKey(),
337 if(exists $mandatory_z3950->{$tag.$subfield}){
338 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
340 # Subfield is hidden depending of hidden and mandatory flag, and is always
341 # shown if it contains anything or if its field is mandatory.
342 my $tdef = $tagslib->{$tag};
343 $subfield_data{visibility} = "display:none;"
344 if $tdef->{$subfield}->{hidden} % 2 == 1 &&
345 $value eq '' &&
346 !$tdef->{$subfield}->{mandatory} &&
347 !$tdef->{mandatory};
348 # expand all subfields of 773 if there is a host item provided in the input
349 $subfield_data{visibility} ="" if ($tag eq 773 and $cgi->param('hostitemnumber'));
352 # it's an authorised field
353 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
354 $subfield_data{marc_value} =
355 build_authorized_values_list( $tag, $subfield, $value, $dbh,
356 $authorised_values_sth,$index_tag,$index_subfield );
358 # it's a subfield $9 linking to an authority record - see bug 2206
360 elsif ($subfield eq "9" and
361 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
362 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
363 $tagslib->{$tag}->{'a'}->{authtypecode} ne '') {
365 $subfield_data{marc_value} = {
366 type => 'text',
367 id => $subfield_data{id},
368 name => $subfield_data{id},
369 value => $value,
370 size => 5,
371 maxlength => $subfield_data{maxlength},
372 readonly => 1,
375 # it's a thesaurus / authority field
377 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
378 # when authorities auto-creation is allowed, do not set readonly
379 my $is_readonly = !C4::Context->preference("BiblioAddsAuthorities");
381 $subfield_data{marc_value} = {
382 type => 'text',
383 id => $subfield_data{id},
384 name => $subfield_data{id},
385 value => $value,
386 size => 67,
387 maxlength => $subfield_data{maxlength},
388 readonly => ($is_readonly) ? 1 : 0,
389 authtype => $tagslib->{$tag}->{$subfield}->{authtypecode},
392 # it's a plugin field
393 } elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
394 require Koha::FrameworkPlugin;
395 my $plugin = Koha::FrameworkPlugin->new( {
396 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
398 my $pars= { dbh => $dbh, record => $rec, tagslib => $tagslib,
399 id => $subfield_data{id}, tabloop => $tabloop };
400 $plugin->build( $pars );
401 if( !$plugin->errstr ) {
402 $subfield_data{marc_value} = {
403 type => 'text_complex',
404 id => $subfield_data{id},
405 name => $subfield_data{id},
406 value => $value,
407 size => 67,
408 maxlength => $subfield_data{maxlength},
409 javascript => $plugin->javascript,
410 noclick => $plugin->noclick,
412 } else {
413 warn $plugin->errstr;
414 # supply default input form
415 $subfield_data{marc_value} = {
416 type => 'text',
417 id => $subfield_data{id},
418 name => $subfield_data{id},
419 value => $value,
420 size => 67,
421 maxlength => $subfield_data{maxlength},
422 readonly => 0,
426 # it's an hidden field
427 } elsif ( $tag eq '' ) {
428 $subfield_data{marc_value} = {
429 type => 'hidden',
430 id => $subfield_data{id},
431 name => $subfield_data{id},
432 value => $value,
433 size => 67,
434 maxlength => $subfield_data{maxlength},
438 else {
439 # it's a standard field
440 if (
441 length($value) > 100
443 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
444 and $tag < 400 && $subfield eq 'a' )
445 or ( $tag >= 500
446 and $tag < 600
447 && C4::Context->preference("marcflavour") eq "MARC21" )
450 $subfield_data{marc_value} = {
451 type => 'textarea',
452 id => $subfield_data{id},
453 name => $subfield_data{id},
454 value => $value,
458 else {
459 $subfield_data{marc_value} = {
460 type => 'text',
461 id => $subfield_data{id},
462 name => $subfield_data{id},
463 value => $value,
464 size => 67,
465 maxlength => $subfield_data{maxlength},
466 readonly => 0,
471 $subfield_data{'index_subfield'} = $index_subfield;
472 return \%subfield_data;
476 =head2 format_indicator
478 Translate indicator value for output form - specifically, map
479 indicator = ' ' to ''. This is for the convenience of a cataloger
480 using a mouse to select an indicator input.
482 =cut
484 sub format_indicator {
485 my $ind_value = shift;
486 return '' if not defined $ind_value;
487 return '' if $ind_value eq ' ';
488 return $ind_value;
491 sub build_tabs {
492 my ( $template, $record, $dbh, $encoding,$input ) = @_;
494 # fill arrays
495 my @loop_data = ();
496 my $tag;
498 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
499 my $query = "SELECT authorised_value, lib
500 FROM authorised_values";
501 $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id )} if $branch_limit;
502 $query .= " WHERE category = ?";
503 $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
504 $query .= " GROUP BY lib ORDER BY lib, lib_opac";
505 my $authorised_values_sth = $dbh->prepare( $query );
507 # in this array, we will push all the 10 tabs
508 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
509 my @BIG_LOOP;
510 my %seen;
511 my @tab_data; # all tags to display
513 foreach my $used ( @$usedTagsLib ){
514 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
515 $seen{$used->{tagfield}}++;
518 my $max_num_tab=-1;
519 foreach(@$usedTagsLib){
520 if($_->{tab} > -1 && $_->{tab} >= $max_num_tab && $_->{tagfield} != '995'){ # FIXME : MARC21 ?
521 $max_num_tab = $_->{tab};
524 if($max_num_tab >= 9){
525 $max_num_tab = 9;
527 # loop through each tab 0 through 9
528 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
529 my @loop_data = (); #innerloop in the template.
530 my $i = 0;
531 foreach my $tag (@tab_data) {
532 $i++;
533 next if ! $tag;
534 my ($indicator1, $indicator2);
535 my $index_tag = CreateKey;
537 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
538 # if MARC::Record is empty => use tab as master loop.
539 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
540 my @fields;
541 if ( $tag ne '000' ) {
542 @fields = $record->field($tag);
544 else {
545 push @fields, $record->leader(); # if tag == 000
547 # loop through each field
548 foreach my $field (@fields) {
550 my @subfields_data;
551 if ( $tag < 10 ) {
552 my ( $value, $subfield );
553 if ( $tag ne '000' ) {
554 $value = $field->data();
555 $subfield = "@";
557 else {
558 $value = $field;
559 $subfield = '@';
561 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
562 next
563 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
564 'biblio.biblionumber' );
565 push(
566 @subfields_data,
567 &create_input(
568 $tag, $subfield, $value, $index_tag, $tabloop, $record,
569 $authorised_values_sth,$input
573 else {
574 my @subfields = $field->subfields();
575 foreach my $subfieldcount ( 0 .. $#subfields ) {
576 my $subfield = $subfields[$subfieldcount][0];
577 my $value = $subfields[$subfieldcount][1];
578 next if ( length $subfield != 1 );
579 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
580 push(
581 @subfields_data,
582 &create_input(
583 $tag, $subfield, $value, $index_tag, $tabloop,
584 $record, $authorised_values_sth,$input
590 # now, loop again to add parameter subfield that are not in the MARC::Record
591 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
593 next if ( length $subfield != 1 );
594 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
595 next if ( $tag < 10 );
596 next
597 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
598 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
599 and not ( $subfield eq "9" and
600 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
601 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
602 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
604 ; #check for visibility flag
605 # if subfield is $9 in a field whose $a is authority-controlled,
606 # always include in the form regardless of the hidden setting - bug 2206
607 next if ( defined( $field->subfield($subfield) ) );
608 push(
609 @subfields_data,
610 &create_input(
611 $tag, $subfield, '', $index_tag, $tabloop, $record,
612 $authorised_values_sth,$input
616 if ( $#subfields_data >= 0 ) {
617 # build the tag entry.
618 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
619 # have twice the same "name" value, and cgi->param() will return only one, making
620 # all subfields to be merged in a single field.
621 my %tag_data = (
622 tag => $tag,
623 index => $index_tag,
624 tag_lib => $tagslib->{$tag}->{lib},
625 repeatable => $tagslib->{$tag}->{repeatable},
626 mandatory => $tagslib->{$tag}->{mandatory},
627 subfield_loop => \@subfields_data,
628 fixedfield => $tag < 10?1:0,
629 random => CreateKey,
631 if ($tag >= 10){ # no indicator for 00x tags
632 $tag_data{indicator1} = format_indicator($field->indicator(1)),
633 $tag_data{indicator2} = format_indicator($field->indicator(2)),
635 push( @loop_data, \%tag_data );
637 } # foreach $field end
639 # if breeding is empty
641 else {
642 my @subfields_data;
643 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
644 next if ( length $subfield != 1 );
645 next
646 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
647 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
648 and not ( $subfield eq "9" and
649 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
650 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
651 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
653 ; #check for visibility flag
654 # if subfield is $9 in a field whose $a is authority-controlled,
655 # always include in the form regardless of the hidden setting - bug 2206
656 next
657 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
658 push(
659 @subfields_data,
660 &create_input(
661 $tag, $subfield, '', $index_tag, $tabloop, $record,
662 $authorised_values_sth,$input
666 if ( $#subfields_data >= 0 ) {
667 my %tag_data = (
668 tag => $tag,
669 index => $index_tag,
670 tag_lib => $tagslib->{$tag}->{lib},
671 repeatable => $tagslib->{$tag}->{repeatable},
672 mandatory => $tagslib->{$tag}->{mandatory},
673 indicator1 => $indicator1,
674 indicator2 => $indicator2,
675 subfield_loop => \@subfields_data,
676 tagfirstsubfield => $subfields_data[0],
677 fixedfield => $tag < 10?1:0,
680 push @loop_data, \%tag_data ;
684 if ( $#loop_data >= 0 ) {
685 push @BIG_LOOP, {
686 number => $tabloop,
687 innerloop => \@loop_data,
691 $authorised_values_sth->finish;
692 $template->param( BIG_LOOP => \@BIG_LOOP );
695 # ========================
696 # MAIN
697 #=========================
698 my $input = new CGI;
699 my $error = $input->param('error');
700 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
701 my $parentbiblio = $input->param('parentbiblionumber');
702 my $breedingid = $input->param('breedingid');
703 my $z3950 = $input->param('z3950');
704 my $op = $input->param('op');
705 my $mode = $input->param('mode');
706 my $frameworkcode = $input->param('frameworkcode');
707 my $redirect = $input->param('redirect');
708 my $searchid = $input->param('searchid');
709 my $dbh = C4::Context->dbh;
710 my $hostbiblionumber = $input->param('hostbiblionumber');
711 my $hostitemnumber = $input->param('hostitemnumber');
712 # fast cataloguing datas in transit
713 my $fa_circborrowernumber = $input->param('circborrowernumber');
714 my $fa_barcode = $input->param('barcode');
715 my $fa_branch = $input->param('branch');
716 my $fa_stickyduedate = $input->param('stickyduedate');
717 my $fa_duedatespec = $input->param('duedatespec');
719 my $userflags = 'edit_catalogue';
721 my $changed_framework = $input->param('changed_framework');
722 $frameworkcode = &GetFrameworkCode($biblionumber)
723 if ( $biblionumber and not($frameworkcode) and $op ne 'addbiblio' );
725 if ($frameworkcode eq 'FA'){
726 $userflags = 'fast_cataloging';
729 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
730 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
732 template_name => "cataloguing/addbiblio.tt",
733 query => $input,
734 type => "intranet",
735 authnotrequired => 0,
736 flagsrequired => { editcatalogue => $userflags },
740 if ($frameworkcode eq 'FA'){
741 # We need to grab and set some variables in the template for use on the additems screen
742 $template->param(
743 'circborrowernumber' => $fa_circborrowernumber,
744 'barcode' => $fa_barcode,
745 'branch' => $fa_branch,
746 'stickyduedate' => $fa_stickyduedate,
747 'duedatespec' => $fa_duedatespec,
751 # Getting the list of all frameworks
752 # get framework list
753 my $frameworks = getframeworks;
754 my @frameworkcodeloop;
755 foreach my $thisframeworkcode ( keys %$frameworks ) {
756 my %row = (
757 value => $thisframeworkcode,
758 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
760 if ($frameworkcode eq $thisframeworkcode){
761 $row{'selected'} = 1;
763 push @frameworkcodeloop, \%row;
765 $template->param( frameworkcodeloop => \@frameworkcodeloop,
766 breedingid => $breedingid );
768 # ++ Global
769 $tagslib = &GetMarcStructure( 1, $frameworkcode );
770 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
771 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
772 # -- Global
774 my $record = -1;
775 my $encoding = "";
776 my (
777 $biblionumbertagfield,
778 $biblionumbertagsubfield,
779 $biblioitemnumtagfield,
780 $biblioitemnumtagsubfield,
781 $bibitem,
782 $biblioitemnumber
785 if (($biblionumber) && !($breedingid)){
786 $record = GetMarcBiblio($biblionumber);
788 if ($breedingid) {
789 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
792 #populate hostfield if hostbiblionumber is available
793 if ($hostbiblionumber) {
794 my $marcflavour = C4::Context->preference("marcflavour");
795 $record = MARC::Record->new();
796 $record->leader('');
797 my $field =
798 PrepHostMarcField( $hostbiblionumber, $hostitemnumber, $marcflavour );
799 $record->append_fields($field);
802 # This is a child record
803 if ($parentbiblio) {
804 my $marcflavour = C4::Context->preference('marcflavour');
805 $record = MARC::Record->new();
806 SetMarcUnicodeFlag($record, $marcflavour);
807 my $hostfield = prepare_host_field($parentbiblio,$marcflavour);
808 if ($hostfield) {
809 $record->append_fields($hostfield);
813 $is_a_modif = 0;
815 if ($biblionumber) {
816 $is_a_modif = 1;
817 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
818 $template->param( title => $title );
820 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
821 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
822 &GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
823 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
824 &GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
826 # search biblioitems value
827 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
828 $sth->execute($biblionumber);
829 ($biblioitemnumber) = $sth->fetchrow;
832 #-------------------------------------------------------------------------------------
833 if ( $op eq "addbiblio" ) {
834 #-------------------------------------------------------------------------------------
835 $template->param(
836 biblionumberdata => $biblionumber,
838 # getting html input
839 my @params = $input->param();
840 $record = TransformHtmlToMarc( $input );
841 # check for a duplicate
842 my ( $duplicatebiblionumber, $duplicatetitle );
843 if ( !$is_a_modif ) {
844 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
846 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
847 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
848 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
849 my $oldbibnum;
850 my $oldbibitemnum;
851 if (C4::Context->preference("BiblioAddsAuthorities")){
852 BiblioAutoLink( $record, $frameworkcode );
854 if ( $is_a_modif ) {
855 ModBiblioframework( $biblionumber, $frameworkcode );
856 ModBiblio( $record, $biblionumber, $frameworkcode );
858 else {
859 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
861 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view" && $redirect ne "just_save")){
862 if ($frameworkcode eq 'FA'){
863 print $input->redirect(
864 '/cgi-bin/koha/cataloguing/additem.pl?'
865 .'biblionumber='.$biblionumber
866 .'&frameworkcode='.$frameworkcode
867 .'&circborrowernumber='.$fa_circborrowernumber
868 .'&branch='.$fa_branch
869 .'&barcode='.uri_escape_utf8($fa_barcode)
870 .'&stickyduedate='.$fa_stickyduedate
871 .'&duedatespec='.$fa_duedatespec
873 exit;
875 else {
876 print $input->redirect(
877 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid"
879 exit;
882 elsif(($is_a_modif || $redirect eq "view") && $redirect ne "just_save"){
883 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
884 my $views = { C4::Search::enabled_staff_search_views };
885 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
886 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
887 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
888 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
889 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
890 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
891 } else {
892 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
894 exit;
897 elsif ($redirect eq "just_save"){
898 my $tab = $input->param('current_tab');
899 print $input->redirect("/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber&framework=$frameworkcode&tab=$tab&searchid=$searchid");
901 else {
902 $template->param(
903 biblionumber => $biblionumber,
904 done =>1,
905 popup =>1
907 if ( $record ne '-1' ) {
908 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
909 $template->param( title => $title );
911 $template->param(
912 popup => $mode,
913 itemtype => $frameworkcode,
915 output_html_with_http_headers $input, $cookie, $template->output;
916 exit;
918 } else {
919 # it may be a duplicate, warn the user and do nothing
920 build_tabs ($template, $record, $dbh,$encoding,$input);
921 $template->param(
922 biblionumber => $biblionumber,
923 biblioitemnumber => $biblioitemnumber,
924 duplicatebiblionumber => $duplicatebiblionumber,
925 duplicatebibid => $duplicatebiblionumber,
926 duplicatetitle => $duplicatetitle,
930 elsif ( $op eq "delete" ) {
932 my $error = &DelBiblio($biblionumber);
933 if ($error) {
934 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
935 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
936 exit;
939 print $input->redirect('/cgi-bin/koha/catalogue/search.pl');
940 exit;
942 } else {
943 #----------------------------------------------------------------------------
944 # If we're in a duplication case, we have to set to "" the biblionumber
945 # as we'll save the biblio as a new one.
946 $template->param(
947 biblionumberdata => $biblionumber,
948 op => $op,
950 if ( $op eq "duplicate" ) {
951 $biblionumber = "";
954 if($changed_framework eq "changed"){
955 $record = TransformHtmlToMarc( $input );
957 elsif( $record ne -1 ) {
958 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
959 eval {
960 my $uxml = $record->as_xml;
961 MARC::Record::default_record_format("UNIMARC")
962 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
963 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
964 $record = $urecord;
967 build_tabs( $template, $record, $dbh, $encoding,$input );
968 $template->param(
969 biblionumber => $biblionumber,
970 biblionumbertagfield => $biblionumbertagfield,
971 biblionumbertagsubfield => $biblionumbertagsubfield,
972 biblioitemnumtagfield => $biblioitemnumtagfield,
973 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
974 biblioitemnumber => $biblioitemnumber,
975 hostbiblionumber => $hostbiblionumber,
976 hostitemnumber => $hostitemnumber
980 if ( $record ne '-1' ) {
981 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
982 $template->param( title => $title );
984 $template->param(
985 popup => $mode,
986 frameworkcode => $frameworkcode,
987 itemtype => $frameworkcode,
988 borrowernumber => $loggedinuser,
989 tab => $input->param('tab')
991 $template->{'VARS'}->{'searchid'} = $searchid;
993 output_html_with_http_headers $input, $cookie, $template->output;