Bug 6679 - [SIGNED-OFF] fix 3 perlcritic violations in C4/Message.pm
[koha.git] / cataloguing / addbiblio.pl
blob3db1a65526ce519a950f5ba98b5ded378a990fcb
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 under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 use strict;
23 #use warnings; FIXME - Bug 2505
24 use CGI;
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;
43 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
44 MARC::File::XML->default_record_format('UNIMARC');
47 our($tagslib,$authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
49 =head1 FUNCTIONS
51 =head2 MARCfindbreeding
53 $record = MARCfindbreeding($breedingid);
55 Look up the import record repository for the record with
56 record with id $breedingid. If found, returns the decoded
57 MARC::Record; otherwise, -1 is returned (FIXME).
58 Returns as second parameter the character encoding.
60 =cut
62 sub MARCfindbreeding {
63 my ( $id ) = @_;
64 my ($marc, $encoding) = GetImportRecordMarc($id);
65 # remove the - in isbn, koha store isbn without any -
66 if ($marc) {
67 my $record = MARC::Record->new_from_usmarc($marc);
68 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
69 if ( $record->field($isbnfield) ) {
70 foreach my $field ( $record->field($isbnfield) ) {
71 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
72 my $newisbn = $field->subfield($isbnsubfield);
73 $newisbn =~ s/-//g;
74 $field->update( $isbnsubfield => $newisbn );
78 # fix the unimarc 100 coded field (with unicode information)
79 if (C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield(100,'a')) {
80 my $f100a=$record->subfield(100,'a');
81 my $f100 = $record->field(100);
82 my $f100temp = $f100->as_string;
83 $record->delete_field($f100);
84 if ( length($f100temp) > 28 ) {
85 substr( $f100temp, 26, 2, "50" );
86 $f100->update( 'a' => $f100temp );
87 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
88 $record->insert_fields_ordered($f100);
92 if ( !defined(ref($record)) ) {
93 return -1;
95 else {
96 # normalize author : probably UNIMARC specific...
97 if ( C4::Context->preference("z3950NormalizeAuthor")
98 and C4::Context->preference("z3950AuthorAuthFields") )
100 my ( $tag, $subfield ) = GetMarcFromKohaField("biblio.author", '');
102 # my $summary = C4::Context->preference("z3950authortemplate");
103 my $auth_fields =
104 C4::Context->preference("z3950AuthorAuthFields");
105 my @auth_fields = split /,/, $auth_fields;
106 my $field;
108 if ( $record->field($tag) ) {
109 foreach my $tmpfield ( $record->field($tag)->subfields ) {
111 # foreach my $subfieldcode ($tmpfield->subfields){
112 my $subfieldcode = shift @$tmpfield;
113 my $subfieldvalue = shift @$tmpfield;
114 if ($field) {
115 $field->add_subfields(
116 "$subfieldcode" => $subfieldvalue )
117 if ( $subfieldcode ne $subfield );
119 else {
120 $field =
121 MARC::Field->new( $tag, "", "",
122 $subfieldcode => $subfieldvalue )
123 if ( $subfieldcode ne $subfield );
127 $record->delete_field( $record->field($tag) );
128 foreach my $fieldtag (@auth_fields) {
129 next unless ( $record->field($fieldtag) );
130 my $lastname = $record->field($fieldtag)->subfield('a');
131 my $firstname = $record->field($fieldtag)->subfield('b');
132 my $title = $record->field($fieldtag)->subfield('c');
133 my $number = $record->field($fieldtag)->subfield('d');
134 if ($title) {
136 # $field->add_subfields("$subfield"=>"[ ".ucfirst($title).ucfirst($firstname)." ".$number." ]");
137 $field->add_subfields(
138 "$subfield" => ucfirst($title) . " "
139 . ucfirst($firstname) . " "
140 . $number );
142 else {
144 # $field->add_subfields("$subfield"=>"[ ".ucfirst($firstname).", ".ucfirst($lastname)." ]");
145 $field->add_subfields(
146 "$subfield" => ucfirst($firstname) . ", "
147 . ucfirst($lastname) );
150 $record->insert_fields_ordered($field);
152 return $record, $encoding;
155 return -1;
158 =head2 build_authorized_values_list
160 =cut
162 sub build_authorized_values_list {
163 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
165 my @authorised_values;
166 my %authorised_lib;
168 # builds list, depending on authorised value...
170 #---- branch
171 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
172 #Use GetBranches($onlymine)
173 my $onlymine=C4::Context->preference('IndependantBranches') &&
174 C4::Context->userenv &&
175 C4::Context->userenv->{flags} % 2 == 0 &&
176 C4::Context->userenv->{branch};
177 my $branches = GetBranches($onlymine);
178 my @branchloop;
179 foreach my $thisbranch ( sort keys %$branches ) {
180 push @authorised_values, $thisbranch;
181 $authorised_lib{$thisbranch} = $branches->{$thisbranch}->{'branchname'};
184 #----- itemtypes
186 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
187 my $sth =
188 $dbh->prepare(
189 "select itemtype,description from itemtypes order by description");
190 $sth->execute;
191 push @authorised_values, ""
192 unless ( $tagslib->{$tag}->{$subfield}->{defaultvalue} and $tagslib->{$tag}->{$subfield}->{mandatory} );
194 my $itemtype;
196 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
197 push @authorised_values, $itemtype;
198 $authorised_lib{$itemtype} = $description;
200 $value = $itemtype unless ($value);
202 #---- class_sources
204 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
205 push @authorised_values, ""
206 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
208 my $class_sources = GetClassSources();
210 my $default_source = C4::Context->preference("DefaultClassificationSource");
212 foreach my $class_source (sort keys %$class_sources) {
213 next unless $class_sources->{$class_source}->{'used'} or
214 ($value and $class_source eq $value) or
215 ($class_source eq $default_source);
216 push @authorised_values, $class_source;
217 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
219 $value = $default_source unless $value;
221 else {
222 $authorised_values_sth->execute(
223 $tagslib->{$tag}->{$subfield}->{authorised_value} );
225 push @authorised_values, ""
226 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
228 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
229 push @authorised_values, $value;
230 $authorised_lib{$value} = $lib;
233 return CGI::scrolling_list(
234 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
235 -values => \@authorised_values,
236 -default => $value,
237 -labels => \%authorised_lib,
238 -override => 1,
239 -size => 1,
240 -multiple => 0,
241 -tabindex => 1,
242 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
243 -class => "input_marceditor",
247 =head2 CreateKey
249 Create a random value to set it into the input name
251 =cut
253 sub CreateKey {
254 return int(rand(1000000));
257 =head2 GetMandatoryFieldZ3950
259 This function return an hashref which containts all mandatory field
260 to search with z3950 server.
262 =cut
264 sub GetMandatoryFieldZ3950 {
265 my $frameworkcode = shift;
266 my @isbn = GetMarcFromKohaField('biblioitems.isbn',$frameworkcode);
267 my @title = GetMarcFromKohaField('biblio.title',$frameworkcode);
268 my @author = GetMarcFromKohaField('biblio.author',$frameworkcode);
269 my @issn = GetMarcFromKohaField('biblioitems.issn',$frameworkcode);
270 my @lccn = GetMarcFromKohaField('biblioitems.lccn',$frameworkcode);
272 return {
273 $isbn[0].$isbn[1] => 'isbn',
274 $title[0].$title[1] => 'title',
275 $author[0].$author[1] => 'author',
276 $issn[0].$issn[1] => 'issn',
277 $lccn[0].$lccn[1] => 'lccn',
281 =head2 create_input
283 builds the <input ...> entry for a subfield.
285 =cut
287 sub create_input {
288 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
290 my $index_subfield = CreateKey(); # create a specifique key for each subfield
292 $value =~ s/"/&quot;/g;
294 # if there is no value provided but a default value in parameters, get it
295 if ( $value eq '' ) {
296 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
298 # get today date & replace YYYY, MM, DD if provided in the default value
299 my ( $year, $month, $day ) = Today();
300 $month = sprintf( "%02d", $month );
301 $day = sprintf( "%02d", $day );
302 $value =~ s/YYYY/$year/g;
303 $value =~ s/MM/$month/g;
304 $value =~ s/DD/$day/g;
305 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
306 $value=~s/user/$username/g;
309 my $dbh = C4::Context->dbh;
311 # map '@' as "subfield" label for fixed fields
312 # to something that's allowed in a div id.
313 my $id_subfield = $subfield;
314 $id_subfield = "00" if $id_subfield eq "@";
316 my %subfield_data = (
317 tag => $tag,
318 subfield => $id_subfield,
319 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
320 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
321 tag_mandatory => $tagslib->{$tag}->{mandatory},
322 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
323 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
324 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
325 index => $index_tag,
326 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
327 value => $value,
328 maxlength => $tagslib->{$tag}->{$subfield}->{maxlength},
329 random => CreateKey(),
332 if(exists $mandatory_z3950->{$tag.$subfield}){
333 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
335 # Subfield is hidden depending of hidden and mandatory flag, and is always
336 # shown if it contains anything or if its field is mandatory.
337 my $tdef = $tagslib->{$tag};
338 $subfield_data{visibility} = "display:none;"
339 if $tdef->{$subfield}->{hidden} % 2 == 1 &&
340 $value eq '' &&
341 !$tdef->{$subfield}->{mandatory} &&
342 !$tdef->{mandatory};
343 # expand all subfields of 773 if there is a host item provided in the input
344 $subfield_data{visibility} ="" if ($tag eq 773 and $cgi->param('hostitemnumber'));
347 # it's an authorised field
348 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
349 $subfield_data{marc_value} =
350 build_authorized_values_list( $tag, $subfield, $value, $dbh,
351 $authorised_values_sth,$index_tag,$index_subfield );
353 # it's a subfield $9 linking to an authority record - see bug 2206
355 elsif ($subfield eq "9" and
356 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
357 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
358 $tagslib->{$tag}->{'a'}->{authtypecode} ne '') {
360 $subfield_data{marc_value} =
361 "<input type=\"text\"
362 id=\"".$subfield_data{id}."\"
363 name=\"".$subfield_data{id}."\"
364 value=\"$value\"
365 class=\"input_marceditor readonly\"
366 tabindex=\"1\"
367 size=\"5\"
368 maxlength=\"".$subfield_data{maxlength}."\"
369 readonly=\"readonly\"
370 \/>";
372 # it's a thesaurus / authority field
374 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
375 if (C4::Context->preference("BiblioAddsAuthorities")) {
376 $subfield_data{marc_value} =
377 "<input type=\"text\"
378 id=\"".$subfield_data{id}."\"
379 name=\"".$subfield_data{id}."\"
380 value=\"$value\"
381 class=\"input_marceditor readonly\"
382 tabindex=\"1\"
383 size=\"67\"
384 maxlength=\"".$subfield_data{maxlength}."\"
386 <span class=\"subfield_controls\"><a href=\"#\" class=\"buttonDot\"
387 onclick=\"openAuth(this.parentNode.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode}."'); return false;\" tabindex=\"1\" title=\"Tag Editor\"><img src=\"/intranet-tmpl/prog/img/edit-tag.png\" alt=\"Tag Editor\" /></a></span>
389 } else {
390 $subfield_data{marc_value} =
391 "<input type=\"text\"
392 id=\"".$subfield_data{id}."\"
393 name=\"".$subfield_data{id}."\"
394 value=\"$value\"
395 class=\"input_marceditor readonly\"
396 tabindex=\"1\"
397 size=\"67\"
398 maxlength=\"".$subfield_data{maxlength}."\"
399 readonly=\"readonly\"
400 \/><span class=\"subfield_controls\"><a href=\"#\" class=\"buttonDot\"
401 onclick=\"openAuth(this.parentNode.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode}."'); return false;\" tabindex=\"1\" title=\"Tag Editor\"><img src=\"/intranet-tmpl/prog/img/edit-tag.png\" alt=\"Tag Editor\" /></a></span>
404 # it's a plugin field
406 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
408 # opening plugin. Just check whether we are on a developer computer on a production one
409 # (the cgidir differs)
410 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
411 unless ( opendir( DIR, "$cgidir" ) ) {
412 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
413 closedir( DIR );
415 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
416 if (do $plugin) {
417 my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
418 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
420 $subfield_data{marc_value} =
421 "<input tabindex=\"1\"
422 type=\"text\"
423 id=\"".$subfield_data{id}."\"
424 name=\"".$subfield_data{id}."\"
425 value=\"$value\"
426 class=\"input_marceditor\"
427 onfocus=\"Focus$function_name($index_tag)\"
428 size=\"67\"
429 maxlength=\"".$subfield_data{maxlength}."\"
430 onblur=\"Blur$function_name($index_tag); \" \/>
431 <span class=\"subfield_controls\"><a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" tabindex=\"1\" title=\"Tag Editor\"><img src=\"/intranet-tmpl/prog/img/edit-tag.png\" alt=\"Tag Editor\" /></a></span>
432 $javascript";
433 } else {
434 warn "Plugin Failed: $plugin";
435 # supply default input form
436 $subfield_data{marc_value} =
437 "<input type=\"text\"
438 id=\"".$subfield_data{id}."\"
439 name=\"".$subfield_data{id}."\"
440 value=\"$value\"
441 tabindex=\"1\"
442 size=\"67\"
443 maxlength=\"".$subfield_data{maxlength}."\"
444 class=\"input_marceditor\"
448 # it's an hidden field
450 elsif ( $tag eq '' ) {
451 $subfield_data{marc_value} =
452 "<input tabindex=\"1\"
453 type=\"hidden\"
454 id=\"".$subfield_data{id}."\"
455 name=\"".$subfield_data{id}."\"
456 size=\"67\"
457 maxlength=\"".$subfield_data{maxlength}."\"
458 value=\"$value\" \/>
461 else {
462 # it's a standard field
463 if (
464 length($value) > 100
466 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
467 and $tag < 400 && $subfield eq 'a' )
468 or ( $tag >= 500
469 and $tag < 600
470 && C4::Context->preference("marcflavour") eq "MARC21" )
473 $subfield_data{marc_value} =
474 "<textarea cols=\"70\"
475 rows=\"4\"
476 id=\"".$subfield_data{id}."\"
477 name=\"".$subfield_data{id}."\"
478 class=\"input_marceditor\"
479 tabindex=\"1\"
480 >$value</textarea>
483 else {
484 $subfield_data{marc_value} =
485 "<input type=\"text\"
486 id=\"".$subfield_data{id}."\"
487 name=\"".$subfield_data{id}."\"
488 value=\"$value\"
489 tabindex=\"1\"
490 size=\"67\"
491 maxlength=\"".$subfield_data{maxlength}."\"
492 class=\"input_marceditor\"
497 $subfield_data{'index_subfield'} = $index_subfield;
498 return \%subfield_data;
502 =head2 format_indicator
504 Translate indicator value for output form - specifically, map
505 indicator = ' ' to ''. This is for the convenience of a cataloger
506 using a mouse to select an indicator input.
508 =cut
510 sub format_indicator {
511 my $ind_value = shift;
512 return '' if not defined $ind_value;
513 return '' if $ind_value eq ' ';
514 return $ind_value;
517 sub build_tabs {
518 my ( $template, $record, $dbh, $encoding,$input ) = @_;
520 # fill arrays
521 my @loop_data = ();
522 my $tag;
524 my $authorised_values_sth = $dbh->prepare(
525 "select authorised_value,lib
526 from authorised_values
527 where category=? order by lib"
530 # in this array, we will push all the 10 tabs
531 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
532 my @BIG_LOOP;
533 my %seen;
534 my @tab_data; # all tags to display
536 foreach my $used ( @$usedTagsLib ){
537 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
538 $seen{$used->{tagfield}}++;
541 my $max_num_tab=-1;
542 foreach(@$usedTagsLib){
543 if($_->{tab} > -1 && $_->{tab} >= $max_num_tab && $_->{tagfield} != '995'){ # FIXME : MARC21 ?
544 $max_num_tab = $_->{tab};
547 if($max_num_tab >= 9){
548 $max_num_tab = 9;
550 # loop through each tab 0 through 9
551 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
552 my @loop_data = (); #innerloop in the template.
553 my $i = 0;
554 foreach my $tag (@tab_data) {
555 $i++;
556 next if ! $tag;
557 my ($indicator1, $indicator2);
558 my $index_tag = CreateKey;
560 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
561 # if MARC::Record is empty => use tab as master loop.
562 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
563 my @fields;
564 if ( $tag ne '000' ) {
565 @fields = $record->field($tag);
567 else {
568 push @fields, $record->leader(); # if tag == 000
570 # loop through each field
571 foreach my $field (@fields) {
573 my @subfields_data;
574 if ( $tag < 10 ) {
575 my ( $value, $subfield );
576 if ( $tag ne '000' ) {
577 $value = $field->data();
578 $subfield = "@";
580 else {
581 $value = $field;
582 $subfield = '@';
584 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
585 next
586 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
587 'biblio.biblionumber' );
588 push(
589 @subfields_data,
590 &create_input(
591 $tag, $subfield, $value, $index_tag, $tabloop, $record,
592 $authorised_values_sth,$input
596 else {
597 my @subfields = $field->subfields();
598 foreach my $subfieldcount ( 0 .. $#subfields ) {
599 my $subfield = $subfields[$subfieldcount][0];
600 my $value = $subfields[$subfieldcount][1];
601 next if ( length $subfield != 1 );
602 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
603 push(
604 @subfields_data,
605 &create_input(
606 $tag, $subfield, $value, $index_tag, $tabloop,
607 $record, $authorised_values_sth,$input
613 # now, loop again to add parameter subfield that are not in the MARC::Record
614 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
616 next if ( length $subfield != 1 );
617 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
618 next if ( $tag < 10 );
619 next
620 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
621 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
622 and not ( $subfield eq "9" and
623 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
624 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
625 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
627 ; #check for visibility flag
628 # if subfield is $9 in a field whose $a is authority-controlled,
629 # always include in the form regardless of the hidden setting - bug 2206
630 next if ( defined( $field->subfield($subfield) ) );
631 push(
632 @subfields_data,
633 &create_input(
634 $tag, $subfield, '', $index_tag, $tabloop, $record,
635 $authorised_values_sth,$input
639 if ( $#subfields_data >= 0 ) {
640 # build the tag entry.
641 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
642 # have twice the same "name" value, and cgi->param() will return only one, making
643 # all subfields to be merged in a single field.
644 my %tag_data = (
645 tag => $tag,
646 index => $index_tag,
647 tag_lib => $tagslib->{$tag}->{lib},
648 repeatable => $tagslib->{$tag}->{repeatable},
649 mandatory => $tagslib->{$tag}->{mandatory},
650 subfield_loop => \@subfields_data,
651 fixedfield => $tag < 10?1:0,
652 random => CreateKey,
654 if ($tag >= 10){ # no indicator for 00x tags
655 $tag_data{indicator1} = format_indicator($field->indicator(1)),
656 $tag_data{indicator2} = format_indicator($field->indicator(2)),
658 push( @loop_data, \%tag_data );
660 } # foreach $field end
662 # if breeding is empty
664 else {
665 my @subfields_data;
666 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
667 next if ( length $subfield != 1 );
668 next
669 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
670 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
671 and not ( $subfield eq "9" and
672 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
673 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
674 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
676 ; #check for visibility flag
677 # if subfield is $9 in a field whose $a is authority-controlled,
678 # always include in the form regardless of the hidden setting - bug 2206
679 next
680 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
681 push(
682 @subfields_data,
683 &create_input(
684 $tag, $subfield, '', $index_tag, $tabloop, $record,
685 $authorised_values_sth,$input
689 if ( $#subfields_data >= 0 ) {
690 my %tag_data = (
691 tag => $tag,
692 index => $index_tag,
693 tag_lib => $tagslib->{$tag}->{lib},
694 repeatable => $tagslib->{$tag}->{repeatable},
695 mandatory => $tagslib->{$tag}->{mandatory},
696 indicator1 => $indicator1,
697 indicator2 => $indicator2,
698 subfield_loop => \@subfields_data,
699 tagfirstsubfield => $subfields_data[0],
700 fixedfield => $tag < 10?1:0,
703 push @loop_data, \%tag_data ;
707 if ( $#loop_data >= 0 ) {
708 push @BIG_LOOP, {
709 number => $tabloop,
710 innerloop => \@loop_data,
714 $template->param( BIG_LOOP => \@BIG_LOOP );
717 # ========================
718 # MAIN
719 #=========================
720 my $input = new CGI;
721 my $error = $input->param('error');
722 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
723 my $parentbiblio = $input->param('parentbiblionumber');
724 my $breedingid = $input->param('breedingid');
725 my $z3950 = $input->param('z3950');
726 my $op = $input->param('op');
727 my $mode = $input->param('mode');
728 my $frameworkcode = $input->param('frameworkcode');
729 my $redirect = $input->param('redirect');
730 my $dbh = C4::Context->dbh;
731 my $hostbiblionumber = $input->param('hostbiblionumber');
732 my $hostitemnumber = $input->param('hostitemnumber');
735 my $userflags = 'edit_catalogue';
736 if ($frameworkcode eq 'FA'){
737 $userflags = 'fast_cataloging';
740 $frameworkcode = &GetFrameworkCode($biblionumber)
741 if ( $biblionumber and not($frameworkcode) and $op ne 'addbiblio' );
743 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
744 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
746 template_name => "cataloguing/addbiblio.tt",
747 query => $input,
748 type => "intranet",
749 authnotrequired => 0,
750 flagsrequired => { editcatalogue => $userflags },
754 if ($frameworkcode eq 'FA'){
755 # We need to grab and set some variables in the template for use on the additems screen
756 $template->{VARS}->{'circborrowernumber'} = $input->param('borrowernumber');
757 $template->{VARS}->{'barcode'} = $input->param('barcode');
758 $template->{VARS}->{'branch'} = $input->param('branch');
759 $template->{VARS}->{'stickyduedate'} = $input->param('stickyduedate');
760 $template->{VARS}->{'duedatespec'} = $input->param('duedatespec');
763 # Getting the list of all frameworks
764 # get framework list
765 my $frameworks = getframeworks;
766 my @frameworkcodeloop;
767 foreach my $thisframeworkcode ( keys %$frameworks ) {
768 my %row = (
769 value => $thisframeworkcode,
770 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
772 if ($frameworkcode eq $thisframeworkcode){
773 $row{'selected'} = 1;
775 push @frameworkcodeloop, \%row;
777 $template->param( frameworkcodeloop => \@frameworkcodeloop,
778 breedingid => $breedingid );
780 # ++ Global
781 $tagslib = &GetMarcStructure( 1, $frameworkcode );
782 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
783 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
784 # -- Global
786 my $record = -1;
787 my $encoding = "";
788 my (
789 $biblionumbertagfield,
790 $biblionumbertagsubfield,
791 $biblioitemnumtagfield,
792 $biblioitemnumtagsubfield,
793 $bibitem,
794 $biblioitemnumber
797 if (($biblionumber) && !($breedingid)){
798 $record = GetMarcBiblio($biblionumber);
800 if ($breedingid) {
801 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
804 #populate hostfield if hostbiblionumber is available
805 if ($hostbiblionumber) {
806 my $marcflavour = C4::Context->preference("marcflavour");
807 $record = MARC::Record->new();
808 $record->leader('');
809 my $field =
810 PrepHostMarcField( $hostbiblionumber, $hostitemnumber, $marcflavour );
811 $record->append_fields($field);
814 # This is a child record
815 if ($parentbiblio) {
816 my $marcflavour = C4::Context->preference('marcflavour');
817 $record = MARC::Record->new();
818 my $hostfield = prepare_host_field($parentbiblio,$marcflavour);
819 if ($hostfield) {
820 $record->append_fields($hostfield);
824 $is_a_modif = 0;
826 if ($biblionumber) {
827 $is_a_modif = 1;
828 $template->param( title => $record->title(), );
830 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
831 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
832 &GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
833 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
834 &GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
836 # search biblioitems value
837 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
838 $sth->execute($biblionumber);
839 ($biblioitemnumber) = $sth->fetchrow;
842 #-------------------------------------------------------------------------------------
843 if ( $op eq "addbiblio" ) {
844 #-------------------------------------------------------------------------------------
845 $template->param(
846 biblionumberdata => $biblionumber,
848 # getting html input
849 my @params = $input->param();
850 $record = TransformHtmlToMarc( $input );
851 # check for a duplicate
852 my ( $duplicatebiblionumber, $duplicatetitle );
853 if ( !$is_a_modif ) {
854 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
856 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
857 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
858 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
859 my $oldbibnum;
860 my $oldbibitemnum;
861 if (C4::Context->preference("BiblioAddsAuthorities")){
862 my ($countlinked,$countcreated)=BiblioAutoLink($record,$frameworkcode);
864 if ( $is_a_modif ) {
865 ModBiblioframework( $biblionumber, $frameworkcode );
866 ModBiblio( $record, $biblionumber, $frameworkcode );
868 else {
869 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
871 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view")){
872 if ($frameworkcode eq 'FA'){
873 my $borrowernumber = $input->param('circborrowernumber');
874 my $barcode = $input->param('barcode');
875 my $branch = $input->param('branch');
876 my $stickyduedate = $input->param('stickyduedate');
877 my $duedatespec = $input->param('duedatespec');
878 print $input->redirect(
879 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&borrowernumber=$borrowernumber&branch=$branch&barcode=$barcode&stickyduedate=$stickyduedate&duedatespec=$duedatespec"
881 exit;
883 else {
884 print $input->redirect(
885 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode"
887 exit;
890 elsif($is_a_modif || $redirect eq "view"){
891 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
892 my $views = { C4::Search::enabled_staff_search_views };
893 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
894 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber");
895 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
896 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
897 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
898 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber");
899 } else {
900 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber");
902 exit;
905 else {
906 $template->param(
907 biblionumber => $biblionumber,
908 done =>1,
909 popup =>1
911 $template->param( title => $record->subfield('200',"a") ) if ($record ne "-1" && C4::Context->preference('marcflavour') =~/unimarc/i);
912 $template->param( title => $record->title() ) if ($record ne "-1" && C4::Context->preference('marcflavour') eq "usmarc");
913 $template->param(
914 popup => $mode,
915 itemtype => $frameworkcode,
917 output_html_with_http_headers $input, $cookie, $template->output;
918 exit;
920 } else {
921 # it may be a duplicate, warn the user and do nothing
922 build_tabs ($template, $record, $dbh,$encoding,$input);
923 $template->param(
924 biblionumber => $biblionumber,
925 biblioitemnumber => $biblioitemnumber,
926 duplicatebiblionumber => $duplicatebiblionumber,
927 duplicatebibid => $duplicatebiblionumber,
928 duplicatetitle => $duplicatetitle,
932 elsif ( $op eq "delete" ) {
934 my $error = &DelBiblio($biblionumber);
935 if ($error) {
936 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
937 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
938 exit;
941 print $input->redirect('/cgi-bin/koha/catalogue/search.pl');
942 exit;
944 } else {
945 #----------------------------------------------------------------------------
946 # If we're in a duplication case, we have to set to "" the biblionumber
947 # as we'll save the biblio as a new one.
948 $template->param(
949 biblionumberdata => $biblionumber,
950 op => $op,
952 if ( $op eq "duplicate" ) {
953 $biblionumber = "";
956 if ( $record ne -1 ) {
957 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
958 eval {
959 my $uxml = $record->as_xml;
960 MARC::Record::default_record_format("UNIMARC")
961 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
962 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
963 $record = $urecord;
966 build_tabs( $template, $record, $dbh, $encoding,$input );
967 $template->param(
968 biblionumber => $biblionumber,
969 biblionumbertagfield => $biblionumbertagfield,
970 biblionumbertagsubfield => $biblionumbertagsubfield,
971 biblioitemnumtagfield => $biblioitemnumtagfield,
972 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
973 biblioitemnumber => $biblioitemnumber,
974 hostbiblionumber => $hostbiblionumber,
975 hostitemnumber => $hostitemnumber
979 $template->param( title => $record->title() ) if ( $record ne "-1" );
980 $template->param(
981 popup => $mode,
982 frameworkcode => $frameworkcode,
983 itemtype => $frameworkcode,
984 borrowernumber => $loggedinuser,
987 output_html_with_http_headers $input, $cookie, $template->output;