Merge remote-tracking branch 'kc/new/enh/bug_6716' into kcmaster
[koha.git] / cataloguing / addbiblio.pl
blob637d82dffd938813d0cde061465e547a64cbcc99
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'};
218 $value = $class_source unless ($value);
219 $value = $default_source unless ($value);
221 #---- "true" authorised value
223 else {
224 $authorised_values_sth->execute(
225 $tagslib->{$tag}->{$subfield}->{authorised_value} );
227 push @authorised_values, ""
228 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
230 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
231 push @authorised_values, $value;
232 $authorised_lib{$value} = $lib;
235 return CGI::scrolling_list(
236 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
237 -values => \@authorised_values,
238 -default => $value,
239 -labels => \%authorised_lib,
240 -override => 1,
241 -size => 1,
242 -multiple => 0,
243 -tabindex => 1,
244 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
245 -class => "input_marceditor",
249 =head2 CreateKey
251 Create a random value to set it into the input name
253 =cut
255 sub CreateKey(){
256 return int(rand(1000000));
259 =head2 GetMandatoryFieldZ3950
261 This function return an hashref which containts all mandatory field
262 to search with z3950 server.
264 =cut
266 sub GetMandatoryFieldZ3950($){
267 my $frameworkcode = shift;
268 my @isbn = GetMarcFromKohaField('biblioitems.isbn',$frameworkcode);
269 my @title = GetMarcFromKohaField('biblio.title',$frameworkcode);
270 my @author = GetMarcFromKohaField('biblio.author',$frameworkcode);
271 my @issn = GetMarcFromKohaField('biblioitems.issn',$frameworkcode);
272 my @lccn = GetMarcFromKohaField('biblioitems.lccn',$frameworkcode);
274 return {
275 $isbn[0].$isbn[1] => 'isbn',
276 $title[0].$title[1] => 'title',
277 $author[0].$author[1] => 'author',
278 $issn[0].$issn[1] => 'issn',
279 $lccn[0].$lccn[1] => 'lccn',
283 =head2 create_input
285 builds the <input ...> entry for a subfield.
287 =cut
289 sub create_input {
290 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
292 my $index_subfield = CreateKey(); # create a specifique key for each subfield
294 $value =~ s/"/&quot;/g;
296 # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
297 my $max_length = 9999;
298 if ($tag eq '000') {
299 $max_length = 24;
300 } elsif ($tag eq '008' and C4::Context->preference('marcflavour') eq 'MARC21') {
301 $max_length = 40;
304 # if there is no value provided but a default value in parameters, get it
305 if ( $value eq '' ) {
306 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
308 # get today date & replace YYYY, MM, DD if provided in the default value
309 my ( $year, $month, $day ) = Today();
310 $month = sprintf( "%02d", $month );
311 $day = sprintf( "%02d", $day );
312 $value =~ s/YYYY/$year/g;
313 $value =~ s/MM/$month/g;
314 $value =~ s/DD/$day/g;
315 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
316 $value=~s/user/$username/g;
319 my $dbh = C4::Context->dbh;
321 # map '@' as "subfield" label for fixed fields
322 # to something that's allowed in a div id.
323 my $id_subfield = $subfield;
324 $id_subfield = "00" if $id_subfield eq "@";
326 my %subfield_data = (
327 tag => $tag,
328 subfield => $id_subfield,
329 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
330 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
331 tag_mandatory => $tagslib->{$tag}->{mandatory},
332 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
333 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
334 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
335 index => $index_tag,
336 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
337 value => $value,
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 # it's an authorised field
353 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
354 $subfield_data{marc_value} =
355 build_authorized_values_list( $tag, $subfield, $value, $dbh,
356 $authorised_values_sth,$index_tag,$index_subfield );
358 # it's a subfield $9 linking to an authority record - see bug 2206
360 elsif ($subfield eq "9" and
361 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
362 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
363 $tagslib->{$tag}->{'a'}->{authtypecode} ne '') {
365 $subfield_data{marc_value} =
366 "<input type=\"text\"
367 id=\"".$subfield_data{id}."\"
368 name=\"".$subfield_data{id}."\"
369 value=\"$value\"
370 class=\"input_marceditor readonly\"
371 tabindex=\"1\"
372 size=\"5\"
373 maxlength=\"$max_length\"
374 readonly=\"readonly\"
375 \/>";
377 # it's a thesaurus / authority field
379 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
380 if (C4::Context->preference("BiblioAddsAuthorities")) {
381 $subfield_data{marc_value} =
382 "<input type=\"text\"
383 id=\"".$subfield_data{id}."\"
384 name=\"".$subfield_data{id}."\"
385 value=\"$value\"
386 class=\"input_marceditor readonly\"
387 tabindex=\"1\"
388 size=\"67\"
389 maxlength=\"$max_length\"
391 <span class=\"subfield_controls\"><a href=\"#\" class=\"buttonDot\"
392 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>
394 } else {
395 $subfield_data{marc_value} =
396 "<input type=\"text\"
397 id=\"".$subfield_data{id}."\"
398 name=\"".$subfield_data{id}."\"
399 value=\"$value\"
400 class=\"input_marceditor readonly\"
401 tabindex=\"1\"
402 size=\"67\"
403 maxlength=\"$max_length\"
404 readonly=\"readonly\"
405 \/><span class=\"subfield_controls\"><a href=\"#\" class=\"buttonDot\"
406 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>
409 # it's a plugin field
411 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
413 # opening plugin. Just check wether we are on a developper computer on a production one
414 # (the cgidir differs)
415 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
416 unless ( opendir( DIR, "$cgidir" ) ) {
417 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
418 closedir( DIR );
420 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
421 if (do $plugin) {
422 my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
423 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
425 $subfield_data{marc_value} =
426 "<input tabindex=\"1\"
427 type=\"text\"
428 id=\"".$subfield_data{id}."\"
429 name=\"".$subfield_data{id}."\"
430 value=\"$value\"
431 class=\"input_marceditor\"
432 onfocus=\"Focus$function_name($index_tag)\"
433 size=\"67\"
434 maxlength=\"$max_length\"
435 onblur=\"Blur$function_name($index_tag); \" \/>
436 <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>
437 $javascript";
438 } else {
439 warn "Plugin Failed: $plugin";
440 # supply default input form
441 $subfield_data{marc_value} =
442 "<input type=\"text\"
443 id=\"".$subfield_data{id}."\"
444 name=\"".$subfield_data{id}."\"
445 value=\"$value\"
446 tabindex=\"1\"
447 size=\"67\"
448 maxlength=\"$max_length\"
449 class=\"input_marceditor\"
453 # it's an hidden field
455 elsif ( $tag eq '' ) {
456 $subfield_data{marc_value} =
457 "<input tabindex=\"1\"
458 type=\"hidden\"
459 id=\"".$subfield_data{id}."\"
460 name=\"".$subfield_data{id}."\"
461 size=\"67\"
462 maxlength=\"$max_length\"
463 value=\"$value\" \/>
466 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
467 $subfield_data{marc_value} =
468 "<input type=\"text\"
469 id=\"".$subfield_data{id}."\"
470 name=\"".$subfield_data{id}."\"
471 class=\"input_marceditor\"
472 tabindex=\"1\"
473 size=\"67\"
474 maxlength=\"$max_length\"
475 value=\"$value\"
476 \/>";
478 # it's a standard field
480 else {
481 if (
482 length($value) > 100
484 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
485 and $tag < 400 && $subfield eq 'a' )
486 or ( $tag >= 500
487 and $tag < 600
488 && C4::Context->preference("marcflavour") eq "MARC21" )
491 $subfield_data{marc_value} =
492 "<textarea cols=\"70\"
493 rows=\"4\"
494 id=\"".$subfield_data{id}."\"
495 name=\"".$subfield_data{id}."\"
496 class=\"input_marceditor\"
497 tabindex=\"1\"
498 >$value</textarea>
501 else {
502 $subfield_data{marc_value} =
503 "<input type=\"text\"
504 id=\"".$subfield_data{id}."\"
505 name=\"".$subfield_data{id}."\"
506 value=\"$value\"
507 tabindex=\"1\"
508 size=\"67\"
509 maxlength=\"$max_length\"
510 class=\"input_marceditor\"
515 $subfield_data{'index_subfield'} = $index_subfield;
516 return \%subfield_data;
520 =head2 format_indicator
522 Translate indicator value for output form - specifically, map
523 indicator = ' ' to ''. This is for the convenience of a cataloger
524 using a mouse to select an indicator input.
526 =cut
528 sub format_indicator {
529 my $ind_value = shift;
530 return '' if not defined $ind_value;
531 return '' if $ind_value eq ' ';
532 return $ind_value;
535 sub build_tabs ($$$$$) {
536 my ( $template, $record, $dbh, $encoding,$input ) = @_;
538 # fill arrays
539 my @loop_data = ();
540 my $tag;
542 my $authorised_values_sth = $dbh->prepare(
543 "select authorised_value,lib
544 from authorised_values
545 where category=? order by lib"
548 # in this array, we will push all the 10 tabs
549 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
550 my @BIG_LOOP;
551 my %seen;
552 my @tab_data; # all tags to display
554 foreach my $used ( @$usedTagsLib ){
555 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
556 $seen{$used->{tagfield}}++;
559 my $max_num_tab=-1;
560 foreach(@$usedTagsLib){
561 if($_->{tab} > -1 && $_->{tab} >= $max_num_tab && $_->{tagfield} != '995'){ # FIXME : MARC21 ?
562 $max_num_tab = $_->{tab};
565 if($max_num_tab >= 9){
566 $max_num_tab = 9;
568 # loop through each tab 0 through 9
569 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
570 my @loop_data = (); #innerloop in the template.
571 my $i = 0;
572 foreach my $tag (@tab_data) {
573 $i++;
574 next if ! $tag;
575 my ($indicator1, $indicator2);
576 my $index_tag = CreateKey;
578 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
579 # if MARC::Record is empty => use tab as master loop.
580 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
581 my @fields;
582 if ( $tag ne '000' ) {
583 @fields = $record->field($tag);
585 else {
586 push @fields, $record->leader(); # if tag == 000
588 # loop through each field
589 foreach my $field (@fields) {
591 my @subfields_data;
592 if ( $tag < 10 ) {
593 my ( $value, $subfield );
594 if ( $tag ne '000' ) {
595 $value = $field->data();
596 $subfield = "@";
598 else {
599 $value = $field;
600 $subfield = '@';
602 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
603 next
604 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
605 'biblio.biblionumber' );
606 push(
607 @subfields_data,
608 &create_input(
609 $tag, $subfield, $value, $index_tag, $tabloop, $record,
610 $authorised_values_sth,$input
614 else {
615 my @subfields = $field->subfields();
616 foreach my $subfieldcount ( 0 .. $#subfields ) {
617 my $subfield = $subfields[$subfieldcount][0];
618 my $value = $subfields[$subfieldcount][1];
619 next if ( length $subfield != 1 );
620 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
621 push(
622 @subfields_data,
623 &create_input(
624 $tag, $subfield, $value, $index_tag, $tabloop,
625 $record, $authorised_values_sth,$input
631 # now, loop again to add parameter subfield that are not in the MARC::Record
632 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
634 next if ( length $subfield != 1 );
635 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
636 next if ( $tag < 10 );
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 if ( defined( $field->subfield($subfield) ) );
649 push(
650 @subfields_data,
651 &create_input(
652 $tag, $subfield, '', $index_tag, $tabloop, $record,
653 $authorised_values_sth,$input
657 if ( $#subfields_data >= 0 ) {
658 # build the tag entry.
659 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
660 # have twice the same "name" value, and cgi->param() will return only one, making
661 # all subfields to be merged in a single field.
662 my %tag_data = (
663 tag => $tag,
664 index => $index_tag,
665 tag_lib => $tagslib->{$tag}->{lib},
666 repeatable => $tagslib->{$tag}->{repeatable},
667 mandatory => $tagslib->{$tag}->{mandatory},
668 subfield_loop => \@subfields_data,
669 fixedfield => $tag < 10?1:0,
670 random => CreateKey,
672 if ($tag >= 10){ # no indicator for 00x tags
673 $tag_data{indicator1} = format_indicator($field->indicator(1)),
674 $tag_data{indicator2} = format_indicator($field->indicator(2)),
676 push( @loop_data, \%tag_data );
678 } # foreach $field end
680 # if breeding is empty
682 else {
683 my @subfields_data;
684 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
685 next if ( length $subfield != 1 );
686 next
687 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
688 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
689 and not ( $subfield eq "9" and
690 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
691 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
692 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
694 ; #check for visibility flag
695 # if subfield is $9 in a field whose $a is authority-controlled,
696 # always include in the form regardless of the hidden setting - bug 2206
697 next
698 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
699 push(
700 @subfields_data,
701 &create_input(
702 $tag, $subfield, '', $index_tag, $tabloop, $record,
703 $authorised_values_sth,$input
707 if ( $#subfields_data >= 0 ) {
708 my %tag_data = (
709 tag => $tag,
710 index => $index_tag,
711 tag_lib => $tagslib->{$tag}->{lib},
712 repeatable => $tagslib->{$tag}->{repeatable},
713 mandatory => $tagslib->{$tag}->{mandatory},
714 indicator1 => $indicator1,
715 indicator2 => $indicator2,
716 subfield_loop => \@subfields_data,
717 tagfirstsubfield => $subfields_data[0],
718 fixedfield => $tag < 10?1:0,
721 push @loop_data, \%tag_data ;
725 if ( $#loop_data >= 0 ) {
726 push @BIG_LOOP, {
727 number => $tabloop,
728 innerloop => \@loop_data,
732 $template->param( BIG_LOOP => \@BIG_LOOP );
736 # sub that tries to find authorities linked to the biblio
737 # the sub :
738 # - search in the authority DB for the same authid (in $9 of the biblio)
739 # - search in the authority DB for the same 001 (in $3 of the biblio in UNIMARC)
740 # - search in the authority DB for the same values (exactly) (in all subfields of the biblio)
741 # if the authority is found, the biblio is modified accordingly to be connected to the authority.
742 # if the authority is not found, it's added, and the biblio is then modified to be connected to the authority.
745 sub BiblioAddAuthorities{
746 my ( $record, $frameworkcode ) = @_;
747 my $dbh=C4::Context->dbh;
748 my $query=$dbh->prepare(qq|
749 SELECT authtypecode,tagfield
750 FROM marc_subfield_structure
751 WHERE frameworkcode=?
752 AND (authtypecode IS NOT NULL AND authtypecode<>\"\")|);
753 # SELECT authtypecode,tagfield
754 # FROM marc_subfield_structure
755 # WHERE frameworkcode=?
756 # AND (authtypecode IS NOT NULL OR authtypecode<>\"\")|);
757 $query->execute($frameworkcode);
758 my ($countcreated,$countlinked);
759 while (my $data=$query->fetchrow_hashref){
760 foreach my $field ($record->field($data->{tagfield})){
761 next if ($field->subfield('3') || $field->subfield('9'));
762 # No authorities id in the tag.
763 # Search if there is any authorities to link to.
764 my $query='at='.$data->{authtypecode}.' ';
765 map {$query.= ' and he,ext="'.$_->[1].'"' if ($_->[0]=~/[A-z]/)} $field->subfields();
766 my ($error, $results, $total_hits)=SimpleSearch( $query, undef, undef, [ "authorityserver" ] );
767 # there is only 1 result
768 if ( $error ) {
769 warn "BIBLIOADDSAUTHORITIES: $error";
770 return (0,0) ;
772 if ( @{$results} == 1) {
773 my $marcrecord = MARC::File::USMARC::decode($results->[0]);
774 $field->add_subfields('9'=>$marcrecord->field('001')->data);
775 $countlinked++;
776 } elsif (@{$results} > 1) {
777 #More than One result
778 #This can comes out of a lack of a subfield.
779 # my $marcrecord = MARC::File::USMARC::decode($results->[0]);
780 # $record->field($data->{tagfield})->add_subfields('9'=>$marcrecord->field('001')->data);
781 $countlinked++;
782 } else {
783 #There are no results, build authority record, add it to Authorities, get authid and add it to 9
784 ###NOTICE : This is only valid if a subfield is linked to one and only one authtypecode
785 ###NOTICE : This can be a problem. We should also look into other types and rejected forms.
786 my $authtypedata=GetAuthType($data->{authtypecode});
787 next unless $authtypedata;
788 my $marcrecordauth=MARC::Record->new();
789 if (C4::Context->preference('marcflavour') eq 'MARC21') {
790 $marcrecordauth->leader(' nz a22 o 4500');
791 SetMarcUnicodeFlag($marcrecordauth, 'MARC21');
793 my $authfield=MARC::Field->new($authtypedata->{auth_tag_to_report},'','',"a"=>"".$field->subfield('a'));
794 map { $authfield->add_subfields($_->[0]=>$_->[1]) if ($_->[0]=~/[A-z]/ && $_->[0] ne "a" )} $field->subfields();
795 $marcrecordauth->insert_fields_ordered($authfield);
797 # bug 2317: ensure new authority knows it's using UTF-8; currently
798 # only need to do this for MARC21, as MARC::Record->as_xml_record() handles
799 # automatically for UNIMARC (by not transcoding)
800 # FIXME: AddAuthority() instead should simply explicitly require that the MARC::Record
801 # use UTF-8, but as of 2008-08-05, did not want to introduce that kind
802 # of change to a core API just before the 3.0 release.
804 if (C4::Context->preference('marcflavour') eq 'MARC21') {
805 $marcrecordauth->insert_fields_ordered(MARC::Field->new('667','','','a'=>"Machine generated authority record."));
806 my $cite = $record->author() . ", " . $record->title_proper() . ", " . $record->publication_date() . " ";
807 $cite =~ s/^[\s\,]*//;
808 $cite =~ s/[\s\,]*$//;
809 $cite = "Work cat.: (" . C4::Context->preference('MARCOrgCode') . ")". $record->subfield('999','c') . ": " . $cite;
810 $marcrecordauth->insert_fields_ordered(MARC::Field->new('670','','','a'=>$cite));
813 # warn "AUTH RECORD ADDED : ".$marcrecordauth->as_formatted;
815 my $authid=AddAuthority($marcrecordauth,'',$data->{authtypecode});
816 $countcreated++;
817 $field->add_subfields('9'=>$authid);
821 return ($countlinked,$countcreated);
824 # ========================
825 # MAIN
826 #=========================
827 my $input = new CGI;
828 my $error = $input->param('error');
829 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
830 my $breedingid = $input->param('breedingid');
831 my $z3950 = $input->param('z3950');
832 my $op = $input->param('op');
833 my $mode = $input->param('mode');
834 my $frameworkcode = $input->param('frameworkcode');
835 my $redirect = $input->param('redirect');
836 my $dbh = C4::Context->dbh;
839 my $userflags = 'edit_catalogue';
840 if ($frameworkcode eq 'FA'){
841 $userflags = 'fast_cataloging';
844 $frameworkcode = &GetFrameworkCode($biblionumber)
845 if ( $biblionumber and not($frameworkcode) and $op ne 'addbiblio' );
847 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
848 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
850 template_name => "cataloguing/addbiblio.tt",
851 query => $input,
852 type => "intranet",
853 authnotrequired => 0,
854 flagsrequired => { editcatalogue => $userflags },
858 if ($frameworkcode eq 'FA'){
859 # We need to grab and set some variables in the template for use on the additems screen
860 $template->{VARS}->{'circborrowernumber'} = $input->param('borrowernumber');
861 $template->{VARS}->{'barcode'} = $input->param('barcode');
862 $template->{VARS}->{'branch'} = $input->param('branch');
863 $template->{VARS}->{'stickyduedate'} = $input->param('stickyduedate');
864 $template->{VARS}->{'duedatespec'} = $input->param('duedatespec');
867 # Getting the list of all frameworks
868 # get framework list
869 my $frameworks = getframeworks;
870 my @frameworkcodeloop;
871 foreach my $thisframeworkcode ( keys %$frameworks ) {
872 my %row = (
873 value => $thisframeworkcode,
874 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
876 if ($frameworkcode eq $thisframeworkcode){
877 $row{'selected'}="selected=\"selected\"";
879 push @frameworkcodeloop, \%row;
881 $template->param( frameworkcodeloop => \@frameworkcodeloop,
882 breedingid => $breedingid );
884 # ++ Global
885 $tagslib = &GetMarcStructure( 1, $frameworkcode );
886 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
887 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
888 # -- Global
890 my $record = -1;
891 my $encoding = "";
892 my (
893 $biblionumbertagfield,
894 $biblionumbertagsubfield,
895 $biblioitemnumtagfield,
896 $biblioitemnumtagsubfield,
897 $bibitem,
898 $biblioitemnumber
901 if (($biblionumber) && !($breedingid)){
902 $record = GetMarcBiblio($biblionumber);
904 if ($breedingid) {
905 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
908 $is_a_modif = 0;
910 if ($biblionumber) {
911 $is_a_modif = 1;
912 $template->param( title => $record->title(), );
914 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
915 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
916 &GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
917 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
918 &GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
920 # search biblioitems value
921 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
922 $sth->execute($biblionumber);
923 ($biblioitemnumber) = $sth->fetchrow;
926 #-------------------------------------------------------------------------------------
927 if ( $op eq "addbiblio" ) {
928 #-------------------------------------------------------------------------------------
929 $template->param(
930 biblionumberdata => $biblionumber,
932 # getting html input
933 my @params = $input->param();
934 $record = TransformHtmlToMarc( \@params , $input );
935 # check for a duplicate
936 my ( $duplicatebiblionumber, $duplicatetitle );
937 if ( !$is_a_modif ) {
938 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
940 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
941 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
942 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
943 my $oldbibnum;
944 my $oldbibitemnum;
945 if (C4::Context->preference("BiblioAddsAuthorities")){
946 my ($countlinked,$countcreated)=BiblioAddAuthorities($record,$frameworkcode);
948 if ( $is_a_modif ) {
949 ModBiblioframework( $biblionumber, $frameworkcode );
950 ModBiblio( $record, $biblionumber, $frameworkcode );
952 else {
953 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
955 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view")){
956 if ($frameworkcode eq 'FA'){
957 my $borrowernumber = $input->param('circborrowernumber');
958 my $barcode = $input->param('barcode');
959 my $branch = $input->param('branch');
960 my $stickyduedate = $input->param('stickyduedate');
961 my $duedatespec = $input->param('duedatespec');
962 print $input->redirect(
963 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&borrowernumber=$borrowernumber&branch=$branch&barcode=$barcode&stickyduedate=$stickyduedate&duedatespec=$duedatespec"
965 exit;
967 else {
968 print $input->redirect(
969 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode"
971 exit;
974 elsif($is_a_modif || $redirect eq "view"){
975 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
976 my $views = { C4::Search::enabled_staff_search_views };
977 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
978 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber");
979 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
980 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
981 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
982 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber");
983 } else {
984 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber");
986 exit;
989 else {
990 $template->param(
991 biblionumber => $biblionumber,
992 done =>1,
993 popup =>1
995 $template->param( title => $record->subfield('200',"a") ) if ($record ne "-1" && C4::Context->preference('marcflavour') =~/unimarc/i);
996 $template->param( title => $record->title() ) if ($record ne "-1" && C4::Context->preference('marcflavour') eq "usmarc");
997 $template->param(
998 popup => $mode,
999 itemtype => $frameworkcode,
1001 output_html_with_http_headers $input, $cookie, $template->output;
1002 exit;
1004 } else {
1005 # it may be a duplicate, warn the user and do nothing
1006 build_tabs ($template, $record, $dbh,$encoding,$input);
1007 $template->param(
1008 biblionumber => $biblionumber,
1009 biblioitemnumber => $biblioitemnumber,
1010 duplicatebiblionumber => $duplicatebiblionumber,
1011 duplicatebibid => $duplicatebiblionumber,
1012 duplicatetitle => $duplicatetitle,
1016 elsif ( $op eq "delete" ) {
1018 my $error = &DelBiblio($biblionumber);
1019 if ($error) {
1020 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
1021 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
1022 exit;
1025 print $input->redirect('/cgi-bin/koha/catalogue/search.pl');
1026 exit;
1028 } else {
1029 #----------------------------------------------------------------------------
1030 # If we're in a duplication case, we have to set to "" the biblionumber
1031 # as we'll save the biblio as a new one.
1032 $template->param(
1033 biblionumberdata => $biblionumber,
1034 op => $op,
1036 if ( $op eq "duplicate" ) {
1037 $biblionumber = "";
1040 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
1041 eval {
1042 my $uxml = $record->as_xml;
1043 MARC::Record::default_record_format("UNIMARC")
1044 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
1045 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
1046 $record = $urecord;
1048 build_tabs( $template, $record, $dbh, $encoding,$input );
1049 $template->param(
1050 biblionumber => $biblionumber,
1051 biblionumbertagfield => $biblionumbertagfield,
1052 biblionumbertagsubfield => $biblionumbertagsubfield,
1053 biblioitemnumtagfield => $biblioitemnumtagfield,
1054 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
1055 biblioitemnumber => $biblioitemnumber,
1059 $template->param( title => $record->title() ) if ( $record ne "-1" );
1060 if (C4::Context->preference("marcflavour") eq "MARC21"){
1061 $template->param(MARC21 => 1);
1065 $template->param(
1066 popup => $mode,
1067 frameworkcode => $frameworkcode,
1068 itemtype => $frameworkcode,
1069 borrowernumber => $loggedinuser
1072 output_html_with_http_headers $input, $cookie, $template->output;