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 and 28022
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 '' and
361 $tagslib->{$tag}->{'a'}->{hidden} > -4 and
362 $tagslib->{$tag}->{'a'}->{hidden} < 5) {
363 $subfield_data{marc_value} = {
365 id => $subfield_data{id},
366 name => $subfield_data{id},
369 maxlength => $subfield_data{maxlength},
373 # it's a thesaurus / authority field
375 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
376 # when authorities auto-creation is allowed, do not set readonly
377 my $is_readonly = !C4::Context->preference("BiblioAddsAuthorities");
379 $subfield_data{marc_value} = {
381 id => $subfield_data{id},
382 name => $subfield_data{id},
385 maxlength => $subfield_data{maxlength},
386 readonly => ($is_readonly) ? 1 : 0,
387 authtype => $tagslib->{$tag}->{$subfield}->{authtypecode},
390 # it's a plugin field
391 } elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
392 require Koha::FrameworkPlugin;
393 my $plugin = Koha::FrameworkPlugin->new( {
394 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
396 my $pars= { dbh => $dbh, record => $rec, tagslib => $tagslib,
397 id => $subfield_data{id}, tabloop => $tabloop };
398 $plugin->build( $pars );
399 if( !$plugin->errstr ) {
400 $subfield_data{marc_value} = {
401 type => 'text_complex',
402 id => $subfield_data{id},
403 name => $subfield_data{id},
406 maxlength => $subfield_data{maxlength},
407 javascript => $plugin->javascript,
408 plugin => $plugin->name,
409 noclick => $plugin->noclick,
412 warn $plugin->errstr;
413 # supply default input form
414 $subfield_data{marc_value} = {
416 id => $subfield_data{id},
417 name => $subfield_data{id},
420 maxlength => $subfield_data{maxlength},
425 # it's an hidden field
426 } elsif ( $tag eq '' ) {
427 $subfield_data{marc_value} = {
429 id => $subfield_data{id},
430 name => $subfield_data{id},
433 maxlength => $subfield_data{maxlength},
438 # it's a standard field
442 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
443 and $tag < 400 && $subfield eq 'a' )
446 && C4::Context->preference("marcflavour") eq "MARC21" )
449 $subfield_data{marc_value} = {
451 id => $subfield_data{id},
452 name => $subfield_data{id},
458 $subfield_data{marc_value} = {
460 id => $subfield_data{id},
461 name => $subfield_data{id},
464 maxlength => $subfield_data{maxlength},
470 $subfield_data{'index_subfield'} = $index_subfield;
471 return \%subfield_data;
475 =head2 format_indicator
477 Translate indicator value for output form - specifically, map
478 indicator = ' ' to ''. This is for the convenience of a cataloger
479 using a mouse to select an indicator input.
483 sub format_indicator {
484 my $ind_value = shift;
485 return '' if not defined $ind_value;
486 return '' if $ind_value eq ' ';
491 my ( $template, $record, $dbh, $encoding,$input ) = @_;
497 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
498 my $query = "SELECT authorised_value, lib
499 FROM authorised_values";
500 $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id )} if $branch_limit;
501 $query .= " WHERE category = ?";
502 $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
503 $query .= " GROUP BY authorised_value,lib ORDER BY lib, lib_opac";
504 my $authorised_values_sth = $dbh->prepare( $query );
506 # in this array, we will push all the 10 tabs
507 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
510 my @tab_data; # all tags to display
513 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" );
514 foreach my $used ( @$usedTagsLib ){
516 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
517 $seen{$used->{tagfield}}++;
519 if ( $used->{tab} > -1
520 && $used->{tab} >= $max_num_tab
521 && $used->{tagfield} ne $itemtag )
523 $max_num_tab = $used->{tab};
526 if($max_num_tab >= 9){
529 # loop through each tab 0 through 9
530 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
531 my @loop_data = (); #innerloop in the template.
533 foreach my $tag (sort @tab_data) {
536 my ($indicator1, $indicator2);
537 my $index_tag = CreateKey;
539 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
540 # if MARC::Record is empty => use tab as master loop.
541 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
543 if ( $tag ne '000' ) {
544 @fields = $record->field($tag);
547 push @fields, $record->leader(); # if tag == 000
549 # loop through each field
550 foreach my $field (@fields) {
554 my ( $value, $subfield );
555 if ( $tag ne '000' ) {
556 $value = $field->data();
563 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
565 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
566 'biblio.biblionumber' );
570 $tag, $subfield, $value, $index_tag, $tabloop, $record,
571 $authorised_values_sth,$input
576 my @subfields = $field->subfields();
577 foreach my $subfieldcount ( 0 .. $#subfields ) {
578 my $subfield = $subfields[$subfieldcount][0];
579 my $value = $subfields[$subfieldcount][1];
580 next if ( length $subfield != 1 );
581 next if ( !defined $tagslib->{$tag}->{$subfield} || $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
585 $tag, $subfield, $value, $index_tag, $tabloop,
586 $record, $authorised_values_sth,$input
592 # now, loop again to add parameter subfield that are not in the MARC::Record
593 foreach my $subfield ( keys %{ $tagslib->{$tag} } )
595 next if ( length $subfield != 1 );
596 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
597 next if ( $tag < 10 );
599 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
600 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
601 and not ( $subfield eq "9" and
602 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
603 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
604 $tagslib->{$tag}->{'a'}->{authtypecode} ne "" and
605 $tagslib->{$tag}->{'a'}->{hidden} > -4 and
606 $tagslib->{$tag}->{'a'}->{hidden} < 5
608 ; #check for visibility flag
609 # if subfield is $9 in a field whose $a is authority-controlled,
610 # always include in the form regardless of the hidden setting - bug 2206 and 28022
611 next if ( defined( $field->subfield($subfield) ) );
615 $tag, $subfield, '', $index_tag, $tabloop, $record,
616 $authorised_values_sth,$input
620 if ( $#subfields_data >= 0 ) {
621 # build the tag entry.
622 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
623 # have twice the same "name" value, and cgi->param() will return only one, making
624 # all subfields to be merged in a single field.
628 tag_lib => $tagslib->{$tag}->{lib},
629 repeatable => $tagslib->{$tag}->{repeatable},
630 mandatory => $tagslib->{$tag}->{mandatory},
631 important => $tagslib->{$tag}->{important},
632 subfield_loop => \@subfields_data,
633 fixedfield => $tag < 10?1:0,
636 if ($tag >= 10){ # no indicator for 00x tags
637 $tag_data{indicator1} = format_indicator($field->indicator(1)),
638 $tag_data{indicator2} = format_indicator($field->indicator(2)),
640 push( @loop_data, \%tag_data );
642 } # foreach $field end
644 # if breeding is empty
648 foreach my $subfield (
649 sort { $a->{display_order} <=> $b->{display_order} || $a->{subfield} cmp $b->{subfield} }
650 grep { ref($_) && %$_ } # Not a subfield (values for "important", "lib", "mandatory", etc.) or empty
651 values %{ $tagslib->{$tag} } )
654 if ( ( $subfield->{hidden} <= -4 )
655 or ( $subfield->{hidden} >= 5 ) )
656 and not ( $subfield->{subfield} eq "9" and
657 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
658 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
659 $tagslib->{$tag}->{'a'}->{authtypecode} ne "" and
660 $tagslib->{$tag}->{'a'}->{hidden} > -4 and
661 $tagslib->{$tag}->{'a'}->{hidden} < 5
663 ; #check for visibility flag
664 # if subfield is $9 in a field whose $a is authority-controlled,
665 # always include in the form regardless of the hidden setting - bug 2206 and 28022
667 if ( $subfield->{tab} ne $tabloop );
671 $tag, $subfield->{subfield}, '', $index_tag, $tabloop, $record,
672 $authorised_values_sth,$input
676 if ( $#subfields_data >= 0 ) {
680 tag_lib => $tagslib->{$tag}->{lib},
681 repeatable => $tagslib->{$tag}->{repeatable},
682 mandatory => $tagslib->{$tag}->{mandatory},
683 important => $tagslib->{$tag}->{important},
684 indicator1 => ( $indicator1 || $tagslib->{$tag}->{ind1_defaultvalue} ), #if not set, try to load the default value
685 indicator2 => ( $indicator2 || $tagslib->{$tag}->{ind2_defaultvalue} ), #use short-circuit operator for efficiency
686 subfield_loop => \@subfields_data,
687 tagfirstsubfield => $subfields_data[0],
688 fixedfield => $tag < 10?1:0,
691 push @loop_data, \%tag_data ;
695 if ( $#loop_data >= 0 ) {
698 innerloop => \@loop_data,
702 $authorised_values_sth->finish;
703 $template->param( BIG_LOOP => \@BIG_LOOP );
706 # ========================
708 #=========================
709 my $input = CGI->new;
710 my $error = $input->param('error');
711 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
712 my $parentbiblio = $input->param('parentbiblionumber');
713 my $breedingid = $input->param('breedingid');
714 my $z3950 = $input->param('z3950');
715 my $op = $input->param('op') // q{};
716 my $mode = $input->param('mode');
717 my $frameworkcode = $input->param('frameworkcode');
718 my $redirect = $input->param('redirect');
719 my $searchid = $input->param('searchid') // "";
720 my $dbh = C4::Context->dbh;
721 my $hostbiblionumber = $input->param('hostbiblionumber');
722 my $hostitemnumber = $input->param('hostitemnumber');
723 # fast cataloguing datas in transit
724 my $fa_circborrowernumber = $input->param('circborrowernumber');
725 my $fa_barcode = $input->param('barcode');
726 my $fa_branch = $input->param('branch');
727 my $fa_stickyduedate = $input->param('stickyduedate');
728 my $fa_duedatespec = $input->param('duedatespec');
730 my $userflags = 'edit_catalogue';
732 my $changed_framework = $input->param('changed_framework') // q{};
733 $frameworkcode = &GetFrameworkCode($biblionumber)
734 if ( $biblionumber and not( defined $frameworkcode) and $op ne 'addbiblio' );
736 if ($frameworkcode eq 'FA'){
737 $userflags = 'fast_cataloging';
740 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
741 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
743 template_name => "cataloguing/addbiblio.tt",
746 flagsrequired => { editcatalogue => $userflags },
751 my $does_bib_exist = Koha::Biblios->find($biblionumber);
752 if (!defined $does_bib_exist){
753 $biblionumber = undef;
754 $template->param( bib_doesnt_exist => 1 );
758 if ($frameworkcode eq 'FA'){
759 # We need to grab and set some variables in the template for use on the additems screen
761 'circborrowernumber' => $fa_circborrowernumber,
762 'barcode' => $fa_barcode,
763 'branch' => $fa_branch,
764 'stickyduedate' => $fa_stickyduedate,
765 'duedatespec' => $fa_duedatespec,
767 } elsif ( $op ne "delete" &&
768 C4::Context->preference('EnableAdvancedCatalogingEditor') &&
769 C4::Auth::haspermission(C4::Context->userenv->{id},{'editcatalogue'=>'advanced_editor'}) &&
770 $input->cookie( 'catalogue_editor_' . $loggedinuser ) eq 'advanced' &&
772 # Only use the advanced editor for non-fast-cataloging.
773 # breedingid is not handled because those would only come off a Z39.50
774 # search initiated by the basic editor.
775 print $input->redirect( '/cgi-bin/koha/cataloguing/editor.pl' . ( $biblionumber ? ( ($op eq 'duplicate'?'#duplicate/':'#catalog/') . $biblionumber ) : '' ) );
779 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
781 frameworks => $frameworks,
782 breedingid => $breedingid,
786 $tagslib = &GetMarcStructure( 1, $frameworkcode );
787 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
788 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
794 $biblionumbertagfield,
795 $biblionumbertagsubfield,
796 $biblioitemnumtagfield,
797 $biblioitemnumtagsubfield,
801 if (($biblionumber) && !($breedingid)){
802 $record = GetMarcBiblio({ biblionumber => $biblionumber });
805 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
808 #populate hostfield if hostbiblionumber is available
809 if ($hostbiblionumber) {
810 my $marcflavour = C4::Context->preference("marcflavour");
811 $record = MARC::Record->new();
814 PrepHostMarcField( $hostbiblionumber, $hostitemnumber, $marcflavour );
815 $record->append_fields($field);
818 # This is a child record
820 my $marcflavour = C4::Context->preference('marcflavour');
821 $record = MARC::Record->new();
822 SetMarcUnicodeFlag($record, $marcflavour);
823 my $hostfield = prepare_host_field($parentbiblio,$marcflavour);
825 $record->append_fields($hostfield);
833 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
834 $template->param( title => $title );
836 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
837 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
838 &GetMarcFromKohaField( "biblio.biblionumber" );
839 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
840 &GetMarcFromKohaField( "biblioitems.biblioitemnumber" );
842 # search biblioitems value
843 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
844 $sth->execute($biblionumber);
845 ($biblioitemnumber) = $sth->fetchrow;
848 #-------------------------------------------------------------------------------------
849 if ( $op eq "addbiblio" ) {
850 #-------------------------------------------------------------------------------------
852 biblionumberdata => $biblionumber,
855 my @params = $input->multi_param();
856 $record = TransformHtmlToMarc( $input, 1 );
857 # check for a duplicate
858 my ( $duplicatebiblionumber, $duplicatetitle );
859 if ( !$is_a_modif ) {
860 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
862 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
863 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
864 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
867 ModBiblio( $record, $biblionumber, $frameworkcode );
870 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
872 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view" && $redirect ne "just_save")){
873 if ($frameworkcode eq 'FA'){
874 print $input->redirect(
875 '/cgi-bin/koha/cataloguing/additem.pl?'
876 .'biblionumber='.$biblionumber
877 .'&frameworkcode='.$frameworkcode
878 .'&circborrowernumber='.$fa_circborrowernumber
879 .'&branch='.$fa_branch
880 .'&barcode='.uri_escape_utf8($fa_barcode)
881 .'&stickyduedate='.$fa_stickyduedate
882 .'&duedatespec='.$fa_duedatespec
887 print $input->redirect(
888 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid"
893 elsif(($is_a_modif || $redirect eq "view") && $redirect ne "just_save"){
894 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
895 my $views = { C4::Search::enabled_staff_search_views };
896 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
897 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
898 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
899 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
900 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
901 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
903 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
908 elsif ($redirect eq "just_save"){
909 my $tab = $input->param('current_tab');
910 print $input->redirect("/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber&framework=$frameworkcode&tab=$tab&searchid=$searchid");
914 biblionumber => $biblionumber,
918 if ( $record ne '-1' ) {
919 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
920 $template->param( title => $title );
924 itemtype => $frameworkcode,
926 output_html_with_http_headers $input, $cookie, $template->output;
930 # it may be a duplicate, warn the user and do nothing
931 build_tabs ($template, $record, $dbh,$encoding,$input);
933 biblionumber => $biblionumber,
934 biblioitemnumber => $biblioitemnumber,
935 duplicatebiblionumber => $duplicatebiblionumber,
936 duplicatebibid => $duplicatebiblionumber,
937 duplicatetitle => $duplicatetitle,
941 elsif ( $op eq "delete" ) {
943 my $error = &DelBiblio($biblionumber);
945 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
946 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
950 print $input->redirect('/cgi-bin/koha/catalogue/search.pl' . ($searchid ? "?searchid=$searchid" : ""));
954 #----------------------------------------------------------------------------
955 # If we're in a duplication case, we have to set to "" the biblionumber
956 # as we'll save the biblio as a new one.
958 biblionumberdata => $biblionumber,
961 if ( $op eq "duplicate" ) {
965 if($changed_framework eq "changed"){
966 $record = TransformHtmlToMarc( $input, 1 );
968 elsif( $record ne -1 ) {
969 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
971 my $uxml = $record->as_xml;
972 MARC::Record::default_record_format("UNIMARC")
973 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
974 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
978 build_tabs( $template, $record, $dbh, $encoding,$input );
980 biblionumber => $biblionumber,
981 biblionumbertagfield => $biblionumbertagfield,
982 biblionumbertagsubfield => $biblionumbertagsubfield,
983 biblioitemnumtagfield => $biblioitemnumtagfield,
984 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
985 biblioitemnumber => $biblioitemnumber,
986 hostbiblionumber => $hostbiblionumber,
987 hostitemnumber => $hostitemnumber
991 if ( $record ne '-1' ) {
992 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
993 $template->param( title => $title );
997 frameworkcode => $frameworkcode,
998 itemtype => $frameworkcode,
999 borrowernumber => $loggedinuser,
1000 tab => scalar $input->param('tab')
1002 $template->{'VARS'}->{'searchid'} = $searchid;
1004 output_html_with_http_headers $input, $cookie, $template->output;