Bug 7618 Escape HTML special characters for SQL displayed above results in Report...
[koha.git] / cataloguing / addbiblio.pl
blob7fabb776ce30c1e806366df5d8aa7d5d5957d152
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 # 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=\"$max_length\"
378 readonly=\"readonly\"
379 \/>";
381 # it's a thesaurus / authority field
383 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
384 if (C4::Context->preference("BiblioAddsAuthorities")) {
385 $subfield_data{marc_value} =
386 "<input type=\"text\"
387 id=\"".$subfield_data{id}."\"
388 name=\"".$subfield_data{id}."\"
389 value=\"$value\"
390 class=\"input_marceditor readonly\"
391 tabindex=\"1\"
392 size=\"67\"
393 maxlength=\"$max_length\"
395 <span class=\"subfield_controls\"><a href=\"#\" class=\"buttonDot\"
396 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>
398 } else {
399 $subfield_data{marc_value} =
400 "<input type=\"text\"
401 id=\"".$subfield_data{id}."\"
402 name=\"".$subfield_data{id}."\"
403 value=\"$value\"
404 class=\"input_marceditor readonly\"
405 tabindex=\"1\"
406 size=\"67\"
407 maxlength=\"$max_length\"
408 readonly=\"readonly\"
409 \/><span class=\"subfield_controls\"><a href=\"#\" class=\"buttonDot\"
410 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>
413 # it's a plugin field
415 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
417 # opening plugin. Just check whether we are on a developer computer on a production one
418 # (the cgidir differs)
419 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
420 unless ( opendir( DIR, "$cgidir" ) ) {
421 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
422 closedir( DIR );
424 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
425 if (do $plugin) {
426 my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
427 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
429 $subfield_data{marc_value} =
430 "<input tabindex=\"1\"
431 type=\"text\"
432 id=\"".$subfield_data{id}."\"
433 name=\"".$subfield_data{id}."\"
434 value=\"$value\"
435 class=\"input_marceditor\"
436 onfocus=\"Focus$function_name($index_tag)\"
437 size=\"67\"
438 maxlength=\"$max_length\"
439 onblur=\"Blur$function_name($index_tag); \" \/>
440 <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>
441 $javascript";
442 } else {
443 warn "Plugin Failed: $plugin";
444 # supply default input form
445 $subfield_data{marc_value} =
446 "<input type=\"text\"
447 id=\"".$subfield_data{id}."\"
448 name=\"".$subfield_data{id}."\"
449 value=\"$value\"
450 tabindex=\"1\"
451 size=\"67\"
452 maxlength=\"$max_length\"
453 class=\"input_marceditor\"
457 # it's an hidden field
459 elsif ( $tag eq '' ) {
460 $subfield_data{marc_value} =
461 "<input tabindex=\"1\"
462 type=\"hidden\"
463 id=\"".$subfield_data{id}."\"
464 name=\"".$subfield_data{id}."\"
465 size=\"67\"
466 maxlength=\"$max_length\"
467 value=\"$value\" \/>
470 else {
471 # it's a standard field
472 if (
473 length($value) > 100
475 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
476 and $tag < 400 && $subfield eq 'a' )
477 or ( $tag >= 500
478 and $tag < 600
479 && C4::Context->preference("marcflavour") eq "MARC21" )
482 $subfield_data{marc_value} =
483 "<textarea cols=\"70\"
484 rows=\"4\"
485 id=\"".$subfield_data{id}."\"
486 name=\"".$subfield_data{id}."\"
487 class=\"input_marceditor\"
488 tabindex=\"1\"
489 >$value</textarea>
492 else {
493 $subfield_data{marc_value} =
494 "<input type=\"text\"
495 id=\"".$subfield_data{id}."\"
496 name=\"".$subfield_data{id}."\"
497 value=\"$value\"
498 tabindex=\"1\"
499 size=\"67\"
500 maxlength=\"$max_length\"
501 class=\"input_marceditor\"
506 $subfield_data{'index_subfield'} = $index_subfield;
507 return \%subfield_data;
511 =head2 format_indicator
513 Translate indicator value for output form - specifically, map
514 indicator = ' ' to ''. This is for the convenience of a cataloger
515 using a mouse to select an indicator input.
517 =cut
519 sub format_indicator {
520 my $ind_value = shift;
521 return '' if not defined $ind_value;
522 return '' if $ind_value eq ' ';
523 return $ind_value;
526 sub build_tabs {
527 my ( $template, $record, $dbh, $encoding,$input ) = @_;
529 # fill arrays
530 my @loop_data = ();
531 my $tag;
533 my $authorised_values_sth = $dbh->prepare(
534 "select authorised_value,lib
535 from authorised_values
536 where category=? order by lib"
539 # in this array, we will push all the 10 tabs
540 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
541 my @BIG_LOOP;
542 my %seen;
543 my @tab_data; # all tags to display
545 foreach my $used ( @$usedTagsLib ){
546 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
547 $seen{$used->{tagfield}}++;
550 my $max_num_tab=-1;
551 foreach(@$usedTagsLib){
552 if($_->{tab} > -1 && $_->{tab} >= $max_num_tab && $_->{tagfield} != '995'){ # FIXME : MARC21 ?
553 $max_num_tab = $_->{tab};
556 if($max_num_tab >= 9){
557 $max_num_tab = 9;
559 # loop through each tab 0 through 9
560 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
561 my @loop_data = (); #innerloop in the template.
562 my $i = 0;
563 foreach my $tag (@tab_data) {
564 $i++;
565 next if ! $tag;
566 my ($indicator1, $indicator2);
567 my $index_tag = CreateKey;
569 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
570 # if MARC::Record is empty => use tab as master loop.
571 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
572 my @fields;
573 if ( $tag ne '000' ) {
574 @fields = $record->field($tag);
576 else {
577 push @fields, $record->leader(); # if tag == 000
579 # loop through each field
580 foreach my $field (@fields) {
582 my @subfields_data;
583 if ( $tag < 10 ) {
584 my ( $value, $subfield );
585 if ( $tag ne '000' ) {
586 $value = $field->data();
587 $subfield = "@";
589 else {
590 $value = $field;
591 $subfield = '@';
593 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
594 next
595 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
596 'biblio.biblionumber' );
597 push(
598 @subfields_data,
599 &create_input(
600 $tag, $subfield, $value, $index_tag, $tabloop, $record,
601 $authorised_values_sth,$input
605 else {
606 my @subfields = $field->subfields();
607 foreach my $subfieldcount ( 0 .. $#subfields ) {
608 my $subfield = $subfields[$subfieldcount][0];
609 my $value = $subfields[$subfieldcount][1];
610 next if ( length $subfield != 1 );
611 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
612 push(
613 @subfields_data,
614 &create_input(
615 $tag, $subfield, $value, $index_tag, $tabloop,
616 $record, $authorised_values_sth,$input
622 # now, loop again to add parameter subfield that are not in the MARC::Record
623 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
625 next if ( length $subfield != 1 );
626 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
627 next if ( $tag < 10 );
628 next
629 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
630 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
631 and not ( $subfield eq "9" and
632 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
633 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
634 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
636 ; #check for visibility flag
637 # if subfield is $9 in a field whose $a is authority-controlled,
638 # always include in the form regardless of the hidden setting - bug 2206
639 next if ( defined( $field->subfield($subfield) ) );
640 push(
641 @subfields_data,
642 &create_input(
643 $tag, $subfield, '', $index_tag, $tabloop, $record,
644 $authorised_values_sth,$input
648 if ( $#subfields_data >= 0 ) {
649 # build the tag entry.
650 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
651 # have twice the same "name" value, and cgi->param() will return only one, making
652 # all subfields to be merged in a single field.
653 my %tag_data = (
654 tag => $tag,
655 index => $index_tag,
656 tag_lib => $tagslib->{$tag}->{lib},
657 repeatable => $tagslib->{$tag}->{repeatable},
658 mandatory => $tagslib->{$tag}->{mandatory},
659 subfield_loop => \@subfields_data,
660 fixedfield => $tag < 10?1:0,
661 random => CreateKey,
663 if ($tag >= 10){ # no indicator for 00x tags
664 $tag_data{indicator1} = format_indicator($field->indicator(1)),
665 $tag_data{indicator2} = format_indicator($field->indicator(2)),
667 push( @loop_data, \%tag_data );
669 } # foreach $field end
671 # if breeding is empty
673 else {
674 my @subfields_data;
675 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
676 next if ( length $subfield != 1 );
677 next
678 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
679 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
680 and not ( $subfield eq "9" and
681 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
682 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
683 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
685 ; #check for visibility flag
686 # if subfield is $9 in a field whose $a is authority-controlled,
687 # always include in the form regardless of the hidden setting - bug 2206
688 next
689 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
690 push(
691 @subfields_data,
692 &create_input(
693 $tag, $subfield, '', $index_tag, $tabloop, $record,
694 $authorised_values_sth,$input
698 if ( $#subfields_data >= 0 ) {
699 my %tag_data = (
700 tag => $tag,
701 index => $index_tag,
702 tag_lib => $tagslib->{$tag}->{lib},
703 repeatable => $tagslib->{$tag}->{repeatable},
704 mandatory => $tagslib->{$tag}->{mandatory},
705 indicator1 => $indicator1,
706 indicator2 => $indicator2,
707 subfield_loop => \@subfields_data,
708 tagfirstsubfield => $subfields_data[0],
709 fixedfield => $tag < 10?1:0,
712 push @loop_data, \%tag_data ;
716 if ( $#loop_data >= 0 ) {
717 push @BIG_LOOP, {
718 number => $tabloop,
719 innerloop => \@loop_data,
723 $template->param( BIG_LOOP => \@BIG_LOOP );
726 # ========================
727 # MAIN
728 #=========================
729 my $input = new CGI;
730 my $error = $input->param('error');
731 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
732 my $breedingid = $input->param('breedingid');
733 my $z3950 = $input->param('z3950');
734 my $op = $input->param('op');
735 my $mode = $input->param('mode');
736 my $frameworkcode = $input->param('frameworkcode');
737 my $redirect = $input->param('redirect');
738 my $dbh = C4::Context->dbh;
739 my $hostbiblionumber = $input->param('hostbiblionumber');
740 my $hostitemnumber = $input->param('hostitemnumber');
743 my $userflags = 'edit_catalogue';
744 if ($frameworkcode eq 'FA'){
745 $userflags = 'fast_cataloging';
748 $frameworkcode = &GetFrameworkCode($biblionumber)
749 if ( $biblionumber and not($frameworkcode) and $op ne 'addbiblio' );
751 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
752 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
754 template_name => "cataloguing/addbiblio.tt",
755 query => $input,
756 type => "intranet",
757 authnotrequired => 0,
758 flagsrequired => { editcatalogue => $userflags },
762 if ($frameworkcode eq 'FA'){
763 # We need to grab and set some variables in the template for use on the additems screen
764 $template->{VARS}->{'circborrowernumber'} = $input->param('borrowernumber');
765 $template->{VARS}->{'barcode'} = $input->param('barcode');
766 $template->{VARS}->{'branch'} = $input->param('branch');
767 $template->{VARS}->{'stickyduedate'} = $input->param('stickyduedate');
768 $template->{VARS}->{'duedatespec'} = $input->param('duedatespec');
771 # Getting the list of all frameworks
772 # get framework list
773 my $frameworks = getframeworks;
774 my @frameworkcodeloop;
775 foreach my $thisframeworkcode ( keys %$frameworks ) {
776 my %row = (
777 value => $thisframeworkcode,
778 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
780 if ($frameworkcode eq $thisframeworkcode){
781 $row{'selected'} = 1;
783 push @frameworkcodeloop, \%row;
785 $template->param( frameworkcodeloop => \@frameworkcodeloop,
786 breedingid => $breedingid );
788 # ++ Global
789 $tagslib = &GetMarcStructure( 1, $frameworkcode );
790 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
791 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
792 # -- Global
794 my $record = -1;
795 my $encoding = "";
796 my (
797 $biblionumbertagfield,
798 $biblionumbertagsubfield,
799 $biblioitemnumtagfield,
800 $biblioitemnumtagsubfield,
801 $bibitem,
802 $biblioitemnumber
805 if (($biblionumber) && !($breedingid)){
806 $record = GetMarcBiblio($biblionumber);
808 if ($breedingid) {
809 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
811 #populate hostfield if hostbiblionumber is available
812 if ($hostbiblionumber){
813 my $marcflavour = C4::Context->preference("marcflavour");
814 $record=MARC::Record->new();
815 $record->leader('');
816 my $field = PrepHostMarcField($hostbiblionumber, $hostitemnumber,$marcflavour);
817 $record->append_fields($field);
820 $is_a_modif = 0;
822 if ($biblionumber) {
823 $is_a_modif = 1;
824 $template->param( title => $record->title(), );
826 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
827 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
828 &GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
829 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
830 &GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
832 # search biblioitems value
833 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
834 $sth->execute($biblionumber);
835 ($biblioitemnumber) = $sth->fetchrow;
838 #-------------------------------------------------------------------------------------
839 if ( $op eq "addbiblio" ) {
840 #-------------------------------------------------------------------------------------
841 $template->param(
842 biblionumberdata => $biblionumber,
844 # getting html input
845 my @params = $input->param();
846 $record = TransformHtmlToMarc( $input );
847 # check for a duplicate
848 my ( $duplicatebiblionumber, $duplicatetitle );
849 if ( !$is_a_modif ) {
850 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
852 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
853 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
854 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
855 my $oldbibnum;
856 my $oldbibitemnum;
857 if (C4::Context->preference("BiblioAddsAuthorities")){
858 my ($countlinked,$countcreated)=BiblioAutoLink($record,$frameworkcode);
860 if ( $is_a_modif ) {
861 ModBiblioframework( $biblionumber, $frameworkcode );
862 ModBiblio( $record, $biblionumber, $frameworkcode );
864 else {
865 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
867 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view")){
868 if ($frameworkcode eq 'FA'){
869 my $borrowernumber = $input->param('circborrowernumber');
870 my $barcode = $input->param('barcode');
871 my $branch = $input->param('branch');
872 my $stickyduedate = $input->param('stickyduedate');
873 my $duedatespec = $input->param('duedatespec');
874 print $input->redirect(
875 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&borrowernumber=$borrowernumber&branch=$branch&barcode=$barcode&stickyduedate=$stickyduedate&duedatespec=$duedatespec"
877 exit;
879 else {
880 print $input->redirect(
881 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode"
883 exit;
886 elsif($is_a_modif || $redirect eq "view"){
887 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
888 my $views = { C4::Search::enabled_staff_search_views };
889 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
890 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber");
891 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
892 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
893 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
894 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber");
895 } else {
896 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber");
898 exit;
901 else {
902 $template->param(
903 biblionumber => $biblionumber,
904 done =>1,
905 popup =>1
907 $template->param( title => $record->subfield('200',"a") ) if ($record ne "-1" && C4::Context->preference('marcflavour') =~/unimarc/i);
908 $template->param( title => $record->title() ) if ($record ne "-1" && C4::Context->preference('marcflavour') eq "usmarc");
909 $template->param(
910 popup => $mode,
911 itemtype => $frameworkcode,
913 output_html_with_http_headers $input, $cookie, $template->output;
914 exit;
916 } else {
917 # it may be a duplicate, warn the user and do nothing
918 build_tabs ($template, $record, $dbh,$encoding,$input);
919 $template->param(
920 biblionumber => $biblionumber,
921 biblioitemnumber => $biblioitemnumber,
922 duplicatebiblionumber => $duplicatebiblionumber,
923 duplicatebibid => $duplicatebiblionumber,
924 duplicatetitle => $duplicatetitle,
928 elsif ( $op eq "delete" ) {
930 my $error = &DelBiblio($biblionumber);
931 if ($error) {
932 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
933 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
934 exit;
937 print $input->redirect('/cgi-bin/koha/catalogue/search.pl');
938 exit;
940 } else {
941 #----------------------------------------------------------------------------
942 # If we're in a duplication case, we have to set to "" the biblionumber
943 # as we'll save the biblio as a new one.
944 $template->param(
945 biblionumberdata => $biblionumber,
946 op => $op,
948 if ( $op eq "duplicate" ) {
949 $biblionumber = "";
952 if ( $record ne -1 ) {
953 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
954 eval {
955 my $uxml = $record->as_xml;
956 MARC::Record::default_record_format("UNIMARC")
957 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
958 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
959 $record = $urecord;
962 build_tabs( $template, $record, $dbh, $encoding,$input );
963 $template->param(
964 biblionumber => $biblionumber,
965 biblionumbertagfield => $biblionumbertagfield,
966 biblionumbertagsubfield => $biblionumbertagsubfield,
967 biblioitemnumtagfield => $biblioitemnumtagfield,
968 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
969 biblioitemnumber => $biblioitemnumber,
970 hostbiblionumber => $hostbiblionumber,
971 hostitemnumber => $hostitemnumber
975 $template->param( title => $record->title() ) if ( $record ne "-1" );
976 $template->param(
977 popup => $mode,
978 frameworkcode => $frameworkcode,
979 itemtype => $frameworkcode,
980 borrowernumber => $loggedinuser,
981 marcflavour => C4::Context->preference("marcflavour"),
984 output_html_with_http_headers $input, $cookie, $template->output;