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 );
39 use C4::Search qw( FindDuplicate enabled_staff_search_views );
40 use C4::Auth qw( get_template_and_user haspermission );
43 use C4::ClassSource qw( GetClassSources );
44 use C4::ImportBatch qw( GetImportRecordMarc );
45 use C4::Charset qw( SetMarcUnicodeFlag );
46 use Koha::BiblioFrameworks;
47 use Koha::DateUtils qw( dt_from_string );
53 use Koha::BiblioFrameworks;
56 use MARC::File::USMARC;
58 use URI::Escape qw( uri_escape_utf8 );
60 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
61 MARC::File::XML->default_record_format('UNIMARC');
64 our($tagslib,$authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
68 =head2 MARCfindbreeding
70 $record = MARCfindbreeding($breedingid);
72 Look up the import record repository for the record with
73 record with id $breedingid. If found, returns the decoded
74 MARC::Record; otherwise, -1 is returned (FIXME).
75 Returns as second parameter the character encoding.
79 sub MARCfindbreeding {
81 my ($marc, $encoding) = GetImportRecordMarc($id);
82 # remove the - in isbn, koha store isbn without any -
84 my $record = MARC::Record->new_from_usmarc($marc);
85 if(C4::Context->preference('autoControlNumber') eq 'biblionumber'){
86 my @control_num = $record->field('001');
87 $record->delete_fields(@control_num);
89 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField( 'biblioitems.isbn' );
90 if ( $record->field($isbnfield) ) {
91 foreach my $field ( $record->field($isbnfield) ) {
92 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
93 my $newisbn = $field->subfield($isbnsubfield);
95 $field->update( $isbnsubfield => $newisbn );
99 # fix the unimarc 100 coded field (with unicode information)
100 if (C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield(100,'a')) {
101 my $f100a=$record->subfield(100,'a');
102 my $f100 = $record->field(100);
103 my $f100temp = $f100->as_string;
104 $record->delete_field($f100);
105 if ( length($f100temp) > 28 ) {
106 substr( $f100temp, 26, 2, "50" );
107 $f100->update( 'a' => $f100temp );
108 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
109 $record->insert_fields_ordered($f100);
113 if ( !defined(ref($record)) ) {
117 # normalize author : UNIMARC specific...
118 if ( C4::Context->preference("z3950NormalizeAuthor")
119 and C4::Context->preference("z3950AuthorAuthFields")
120 and C4::Context->preference("marcflavour") eq 'UNIMARC' )
122 my ( $tag, $subfield ) = GetMarcFromKohaField( "biblio.author" );
125 C4::Context->preference("z3950AuthorAuthFields");
126 my @auth_fields = split /,/, $auth_fields;
129 if ( $record->field($tag) ) {
130 foreach my $tmpfield ( $record->field($tag)->subfields ) {
132 my $subfieldcode = shift @$tmpfield;
133 my $subfieldvalue = shift @$tmpfield;
135 $field->add_subfields(
136 "$subfieldcode" => $subfieldvalue )
137 if ( $subfieldcode ne $subfield );
141 MARC::Field->new( $tag, "", "",
142 $subfieldcode => $subfieldvalue )
143 if ( $subfieldcode ne $subfield );
147 $record->delete_field( $record->field($tag) );
148 foreach my $fieldtag (@auth_fields) {
149 next unless ( $record->field($fieldtag) );
150 my $lastname = $record->field($fieldtag)->subfield('a');
151 my $firstname = $record->field($fieldtag)->subfield('b');
152 my $title = $record->field($fieldtag)->subfield('c');
153 my $number = $record->field($fieldtag)->subfield('d');
155 $field->add_subfields(
156 "$subfield" => ucfirst($title) . " "
157 . ucfirst($firstname) . " "
161 $field->add_subfields(
162 "$subfield" => ucfirst($firstname) . ", "
163 . ucfirst($lastname) );
166 $record->insert_fields_ordered($field);
168 return $record, $encoding;
174 =head2 build_authorized_values_list
178 sub build_authorized_values_list {
179 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
181 my @authorised_values;
184 # builds list, depending on authorised value...
187 my $category = $tagslib->{$tag}->{$subfield}->{authorised_value};
188 if ( $category eq "branches" ) {
189 my $libraries = Koha::Libraries->search_filtered({}, {order_by => ['branchname']});
190 while ( my $l = $libraries->next ) {
191 push @authorised_values, $l->branchcode;;
192 $authorised_lib{$l->branchcode} = $l->branchname;
195 elsif ( $category eq "itemtypes" ) {
196 push @authorised_values, "";
199 my $itemtypes = Koha::ItemTypes->search_with_localization;
200 while ( $itemtype = $itemtypes->next ) {
201 push @authorised_values, $itemtype->itemtype;
202 $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
204 $value = $itemtype unless ($value);
206 elsif ( $category eq "cn_source" ) {
207 push @authorised_values, "";
209 my $class_sources = GetClassSources();
211 my $default_source = C4::Context->preference("DefaultClassificationSource");
213 foreach my $class_source (sort keys %$class_sources) {
214 next unless $class_sources->{$class_source}->{'used'} or
215 ($value and $class_source eq $value) or
216 ($class_source eq $default_source);
217 push @authorised_values, $class_source;
218 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
220 $value = $default_source unless $value;
223 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
224 $authorised_values_sth->execute(
225 $tagslib->{$tag}->{$subfield}->{authorised_value},
226 $branch_limit ? $branch_limit : (),
229 push @authorised_values, "";
231 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
232 push @authorised_values, $value;
233 $authorised_lib{$value} = $lib;
236 $authorised_values_sth->finish;
240 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
241 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
243 values => \@authorised_values,
244 labels => \%authorised_lib,
245 ( ( grep { $_ eq $category } ( qw(branches itemtypes cn_source) ) ) ? () : ( category => $category ) ),
252 Create a random value to set it into the input name
257 return int(rand(1000000));
260 =head2 GetMandatoryFieldZ3950
262 This function returns a hashref which contains all mandatory field
263 to search with z3950 server.
267 sub GetMandatoryFieldZ3950 {
268 my $frameworkcode = shift;
269 my @isbn = GetMarcFromKohaField( 'biblioitems.isbn' );
270 my @title = GetMarcFromKohaField( 'biblio.title' );
271 my @author = GetMarcFromKohaField( 'biblio.author' );
272 my @issn = GetMarcFromKohaField( 'biblioitems.issn' );
273 my @lccn = GetMarcFromKohaField( 'biblioitems.lccn' );
276 $isbn[0].$isbn[1] => 'isbn',
277 $title[0].$title[1] => 'title',
278 $author[0].$author[1] => 'author',
279 $issn[0].$issn[1] => 'issn',
280 $lccn[0].$lccn[1] => 'lccn',
286 builds the <input ...> entry for a subfield.
291 my ( $tag, $subfield, $value, $index_tag, $rec, $authorised_values_sth,$cgi ) = @_;
293 my $index_subfield = CreateKey(); # create a specifique key for each subfield
295 # Apply optional framework default value when it is a new record,
296 # or when editing as new (duplicating a record),
297 # or when changing a record's framework,
298 # based on the ApplyFrameworkDefaults setting.
299 # Substitute date parts, user name
300 my $applydefaults = C4::Context->preference('ApplyFrameworkDefaults');
301 if ( $value eq '' && (
302 ( $applydefaults =~ /new/ && !$cgi->param('biblionumber') ) ||
303 ( $applydefaults =~ /duplicate/ && $cgi->param('op') eq 'duplicate' ) ||
304 ( $applydefaults =~ /changed/ && $cgi->param('changed_framework') )
306 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue} // q{};
308 # get today date & replace <<YYYY>>, <<YY>>, <<MM>>, <<DD>> if provided in the default value
309 my $today_dt = dt_from_string;
310 my $year = $today_dt->strftime('%Y');
311 my $shortyear = $today_dt->strftime('%y');
312 my $month = $today_dt->strftime('%m');
313 my $day = $today_dt->strftime('%d');
314 $value =~ s/<<YYYY>>/$year/g;
315 $value =~ s/<<YY>>/$shortyear/g;
316 $value =~ s/<<MM>>/$month/g;
317 $value =~ s/<<DD>>/$day/g;
318 # And <<USER>> with surname (?)
319 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
320 $value=~s/<<USER>>/$username/g;
323 my $dbh = C4::Context->dbh;
325 # map '@' as "subfield" label for fixed fields
326 # to something that's allowed in a div id.
327 my $id_subfield = $subfield;
328 $id_subfield = "00" if $id_subfield eq "@";
330 my %subfield_data = (
332 subfield => $id_subfield,
333 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
334 tag_mandatory => $tagslib->{$tag}->{mandatory},
335 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
336 important => $tagslib->{$tag}->{$subfield}->{important},
337 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
338 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
340 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
342 maxlength => $tagslib->{$tag}->{$subfield}->{maxlength},
343 random => CreateKey(),
346 if(exists $mandatory_z3950->{$tag.$subfield}){
347 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
349 # Subfield is hidden depending of hidden and mandatory flag, and is always
350 # shown if it contains anything or if its field is mandatory or important.
351 my $tdef = $tagslib->{$tag};
352 $subfield_data{visibility} = "display:none;"
353 if $tdef->{$subfield}->{hidden} % 2 == 1 &&
355 !$tdef->{$subfield}->{mandatory} &&
356 !$tdef->{mandatory} &&
357 !$tdef->{$subfield}->{important} &&
359 # expand all subfields of 773 if there is a host item provided in the input
360 $subfield_data{visibility} ="" if ($tag eq 773 and $cgi->param('hostitemnumber'));
363 # it's an authorised field
364 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
365 $subfield_data{marc_value} =
366 build_authorized_values_list( $tag, $subfield, $value, $dbh,
367 $authorised_values_sth,$index_tag,$index_subfield );
369 # it's a subfield $9 linking to an authority record - see bug 2206 and 28022
371 elsif ($subfield eq "9" and
372 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
373 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
374 $tagslib->{$tag}->{'a'}->{authtypecode} ne '' and
375 $tagslib->{$tag}->{'a'}->{hidden} > -4 and
376 $tagslib->{$tag}->{'a'}->{hidden} < 5) {
377 $subfield_data{marc_value} = {
379 id => $subfield_data{id},
380 name => $subfield_data{id},
383 maxlength => $subfield_data{maxlength},
387 # it's a thesaurus / authority field
389 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
390 # when authorities auto-creation is allowed, do not set readonly
391 my $is_readonly = C4::Context->preference("RequireChoosingExistingAuthority");
393 $subfield_data{marc_value} = {
395 id => $subfield_data{id},
396 name => $subfield_data{id},
399 maxlength => $subfield_data{maxlength},
400 readonly => ($is_readonly) ? 1 : 0,
401 authtype => $tagslib->{$tag}->{$subfield}->{authtypecode},
404 # it's a plugin field
405 } elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
406 require Koha::FrameworkPlugin;
407 my $plugin = Koha::FrameworkPlugin->new( {
408 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
410 my $pars= { dbh => $dbh, record => $rec, tagslib => $tagslib,
411 id => $subfield_data{id} };
412 $plugin->build( $pars );
413 if( !$plugin->errstr ) {
414 $subfield_data{marc_value} = {
415 type => 'text_complex',
416 id => $subfield_data{id},
417 name => $subfield_data{id},
420 maxlength => $subfield_data{maxlength},
421 javascript => $plugin->javascript,
422 plugin => $plugin->name,
423 noclick => $plugin->noclick,
426 warn $plugin->errstr;
427 # supply default input form
428 $subfield_data{marc_value} = {
430 id => $subfield_data{id},
431 name => $subfield_data{id},
434 maxlength => $subfield_data{maxlength},
439 # it's an hidden field
440 } elsif ( $tag eq '' ) {
441 $subfield_data{marc_value} = {
443 id => $subfield_data{id},
444 name => $subfield_data{id},
447 maxlength => $subfield_data{maxlength},
452 # it's a standard field
456 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
457 and $tag < 400 && $subfield eq 'a' )
460 && C4::Context->preference("marcflavour") eq "MARC21" )
463 $subfield_data{marc_value} = {
465 id => $subfield_data{id},
466 name => $subfield_data{id},
472 $subfield_data{marc_value} = {
474 id => $subfield_data{id},
475 name => $subfield_data{id},
478 maxlength => $subfield_data{maxlength},
484 $subfield_data{'index_subfield'} = $index_subfield;
485 return \%subfield_data;
489 =head2 format_indicator
491 Translate indicator value for output form - specifically, map
492 indicator = ' ' to ''. This is for the convenience of a cataloger
493 using a mouse to select an indicator input.
497 sub format_indicator {
498 my $ind_value = shift;
499 return '' if not defined $ind_value;
500 return '' if $ind_value eq ' ';
505 my ( $template, $record, $dbh, $encoding,$input ) = @_;
511 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
512 my $query = "SELECT authorised_value, lib
513 FROM authorised_values";
514 $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id )} if $branch_limit;
515 $query .= " WHERE category = ?";
516 $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
517 $query .= " GROUP BY authorised_value,lib ORDER BY lib, lib_opac";
518 my $authorised_values_sth = $dbh->prepare( $query );
520 # in this array, we will push all the 10 tabs
521 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
524 my @tab_data; # all tags to display
527 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" );
528 foreach my $used ( @$usedTagsLib ){
530 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
531 $seen{$used->{tagfield}}++;
533 if ( $used->{tab} > -1
534 && $used->{tab} >= $max_num_tab
535 && $used->{tagfield} ne $itemtag )
537 $max_num_tab = $used->{tab};
540 if($max_num_tab >= 9){
543 # loop through each tab 0 through 9
544 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
545 my @loop_data = (); #innerloop in the template.
547 foreach my $tag (sort @tab_data) {
550 my ($indicator1, $indicator2);
551 my $index_tag = CreateKey;
553 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
554 # if MARC::Record is empty => use tab as master loop.
555 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
557 if ( $tag ne '000' ) {
558 @fields = $record->field($tag);
561 push @fields, $record->leader(); # if tag == 000
563 # loop through each field
564 foreach my $field (@fields) {
568 my ( $value, $subfield );
569 if ( $tag ne '000' ) {
570 $value = $field->data();
577 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
579 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
580 'biblio.biblionumber' );
584 $tag, $subfield, $value, $index_tag, $record,
585 $authorised_values_sth,$input
590 my @subfields = $field->subfields();
591 foreach my $subfieldcount ( 0 .. $#subfields ) {
592 my $subfield = $subfields[$subfieldcount][0];
593 my $value = $subfields[$subfieldcount][1];
594 next if ( length $subfield != 1 );
595 next if ( !defined $tagslib->{$tag}->{$subfield} || $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
599 $tag, $subfield, $value, $index_tag,
600 $record, $authorised_values_sth,$input
606 # now, loop again to add parameter subfield that are not in the MARC::Record
607 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
609 next if ( length $subfield != 1 );
610 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
611 next if ( $tag < 10 );
613 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
614 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 ) )
615 and not ( $subfield eq "9" and
616 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
617 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
618 $tagslib->{$tag}->{'a'}->{authtypecode} ne "" and
619 $tagslib->{$tag}->{'a'}->{hidden} > -4 and
620 $tagslib->{$tag}->{'a'}->{hidden} < 5
622 ; #check for visibility flag
623 # if subfield is $9 in a field whose $a is authority-controlled,
624 # always include in the form regardless of the hidden setting - bug 2206 and 28022
625 next if ( defined( $field->subfield($subfield) ) );
629 $tag, $subfield, '', $index_tag, $record,
630 $authorised_values_sth,$input
634 if ( $#subfields_data >= 0 ) {
635 # build the tag entry.
636 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
637 # have twice the same "name" value, and cgi->param() will return only one, making
638 # all subfields to be merged in a single field.
642 tag_lib => $tagslib->{$tag}->{lib},
643 repeatable => $tagslib->{$tag}->{repeatable},
644 mandatory => $tagslib->{$tag}->{mandatory},
645 important => $tagslib->{$tag}->{important},
646 subfield_loop => \@subfields_data,
647 fixedfield => $tag < 10?1:0,
650 if ($tag >= 10){ # no indicator for 00x tags
651 $tag_data{indicator1} = format_indicator($field->indicator(1)),
652 $tag_data{indicator2} = format_indicator($field->indicator(2)),
654 push( @loop_data, \%tag_data );
656 } # foreach $field end
658 # if breeding is empty
662 foreach my $subfield (
663 sort { $a->{display_order} <=> $b->{display_order} || $a->{subfield} cmp $b->{subfield} }
664 grep { ref($_) && %$_ } # Not a subfield (values for "important", "lib", "mandatory", etc.) or empty
665 values %{ $tagslib->{$tag} } )
668 if ( ( $subfield->{hidden} <= -4 )
669 or ( $subfield->{hidden} >= 5 ) )
670 and not ( $subfield->{subfield} eq "9" and
671 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
672 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
673 $tagslib->{$tag}->{'a'}->{authtypecode} ne "" and
674 $tagslib->{$tag}->{'a'}->{hidden} > -4 and
675 $tagslib->{$tag}->{'a'}->{hidden} < 5
677 ; #check for visibility flag
678 # if subfield is $9 in a field whose $a is authority-controlled,
679 # always include in the form regardless of the hidden setting - bug 2206 and 28022
681 if ( $subfield->{tab} ne $tabloop );
685 $tag, $subfield->{subfield}, '', $index_tag, $record,
686 $authorised_values_sth,$input
690 if ( $#subfields_data >= 0 ) {
694 tag_lib => $tagslib->{$tag}->{lib},
695 repeatable => $tagslib->{$tag}->{repeatable},
696 mandatory => $tagslib->{$tag}->{mandatory},
697 important => $tagslib->{$tag}->{important},
698 indicator1 => ( $indicator1 || $tagslib->{$tag}->{ind1_defaultvalue} ), #if not set, try to load the default value
699 indicator2 => ( $indicator2 || $tagslib->{$tag}->{ind2_defaultvalue} ), #use short-circuit operator for efficiency
700 subfield_loop => \@subfields_data,
701 tagfirstsubfield => $subfields_data[0],
702 fixedfield => $tag < 10?1:0,
705 push @loop_data, \%tag_data ;
709 if ( $#loop_data >= 0 ) {
712 innerloop => \@loop_data,
716 $authorised_values_sth->finish;
717 $template->param( BIG_LOOP => \@BIG_LOOP );
720 # ========================
722 #=========================
723 my $input = CGI->new;
724 my $error = $input->param('error');
725 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
726 my $parentbiblio = $input->param('parentbiblionumber');
727 my $breedingid = $input->param('breedingid');
728 my $z3950 = $input->param('z3950');
729 my $op = $input->param('op') // q{};
730 my $mode = $input->param('mode') // q{};
731 my $frameworkcode = $input->param('frameworkcode');
732 my $redirect = $input->param('redirect');
733 my $searchid = $input->param('searchid') // "";
734 my $dbh = C4::Context->dbh;
735 my $hostbiblionumber = $input->param('hostbiblionumber');
736 my $hostitemnumber = $input->param('hostitemnumber');
737 # fast cataloguing datas in transit
738 my $fa_circborrowernumber = $input->param('circborrowernumber');
739 my $fa_barcode = $input->param('barcode');
740 my $fa_branch = $input->param('branch');
741 my $fa_stickyduedate = $input->param('stickyduedate');
742 my $fa_duedatespec = $input->param('duedatespec');
744 my $userflags = 'edit_catalogue';
746 my $changed_framework = $input->param('changed_framework') // q{};
747 $frameworkcode = &GetFrameworkCode($biblionumber)
748 if ( $biblionumber and not( defined $frameworkcode) and $op ne 'addbiblio' );
750 if ($frameworkcode eq 'FA'){
751 $userflags = 'fast_cataloging';
754 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
755 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
757 template_name => "cataloguing/addbiblio.tt",
760 flagsrequired => { editcatalogue => $userflags },
766 $biblio = Koha::Biblios->find($biblionumber);
768 $biblionumber = undef;
769 $template->param( bib_doesnt_exist => 1 );
773 if ($frameworkcode eq 'FA'){
774 # We need to grab and set some variables in the template for use on the additems screen
776 'circborrowernumber' => $fa_circborrowernumber,
777 'barcode' => $fa_barcode,
778 'branch' => $fa_branch,
779 'stickyduedate' => $fa_stickyduedate,
780 'duedatespec' => $fa_duedatespec,
782 } elsif ( $op ne "delete" &&
783 C4::Context->preference('EnableAdvancedCatalogingEditor') &&
784 C4::Auth::haspermission(C4::Context->userenv->{id},{'editcatalogue'=>'advanced_editor'}) &&
785 $input->cookie( 'catalogue_editor_' . $loggedinuser ) eq 'advanced' &&
787 # Only use the advanced editor for non-fast-cataloging.
788 # breedingid is not handled because those would only come off a Z39.50
789 # search initiated by the basic editor.
790 print $input->redirect( '/cgi-bin/koha/cataloguing/editor.pl' . ( $biblionumber ? ( ($op eq 'duplicate'?'#duplicate/':'#catalog/') . $biblionumber ) : '' ) );
794 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
796 frameworks => $frameworks,
797 breedingid => $breedingid,
801 $tagslib = &GetMarcStructure( 1, $frameworkcode );
802 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
803 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
809 $biblionumbertagfield,
810 $biblionumbertagsubfield,
811 $biblioitemnumtagfield,
812 $biblioitemnumtagsubfield,
816 if ( $biblio && !$breedingid ) {
817 $record = $biblio->metadata->record;
820 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
822 if ( $record && $op eq 'duplicate' &&
823 C4::Context->preference('autoControlNumber') eq 'biblionumber' ){
824 my @control_num = $record->field('001');
825 $record->delete_fields(@control_num);
827 #populate hostfield if hostbiblionumber is available
828 if ($hostbiblionumber) {
829 my $marcflavour = C4::Context->preference("marcflavour");
830 $record = MARC::Record->new();
833 PrepHostMarcField( $hostbiblionumber, $hostitemnumber, $marcflavour );
834 $record->append_fields($field);
837 # This is a child record
839 my $marcflavour = C4::Context->preference('marcflavour');
840 $record = MARC::Record->new();
841 SetMarcUnicodeFlag($record, $marcflavour);
842 my $hostfield = prepare_host_field($parentbiblio,$marcflavour);
844 $record->append_fields($hostfield);
852 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
853 $template->param( title => $title );
855 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
856 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
857 &GetMarcFromKohaField( "biblio.biblionumber" );
858 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
859 &GetMarcFromKohaField( "biblioitems.biblioitemnumber" );
861 # search biblioitems value
862 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
863 $sth->execute($biblionumber);
864 ($biblioitemnumber) = $sth->fetchrow;
867 #-------------------------------------------------------------------------------------
868 if ( $op eq "addbiblio" ) {
869 #-------------------------------------------------------------------------------------
871 biblionumberdata => $biblionumber,
874 my @params = $input->multi_param();
875 $record = TransformHtmlToMarc( $input, 1 );
876 # check for a duplicate
877 my ( $duplicatebiblionumber, $duplicatetitle );
878 if ( !$is_a_modif ) {
879 ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($record);
881 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
882 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
883 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
886 my $member = Koha::Patrons->find($loggedinuser);
893 source => $z3950 ? 'z39.50' : 'intranet',
894 categorycode => $member->categorycode,
895 userid => $member->userid
901 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
903 if ($redirect eq "items" || ($mode ne "popup" && !$is_a_modif && $redirect ne "view" && $redirect ne "just_save")){
904 if ($frameworkcode eq 'FA'){
905 print $input->redirect(
906 '/cgi-bin/koha/cataloguing/additem.pl?'
907 .'biblionumber='.$biblionumber
908 .'&frameworkcode='.$frameworkcode
909 .'&circborrowernumber='.$fa_circborrowernumber
910 .'&branch='.$fa_branch
911 .'&barcode='.uri_escape_utf8($fa_barcode)
912 .'&stickyduedate='.$fa_stickyduedate
913 .'&duedatespec='.$fa_duedatespec
918 print $input->redirect(
919 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid"
924 elsif(($is_a_modif || $redirect eq "view") && $redirect ne "just_save"){
925 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
926 my $views = { C4::Search::enabled_staff_search_views };
927 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
928 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
929 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
930 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
931 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
932 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
934 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
939 elsif ($redirect eq "just_save"){
940 my $tab = $input->param('current_tab');
941 print $input->redirect("/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber&framework=$frameworkcode&tab=$tab&searchid=$searchid");
945 biblionumber => $biblionumber,
949 if ( $record ne '-1' ) {
950 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
951 $template->param( title => $title );
955 itemtype => $frameworkcode,
957 output_html_with_http_headers $input, $cookie, $template->output;
961 # it may be a duplicate, warn the user and do nothing
962 build_tabs ($template, $record, $dbh,$encoding,$input);
964 biblionumber => $biblionumber,
965 biblioitemnumber => $biblioitemnumber,
966 duplicatebiblionumber => $duplicatebiblionumber,
967 duplicatebibid => $duplicatebiblionumber,
968 duplicatetitle => $duplicatetitle,
972 elsif ( $op eq "delete" ) {
974 my $error = &DelBiblio($biblionumber);
976 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
977 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
981 print $input->redirect('/cgi-bin/koha/catalogue/search.pl' . ($searchid ? "?searchid=$searchid" : ""));
985 #----------------------------------------------------------------------------
986 # If we're in a duplication case, we have to set to "" the biblionumber
987 # as we'll save the biblio as a new one.
989 biblionumberdata => $biblionumber,
993 if ( $op eq "duplicate" ) {
997 if($changed_framework eq "changed"){
998 $record = TransformHtmlToMarc( $input, 1 );
1000 elsif( $record ne -1 ) {
1001 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
1003 my $uxml = $record->as_xml;
1004 MARC::Record::default_record_format("UNIMARC")
1005 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
1006 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
1010 build_tabs( $template, $record, $dbh, $encoding,$input );
1012 biblionumber => $biblionumber,
1013 biblionumbertagfield => $biblionumbertagfield,
1014 biblionumbertagsubfield => $biblionumbertagsubfield,
1015 biblioitemnumtagfield => $biblioitemnumtagfield,
1016 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
1017 biblioitemnumber => $biblioitemnumber,
1018 hostbiblionumber => $hostbiblionumber,
1019 hostitemnumber => $hostitemnumber
1023 if ( $record ne '-1' ) {
1024 my $title = C4::Context->preference('marcflavour') eq "UNIMARC" ? $record->subfield('200', 'a') : $record->title;
1025 $template->param( title => $title );
1029 frameworkcode => $frameworkcode,
1030 itemtype => $frameworkcode,
1031 borrowernumber => $loggedinuser,
1032 tab => scalar $input->param('tab')
1034 $template->{'VARS'}->{'searchid'} = $searchid;
1036 output_html_with_http_headers $input, $cookie, $template->output;