Bug 16554: Fix fr-FR sample files
[koha.git] / cataloguing / addbiblio.pl
bloba1f7208edb68781c8e797e7564cf2583dd2baa8e
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
5 # Copyright 2004-2010 BibLibre
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use strict;
23 #use warnings; FIXME - Bug 2505
24 use CGI q(-utf8);
25 use C4::Output;
26 use C4::Auth;
27 use C4::Biblio;
28 use C4::Search;
29 use C4::AuthoritiesMarc;
30 use C4::Context;
31 use MARC::Record;
32 use C4::Log;
33 use C4::Koha;
34 use C4::ClassSource;
35 use C4::ImportBatch;
36 use C4::Charset;
37 use Koha::BiblioFrameworks;
38 use Koha::DateUtils;
40 use Koha::Libraries;
42 use Date::Calc qw(Today);
43 use MARC::File::USMARC;
44 use MARC::File::XML;
45 use URI::Escape;
47 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
48 MARC::File::XML->default_record_format('UNIMARC');
51 our($tagslib,$authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
53 =head1 FUNCTIONS
55 =head2 MARCfindbreeding
57 $record = MARCfindbreeding($breedingid);
59 Look up the import record repository for the record with
60 record with id $breedingid. If found, returns the decoded
61 MARC::Record; otherwise, -1 is returned (FIXME).
62 Returns as second parameter the character encoding.
64 =cut
66 sub MARCfindbreeding {
67 my ( $id ) = @_;
68 my ($marc, $encoding) = GetImportRecordMarc($id);
69 # remove the - in isbn, koha store isbn without any -
70 if ($marc) {
71 my $record = MARC::Record->new_from_usmarc($marc);
72 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
73 if ( $record->field($isbnfield) ) {
74 foreach my $field ( $record->field($isbnfield) ) {
75 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
76 my $newisbn = $field->subfield($isbnsubfield);
77 $newisbn =~ s/-//g;
78 $field->update( $isbnsubfield => $newisbn );
82 # fix the unimarc 100 coded field (with unicode information)
83 if (C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield(100,'a')) {
84 my $f100a=$record->subfield(100,'a');
85 my $f100 = $record->field(100);
86 my $f100temp = $f100->as_string;
87 $record->delete_field($f100);
88 if ( length($f100temp) > 28 ) {
89 substr( $f100temp, 26, 2, "50" );
90 $f100->update( 'a' => $f100temp );
91 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
92 $record->insert_fields_ordered($f100);
96 if ( !defined(ref($record)) ) {
97 return -1;
99 else {
100 # normalize author : UNIMARC specific...
101 if ( C4::Context->preference("z3950NormalizeAuthor")
102 and C4::Context->preference("z3950AuthorAuthFields")
103 and C4::Context->preference("marcflavour") eq 'UNIMARC' )
105 my ( $tag, $subfield ) = GetMarcFromKohaField("biblio.author", '');
107 # my $summary = C4::Context->preference("z3950authortemplate");
108 my $auth_fields =
109 C4::Context->preference("z3950AuthorAuthFields");
110 my @auth_fields = split /,/, $auth_fields;
111 my $field;
113 if ( $record->field($tag) ) {
114 foreach my $tmpfield ( $record->field($tag)->subfields ) {
116 # foreach my $subfieldcode ($tmpfield->subfields){
117 my $subfieldcode = shift @$tmpfield;
118 my $subfieldvalue = shift @$tmpfield;
119 if ($field) {
120 $field->add_subfields(
121 "$subfieldcode" => $subfieldvalue )
122 if ( $subfieldcode ne $subfield );
124 else {
125 $field =
126 MARC::Field->new( $tag, "", "",
127 $subfieldcode => $subfieldvalue )
128 if ( $subfieldcode ne $subfield );
132 $record->delete_field( $record->field($tag) );
133 foreach my $fieldtag (@auth_fields) {
134 next unless ( $record->field($fieldtag) );
135 my $lastname = $record->field($fieldtag)->subfield('a');
136 my $firstname = $record->field($fieldtag)->subfield('b');
137 my $title = $record->field($fieldtag)->subfield('c');
138 my $number = $record->field($fieldtag)->subfield('d');
139 if ($title) {
141 # $field->add_subfields("$subfield"=>"[ ".ucfirst($title).ucfirst($firstname)." ".$number." ]");
142 $field->add_subfields(
143 "$subfield" => ucfirst($title) . " "
144 . ucfirst($firstname) . " "
145 . $number );
147 else {
149 # $field->add_subfields("$subfield"=>"[ ".ucfirst($firstname).", ".ucfirst($lastname)." ]");
150 $field->add_subfields(
151 "$subfield" => ucfirst($firstname) . ", "
152 . ucfirst($lastname) );
155 $record->insert_fields_ordered($field);
157 return $record, $encoding;
160 return -1;
163 =head2 build_authorized_values_list
165 =cut
167 sub build_authorized_values_list {
168 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
170 my @authorised_values;
171 my %authorised_lib;
173 # builds list, depending on authorised value...
175 #---- branch
176 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
177 my $libraries = Koha::Libraries->search_filtered({}, {order_by => ['branchname']});
178 while ( my $l = $libraries->next ) {
179 push @authorised_values, $l->branchcode;;
180 $authorised_lib{$l->branchcode} = $l->branchname;
183 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
184 push @authorised_values, ""
185 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
186 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
188 my $itemtype;
189 my $itemtypes = GetItemTypes( style => 'array' );
190 for my $itemtype ( @$itemtypes ) {
191 push @authorised_values, $itemtype->{itemtype};
192 $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description};
194 $value = $itemtype unless ($value);
196 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
197 push @authorised_values, ""
198 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
200 my $class_sources = GetClassSources();
202 my $default_source = C4::Context->preference("DefaultClassificationSource");
204 foreach my $class_source (sort keys %$class_sources) {
205 next unless $class_sources->{$class_source}->{'used'} or
206 ($value and $class_source eq $value) or
207 ($class_source eq $default_source);
208 push @authorised_values, $class_source;
209 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
211 $value = $default_source unless $value;
213 else {
214 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
215 $authorised_values_sth->execute(
216 $tagslib->{$tag}->{$subfield}->{authorised_value},
217 $branch_limit ? $branch_limit : (),
220 push @authorised_values, ""
221 unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
222 && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
224 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
225 push @authorised_values, $value;
226 $authorised_lib{$value} = $lib;
229 $authorised_values_sth->finish;
230 return {
231 type => 'select',
232 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
233 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
234 default => $value,
235 values => \@authorised_values,
236 labels => \%authorised_lib,
241 =head2 CreateKey
243 Create a random value to set it into the input name
245 =cut
247 sub CreateKey {
248 return int(rand(1000000));
251 =head2 GetMandatoryFieldZ3950
253 This function return an hashref which containts all mandatory field
254 to search with z3950 server.
256 =cut
258 sub GetMandatoryFieldZ3950 {
259 my $frameworkcode = shift;
260 my @isbn = GetMarcFromKohaField('biblioitems.isbn',$frameworkcode);
261 my @title = GetMarcFromKohaField('biblio.title',$frameworkcode);
262 my @author = GetMarcFromKohaField('biblio.author',$frameworkcode);
263 my @issn = GetMarcFromKohaField('biblioitems.issn',$frameworkcode);
264 my @lccn = GetMarcFromKohaField('biblioitems.lccn',$frameworkcode);
266 return {
267 $isbn[0].$isbn[1] => 'isbn',
268 $title[0].$title[1] => 'title',
269 $author[0].$author[1] => 'author',
270 $issn[0].$issn[1] => 'issn',
271 $lccn[0].$lccn[1] => 'lccn',
275 =head2 create_input
277 builds the <input ...> entry for a subfield.
279 =cut
281 sub create_input {
282 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
284 my $index_subfield = CreateKey(); # create a specifique key for each subfield
286 $value =~ s/"/&quot;/g;
288 # if there is no value provided but a default value in parameters, get it
289 if ( $value eq '' ) {
290 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
292 # get today date & replace <<YYYY>>, <<MM>>, <<DD>> if provided in the default value
293 my $today_dt = dt_from_string;
294 my $year = $today_dt->year;
295 my $month = $today_dt->month;
296 my $day = $today_dt->day;
297 $value =~ s/<<YYYY>>/$year/g;
298 $value =~ s/<<MM>>/$month/g;
299 $value =~ s/<<DD>>/$day/g;
300 # And <<USER>> with surname (?)
301 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
302 $value=~s/<<USER>>/$username/g;
305 my $dbh = C4::Context->dbh;
307 # map '@' as "subfield" label for fixed fields
308 # to something that's allowed in a div id.
309 my $id_subfield = $subfield;
310 $id_subfield = "00" if $id_subfield eq "@";
312 my %subfield_data = (
313 tag => $tag,
314 subfield => $id_subfield,
315 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
316 tag_mandatory => $tagslib->{$tag}->{mandatory},
317 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
318 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
319 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
320 index => $index_tag,
321 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
322 value => $value,
323 maxlength => $tagslib->{$tag}->{$subfield}->{maxlength},
324 random => CreateKey(),
327 if(exists $mandatory_z3950->{$tag.$subfield}){
328 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
330 # Subfield is hidden depending of hidden and mandatory flag, and is always
331 # shown if it contains anything or if its field is mandatory.
332 my $tdef = $tagslib->{$tag};
333 $subfield_data{visibility} = "display:none;"
334 if $tdef->{$subfield}->{hidden} % 2 == 1 &&
335 $value eq '' &&
336 !$tdef->{$subfield}->{mandatory} &&
337 !$tdef->{mandatory};
338 # expand all subfields of 773 if there is a host item provided in the input
339 $subfield_data{visibility} ="" if ($tag eq 773 and $cgi->param('hostitemnumber'));
342 # it's an authorised field
343 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
344 $subfield_data{marc_value} =
345 build_authorized_values_list( $tag, $subfield, $value, $dbh,
346 $authorised_values_sth,$index_tag,$index_subfield );
348 # it's a subfield $9 linking to an authority record - see bug 2206
350 elsif ($subfield eq "9" and
351 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
352 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
353 $tagslib->{$tag}->{'a'}->{authtypecode} ne '') {
355 $subfield_data{marc_value} = {
356 type => 'text',
357 id => $subfield_data{id},
358 name => $subfield_data{id},
359 value => $value,
360 size => 5,
361 maxlength => $subfield_data{maxlength},
362 readonly => 1,
365 # it's a thesaurus / authority field
367 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
368 # when authorities auto-creation is allowed, do not set readonly
369 my $is_readonly = !C4::Context->preference("BiblioAddsAuthorities");
371 $subfield_data{marc_value} = {
372 type => 'text',
373 id => $subfield_data{id},
374 name => $subfield_data{id},
375 value => $value,
376 size => 67,
377 maxlength => $subfield_data{maxlength},
378 readonly => ($is_readonly) ? 1 : 0,
379 authtype => $tagslib->{$tag}->{$subfield}->{authtypecode},
382 # it's a plugin field
383 } elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
384 require Koha::FrameworkPlugin;
385 my $plugin = Koha::FrameworkPlugin->new( {
386 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
388 my $pars= { dbh => $dbh, record => $rec, tagslib => $tagslib,
389 id => $subfield_data{id}, tabloop => $tabloop };
390 $plugin->build( $pars );
391 if( !$plugin->errstr ) {
392 $subfield_data{marc_value} = {
393 type => 'text_complex',
394 id => $subfield_data{id},
395 name => $subfield_data{id},
396 value => $value,
397 size => 67,
398 maxlength => $subfield_data{maxlength},
399 javascript => $plugin->javascript,
400 noclick => $plugin->noclick,
402 } else {
403 warn $plugin->errstr;
404 # supply default input form
405 $subfield_data{marc_value} = {
406 type => 'text',
407 id => $subfield_data{id},
408 name => $subfield_data{id},
409 value => $value,
410 size => 67,
411 maxlength => $subfield_data{maxlength},
412 readonly => 0,
416 # it's an hidden field
417 } elsif ( $tag eq '' ) {
418 $subfield_data{marc_value} = {
419 type => 'hidden',
420 id => $subfield_data{id},
421 name => $subfield_data{id},
422 value => $value,
423 size => 67,
424 maxlength => $subfield_data{maxlength},
428 else {
429 # it's a standard field
430 if (
431 length($value) > 100
433 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
434 and $tag < 400 && $subfield eq 'a' )
435 or ( $tag >= 500
436 and $tag < 600
437 && C4::Context->preference("marcflavour") eq "MARC21" )
440 $subfield_data{marc_value} = {
441 type => 'textarea',
442 id => $subfield_data{id},
443 name => $subfield_data{id},
444 value => $value,
448 else {
449 $subfield_data{marc_value} = {
450 type => 'text',
451 id => $subfield_data{id},
452 name => $subfield_data{id},
453 value => $value,
454 size => 67,
455 maxlength => $subfield_data{maxlength},
456 readonly => 0,
461 $subfield_data{'index_subfield'} = $index_subfield;
462 return \%subfield_data;
466 =head2 format_indicator
468 Translate indicator value for output form - specifically, map
469 indicator = ' ' to ''. This is for the convenience of a cataloger
470 using a mouse to select an indicator input.
472 =cut
474 sub format_indicator {
475 my $ind_value = shift;
476 return '' if not defined $ind_value;
477 return '' if $ind_value eq ' ';
478 return $ind_value;
481 sub build_tabs {
482 my ( $template, $record, $dbh, $encoding,$input ) = @_;
484 # fill arrays
485 my @loop_data = ();
486 my $tag;
488 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
489 my $query = "SELECT authorised_value, lib
490 FROM authorised_values";
491 $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id )} if $branch_limit;
492 $query .= " WHERE category = ?";
493 $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
494 $query .= " GROUP BY lib ORDER BY lib, lib_opac";
495 my $authorised_values_sth = $dbh->prepare( $query );
497 # in this array, we will push all the 10 tabs
498 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
499 my @BIG_LOOP;
500 my %seen;
501 my @tab_data; # all tags to display
503 foreach my $used ( @$usedTagsLib ){
504 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
505 $seen{$used->{tagfield}}++;
508 my $max_num_tab=-1;
509 foreach(@$usedTagsLib){
510 if($_->{tab} > -1 && $_->{tab} >= $max_num_tab && $_->{tagfield} != '995'){ # FIXME : MARC21 ?
511 $max_num_tab = $_->{tab};
514 if($max_num_tab >= 9){
515 $max_num_tab = 9;
517 # loop through each tab 0 through 9
518 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
519 my @loop_data = (); #innerloop in the template.
520 my $i = 0;
521 foreach my $tag (@tab_data) {
522 $i++;
523 next if ! $tag;
524 my ($indicator1, $indicator2);
525 my $index_tag = CreateKey;
527 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
528 # if MARC::Record is empty => use tab as master loop.
529 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
530 my @fields;
531 if ( $tag ne '000' ) {
532 @fields = $record->field($tag);
534 else {
535 push @fields, $record->leader(); # if tag == 000
537 # loop through each field
538 foreach my $field (@fields) {
540 my @subfields_data;
541 if ( $tag < 10 ) {
542 my ( $value, $subfield );
543 if ( $tag ne '000' ) {
544 $value = $field->data();
545 $subfield = "@";
547 else {
548 $value = $field;
549 $subfield = '@';
551 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
552 next
553 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
554 'biblio.biblionumber' );
555 push(
556 @subfields_data,
557 &create_input(
558 $tag, $subfield, $value, $index_tag, $tabloop, $record,
559 $authorised_values_sth,$input
563 else {
564 my @subfields = $field->subfields();
565 foreach my $subfieldcount ( 0 .. $#subfields ) {
566 my $subfield = $subfields[$subfieldcount][0];
567 my $value = $subfields[$subfieldcount][1];
568 next if ( length $subfield != 1 );
569 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
570 push(
571 @subfields_data,
572 &create_input(
573 $tag, $subfield, $value, $index_tag, $tabloop,
574 $record, $authorised_values_sth,$input
580 # now, loop again to add parameter subfield that are not in the MARC::Record
581 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
583 next if ( length $subfield != 1 );
584 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
585 next if ( $tag < 10 );
586 next
587 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
588 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
589 and not ( $subfield eq "9" and
590 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
591 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
592 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
594 ; #check for visibility flag
595 # if subfield is $9 in a field whose $a is authority-controlled,
596 # always include in the form regardless of the hidden setting - bug 2206
597 next if ( defined( $field->subfield($subfield) ) );
598 push(
599 @subfields_data,
600 &create_input(
601 $tag, $subfield, '', $index_tag, $tabloop, $record,
602 $authorised_values_sth,$input
606 if ( $#subfields_data >= 0 ) {
607 # build the tag entry.
608 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
609 # have twice the same "name" value, and cgi->param() will return only one, making
610 # all subfields to be merged in a single field.
611 my %tag_data = (
612 tag => $tag,
613 index => $index_tag,
614 tag_lib => $tagslib->{$tag}->{lib},
615 repeatable => $tagslib->{$tag}->{repeatable},
616 mandatory => $tagslib->{$tag}->{mandatory},
617 subfield_loop => \@subfields_data,
618 fixedfield => $tag < 10?1:0,
619 random => CreateKey,
621 if ($tag >= 10){ # no indicator for 00x tags
622 $tag_data{indicator1} = format_indicator($field->indicator(1)),
623 $tag_data{indicator2} = format_indicator($field->indicator(2)),
625 push( @loop_data, \%tag_data );
627 } # foreach $field end
629 # if breeding is empty
631 else {
632 my @subfields_data;
633 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
634 next if ( length $subfield != 1 );
635 next
636 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
637 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
638 and not ( $subfield eq "9" and
639 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
640 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
641 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
643 ; #check for visibility flag
644 # if subfield is $9 in a field whose $a is authority-controlled,
645 # always include in the form regardless of the hidden setting - bug 2206
646 next
647 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
648 push(
649 @subfields_data,
650 &create_input(
651 $tag, $subfield, '', $index_tag, $tabloop, $record,
652 $authorised_values_sth,$input
656 if ( $#subfields_data >= 0 ) {
657 my %tag_data = (
658 tag => $tag,
659 index => $index_tag,
660 tag_lib => $tagslib->{$tag}->{lib},
661 repeatable => $tagslib->{$tag}->{repeatable},
662 mandatory => $tagslib->{$tag}->{mandatory},
663 indicator1 => $indicator1,
664 indicator2 => $indicator2,
665 subfield_loop => \@subfields_data,
666 tagfirstsubfield => $subfields_data[0],
667 fixedfield => $tag < 10?1:0,
670 push @loop_data, \%tag_data ;
674 if ( $#loop_data >= 0 ) {
675 push @BIG_LOOP, {
676 number => $tabloop,
677 innerloop => \@loop_data,
681 $authorised_values_sth->finish;
682 $template->param( BIG_LOOP => \@BIG_LOOP );
685 # ========================
686 # MAIN
687 #=========================
688 my $input = new CGI;
689 my $error = $input->param('error');
690 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
691 my $parentbiblio = $input->param('parentbiblionumber');
692 my $breedingid = $input->param('breedingid');
693 my $z3950 = $input->param('z3950');
694 my $op = $input->param('op');
695 my $mode = $input->param('mode');
696 my $frameworkcode = $input->param('frameworkcode');
697 my $redirect = $input->param('redirect');
698 my $searchid = $input->param('searchid');
699 my $dbh = C4::Context->dbh;
700 my $hostbiblionumber = $input->param('hostbiblionumber');
701 my $hostitemnumber = $input->param('hostitemnumber');
702 # fast cataloguing datas in transit
703 my $fa_circborrowernumber = $input->param('circborrowernumber');
704 my $fa_barcode = $input->param('barcode');
705 my $fa_branch = $input->param('branch');
706 my $fa_stickyduedate = $input->param('stickyduedate');
707 my $fa_duedatespec = $input->param('duedatespec');
709 my $userflags = 'edit_catalogue';
711 my $changed_framework = $input->param('changed_framework');
712 $frameworkcode = &GetFrameworkCode($biblionumber)
713 if ( $biblionumber and not( defined $frameworkcode) and $op ne 'addbiblio' );
715 if ($frameworkcode eq 'FA'){
716 $userflags = 'fast_cataloging';
719 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
720 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
722 template_name => "cataloguing/addbiblio.tt",
723 query => $input,
724 type => "intranet",
725 authnotrequired => 0,
726 flagsrequired => { editcatalogue => $userflags },
730 if ($frameworkcode eq 'FA'){
731 # We need to grab and set some variables in the template for use on the additems screen
732 $template->param(
733 'circborrowernumber' => $fa_circborrowernumber,
734 'barcode' => $fa_barcode,
735 'branch' => $fa_branch,
736 'stickyduedate' => $fa_stickyduedate,
737 'duedatespec' => $fa_duedatespec,
739 } elsif ( C4::Context->preference('EnableAdvancedCatalogingEditor') && $input->cookie( 'catalogue_editor_' . $loggedinuser ) eq 'advanced' && !$breedingid ) {
740 # Only use the advanced editor for non-fast-cataloging.
741 # breedingid is not handled because those would only come off a Z39.50
742 # search initiated by the basic editor.
743 print $input->redirect( '/cgi-bin/koha/cataloguing/editor.pl' . ( $biblionumber ? ( '#catalog/' . $biblionumber ) : '' ) );
746 my $frameworkcodeloop = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
747 $template->param( frameworkcodeloop => $frameworkcodeloop ,
748 breedingid => $breedingid );
750 # ++ Global
751 $tagslib = &GetMarcStructure( 1, $frameworkcode );
752 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
753 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
754 # -- Global
756 my $record = -1;
757 my $encoding = "";
758 my (
759 $biblionumbertagfield,
760 $biblionumbertagsubfield,
761 $biblioitemnumtagfield,
762 $biblioitemnumtagsubfield,
763 $biblioitemnumber
766 if (($biblionumber) && !($breedingid)){
767 $record = GetMarcBiblio($biblionumber);
769 if ($breedingid) {
770 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
773 #populate hostfield if hostbiblionumber is available
774 if ($hostbiblionumber) {
775 my $marcflavour = C4::Context->preference("marcflavour");
776 $record = MARC::Record->new();
777 $record->leader('');
778 my $field =
779 PrepHostMarcField( $hostbiblionumber, $hostitemnumber, $marcflavour );
780 $record->append_fields($field);
783 # This is a child record
784 if ($parentbiblio) {
785 my $marcflavour = C4::Context->preference('marcflavour');
786 $record = MARC::Record->new();
787 SetMarcUnicodeFlag($record, $marcflavour);
788 my $hostfield = prepare_host_field($parentbiblio,$marcflavour);
789 if ($hostfield) {
790 $record->append_fields($hostfield);
794 $is_a_modif = 0;
796 if ($biblionumber) {
797 $is_a_modif = 1;
798 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
799 $template->param( title => $title );
801 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
802 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
803 &GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
804 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
805 &GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
807 # search biblioitems value
808 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
809 $sth->execute($biblionumber);
810 ($biblioitemnumber) = $sth->fetchrow;
813 #-------------------------------------------------------------------------------------
814 if ( $op eq "addbiblio" ) {
815 #-------------------------------------------------------------------------------------
816 $template->param(
817 biblionumberdata => $biblionumber,
819 # getting html input
820 my @params = $input->multi_param();
821 $record = TransformHtmlToMarc( $input, 1 );
822 # check for a duplicate
823 my ( $duplicatebiblionumber, $duplicatetitle );
824 if ( !$is_a_modif ) {
825 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
827 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
828 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
829 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
830 my $oldbibitemnum;
831 if (C4::Context->preference("BiblioAddsAuthorities")){
832 BiblioAutoLink( $record, $frameworkcode );
834 if ( $is_a_modif ) {
835 ModBiblioframework( $biblionumber, $frameworkcode );
836 ModBiblio( $record, $biblionumber, $frameworkcode );
838 else {
839 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
841 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view" && $redirect ne "just_save")){
842 if ($frameworkcode eq 'FA'){
843 print $input->redirect(
844 '/cgi-bin/koha/cataloguing/additem.pl?'
845 .'biblionumber='.$biblionumber
846 .'&frameworkcode='.$frameworkcode
847 .'&circborrowernumber='.$fa_circborrowernumber
848 .'&branch='.$fa_branch
849 .'&barcode='.uri_escape_utf8($fa_barcode)
850 .'&stickyduedate='.$fa_stickyduedate
851 .'&duedatespec='.$fa_duedatespec
853 exit;
855 else {
856 print $input->redirect(
857 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid"
859 exit;
862 elsif(($is_a_modif || $redirect eq "view") && $redirect ne "just_save"){
863 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
864 my $views = { C4::Search::enabled_staff_search_views };
865 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
866 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
867 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
868 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
869 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
870 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
871 } else {
872 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
874 exit;
877 elsif ($redirect eq "just_save"){
878 my $tab = $input->param('current_tab');
879 print $input->redirect("/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber&framework=$frameworkcode&tab=$tab&searchid=$searchid");
881 else {
882 $template->param(
883 biblionumber => $biblionumber,
884 done =>1,
885 popup =>1
887 if ( $record ne '-1' ) {
888 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
889 $template->param( title => $title );
891 $template->param(
892 popup => $mode,
893 itemtype => $frameworkcode,
895 output_html_with_http_headers $input, $cookie, $template->output;
896 exit;
898 } else {
899 # it may be a duplicate, warn the user and do nothing
900 build_tabs ($template, $record, $dbh,$encoding,$input);
901 $template->param(
902 biblionumber => $biblionumber,
903 biblioitemnumber => $biblioitemnumber,
904 duplicatebiblionumber => $duplicatebiblionumber,
905 duplicatebibid => $duplicatebiblionumber,
906 duplicatetitle => $duplicatetitle,
910 elsif ( $op eq "delete" ) {
912 my $error = &DelBiblio($biblionumber);
913 if ($error) {
914 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
915 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
916 exit;
919 print $input->redirect('/cgi-bin/koha/catalogue/search.pl');
920 exit;
922 } else {
923 #----------------------------------------------------------------------------
924 # If we're in a duplication case, we have to set to "" the biblionumber
925 # as we'll save the biblio as a new one.
926 $template->param(
927 biblionumberdata => $biblionumber,
928 op => $op,
930 if ( $op eq "duplicate" ) {
931 $biblionumber = "";
934 if($changed_framework eq "changed"){
935 $record = TransformHtmlToMarc( $input, 1 );
937 elsif( $record ne -1 ) {
938 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
939 eval {
940 my $uxml = $record->as_xml;
941 MARC::Record::default_record_format("UNIMARC")
942 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
943 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
944 $record = $urecord;
947 build_tabs( $template, $record, $dbh, $encoding,$input );
948 $template->param(
949 biblionumber => $biblionumber,
950 biblionumbertagfield => $biblionumbertagfield,
951 biblionumbertagsubfield => $biblionumbertagsubfield,
952 biblioitemnumtagfield => $biblioitemnumtagfield,
953 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
954 biblioitemnumber => $biblioitemnumber,
955 hostbiblionumber => $hostbiblionumber,
956 hostitemnumber => $hostitemnumber
960 if ( $record ne '-1' ) {
961 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
962 $template->param( title => $title );
964 $template->param(
965 popup => $mode,
966 frameworkcode => $frameworkcode,
967 itemtype => $frameworkcode,
968 borrowernumber => $loggedinuser,
969 tab => scalar $input->param('tab')
971 $template->{'VARS'}->{'searchid'} = $searchid;
973 output_html_with_http_headers $input, $cookie, $template->output;