Bug 15071: Update compiled opac.css
[koha.git] / cataloguing / addbiblio.pl
blobe34557c4ae62782014fa8cbadf79ee36c2952e2f
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 foreach my $thisbranch ( sort keys %$branches ) {
182 push @authorised_values, $thisbranch;
183 $authorised_lib{$thisbranch} = $branches->{$thisbranch}->{'branchname'};
187 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
188 push @authorised_values, ""
189 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
190 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
192 my $itemtype;
193 my $itemtypes = GetItemTypes( style => 'array' );
194 for my $itemtype ( @$itemtypes ) {
195 push @authorised_values, $itemtype->{itemtype};
196 $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description};
198 $value = $itemtype unless ($value);
200 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
201 push @authorised_values, ""
202 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
204 my $class_sources = GetClassSources();
206 my $default_source = C4::Context->preference("DefaultClassificationSource");
208 foreach my $class_source (sort keys %$class_sources) {
209 next unless $class_sources->{$class_source}->{'used'} or
210 ($value and $class_source eq $value) or
211 ($class_source eq $default_source);
212 push @authorised_values, $class_source;
213 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
215 $value = $default_source unless $value;
217 else {
218 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
219 $authorised_values_sth->execute(
220 $tagslib->{$tag}->{$subfield}->{authorised_value},
221 $branch_limit ? $branch_limit : (),
224 push @authorised_values, ""
225 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
226 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
228 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
229 push @authorised_values, $value;
230 $authorised_lib{$value} = $lib;
233 $authorised_values_sth->finish;
234 return {
235 type => 'select',
236 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
237 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
238 default => $value,
239 values => \@authorised_values,
240 labels => \%authorised_lib,
245 =head2 CreateKey
247 Create a random value to set it into the input name
249 =cut
251 sub CreateKey {
252 return int(rand(1000000));
255 =head2 GetMandatoryFieldZ3950
257 This function return an hashref which containts all mandatory field
258 to search with z3950 server.
260 =cut
262 sub GetMandatoryFieldZ3950 {
263 my $frameworkcode = shift;
264 my @isbn = GetMarcFromKohaField('biblioitems.isbn',$frameworkcode);
265 my @title = GetMarcFromKohaField('biblio.title',$frameworkcode);
266 my @author = GetMarcFromKohaField('biblio.author',$frameworkcode);
267 my @issn = GetMarcFromKohaField('biblioitems.issn',$frameworkcode);
268 my @lccn = GetMarcFromKohaField('biblioitems.lccn',$frameworkcode);
270 return {
271 $isbn[0].$isbn[1] => 'isbn',
272 $title[0].$title[1] => 'title',
273 $author[0].$author[1] => 'author',
274 $issn[0].$issn[1] => 'issn',
275 $lccn[0].$lccn[1] => 'lccn',
279 =head2 create_input
281 builds the <input ...> entry for a subfield.
283 =cut
285 sub create_input {
286 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
288 my $index_subfield = CreateKey(); # create a specifique key for each subfield
290 $value =~ s/"/&quot;/g;
292 # if there is no value provided but a default value in parameters, get it
293 if ( $value eq '' ) {
294 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
296 # get today date & replace YYYY, MM, DD if provided in the default value
297 my ( $year, $month, $day ) = Today();
298 $month = sprintf( "%02d", $month );
299 $day = sprintf( "%02d", $day );
300 $value =~ s/YYYY/$year/g;
301 $value =~ s/MM/$month/g;
302 $value =~ s/DD/$day/g;
303 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
304 $value=~s/user/$username/g;
307 my $dbh = C4::Context->dbh;
309 # map '@' as "subfield" label for fixed fields
310 # to something that's allowed in a div id.
311 my $id_subfield = $subfield;
312 $id_subfield = "00" if $id_subfield eq "@";
314 my %subfield_data = (
315 tag => $tag,
316 subfield => $id_subfield,
317 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
318 tag_mandatory => $tagslib->{$tag}->{mandatory},
319 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
320 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
321 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
322 index => $index_tag,
323 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
324 value => $value,
325 maxlength => $tagslib->{$tag}->{$subfield}->{maxlength},
326 random => CreateKey(),
329 if(exists $mandatory_z3950->{$tag.$subfield}){
330 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
332 # Subfield is hidden depending of hidden and mandatory flag, and is always
333 # shown if it contains anything or if its field is mandatory.
334 my $tdef = $tagslib->{$tag};
335 $subfield_data{visibility} = "display:none;"
336 if $tdef->{$subfield}->{hidden} % 2 == 1 &&
337 $value eq '' &&
338 !$tdef->{$subfield}->{mandatory} &&
339 !$tdef->{mandatory};
340 # expand all subfields of 773 if there is a host item provided in the input
341 $subfield_data{visibility} ="" if ($tag eq 773 and $cgi->param('hostitemnumber'));
344 # it's an authorised field
345 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
346 $subfield_data{marc_value} =
347 build_authorized_values_list( $tag, $subfield, $value, $dbh,
348 $authorised_values_sth,$index_tag,$index_subfield );
350 # it's a subfield $9 linking to an authority record - see bug 2206
352 elsif ($subfield eq "9" and
353 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
354 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
355 $tagslib->{$tag}->{'a'}->{authtypecode} ne '') {
357 $subfield_data{marc_value} = {
358 type => 'text',
359 id => $subfield_data{id},
360 name => $subfield_data{id},
361 value => $value,
362 size => 5,
363 maxlength => $subfield_data{maxlength},
364 readonly => 1,
367 # it's a thesaurus / authority field
369 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
370 # when authorities auto-creation is allowed, do not set readonly
371 my $is_readonly = !C4::Context->preference("BiblioAddsAuthorities");
373 $subfield_data{marc_value} = {
374 type => 'text',
375 id => $subfield_data{id},
376 name => $subfield_data{id},
377 value => $value,
378 size => 67,
379 maxlength => $subfield_data{maxlength},
380 readonly => ($is_readonly) ? 1 : 0,
381 authtype => $tagslib->{$tag}->{$subfield}->{authtypecode},
384 # it's a plugin field
385 } elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
386 require Koha::FrameworkPlugin;
387 my $plugin = Koha::FrameworkPlugin->new( {
388 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
390 my $pars= { dbh => $dbh, record => $rec, tagslib => $tagslib,
391 id => $subfield_data{id}, tabloop => $tabloop };
392 $plugin->build( $pars );
393 if( !$plugin->errstr ) {
394 $subfield_data{marc_value} = {
395 type => 'text_complex',
396 id => $subfield_data{id},
397 name => $subfield_data{id},
398 value => $value,
399 size => 67,
400 maxlength => $subfield_data{maxlength},
401 javascript => $plugin->javascript,
402 noclick => $plugin->noclick,
404 } else {
405 warn $plugin->errstr;
406 # supply default input form
407 $subfield_data{marc_value} = {
408 type => 'text',
409 id => $subfield_data{id},
410 name => $subfield_data{id},
411 value => $value,
412 size => 67,
413 maxlength => $subfield_data{maxlength},
414 readonly => 0,
418 # it's an hidden field
419 } elsif ( $tag eq '' ) {
420 $subfield_data{marc_value} = {
421 type => 'hidden',
422 id => $subfield_data{id},
423 name => $subfield_data{id},
424 value => $value,
425 size => 67,
426 maxlength => $subfield_data{maxlength},
430 else {
431 # it's a standard field
432 if (
433 length($value) > 100
435 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
436 and $tag < 400 && $subfield eq 'a' )
437 or ( $tag >= 500
438 and $tag < 600
439 && C4::Context->preference("marcflavour") eq "MARC21" )
442 $subfield_data{marc_value} = {
443 type => 'textarea',
444 id => $subfield_data{id},
445 name => $subfield_data{id},
446 value => $value,
450 else {
451 $subfield_data{marc_value} = {
452 type => 'text',
453 id => $subfield_data{id},
454 name => $subfield_data{id},
455 value => $value,
456 size => 67,
457 maxlength => $subfield_data{maxlength},
458 readonly => 0,
463 $subfield_data{'index_subfield'} = $index_subfield;
464 return \%subfield_data;
468 =head2 format_indicator
470 Translate indicator value for output form - specifically, map
471 indicator = ' ' to ''. This is for the convenience of a cataloger
472 using a mouse to select an indicator input.
474 =cut
476 sub format_indicator {
477 my $ind_value = shift;
478 return '' if not defined $ind_value;
479 return '' if $ind_value eq ' ';
480 return $ind_value;
483 sub build_tabs {
484 my ( $template, $record, $dbh, $encoding,$input ) = @_;
486 # fill arrays
487 my @loop_data = ();
488 my $tag;
490 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
491 my $query = "SELECT authorised_value, lib
492 FROM authorised_values";
493 $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id )} if $branch_limit;
494 $query .= " WHERE category = ?";
495 $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
496 $query .= " GROUP BY lib ORDER BY lib, lib_opac";
497 my $authorised_values_sth = $dbh->prepare( $query );
499 # in this array, we will push all the 10 tabs
500 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
501 my @BIG_LOOP;
502 my %seen;
503 my @tab_data; # all tags to display
505 foreach my $used ( @$usedTagsLib ){
506 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
507 $seen{$used->{tagfield}}++;
510 my $max_num_tab=-1;
511 foreach(@$usedTagsLib){
512 if($_->{tab} > -1 && $_->{tab} >= $max_num_tab && $_->{tagfield} != '995'){ # FIXME : MARC21 ?
513 $max_num_tab = $_->{tab};
516 if($max_num_tab >= 9){
517 $max_num_tab = 9;
519 # loop through each tab 0 through 9
520 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
521 my @loop_data = (); #innerloop in the template.
522 my $i = 0;
523 foreach my $tag (@tab_data) {
524 $i++;
525 next if ! $tag;
526 my ($indicator1, $indicator2);
527 my $index_tag = CreateKey;
529 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
530 # if MARC::Record is empty => use tab as master loop.
531 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
532 my @fields;
533 if ( $tag ne '000' ) {
534 @fields = $record->field($tag);
536 else {
537 push @fields, $record->leader(); # if tag == 000
539 # loop through each field
540 foreach my $field (@fields) {
542 my @subfields_data;
543 if ( $tag < 10 ) {
544 my ( $value, $subfield );
545 if ( $tag ne '000' ) {
546 $value = $field->data();
547 $subfield = "@";
549 else {
550 $value = $field;
551 $subfield = '@';
553 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
554 next
555 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
556 'biblio.biblionumber' );
557 push(
558 @subfields_data,
559 &create_input(
560 $tag, $subfield, $value, $index_tag, $tabloop, $record,
561 $authorised_values_sth,$input
565 else {
566 my @subfields = $field->subfields();
567 foreach my $subfieldcount ( 0 .. $#subfields ) {
568 my $subfield = $subfields[$subfieldcount][0];
569 my $value = $subfields[$subfieldcount][1];
570 next if ( length $subfield != 1 );
571 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
572 push(
573 @subfields_data,
574 &create_input(
575 $tag, $subfield, $value, $index_tag, $tabloop,
576 $record, $authorised_values_sth,$input
582 # now, loop again to add parameter subfield that are not in the MARC::Record
583 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
585 next if ( length $subfield != 1 );
586 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
587 next if ( $tag < 10 );
588 next
589 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
590 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
591 and not ( $subfield eq "9" and
592 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
593 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
594 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
596 ; #check for visibility flag
597 # if subfield is $9 in a field whose $a is authority-controlled,
598 # always include in the form regardless of the hidden setting - bug 2206
599 next if ( defined( $field->subfield($subfield) ) );
600 push(
601 @subfields_data,
602 &create_input(
603 $tag, $subfield, '', $index_tag, $tabloop, $record,
604 $authorised_values_sth,$input
608 if ( $#subfields_data >= 0 ) {
609 # build the tag entry.
610 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
611 # have twice the same "name" value, and cgi->param() will return only one, making
612 # all subfields to be merged in a single field.
613 my %tag_data = (
614 tag => $tag,
615 index => $index_tag,
616 tag_lib => $tagslib->{$tag}->{lib},
617 repeatable => $tagslib->{$tag}->{repeatable},
618 mandatory => $tagslib->{$tag}->{mandatory},
619 subfield_loop => \@subfields_data,
620 fixedfield => $tag < 10?1:0,
621 random => CreateKey,
623 if ($tag >= 10){ # no indicator for 00x tags
624 $tag_data{indicator1} = format_indicator($field->indicator(1)),
625 $tag_data{indicator2} = format_indicator($field->indicator(2)),
627 push( @loop_data, \%tag_data );
629 } # foreach $field end
631 # if breeding is empty
633 else {
634 my @subfields_data;
635 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
636 next if ( length $subfield != 1 );
637 next
638 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
639 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
640 and not ( $subfield eq "9" and
641 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
642 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
643 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
645 ; #check for visibility flag
646 # if subfield is $9 in a field whose $a is authority-controlled,
647 # always include in the form regardless of the hidden setting - bug 2206
648 next
649 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
650 push(
651 @subfields_data,
652 &create_input(
653 $tag, $subfield, '', $index_tag, $tabloop, $record,
654 $authorised_values_sth,$input
658 if ( $#subfields_data >= 0 ) {
659 my %tag_data = (
660 tag => $tag,
661 index => $index_tag,
662 tag_lib => $tagslib->{$tag}->{lib},
663 repeatable => $tagslib->{$tag}->{repeatable},
664 mandatory => $tagslib->{$tag}->{mandatory},
665 indicator1 => $indicator1,
666 indicator2 => $indicator2,
667 subfield_loop => \@subfields_data,
668 tagfirstsubfield => $subfields_data[0],
669 fixedfield => $tag < 10?1:0,
672 push @loop_data, \%tag_data ;
676 if ( $#loop_data >= 0 ) {
677 push @BIG_LOOP, {
678 number => $tabloop,
679 innerloop => \@loop_data,
683 $authorised_values_sth->finish;
684 $template->param( BIG_LOOP => \@BIG_LOOP );
687 # ========================
688 # MAIN
689 #=========================
690 my $input = new CGI;
691 my $error = $input->param('error');
692 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
693 my $parentbiblio = $input->param('parentbiblionumber');
694 my $breedingid = $input->param('breedingid');
695 my $z3950 = $input->param('z3950');
696 my $op = $input->param('op');
697 my $mode = $input->param('mode');
698 my $frameworkcode = $input->param('frameworkcode');
699 my $redirect = $input->param('redirect');
700 my $searchid = $input->param('searchid');
701 my $dbh = C4::Context->dbh;
702 my $hostbiblionumber = $input->param('hostbiblionumber');
703 my $hostitemnumber = $input->param('hostitemnumber');
704 # fast cataloguing datas in transit
705 my $fa_circborrowernumber = $input->param('circborrowernumber');
706 my $fa_barcode = $input->param('barcode');
707 my $fa_branch = $input->param('branch');
708 my $fa_stickyduedate = $input->param('stickyduedate');
709 my $fa_duedatespec = $input->param('duedatespec');
711 my $userflags = 'edit_catalogue';
713 my $changed_framework = $input->param('changed_framework');
714 $frameworkcode = &GetFrameworkCode($biblionumber)
715 if ( $biblionumber and not($frameworkcode) and $op ne 'addbiblio' );
717 if ($frameworkcode eq 'FA'){
718 $userflags = 'fast_cataloging';
721 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
722 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
724 template_name => "cataloguing/addbiblio.tt",
725 query => $input,
726 type => "intranet",
727 authnotrequired => 0,
728 flagsrequired => { editcatalogue => $userflags },
732 if ($frameworkcode eq 'FA'){
733 # We need to grab and set some variables in the template for use on the additems screen
734 $template->param(
735 'circborrowernumber' => $fa_circborrowernumber,
736 'barcode' => $fa_barcode,
737 'branch' => $fa_branch,
738 'stickyduedate' => $fa_stickyduedate,
739 'duedatespec' => $fa_duedatespec,
741 } elsif ( C4::Context->preference('EnableAdvancedCatalogingEditor') && $input->cookie( 'catalogue_editor_' . $loggedinuser ) eq 'advanced' && !$breedingid ) {
742 # Only use the advanced editor for non-fast-cataloging.
743 # breedingid is not handled because those would only come off a Z39.50
744 # search initiated by the basic editor.
745 print $input->redirect( '/cgi-bin/koha/cataloguing/editor.pl' . ( $biblionumber ? ( '#catalog/' . $biblionumber ) : '' ) );
749 # Getting the list of all frameworks
750 # get framework list
751 my $frameworks = getframeworks;
752 my @frameworkcodeloop;
753 foreach my $thisframeworkcode ( keys %$frameworks ) {
754 my %row = (
755 value => $thisframeworkcode,
756 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
758 if ($frameworkcode eq $thisframeworkcode){
759 $row{'selected'} = 1;
761 push @frameworkcodeloop, \%row;
763 $template->param( frameworkcodeloop => \@frameworkcodeloop,
764 breedingid => $breedingid );
766 # ++ Global
767 $tagslib = &GetMarcStructure( 1, $frameworkcode );
768 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
769 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
770 # -- Global
772 my $record = -1;
773 my $encoding = "";
774 my (
775 $biblionumbertagfield,
776 $biblionumbertagsubfield,
777 $biblioitemnumtagfield,
778 $biblioitemnumtagsubfield,
779 $biblioitemnumber
782 if (($biblionumber) && !($breedingid)){
783 $record = GetMarcBiblio($biblionumber);
785 if ($breedingid) {
786 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
789 #populate hostfield if hostbiblionumber is available
790 if ($hostbiblionumber) {
791 my $marcflavour = C4::Context->preference("marcflavour");
792 $record = MARC::Record->new();
793 $record->leader('');
794 my $field =
795 PrepHostMarcField( $hostbiblionumber, $hostitemnumber, $marcflavour );
796 $record->append_fields($field);
799 # This is a child record
800 if ($parentbiblio) {
801 my $marcflavour = C4::Context->preference('marcflavour');
802 $record = MARC::Record->new();
803 SetMarcUnicodeFlag($record, $marcflavour);
804 my $hostfield = prepare_host_field($parentbiblio,$marcflavour);
805 if ($hostfield) {
806 $record->append_fields($hostfield);
810 $is_a_modif = 0;
812 if ($biblionumber) {
813 $is_a_modif = 1;
814 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
815 $template->param( title => $title );
817 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
818 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
819 &GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
820 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
821 &GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
823 # search biblioitems value
824 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
825 $sth->execute($biblionumber);
826 ($biblioitemnumber) = $sth->fetchrow;
829 #-------------------------------------------------------------------------------------
830 if ( $op eq "addbiblio" ) {
831 #-------------------------------------------------------------------------------------
832 $template->param(
833 biblionumberdata => $biblionumber,
835 # getting html input
836 my @params = $input->param();
837 $record = TransformHtmlToMarc( $input, 1 );
838 # check for a duplicate
839 my ( $duplicatebiblionumber, $duplicatetitle );
840 if ( !$is_a_modif ) {
841 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
843 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
844 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
845 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
846 my $oldbibitemnum;
847 if (C4::Context->preference("BiblioAddsAuthorities")){
848 BiblioAutoLink( $record, $frameworkcode );
850 if ( $is_a_modif ) {
851 ModBiblioframework( $biblionumber, $frameworkcode );
852 ModBiblio( $record, $biblionumber, $frameworkcode );
854 else {
855 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
857 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view" && $redirect ne "just_save")){
858 if ($frameworkcode eq 'FA'){
859 print $input->redirect(
860 '/cgi-bin/koha/cataloguing/additem.pl?'
861 .'biblionumber='.$biblionumber
862 .'&frameworkcode='.$frameworkcode
863 .'&circborrowernumber='.$fa_circborrowernumber
864 .'&branch='.$fa_branch
865 .'&barcode='.uri_escape_utf8($fa_barcode)
866 .'&stickyduedate='.$fa_stickyduedate
867 .'&duedatespec='.$fa_duedatespec
869 exit;
871 else {
872 print $input->redirect(
873 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid"
875 exit;
878 elsif(($is_a_modif || $redirect eq "view") && $redirect ne "just_save"){
879 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
880 my $views = { C4::Search::enabled_staff_search_views };
881 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
882 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
883 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
884 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
885 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
886 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
887 } else {
888 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
890 exit;
893 elsif ($redirect eq "just_save"){
894 my $tab = $input->param('current_tab');
895 print $input->redirect("/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber&framework=$frameworkcode&tab=$tab&searchid=$searchid");
897 else {
898 $template->param(
899 biblionumber => $biblionumber,
900 done =>1,
901 popup =>1
903 if ( $record ne '-1' ) {
904 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
905 $template->param( title => $title );
907 $template->param(
908 popup => $mode,
909 itemtype => $frameworkcode,
911 output_html_with_http_headers $input, $cookie, $template->output;
912 exit;
914 } else {
915 # it may be a duplicate, warn the user and do nothing
916 build_tabs ($template, $record, $dbh,$encoding,$input);
917 $template->param(
918 biblionumber => $biblionumber,
919 biblioitemnumber => $biblioitemnumber,
920 duplicatebiblionumber => $duplicatebiblionumber,
921 duplicatebibid => $duplicatebiblionumber,
922 duplicatetitle => $duplicatetitle,
926 elsif ( $op eq "delete" ) {
928 my $error = &DelBiblio($biblionumber);
929 if ($error) {
930 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
931 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
932 exit;
935 print $input->redirect('/cgi-bin/koha/catalogue/search.pl');
936 exit;
938 } else {
939 #----------------------------------------------------------------------------
940 # If we're in a duplication case, we have to set to "" the biblionumber
941 # as we'll save the biblio as a new one.
942 $template->param(
943 biblionumberdata => $biblionumber,
944 op => $op,
946 if ( $op eq "duplicate" ) {
947 $biblionumber = "";
950 if($changed_framework eq "changed"){
951 $record = TransformHtmlToMarc( $input, 1 );
953 elsif( $record ne -1 ) {
954 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
955 eval {
956 my $uxml = $record->as_xml;
957 MARC::Record::default_record_format("UNIMARC")
958 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
959 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
960 $record = $urecord;
963 build_tabs( $template, $record, $dbh, $encoding,$input );
964 $template->param(
965 biblionumber => $biblionumber,
966 biblionumbertagfield => $biblionumbertagfield,
967 biblionumbertagsubfield => $biblionumbertagsubfield,
968 biblioitemnumtagfield => $biblioitemnumtagfield,
969 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
970 biblioitemnumber => $biblioitemnumber,
971 hostbiblionumber => $hostbiblionumber,
972 hostitemnumber => $hostitemnumber
976 if ( $record ne '-1' ) {
977 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
978 $template->param( title => $title );
980 $template->param(
981 popup => $mode,
982 frameworkcode => $frameworkcode,
983 itemtype => $frameworkcode,
984 borrowernumber => $loggedinuser,
985 tab => $input->param('tab')
987 $template->{'VARS'}->{'searchid'} = $searchid;
989 output_html_with_http_headers $input, $cookie, $template->output;