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>.
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::Auth qw( get_template_and_user haspermission );
40 use C4::Search qw( FindDuplicate enabled_staff_search_views );
41 use C4::Auth qw( get_template_and_user haspermission );
44 use C4::ClassSource qw( GetClassSources );
45 use C4::ImportBatch qw( GetImportRecordMarc );
46 use C4::Charset qw( SetMarcUnicodeFlag );
47 use Koha::BiblioFrameworks;
48 use Koha::DateUtils qw( dt_from_string );
53 use Koha::BiblioFrameworks;
55 use MARC::File::USMARC;
57 use URI::Escape qw( uri_escape_utf8 );
59 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
60 MARC::File::XML->default_record_format('UNIMARC');
63 our($tagslib,$authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
67 =head2 MARCfindbreeding
69 $record = MARCfindbreeding($breedingid);
71 Look up the import record repository for the record with
72 record with id $breedingid. If found, returns the decoded
73 MARC::Record; otherwise, -1 is returned (FIXME).
74 Returns as second parameter the character encoding.
78 sub MARCfindbreeding {
80 my ($marc, $encoding) = GetImportRecordMarc($id);
81 # remove the - in isbn, koha store isbn without any -
83 my $record = MARC::Record->new_from_usmarc($marc);
84 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField( 'biblioitems.isbn' );
85 if ( $record->field($isbnfield) ) {
86 foreach my $field ( $record->field($isbnfield) ) {
87 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
88 my $newisbn = $field->subfield($isbnsubfield);
90 $field->update( $isbnsubfield => $newisbn );
94 # fix the unimarc 100 coded field (with unicode information)
95 if (C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield(100,'a')) {
96 my $f100a=$record->subfield(100,'a');
97 my $f100 = $record->field(100);
98 my $f100temp = $f100->as_string;
99 $record->delete_field($f100);
100 if ( length($f100temp) > 28 ) {
101 substr( $f100temp, 26, 2, "50" );
102 $f100->update( 'a' => $f100temp );
103 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
104 $record->insert_fields_ordered($f100);
108 if ( !defined(ref($record)) ) {
112 # normalize author : UNIMARC specific...
113 if ( C4::Context->preference("z3950NormalizeAuthor")
114 and C4::Context->preference("z3950AuthorAuthFields")
115 and C4::Context->preference("marcflavour") eq 'UNIMARC' )
117 my ( $tag, $subfield ) = GetMarcFromKohaField( "biblio.author" );
120 C4::Context->preference("z3950AuthorAuthFields");
121 my @auth_fields = split /,/, $auth_fields;
124 if ( $record->field($tag) ) {
125 foreach my $tmpfield ( $record->field($tag)->subfields ) {
127 my $subfieldcode = shift @$tmpfield;
128 my $subfieldvalue = shift @$tmpfield;
130 $field->add_subfields(
131 "$subfieldcode" => $subfieldvalue )
132 if ( $subfieldcode ne $subfield );
136 MARC::Field->new( $tag, "", "",
137 $subfieldcode => $subfieldvalue )
138 if ( $subfieldcode ne $subfield );
142 $record->delete_field( $record->field($tag) );
143 foreach my $fieldtag (@auth_fields) {
144 next unless ( $record->field($fieldtag) );
145 my $lastname = $record->field($fieldtag)->subfield('a');
146 my $firstname = $record->field($fieldtag)->subfield('b');
147 my $title = $record->field($fieldtag)->subfield('c');
148 my $number = $record->field($fieldtag)->subfield('d');
150 $field->add_subfields(
151 "$subfield" => ucfirst($title) . " "
152 . ucfirst($firstname) . " "
156 $field->add_subfields(
157 "$subfield" => ucfirst($firstname) . ", "
158 . ucfirst($lastname) );
161 $record->insert_fields_ordered($field);
163 return $record, $encoding;
169 =head2 build_authorized_values_list
173 sub build_authorized_values_list {
174 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
176 my @authorised_values;
179 # builds list, depending on authorised value...
182 my $category = $tagslib->{$tag}->{$subfield}->{authorised_value};
183 if ( $category eq "branches" ) {
184 my $libraries = Koha::Libraries->search_filtered({}, {order_by => ['branchname']});
185 while ( my $l = $libraries->next ) {
186 push @authorised_values, $l->branchcode;;
187 $authorised_lib{$l->branchcode} = $l->branchname;
190 elsif ( $category eq "itemtypes" ) {
191 push @authorised_values, "";
194 my $itemtypes = Koha::ItemTypes->search_with_localization;
195 while ( $itemtype = $itemtypes->next ) {
196 push @authorised_values, $itemtype->itemtype;
197 $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
199 $value = $itemtype unless ($value);
201 elsif ( $category eq "cn_source" ) {
202 push @authorised_values, "";
204 my $class_sources = GetClassSources();
206 my $default_source = C4::Context->preference("DefaultClassificationSource");
208 foreach my $class_source (sort keys %$class_sources) {
209 next unless $class_sources->{$class_source}->{'used'} or
210 ($value and $class_source eq $value) or
211 ($class_source eq $default_source);
212 push @authorised_values, $class_source;
213 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
215 $value = $default_source unless $value;
218 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
219 $authorised_values_sth->execute(
220 $tagslib->{$tag}->{$subfield}->{authorised_value},
221 $branch_limit ? $branch_limit : (),
224 push @authorised_values, "";
226 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
227 push @authorised_values, $value;
228 $authorised_lib{$value} = $lib;
231 $authorised_values_sth->finish;
235 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
236 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
238 values => \@authorised_values,
239 labels => \%authorised_lib,
240 ( ( grep { $_ eq $category } ( qw(branches itemtypes cn_source) ) ) ? () : ( category => $category ) ),
247 Create a random value to set it into the input name
252 return int(rand(1000000));
255 =head2 GetMandatoryFieldZ3950
257 This function returns a hashref which contains all mandatory field
258 to search with z3950 server.
262 sub GetMandatoryFieldZ3950 {
263 my $frameworkcode = shift;
264 my @isbn = GetMarcFromKohaField( 'biblioitems.isbn' );
265 my @title = GetMarcFromKohaField( 'biblio.title' );
266 my @author = GetMarcFromKohaField( 'biblio.author' );
267 my @issn = GetMarcFromKohaField( 'biblioitems.issn' );
268 my @lccn = GetMarcFromKohaField( 'biblioitems.lccn' );
271 $isbn[0].$isbn[1] => 'isbn',
272 $title[0].$title[1] => 'title',
273 $author[0].$author[1] => 'author',
274 $issn[0].$issn[1] => 'issn',
275 $lccn[0].$lccn[1] => 'lccn',
281 builds the <input ...> entry for a subfield.
286 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
288 my $index_subfield = CreateKey(); # create a specifique key for each subfield
290 # if there is no value provided but a default value in parameters, get it
291 if ( $value eq '' ) {
292 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue} // q{};
294 # get today date & replace <<YYYY>>, <<YY>>, <<MM>>, <<DD>> if provided in the default value
295 my $today_dt = dt_from_string;
296 my $year = $today_dt->strftime('%Y');
297 my $shortyear = $today_dt->strftime('%y');
298 my $month = $today_dt->strftime('%m');
299 my $day = $today_dt->strftime('%d');
300 $value =~ s/<<YYYY>>/$year/g;
301 $value =~ s/<<YY>>/$shortyear/g;
302 $value =~ s/<<MM>>/$month/g;
303 $value =~ s/<<DD>>/$day/g;
304 # And <<USER>> with surname (?)
305 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
306 $value=~s/<<USER>>/$username/g;
309 my $dbh = C4::Context->dbh;
311 # map '@' as "subfield" label for fixed fields
312 # to something that's allowed in a div id.
313 my $id_subfield = $subfield;
314 $id_subfield = "00" if $id_subfield eq "@";
316 my %subfield_data = (
318 subfield => $id_subfield,
319 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
320 tag_mandatory => $tagslib->{$tag}->{mandatory},
321 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
322 important => $tagslib->{$tag}->{$subfield}->{important},
323 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
324 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
326 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
328 maxlength => $tagslib->{$tag}->{$subfield}->{maxlength},
329 random => CreateKey(),
332 if(exists $mandatory_z3950->{$tag.$subfield}){
333 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
335 # Subfield is hidden depending of hidden and mandatory flag, and is always
336 # shown if it contains anything or if its field is mandatory or important.
337 my $tdef = $tagslib->{$tag};
338 $subfield_data{visibility} = "display:none;"
339 if $tdef->{$subfield}->{hidden} % 2 == 1 &&
341 !$tdef->{$subfield}->{mandatory} &&
342 !$tdef->{mandatory} &&
343 !$tdef->{$subfield}->{important} &&
345 # expand all subfields of 773 if there is a host item provided in the input
346 $subfield_data{visibility} ="" if ($tag eq 773 and $cgi->param('hostitemnumber'));
349 # it's an authorised field
350 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
351 $subfield_data{marc_value} =
352 build_authorized_values_list( $tag, $subfield, $value, $dbh,
353 $authorised_values_sth,$index_tag,$index_subfield );
355 # it's a subfield $9 linking to an authority record - see bug 2206
357 elsif ($subfield eq "9" and
358 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
359 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
360 $tagslib->{$tag}->{'a'}->{authtypecode} ne '') {
362 $subfield_data{marc_value} = {
364 id => $subfield_data{id},
365 name => $subfield_data{id},
368 maxlength => $subfield_data{maxlength},
372 # it's a thesaurus / authority field
374 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
375 # when authorities auto-creation is allowed, do not set readonly
376 my $is_readonly = !C4::Context->preference("BiblioAddsAuthorities");
378 $subfield_data{marc_value} = {
380 id => $subfield_data{id},
381 name => $subfield_data{id},
384 maxlength => $subfield_data{maxlength},
385 readonly => ($is_readonly) ? 1 : 0,
386 authtype => $tagslib->{$tag}->{$subfield}->{authtypecode},
389 # it's a plugin field
390 } elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
391 require Koha::FrameworkPlugin;
392 my $plugin = Koha::FrameworkPlugin->new( {
393 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
395 my $pars= { dbh => $dbh, record => $rec, tagslib => $tagslib,
396 id => $subfield_data{id}, tabloop => $tabloop };
397 $plugin->build( $pars );
398 if( !$plugin->errstr ) {
399 $subfield_data{marc_value} = {
400 type => 'text_complex',
401 id => $subfield_data{id},
402 name => $subfield_data{id},
405 maxlength => $subfield_data{maxlength},
406 javascript => $plugin->javascript,
407 plugin => $plugin->name,
408 noclick => $plugin->noclick,
411 warn $plugin->errstr;
412 # supply default input form
413 $subfield_data{marc_value} = {
415 id => $subfield_data{id},
416 name => $subfield_data{id},
419 maxlength => $subfield_data{maxlength},
424 # it's an hidden field
425 } elsif ( $tag eq '' ) {
426 $subfield_data{marc_value} = {
428 id => $subfield_data{id},
429 name => $subfield_data{id},
432 maxlength => $subfield_data{maxlength},
437 # it's a standard field
441 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
442 and $tag < 400 && $subfield eq 'a' )
445 && C4::Context->preference("marcflavour") eq "MARC21" )
448 $subfield_data{marc_value} = {
450 id => $subfield_data{id},
451 name => $subfield_data{id},
457 $subfield_data{marc_value} = {
459 id => $subfield_data{id},
460 name => $subfield_data{id},
463 maxlength => $subfield_data{maxlength},
469 $subfield_data{'index_subfield'} = $index_subfield;
470 return \%subfield_data;
474 =head2 format_indicator
476 Translate indicator value for output form - specifically, map
477 indicator = ' ' to ''. This is for the convenience of a cataloger
478 using a mouse to select an indicator input.
482 sub format_indicator {
483 my $ind_value = shift;
484 return '' if not defined $ind_value;
485 return '' if $ind_value eq ' ';
490 my ( $template, $record, $dbh, $encoding,$input ) = @_;
496 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
497 my $query = "SELECT authorised_value, lib
498 FROM authorised_values";
499 $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id )} if $branch_limit;
500 $query .= " WHERE category = ?";
501 $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
502 $query .= " GROUP BY authorised_value,lib ORDER BY lib, lib_opac";
503 my $authorised_values_sth = $dbh->prepare( $query );
505 # in this array, we will push all the 10 tabs
506 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
509 my @tab_data; # all tags to display
512 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" );
513 foreach my $used ( @$usedTagsLib ){
515 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
516 $seen{$used->{tagfield}}++;
518 if ( $used->{tab} > -1
519 && $used->{tab} >= $max_num_tab
520 && $used->{tagfield} ne $itemtag )
522 $max_num_tab = $used->{tab};
525 if($max_num_tab >= 9){
528 # loop through each tab 0 through 9
529 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
530 my @loop_data = (); #innerloop in the template.
532 foreach my $tag (sort @tab_data) {
535 my ($indicator1, $indicator2);
536 my $index_tag = CreateKey;
538 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
539 # if MARC::Record is empty => use tab as master loop.
540 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
542 if ( $tag ne '000' ) {
543 @fields = $record->field($tag);
546 push @fields, $record->leader(); # if tag == 000
548 # loop through each field
549 foreach my $field (@fields) {
553 my ( $value, $subfield );
554 if ( $tag ne '000' ) {
555 $value = $field->data();
562 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
564 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
565 'biblio.biblionumber' );
569 $tag, $subfield, $value, $index_tag, $tabloop, $record,
570 $authorised_values_sth,$input
575 my @subfields = $field->subfields();
576 foreach my $subfieldcount ( 0 .. $#subfields ) {
577 my $subfield = $subfields[$subfieldcount][0];
578 my $value = $subfields[$subfieldcount][1];
579 next if ( length $subfield != 1 );
580 next if ( !defined $tagslib->{$tag}->{$subfield} || $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
584 $tag, $subfield, $value, $index_tag, $tabloop,
585 $record, $authorised_values_sth,$input
591 # now, loop again to add parameter subfield that are not in the MARC::Record
592 foreach my $subfield ( keys %{ $tagslib->{$tag} } )
594 next if ( length $subfield != 1 );
595 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
596 next if ( $tag < 10 );
598 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
599 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
600 and not ( $subfield eq "9" and
601 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
602 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
603 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
605 ; #check for visibility flag
606 # if subfield is $9 in a field whose $a is authority-controlled,
607 # always include in the form regardless of the hidden setting - bug 2206
608 next if ( defined( $field->subfield($subfield) ) );
612 $tag, $subfield, '', $index_tag, $tabloop, $record,
613 $authorised_values_sth,$input
617 if ( $#subfields_data >= 0 ) {
618 # build the tag entry.
619 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
620 # have twice the same "name" value, and cgi->param() will return only one, making
621 # all subfields to be merged in a single field.
625 tag_lib => $tagslib->{$tag}->{lib},
626 repeatable => $tagslib->{$tag}->{repeatable},
627 mandatory => $tagslib->{$tag}->{mandatory},
628 important => $tagslib->{$tag}->{important},
629 subfield_loop => \@subfields_data,
630 fixedfield => $tag < 10?1:0,
633 if ($tag >= 10){ # no indicator for 00x tags
634 $tag_data{indicator1} = format_indicator($field->indicator(1)),
635 $tag_data{indicator2} = format_indicator($field->indicator(2)),
637 push( @loop_data, \%tag_data );
639 } # foreach $field end
641 # if breeding is empty
645 foreach my $subfield (
646 sort { $a->{display_order} <=> $b->{display_order} || $a->{subfield} cmp $b->{subfield} }
647 grep { ref($_) && %$_ } # Not a subfield (values for "important", "lib", "mandatory", etc.) or empty
648 values %{ $tagslib->{$tag} } )
651 if ( ( $subfield->{hidden} <= -4 )
652 or ( $subfield->{hidden} >= 5 ) )
653 and not ( $subfield->{subfield} eq "9" and
654 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
655 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
656 $tagslib->{$tag}->{'a'}->{authtypecode} ne ""
658 ; #check for visibility flag
659 # if subfield is $9 in a field whose $a is authority-controlled,
660 # always include in the form regardless of the hidden setting - bug 2206
662 if ( $subfield->{tab} ne $tabloop );
666 $tag, $subfield->{subfield}, '', $index_tag, $tabloop, $record,
667 $authorised_values_sth,$input
671 if ( $#subfields_data >= 0 ) {
675 tag_lib => $tagslib->{$tag}->{lib},
676 repeatable => $tagslib->{$tag}->{repeatable},
677 mandatory => $tagslib->{$tag}->{mandatory},
678 important => $tagslib->{$tag}->{important},
679 indicator1 => ( $indicator1 || $tagslib->{$tag}->{ind1_defaultvalue} ), #if not set, try to load the default value
680 indicator2 => ( $indicator2 || $tagslib->{$tag}->{ind2_defaultvalue} ), #use short-circuit operator for efficiency
681 subfield_loop => \@subfields_data,
682 tagfirstsubfield => $subfields_data[0],
683 fixedfield => $tag < 10?1:0,
686 push @loop_data, \%tag_data ;
690 if ( $#loop_data >= 0 ) {
693 innerloop => \@loop_data,
697 $authorised_values_sth->finish;
698 $template->param( BIG_LOOP => \@BIG_LOOP );
701 # ========================
703 #=========================
704 my $input = CGI->new;
705 my $error = $input->param('error');
706 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
707 my $parentbiblio = $input->param('parentbiblionumber');
708 my $breedingid = $input->param('breedingid');
709 my $z3950 = $input->param('z3950');
710 my $op = $input->param('op') // q{};
711 my $mode = $input->param('mode');
712 my $frameworkcode = $input->param('frameworkcode');
713 my $redirect = $input->param('redirect');
714 my $searchid = $input->param('searchid') // "";
715 my $dbh = C4::Context->dbh;
716 my $hostbiblionumber = $input->param('hostbiblionumber');
717 my $hostitemnumber = $input->param('hostitemnumber');
718 # fast cataloguing datas in transit
719 my $fa_circborrowernumber = $input->param('circborrowernumber');
720 my $fa_barcode = $input->param('barcode');
721 my $fa_branch = $input->param('branch');
722 my $fa_stickyduedate = $input->param('stickyduedate');
723 my $fa_duedatespec = $input->param('duedatespec');
725 my $userflags = 'edit_catalogue';
727 my $changed_framework = $input->param('changed_framework') // q{};
728 $frameworkcode = &GetFrameworkCode($biblionumber)
729 if ( $biblionumber and not( defined $frameworkcode) and $op ne 'addbiblio' );
731 if ($frameworkcode eq 'FA'){
732 $userflags = 'fast_cataloging';
735 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
736 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
738 template_name => "cataloguing/addbiblio.tt",
741 flagsrequired => { editcatalogue => $userflags },
746 my $does_bib_exist = Koha::Biblios->find($biblionumber);
747 if (!defined $does_bib_exist){
748 $biblionumber = undef;
749 $template->param( bib_doesnt_exist => 1 );
753 if ($frameworkcode eq 'FA'){
754 # We need to grab and set some variables in the template for use on the additems screen
756 'circborrowernumber' => $fa_circborrowernumber,
757 'barcode' => $fa_barcode,
758 'branch' => $fa_branch,
759 'stickyduedate' => $fa_stickyduedate,
760 'duedatespec' => $fa_duedatespec,
762 } elsif ( $op ne "delete" &&
763 C4::Context->preference('EnableAdvancedCatalogingEditor') &&
764 C4::Auth::haspermission(C4::Context->userenv->{id},{'editcatalogue'=>'advanced_editor'}) &&
765 $input->cookie( 'catalogue_editor_' . $loggedinuser ) eq 'advanced' &&
767 # Only use the advanced editor for non-fast-cataloging.
768 # breedingid is not handled because those would only come off a Z39.50
769 # search initiated by the basic editor.
770 print $input->redirect( '/cgi-bin/koha/cataloguing/editor.pl' . ( $biblionumber ? ( ($op eq 'duplicate'?'#duplicate/':'#catalog/') . $biblionumber ) : '' ) );
774 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
776 frameworks => $frameworks,
777 breedingid => $breedingid,
781 $tagslib = &GetMarcStructure( 1, $frameworkcode );
782 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
783 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
789 $biblionumbertagfield,
790 $biblionumbertagsubfield,
791 $biblioitemnumtagfield,
792 $biblioitemnumtagsubfield,
796 if (($biblionumber) && !($breedingid)){
797 $record = GetMarcBiblio({ biblionumber => $biblionumber });
800 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
803 #populate hostfield if hostbiblionumber is available
804 if ($hostbiblionumber) {
805 my $marcflavour = C4::Context->preference("marcflavour");
806 $record = MARC::Record->new();
809 PrepHostMarcField( $hostbiblionumber, $hostitemnumber, $marcflavour );
810 $record->append_fields($field);
813 # This is a child record
815 my $marcflavour = C4::Context->preference('marcflavour');
816 $record = MARC::Record->new();
817 SetMarcUnicodeFlag($record, $marcflavour);
818 my $hostfield = prepare_host_field($parentbiblio,$marcflavour);
820 $record->append_fields($hostfield);
828 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
829 $template->param( title => $title );
831 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
832 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
833 &GetMarcFromKohaField( "biblio.biblionumber" );
834 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
835 &GetMarcFromKohaField( "biblioitems.biblioitemnumber" );
837 # search biblioitems value
838 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
839 $sth->execute($biblionumber);
840 ($biblioitemnumber) = $sth->fetchrow;
843 #-------------------------------------------------------------------------------------
844 if ( $op eq "addbiblio" ) {
845 #-------------------------------------------------------------------------------------
847 biblionumberdata => $biblionumber,
850 my @params = $input->multi_param();
851 $record = TransformHtmlToMarc( $input, 1 );
852 # check for a duplicate
853 my ( $duplicatebiblionumber, $duplicatetitle );
854 if ( !$is_a_modif ) {
855 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
857 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
858 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
859 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
862 ModBiblio( $record, $biblionumber, $frameworkcode );
865 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
867 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view" && $redirect ne "just_save")){
868 if ($frameworkcode eq 'FA'){
869 print $input->redirect(
870 '/cgi-bin/koha/cataloguing/additem.pl?'
871 .'biblionumber='.$biblionumber
872 .'&frameworkcode='.$frameworkcode
873 .'&circborrowernumber='.$fa_circborrowernumber
874 .'&branch='.$fa_branch
875 .'&barcode='.uri_escape_utf8($fa_barcode)
876 .'&stickyduedate='.$fa_stickyduedate
877 .'&duedatespec='.$fa_duedatespec
882 print $input->redirect(
883 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid"
888 elsif(($is_a_modif || $redirect eq "view") && $redirect ne "just_save"){
889 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
890 my $views = { C4::Search::enabled_staff_search_views };
891 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
892 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
893 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
894 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
895 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
896 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
898 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
903 elsif ($redirect eq "just_save"){
904 my $tab = $input->param('current_tab');
905 print $input->redirect("/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber&framework=$frameworkcode&tab=$tab&searchid=$searchid");
909 biblionumber => $biblionumber,
913 if ( $record ne '-1' ) {
914 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
915 $template->param( title => $title );
919 itemtype => $frameworkcode,
921 output_html_with_http_headers $input, $cookie, $template->output;
925 # it may be a duplicate, warn the user and do nothing
926 build_tabs ($template, $record, $dbh,$encoding,$input);
928 biblionumber => $biblionumber,
929 biblioitemnumber => $biblioitemnumber,
930 duplicatebiblionumber => $duplicatebiblionumber,
931 duplicatebibid => $duplicatebiblionumber,
932 duplicatetitle => $duplicatetitle,
936 elsif ( $op eq "delete" ) {
938 my $error = &DelBiblio($biblionumber);
940 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
941 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
945 print $input->redirect('/cgi-bin/koha/catalogue/search.pl' . ($searchid ? "?searchid=$searchid" : ""));
949 #----------------------------------------------------------------------------
950 # If we're in a duplication case, we have to set to "" the biblionumber
951 # as we'll save the biblio as a new one.
953 biblionumberdata => $biblionumber,
956 if ( $op eq "duplicate" ) {
960 if($changed_framework eq "changed"){
961 $record = TransformHtmlToMarc( $input, 1 );
963 elsif( $record ne -1 ) {
964 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
966 my $uxml = $record->as_xml;
967 MARC::Record::default_record_format("UNIMARC")
968 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
969 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
973 build_tabs( $template, $record, $dbh, $encoding,$input );
975 biblionumber => $biblionumber,
976 biblionumbertagfield => $biblionumbertagfield,
977 biblionumbertagsubfield => $biblionumbertagsubfield,
978 biblioitemnumtagfield => $biblioitemnumtagfield,
979 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
980 biblioitemnumber => $biblioitemnumber,
981 hostbiblionumber => $hostbiblionumber,
982 hostitemnumber => $hostitemnumber
986 if ( $record ne '-1' ) {
987 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
988 $template->param( title => $title );
992 frameworkcode => $frameworkcode,
993 itemtype => $frameworkcode,
994 borrowernumber => $loggedinuser,
995 tab => scalar $input->param('tab')
997 $template->{'VARS'}->{'searchid'} = $searchid;
999 output_html_with_http_headers $input, $cookie, $template->output;