Bug 8566: [SIGNED-OFF] Enable koha-foreach to insert the instancename into commands
[koha.git] / cataloguing / addbiblio.pl
blob118bfc2db1abe60ceaa32a9d0322e3745886124a
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;
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 CGI::scrolling_list(
243 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
244 -values => \@authorised_values,
245 -default => $value,
246 -labels => \%authorised_lib,
247 -override => 1,
248 -size => 1,
249 -multiple => 0,
250 -tabindex => 1,
251 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
252 -class => "input_marceditor",
256 =head2 CreateKey
258 Create a random value to set it into the input name
260 =cut
262 sub CreateKey {
263 return int(rand(1000000));
266 =head2 GetMandatoryFieldZ3950
268 This function return an hashref which containts all mandatory field
269 to search with z3950 server.
271 =cut
273 sub GetMandatoryFieldZ3950 {
274 my $frameworkcode = shift;
275 my @isbn = GetMarcFromKohaField('biblioitems.isbn',$frameworkcode);
276 my @title = GetMarcFromKohaField('biblio.title',$frameworkcode);
277 my @author = GetMarcFromKohaField('biblio.author',$frameworkcode);
278 my @issn = GetMarcFromKohaField('biblioitems.issn',$frameworkcode);
279 my @lccn = GetMarcFromKohaField('biblioitems.lccn',$frameworkcode);
281 return {
282 $isbn[0].$isbn[1] => 'isbn',
283 $title[0].$title[1] => 'title',
284 $author[0].$author[1] => 'author',
285 $issn[0].$issn[1] => 'issn',
286 $lccn[0].$lccn[1] => 'lccn',
290 =head2 create_input
292 builds the <input ...> entry for a subfield.
294 =cut
296 sub create_input {
297 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
299 my $index_subfield = CreateKey(); # create a specifique key for each subfield
301 $value =~ s/"/&quot;/g;
303 # if there is no value provided but a default value in parameters, get it
304 if ( $value eq '' ) {
305 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
307 # get today date & replace YYYY, MM, DD if provided in the default value
308 my ( $year, $month, $day ) = Today();
309 $month = sprintf( "%02d", $month );
310 $day = sprintf( "%02d", $day );
311 $value =~ s/YYYY/$year/g;
312 $value =~ s/MM/$month/g;
313 $value =~ s/DD/$day/g;
314 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
315 $value=~s/user/$username/g;
318 my $dbh = C4::Context->dbh;
320 # map '@' as "subfield" label for fixed fields
321 # to something that's allowed in a div id.
322 my $id_subfield = $subfield;
323 $id_subfield = "00" if $id_subfield eq "@";
325 my %subfield_data = (
326 tag => $tag,
327 subfield => $id_subfield,
328 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
329 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
330 tag_mandatory => $tagslib->{$tag}->{mandatory},
331 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
332 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
333 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
334 index => $index_tag,
335 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
336 value => $value,
337 maxlength => $tagslib->{$tag}->{$subfield}->{maxlength},
338 random => CreateKey(),
341 if(exists $mandatory_z3950->{$tag.$subfield}){
342 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
344 # Subfield is hidden depending of hidden and mandatory flag, and is always
345 # shown if it contains anything or if its field is mandatory.
346 my $tdef = $tagslib->{$tag};
347 $subfield_data{visibility} = "display:none;"
348 if $tdef->{$subfield}->{hidden} % 2 == 1 &&
349 $value eq '' &&
350 !$tdef->{$subfield}->{mandatory} &&
351 !$tdef->{mandatory};
352 # expand all subfields of 773 if there is a host item provided in the input
353 $subfield_data{visibility} ="" if ($tag eq 773 and $cgi->param('hostitemnumber'));
356 # it's an authorised field
357 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
358 $subfield_data{marc_value} =
359 build_authorized_values_list( $tag, $subfield, $value, $dbh,
360 $authorised_values_sth,$index_tag,$index_subfield );
362 # it's a subfield $9 linking to an authority record - see bug 2206
364 elsif ($subfield eq "9" and
365 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
366 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
367 $tagslib->{$tag}->{'a'}->{authtypecode} ne '') {
369 $subfield_data{marc_value} =
370 "<input type=\"text\"
371 id=\"".$subfield_data{id}."\"
372 name=\"".$subfield_data{id}."\"
373 value=\"$value\"
374 class=\"input_marceditor readonly\"
375 tabindex=\"1\"
376 size=\"5\"
377 maxlength=\"".$subfield_data{maxlength}."\"
378 readonly=\"readonly\"
379 \/>";
381 # it's a thesaurus / authority field
383 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
384 # when authorities auto-creation is allowed, do not set readonly
385 my $is_readonly = !C4::Context->preference("BiblioAddsAuthorities");
386 $subfield_data{marc_value} =
387 "<input type=\"text\"
388 id=\"".$subfield_data{id}."\"
389 name=\"".$subfield_data{id}."\"
390 value=\"$value\"
391 class=\"input_marceditor readonly\"
392 tabindex=\"1\"
393 size=\"67\"
394 maxlength=\"".$subfield_data{maxlength}."\"".
395 ($is_readonly ? "readonly=\"readonly\"" : "").
396 "\/>
397 <span class=\"subfield_controls\"><a href=\"#\" class=\"buttonDot tag_editor\"
398 onclick=\"openAuth(this.parentNode.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode}."','biblio'); return false;\" tabindex=\"1\" title=\"Tag editor\">Tag editor</a></span>
400 # it's a plugin field
402 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
404 # opening plugin. Just check whether we are on a developer computer on a production one
405 # (the cgidir differs)
406 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
407 unless ( opendir( DIR, "$cgidir" ) ) {
408 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
409 closedir( DIR );
411 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
412 if (do $plugin) {
413 my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
414 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
416 $subfield_data{marc_value} =
417 "<input tabindex=\"1\"
418 type=\"text\"
419 id=\"".$subfield_data{id}."\"
420 name=\"".$subfield_data{id}."\"
421 value=\"$value\"
422 class=\"input_marceditor\"
423 onfocus=\"Focus$function_name($index_tag)\"
424 size=\"67\"
425 maxlength=\"".$subfield_data{maxlength}."\"
426 onblur=\"Blur$function_name($index_tag); \" \/>
427 <span class=\"subfield_controls\"><a href=\"#\" class=\"buttonDot tag_editor\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" tabindex=\"1\" title=\"Tag editor\">Tag editor</a></span>
428 $javascript";
429 } else {
430 warn "Plugin Failed: $plugin";
431 # supply default input form
432 $subfield_data{marc_value} =
433 "<input type=\"text\"
434 id=\"".$subfield_data{id}."\"
435 name=\"".$subfield_data{id}."\"
436 value=\"$value\"
437 tabindex=\"1\"
438 size=\"67\"
439 maxlength=\"".$subfield_data{maxlength}."\"
440 class=\"input_marceditor\"
444 # it's an hidden field
446 elsif ( $tag eq '' ) {
447 $subfield_data{marc_value} =
448 "<input tabindex=\"1\"
449 type=\"hidden\"
450 id=\"".$subfield_data{id}."\"
451 name=\"".$subfield_data{id}."\"
452 size=\"67\"
453 maxlength=\"".$subfield_data{maxlength}."\"
454 value=\"$value\" \/>
457 else {
458 # it's a standard field
459 if (
460 length($value) > 100
462 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
463 and $tag < 400 && $subfield eq 'a' )
464 or ( $tag >= 500
465 and $tag < 600
466 && C4::Context->preference("marcflavour") eq "MARC21" )
469 $subfield_data{marc_value} =
470 "<textarea cols=\"70\"
471 rows=\"4\"
472 id=\"".$subfield_data{id}."\"
473 name=\"".$subfield_data{id}."\"
474 class=\"input_marceditor\"
475 tabindex=\"1\"
476 >$value</textarea>
479 else {
480 $subfield_data{marc_value} =
481 "<input type=\"text\"
482 id=\"".$subfield_data{id}."\"
483 name=\"".$subfield_data{id}."\"
484 value=\"$value\"
485 tabindex=\"1\"
486 size=\"67\"
487 maxlength=\"".$subfield_data{maxlength}."\"
488 class=\"input_marceditor\"
493 $subfield_data{'index_subfield'} = $index_subfield;
494 return \%subfield_data;
498 =head2 format_indicator
500 Translate indicator value for output form - specifically, map
501 indicator = ' ' to ''. This is for the convenience of a cataloger
502 using a mouse to select an indicator input.
504 =cut
506 sub format_indicator {
507 my $ind_value = shift;
508 return '' if not defined $ind_value;
509 return '' if $ind_value eq ' ';
510 return $ind_value;
513 sub build_tabs {
514 my ( $template, $record, $dbh, $encoding,$input ) = @_;
516 # fill arrays
517 my @loop_data = ();
518 my $tag;
520 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
521 my $query = "SELECT authorised_value, lib
522 FROM authorised_values";
523 $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id )} if $branch_limit;
524 $query .= " WHERE category = ?";
525 $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
526 $query .= " GROUP BY lib ORDER BY lib, lib_opac";
527 my $authorised_values_sth = $dbh->prepare( $query );
529 # in this array, we will push all the 10 tabs
530 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
531 my @BIG_LOOP;
532 my %seen;
533 my @tab_data; # all tags to display
535 foreach my $used ( @$usedTagsLib ){
536 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
537 $seen{$used->{tagfield}}++;
540 my $max_num_tab=-1;
541 foreach(@$usedTagsLib){
542 if($_->{tab} > -1 && $_->{tab} >= $max_num_tab && $_->{tagfield} != '995'){ # FIXME : MARC21 ?
543 $max_num_tab = $_->{tab};
546 if($max_num_tab >= 9){
547 $max_num_tab = 9;
549 # loop through each tab 0 through 9
550 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
551 my @loop_data = (); #innerloop in the template.
552 my $i = 0;
553 foreach my $tag (@tab_data) {
554 $i++;
555 next if ! $tag;
556 my ($indicator1, $indicator2);
557 my $index_tag = CreateKey;
559 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
560 # if MARC::Record is empty => use tab as master loop.
561 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
562 my @fields;
563 if ( $tag ne '000' ) {
564 @fields = $record->field($tag);
566 else {
567 push @fields, $record->leader(); # if tag == 000
569 # loop through each field
570 foreach my $field (@fields) {
572 my @subfields_data;
573 if ( $tag < 10 ) {
574 my ( $value, $subfield );
575 if ( $tag ne '000' ) {
576 $value = $field->data();
577 $subfield = "@";
579 else {
580 $value = $field;
581 $subfield = '@';
583 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
584 next
585 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
586 'biblio.biblionumber' );
587 push(
588 @subfields_data,
589 &create_input(
590 $tag, $subfield, $value, $index_tag, $tabloop, $record,
591 $authorised_values_sth,$input
595 else {
596 my @subfields = $field->subfields();
597 foreach my $subfieldcount ( 0 .. $#subfields ) {
598 my $subfield = $subfields[$subfieldcount][0];
599 my $value = $subfields[$subfieldcount][1];
600 next if ( length $subfield != 1 );
601 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
602 push(
603 @subfields_data,
604 &create_input(
605 $tag, $subfield, $value, $index_tag, $tabloop,
606 $record, $authorised_values_sth,$input
612 # now, loop again to add parameter subfield that are not in the MARC::Record
613 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
615 next if ( length $subfield != 1 );
616 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
617 next if ( $tag < 10 );
618 next
619 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
620 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
621 and not ( $subfield eq "9" and
622 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
623 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
624 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
626 ; #check for visibility flag
627 # if subfield is $9 in a field whose $a is authority-controlled,
628 # always include in the form regardless of the hidden setting - bug 2206
629 next if ( defined( $field->subfield($subfield) ) );
630 push(
631 @subfields_data,
632 &create_input(
633 $tag, $subfield, '', $index_tag, $tabloop, $record,
634 $authorised_values_sth,$input
638 if ( $#subfields_data >= 0 ) {
639 # build the tag entry.
640 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
641 # have twice the same "name" value, and cgi->param() will return only one, making
642 # all subfields to be merged in a single field.
643 my %tag_data = (
644 tag => $tag,
645 index => $index_tag,
646 tag_lib => $tagslib->{$tag}->{lib},
647 repeatable => $tagslib->{$tag}->{repeatable},
648 mandatory => $tagslib->{$tag}->{mandatory},
649 subfield_loop => \@subfields_data,
650 fixedfield => $tag < 10?1:0,
651 random => CreateKey,
653 if ($tag >= 10){ # no indicator for 00x tags
654 $tag_data{indicator1} = format_indicator($field->indicator(1)),
655 $tag_data{indicator2} = format_indicator($field->indicator(2)),
657 push( @loop_data, \%tag_data );
659 } # foreach $field end
661 # if breeding is empty
663 else {
664 my @subfields_data;
665 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
666 next if ( length $subfield != 1 );
667 next
668 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
669 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
670 and not ( $subfield eq "9" and
671 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
672 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
673 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
675 ; #check for visibility flag
676 # if subfield is $9 in a field whose $a is authority-controlled,
677 # always include in the form regardless of the hidden setting - bug 2206
678 next
679 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
680 push(
681 @subfields_data,
682 &create_input(
683 $tag, $subfield, '', $index_tag, $tabloop, $record,
684 $authorised_values_sth,$input
688 if ( $#subfields_data >= 0 ) {
689 my %tag_data = (
690 tag => $tag,
691 index => $index_tag,
692 tag_lib => $tagslib->{$tag}->{lib},
693 repeatable => $tagslib->{$tag}->{repeatable},
694 mandatory => $tagslib->{$tag}->{mandatory},
695 indicator1 => $indicator1,
696 indicator2 => $indicator2,
697 subfield_loop => \@subfields_data,
698 tagfirstsubfield => $subfields_data[0],
699 fixedfield => $tag < 10?1:0,
702 push @loop_data, \%tag_data ;
706 if ( $#loop_data >= 0 ) {
707 push @BIG_LOOP, {
708 number => $tabloop,
709 innerloop => \@loop_data,
713 $authorised_values_sth->finish;
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 $searchid = $input->param('searchid');
731 my $dbh = C4::Context->dbh;
732 my $hostbiblionumber = $input->param('hostbiblionumber');
733 my $hostitemnumber = $input->param('hostitemnumber');
734 # fast cataloguing datas in transit
735 my $fa_circborrowernumber = $input->param('circborrowernumber');
736 my $fa_barcode = $input->param('barcode');
737 my $fa_branch = $input->param('branch');
738 my $fa_stickyduedate = $input->param('stickyduedate');
739 my $fa_duedatespec = $input->param('duedatespec');
741 my $userflags = 'edit_catalogue';
742 if ($frameworkcode eq 'FA'){
743 $userflags = 'fast_cataloging';
746 my $changed_framework = $input->param('changed_framework');
747 $frameworkcode = &GetFrameworkCode($biblionumber)
748 if ( $biblionumber and not($frameworkcode) and $op ne 'addbiblio' );
750 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
751 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
753 template_name => "cataloguing/addbiblio.tt",
754 query => $input,
755 type => "intranet",
756 authnotrequired => 0,
757 flagsrequired => { editcatalogue => $userflags },
761 if ($frameworkcode eq 'FA'){
762 # We need to grab and set some variables in the template for use on the additems screen
763 $template->param(
764 'circborrowernumber' => $fa_circborrowernumber,
765 'barcode' => $fa_barcode,
766 'branch' => $fa_branch,
767 'stickyduedate' => $fa_stickyduedate,
768 'duedatespec' => $fa_duedatespec,
772 # Getting the list of all frameworks
773 # get framework list
774 my $frameworks = getframeworks;
775 my @frameworkcodeloop;
776 foreach my $thisframeworkcode ( keys %$frameworks ) {
777 my %row = (
778 value => $thisframeworkcode,
779 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
781 if ($frameworkcode eq $thisframeworkcode){
782 $row{'selected'} = 1;
784 push @frameworkcodeloop, \%row;
786 $template->param( frameworkcodeloop => \@frameworkcodeloop,
787 breedingid => $breedingid );
789 # ++ Global
790 $tagslib = &GetMarcStructure( 1, $frameworkcode );
791 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
792 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
793 # -- Global
795 my $record = -1;
796 my $encoding = "";
797 my (
798 $biblionumbertagfield,
799 $biblionumbertagsubfield,
800 $biblioitemnumtagfield,
801 $biblioitemnumtagsubfield,
802 $bibitem,
803 $biblioitemnumber
806 if (($biblionumber) && !($breedingid)){
807 $record = GetMarcBiblio($biblionumber);
809 if ($breedingid) {
810 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
813 #populate hostfield if hostbiblionumber is available
814 if ($hostbiblionumber) {
815 my $marcflavour = C4::Context->preference("marcflavour");
816 $record = MARC::Record->new();
817 $record->leader('');
818 my $field =
819 PrepHostMarcField( $hostbiblionumber, $hostitemnumber, $marcflavour );
820 $record->append_fields($field);
823 # This is a child record
824 if ($parentbiblio) {
825 my $marcflavour = C4::Context->preference('marcflavour');
826 $record = MARC::Record->new();
827 SetMarcUnicodeFlag($record, $marcflavour);
828 my $hostfield = prepare_host_field($parentbiblio,$marcflavour);
829 if ($hostfield) {
830 $record->append_fields($hostfield);
834 $is_a_modif = 0;
836 if ($biblionumber) {
837 $is_a_modif = 1;
838 $template->param( title => $record->title(), );
840 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
841 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
842 &GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
843 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
844 &GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
846 # search biblioitems value
847 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
848 $sth->execute($biblionumber);
849 ($biblioitemnumber) = $sth->fetchrow;
852 #-------------------------------------------------------------------------------------
853 if ( $op eq "addbiblio" ) {
854 #-------------------------------------------------------------------------------------
855 $template->param(
856 biblionumberdata => $biblionumber,
858 # getting html input
859 my @params = $input->param();
860 $record = TransformHtmlToMarc( $input );
861 # check for a duplicate
862 my ( $duplicatebiblionumber, $duplicatetitle );
863 if ( !$is_a_modif ) {
864 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
866 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
867 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
868 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
869 my $oldbibnum;
870 my $oldbibitemnum;
871 if (C4::Context->preference("BiblioAddsAuthorities")){
872 BiblioAutoLink( $record, $frameworkcode );
874 if ( $is_a_modif ) {
875 ModBiblioframework( $biblionumber, $frameworkcode );
876 ModBiblio( $record, $biblionumber, $frameworkcode );
878 else {
879 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
881 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view" && $redirect ne "just_save")){
882 if ($frameworkcode eq 'FA'){
883 print $input->redirect(
884 '/cgi-bin/koha/cataloguing/additem.pl?'
885 .'biblionumber='.$biblionumber
886 .'&frameworkcode='.$frameworkcode
887 .'&circborrowernumber='.$fa_circborrowernumber
888 .'&branch='.$fa_branch
889 .'&barcode='.uri_escape($fa_barcode)
890 .'&stickyduedate='.$fa_stickyduedate
891 .'&duedatespec='.$fa_duedatespec
893 exit;
895 else {
896 print $input->redirect(
897 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid"
899 exit;
902 elsif(($is_a_modif || $redirect eq "view") && $redirect ne "just_save"){
903 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
904 my $views = { C4::Search::enabled_staff_search_views };
905 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
906 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
907 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
908 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
909 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
910 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
911 } else {
912 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
914 exit;
917 elsif ($redirect eq "just_save"){
918 my $tab = $input->param('current_tab');
919 print $input->redirect("/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber&framework=$frameworkcode&tab=$tab&searchid=$searchid");
921 else {
922 $template->param(
923 biblionumber => $biblionumber,
924 done =>1,
925 popup =>1
927 $template->param( title => $record->subfield('200',"a") ) if ($record ne "-1" && C4::Context->preference('marcflavour') =~/unimarc/i);
928 $template->param( title => $record->title() ) if ($record ne "-1" && C4::Context->preference('marcflavour') eq "usmarc");
929 $template->param(
930 popup => $mode,
931 itemtype => $frameworkcode,
933 output_html_with_http_headers $input, $cookie, $template->output;
934 exit;
936 } else {
937 # it may be a duplicate, warn the user and do nothing
938 build_tabs ($template, $record, $dbh,$encoding,$input);
939 $template->param(
940 biblionumber => $biblionumber,
941 biblioitemnumber => $biblioitemnumber,
942 duplicatebiblionumber => $duplicatebiblionumber,
943 duplicatebibid => $duplicatebiblionumber,
944 duplicatetitle => $duplicatetitle,
948 elsif ( $op eq "delete" ) {
950 my $error = &DelBiblio($biblionumber);
951 if ($error) {
952 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
953 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
954 exit;
957 print $input->redirect('/cgi-bin/koha/catalogue/search.pl');
958 exit;
960 } else {
961 #----------------------------------------------------------------------------
962 # If we're in a duplication case, we have to set to "" the biblionumber
963 # as we'll save the biblio as a new one.
964 $template->param(
965 biblionumberdata => $biblionumber,
966 op => $op,
968 if ( $op eq "duplicate" ) {
969 $biblionumber = "";
972 if($changed_framework eq "changed"){
973 $record = TransformHtmlToMarc( $input );
975 elsif( $record ne -1 ) {
976 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
977 eval {
978 my $uxml = $record->as_xml;
979 MARC::Record::default_record_format("UNIMARC")
980 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
981 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
982 $record = $urecord;
985 build_tabs( $template, $record, $dbh, $encoding,$input );
986 $template->param(
987 biblionumber => $biblionumber,
988 biblionumbertagfield => $biblionumbertagfield,
989 biblionumbertagsubfield => $biblionumbertagsubfield,
990 biblioitemnumtagfield => $biblioitemnumtagfield,
991 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
992 biblioitemnumber => $biblioitemnumber,
993 hostbiblionumber => $hostbiblionumber,
994 hostitemnumber => $hostitemnumber
998 $template->param( title => $record->title() ) if ( $record ne "-1" );
999 $template->param(
1000 popup => $mode,
1001 frameworkcode => $frameworkcode,
1002 itemtype => $frameworkcode,
1003 borrowernumber => $loggedinuser,
1004 tab => $input->param('tab')
1006 $template->{'VARS'}->{'searchid'} = $searchid;
1008 output_html_with_http_headers $input, $cookie, $template->output;