bug-1494, fixed bookfund modify code
[koha.git] / cataloguing / addbiblio.pl
blob3dfa1aabb2a6b2640be2456ef66f472a44929ba3
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
21 use strict;
22 use CGI;
23 use C4::Output;
24 use C4::Auth;
25 use C4::Biblio;
26 use C4::Search;
27 use C4::AuthoritiesMarc;
28 use C4::Context;
29 use MARC::Record;
30 use C4::Log;
31 use C4::Koha; # XXX subfield_is_koha_internal_p
32 use C4::Branch; # XXX subfield_is_koha_internal_p
33 use C4::ClassSource;
34 use C4::ImportBatch;
36 use Date::Calc qw(Today);
37 use MARC::File::USMARC;
38 use MARC::File::XML;
40 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
41 MARC::File::XML->default_record_format('UNIMARC');
44 our($tagslib,$authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
46 =item MARCfindbreeding
48 $record = MARCfindbreeding($breedingid);
50 Look up the import record repository for the record with
51 record with id $breedingid. If found, returns the decoded
52 MARC::Record; otherwise, -1 is returned (FIXME).
53 Returns as second parameter the character encoding.
55 =cut
57 sub MARCfindbreeding {
58 my ( $id ) = @_;
59 my ($marc, $encoding) = GetImportRecordMarc($id);
60 # remove the - in isbn, koha store isbn without any -
61 if ($marc) {
62 my $record = MARC::Record->new_from_usmarc($marc);
63 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
64 if ( $record->field($isbnfield) ) {
65 foreach my $field ( $record->field($isbnfield) ) {
66 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
67 my $newisbn = $field->subfield($isbnsubfield);
68 $newisbn =~ s/-//g;
69 $field->update( $isbnsubfield => $newisbn );
73 # fix the unimarc 100 coded field (with unicode information)
74 if (C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield(100,'a')) {
75 my $f100a=$record->subfield(100,'a');
76 my $f100 = $record->field(100);
77 my $f100temp = $f100->as_string;
78 $record->delete_field($f100);
79 if ( length($f100temp) > 28 ) {
80 substr( $f100temp, 26, 2, "50" );
81 $f100->update( 'a' => $f100temp );
82 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
83 $record->insert_fields_ordered($f100);
87 if ( ref($record) eq undef ) {
88 return -1;
90 else {
91 # normalize author : probably UNIMARC specific...
92 if ( C4::Context->preference("z3950NormalizeAuthor")
93 and C4::Context->preference("z3950AuthorAuthFields") )
95 my ( $tag, $subfield ) = GetMarcFromKohaField("biblio.author");
97 # my $summary = C4::Context->preference("z3950authortemplate");
98 my $auth_fields =
99 C4::Context->preference("z3950AuthorAuthFields");
100 my @auth_fields = split /,/, $auth_fields;
101 my $field;
103 if ( $record->field($tag) ) {
104 foreach my $tmpfield ( $record->field($tag)->subfields ) {
106 # foreach my $subfieldcode ($tmpfield->subfields){
107 my $subfieldcode = shift @$tmpfield;
108 my $subfieldvalue = shift @$tmpfield;
109 if ($field) {
110 $field->add_subfields(
111 "$subfieldcode" => $subfieldvalue )
112 if ( $subfieldcode ne $subfield );
114 else {
115 $field =
116 MARC::Field->new( $tag, "", "",
117 $subfieldcode => $subfieldvalue )
118 if ( $subfieldcode ne $subfield );
122 $record->delete_field( $record->field($tag) );
123 foreach my $fieldtag (@auth_fields) {
124 next unless ( $record->field($fieldtag) );
125 my $lastname = $record->field($fieldtag)->subfield('a');
126 my $firstname = $record->field($fieldtag)->subfield('b');
127 my $title = $record->field($fieldtag)->subfield('c');
128 my $number = $record->field($fieldtag)->subfield('d');
129 if ($title) {
131 # $field->add_subfields("$subfield"=>"[ ".ucfirst($title).ucfirst($firstname)." ".$number." ]");
132 $field->add_subfields(
133 "$subfield" => ucfirst($title) . " "
134 . ucfirst($firstname) . " "
135 . $number );
137 else {
139 # $field->add_subfields("$subfield"=>"[ ".ucfirst($firstname).", ".ucfirst($lastname)." ]");
140 $field->add_subfields(
141 "$subfield" => ucfirst($firstname) . ", "
142 . ucfirst($lastname) );
145 $record->insert_fields_ordered($field);
147 return $record, $encoding;
150 return -1;
153 =item build_authorized_values_list
155 =cut
157 sub build_authorized_values_list ($$$$$$$) {
158 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
160 my @authorised_values;
161 my %authorised_lib;
163 # builds list, depending on authorised value...
165 #---- branch
166 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
167 #Use GetBranches($onlymine)
168 my $onlymine=C4::Context->preference('IndependantBranches') &&
169 C4::Context->userenv &&
170 C4::Context->userenv->{flags}!=1 &&
171 C4::Context->userenv->{branch};
172 my $branches = GetBranches($onlymine);
173 my @branchloop;
174 foreach my $thisbranch ( sort keys %$branches ) {
175 push @authorised_values, $thisbranch;
176 $authorised_lib{$thisbranch} = $branches->{$thisbranch}->{'branchname'};
179 #----- itemtypes
181 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
182 my $sth =
183 $dbh->prepare(
184 "select itemtype,description from itemtypes order by description");
185 $sth->execute;
186 push @authorised_values, ""
187 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
189 my $itemtype;
191 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
192 push @authorised_values, $itemtype;
193 $authorised_lib{$itemtype} = $description;
195 $value = $itemtype unless ($value);
197 #---- class_sources
199 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
200 push @authorised_values, ""
201 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
203 my $class_sources = GetClassSources();
205 my $default_source = C4::Context->preference("DefaultClassificationSource");
207 foreach my $class_source (sort keys %$class_sources) {
208 next unless $class_sources->{$class_source}->{'used'} or
209 ($value and $class_source eq $value) or
210 ($class_source eq $default_source);
211 push @authorised_values, $class_source;
212 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
213 $value = $class_source unless ($value);
214 $value = $default_source unless ($value);
216 #---- "true" authorised value
218 else {
219 $authorised_values_sth->execute(
220 $tagslib->{$tag}->{$subfield}->{authorised_value} );
222 push @authorised_values, ""
223 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
225 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
226 push @authorised_values, $value;
227 $authorised_lib{$value} = $lib;
230 return CGI::scrolling_list(
231 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
232 -values => \@authorised_values,
233 -default => $value,
234 -labels => \%authorised_lib,
235 -override => 1,
236 -size => 1,
237 -multiple => 0,
238 -tabindex => 1,
239 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
240 -class => "input_marceditor",
244 =item CreateKey
246 Create a random value to set it into the input name
248 =cut
250 sub CreateKey(){
251 return int(rand(1000000));
254 =item GetMandatoryFieldZ3950
256 This function return an hashref which containts all mandatory field
257 to search with z3950 server.
259 =cut
261 sub GetMandatoryFieldZ3950($){
262 my $frameworkcode = shift;
263 my @isbn = GetMarcFromKohaField('biblioitems.isbn',$frameworkcode);
264 my @title = GetMarcFromKohaField('biblio.title',$frameworkcode);
265 my @author = GetMarcFromKohaField('biblio.author',$frameworkcode);
266 my @issn = GetMarcFromKohaField('biblioitems.issn',$frameworkcode);
268 return {
269 $isbn[0].$isbn[1] => 'isbn',
270 $title[0].$title[1] => 'title',
271 $author[0].$author[1] => 'author',
272 $issn[0].$issn[1] => 'issn',
276 =item create_input
278 builds the <input ...> entry for a subfield.
280 =cut
282 sub create_input {
283 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
285 my $index_subfield = CreateKey(); # create a specifique key for each subfield
287 $value =~ s/"/&quot;/g;
289 # if there is no value provided but a default value in parameters, get it
290 unless ($value) {
291 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
293 # get today date & replace YYYY, MM, DD if provided in the default value
294 my ( $year, $month, $day ) = Today();
295 $month = sprintf( "%02d", $month );
296 $day = sprintf( "%02d", $day );
297 $value =~ s/YYYY/$year/g;
298 $value =~ s/MM/$month/g;
299 $value =~ s/DD/$day/g;
301 my $dbh = C4::Context->dbh;
302 my %subfield_data = (
303 tag => $tag,
304 subfield => $subfield,
305 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
306 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
307 tag_mandatory => $tagslib->{$tag}->{mandatory},
308 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
309 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
310 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
311 index => $index_tag,
312 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
313 value => $value,
314 random => CreateKey(),
316 # deal with a <010 tag
317 if($subfield eq '@'){
318 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
319 } else {
320 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
323 if(exists $mandatory_z3950->{$tag.$subfield}){
324 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
326 # decide if the subfield must be expanded (visible) by default or not
327 # if it is mandatory, then expand. If it is hidden explicitly by the hidden flag, hidden anyway
328 $subfield_data{visibility} = "display:none;"
329 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
330 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
332 # always expand all subfields of a mandatory field
333 $subfield_data{visibility} = "" if $tagslib->{$tag}->{mandatory};
334 # it's an authorised field
335 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
336 $subfield_data{marc_value} =
337 build_authorized_values_list( $tag, $subfield, $value, $dbh,
338 $authorised_values_sth,$index_tag,$index_subfield );
340 # it's a thesaurus / authority field
342 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
343 $subfield_data{marc_value} =
344 "<input type=\"text\"
345 id=\"".$subfield_data{id}."\"
346 name=\"".$subfield_data{id}."\"
347 value=\"$value\"
348 class=\"input_marceditor\"
349 tabindex=\"1\"
350 size=\"67\"
351 maxlength=\"255\"
353 <a href=\"#\" class=\"buttonDot\"
354 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
356 # it's a plugin field
358 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
360 # opening plugin. Just check wether we are on a developper computer on a production one
361 # (the cgidir differs)
362 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
363 unless ( opendir( DIR, "$cgidir" ) ) {
364 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
365 closedir( DIR );
367 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
368 if (do $plugin) {
369 my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
370 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
372 $subfield_data{marc_value} =
373 "<input tabindex=\"1\"
374 type=\"text\"
375 id=\"".$subfield_data{id}."\"
376 name=\"".$subfield_data{id}."\"
377 value=\"$value\"
378 class=\"input_marceditor\"
379 onfocus=\"Focus$function_name($index_tag)\"
380 size=\"67\"
381 maxlength=\"255\"
382 onblur=\"Blur$function_name($index_tag); \" \/>
383 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id})'; return false;)\" title=\"Tag Editor\">...</a>
384 $javascript";
385 } else {
386 warn "Plugin Failed: $plugin";
387 # supply default input form
388 $subfield_data{marc_value} =
389 "<input type=\"text\"
390 id=\"".$subfield_data{id}."\"
391 name=\"".$subfield_data{id}."\"
392 value=\"$value\"
393 tabindex=\"1\"
394 size=\"67\"
395 maxlength=\"255\"
396 class=\"input_marceditor\"
400 # it's an hidden field
402 elsif ( $tag eq '' ) {
403 $subfield_data{marc_value} =
404 "<input tabindex=\"1\"
405 type=\"hidden\"
406 id=\"".$subfield_data{id}."\"
407 name=\"".$subfield_data{id}."\"
408 size=\"67\"
409 maxlength=\"255\"
410 value=\"$value\" \/>
413 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
414 $subfield_data{marc_value} =
415 "<input type=\"text\"
416 id=\"".$subfield_data{id}."\"
417 name=\"".$subfield_data{id}."\"
418 class=\"input_marceditor\"
419 tabindex=\"1\"
420 size=\"67\"
421 maxlength=\"255\"
422 value=\"$value\"
423 \/>";
425 # it's a standard field
427 else {
428 if (
429 length($value) > 100
431 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
432 and $tag < 400 && $subfield eq 'a' )
433 or ( $tag >= 500
434 and $tag < 600
435 && C4::Context->preference("marcflavour") eq "MARC21" )
438 $subfield_data{marc_value} =
439 "<textarea cols=\"70\"
440 rows=\"4\"
441 id=\"".$subfield_data{id}."\"
442 name=\"".$subfield_data{id}."\"
443 class=\"input_marceditor\"
444 tabindex=\"1\"
445 size=\"67\"
446 maxlength=\"255\"
447 >$value</textarea>
450 else {
451 $subfield_data{marc_value} =
452 "<input type=\"text\"
453 id=\"".$subfield_data{id}."\"
454 name=\"".$subfield_data{id}."\"
455 value=\"$value\"
456 tabindex=\"1\"
457 size=\"67\"
458 maxlength=\"255\"
459 class=\"input_marceditor\"
464 $subfield_data{'index_subfield'} = $index_subfield;
465 return \%subfield_data;
468 sub build_tabs ($$$$$) {
469 my ( $template, $record, $dbh, $encoding,$input ) = @_;
471 # fill arrays
472 my @loop_data = ();
473 my $tag;
475 my $authorised_values_sth = $dbh->prepare(
476 "select authorised_value,lib
477 from authorised_values
478 where category=? order by lib"
481 # in this array, we will push all the 10 tabs
482 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
483 my @BIG_LOOP;
484 my %seen;
485 my @tab_data; # all tags to display
487 foreach my $used ( @$usedTagsLib ){
488 push @tab_data,$used->{tagfield} if not $seen{$used->{tagfield}};
489 $seen{$used->{tagfield}}++;
492 my $max_num_tab=-1;
493 foreach(@$usedTagsLib){
494 if($_->{tab} > -1 && $_->{tab} >= $max_num_tab && $_->{tagfield} != '995'){ # FIXME : MARC21 ?
495 $max_num_tab = $_->{tab};
498 if($max_num_tab >= 9){
499 $max_num_tab = 9;
501 # loop through each tab 0 through 9
502 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
503 my @loop_data = (); #innerloop in the template.
504 my $i = 0;
505 foreach my $tag (@tab_data) {
506 $i++;
507 next if ! $tag;
508 my $indicator;
509 my $index_tag = CreateKey;
511 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
512 # if MARC::Record is empty => use tab as master loop.
513 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
514 my @fields;
515 if ( $tag ne '000' ) {
516 @fields = $record->field($tag);
518 else {
519 push @fields, $record->leader(); # if tag == 000
521 # loop through each field
522 foreach my $field (@fields) {
524 my @subfields_data;
525 if ( $tag < 10 ) {
526 my ( $value, $subfield );
527 if ( $tag ne '000' ) {
528 $value = $field->data();
529 $subfield = "@";
531 else {
532 $value = $field;
533 $subfield = '@';
535 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
536 next
537 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
538 'biblio.biblionumber' );
539 push(
540 @subfields_data,
541 &create_input(
542 $tag, $subfield, $value, $index_tag, $tabloop, $record,
543 $authorised_values_sth,$input
547 else {
548 my @subfields = $field->subfields();
549 foreach my $subfieldcount ( 0 .. $#subfields ) {
550 my $subfield = $subfields[$subfieldcount][0];
551 my $value = $subfields[$subfieldcount][1];
552 next if ( length $subfield != 1 );
553 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
554 push(
555 @subfields_data,
556 &create_input(
557 $tag, $subfield, $value, $index_tag, $tabloop,
558 $record, $authorised_values_sth,$input
564 # now, loop again to add parameter subfield that are not in the MARC::Record
565 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
567 next if ( length $subfield != 1 );
568 next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
569 next if ( $tag < 10 );
570 next
571 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
572 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
573 ); #check for visibility flag
574 next if ( defined( $field->subfield($subfield) ) );
575 push(
576 @subfields_data,
577 &create_input(
578 $tag, $subfield, '', $index_tag, $tabloop, $record,
579 $authorised_values_sth,$input
583 if ( $#subfields_data >= 0 ) {
584 # build the tag entry.
585 # note that the random() field is mandatory. Otherwise, on repeated fields, you'll
586 # have twice the same "name" value, and cgi->param() will return only one, making
587 # all subfields to be merged in a single field.
588 my %tag_data = (
589 tag => $tag,
590 index => $index_tag,
591 tag_lib => $tagslib->{$tag}->{lib},
592 repeatable => $tagslib->{$tag}->{repeatable},
593 subfield_loop => \@subfields_data,
594 fixedfield => $tag < 10?1:0,
595 random => CreateKey,
597 if ($tag >= 010){ # no indicator for theses tag
598 $tag_data{indicator} = $field->indicator(1).$field->indicator(2);
600 push( @loop_data, \%tag_data );
602 } # foreach $field end
604 # if breeding is empty
606 else {
607 my @subfields_data;
608 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
609 next if ( length $subfield != 1 );
610 next
611 if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
612 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
613 ; #check for visibility flag
614 next
615 if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
616 push(
617 @subfields_data,
618 &create_input(
619 $tag, $subfield, '', $index_tag, $tabloop, $record,
620 $authorised_values_sth,$input
624 if ( $#subfields_data >= 0 ) {
625 my %tag_data = (
626 tag => $tag,
627 index => $index_tag,
628 tag_lib => $tagslib->{$tag}->{lib},
629 repeatable => $tagslib->{$tag}->{repeatable},
630 indicator => $indicator,
631 subfield_loop => \@subfields_data,
632 tagfirstsubfield => $subfields_data[0],
633 fixedfield => $tag < 10?1:0,
636 push @loop_data, \%tag_data ;
640 if ( $#loop_data >= 0 ) {
641 push @BIG_LOOP, {
642 number => $tabloop,
643 innerloop => \@loop_data,
647 $template->param( BIG_LOOP => \@BIG_LOOP );
650 sub BiblioAddAuthorities{
651 my ( $record, $frameworkcode ) = @_;
652 my $dbh=C4::Context->dbh;
653 my $query=$dbh->prepare(qq|
654 SELECT authtypecode,tagfield
655 FROM marc_subfield_structure
656 WHERE frameworkcode=?
657 AND (authtypecode IS NOT NULL AND authtypecode<>\"\")|);
658 # SELECT authtypecode,tagfield
659 # FROM marc_subfield_structure
660 # WHERE frameworkcode=?
661 # AND (authtypecode IS NOT NULL OR authtypecode<>\"\")|);
662 $query->execute($frameworkcode);
663 my ($countcreated,$countlinked);
664 while (my $data=$query->fetchrow_hashref){
665 if ($record->field($data->{tagfield})){
666 next if ($record->subfield($data->{tagfield},'3')||$record->subfield($data->{tagfield},'9'));
667 # No authorities id in the tag.
668 # Search if there is any authorities to link to.
669 my $query='at='.$data->{authtypecode}.' ';
670 map {$query.= " and he=".$_->[1] if ($_->[0]=~/[A-z]/)} $record->field($data->{tagfield})->subfields();
671 my ($error,$results)=SimpleSearch($query,"authorityserver");
672 # there is at least 1 result => return the 1st one
673 if (@$results>1) {
674 my $marcrecord = MARC::File::USMARC::decode($results->[0]);
675 $record->field($data->{tagfield})->add_subfields('9'=>$marcrecord->field('001')->data);
676 $countlinked++;
677 } else {
678 #There are no results, build authority record, add it to Authorities, get authid and add it to 9
679 ###NOTICE : This is only valid if a subfield is linked to one and only one authtypecode
681 my $authtypedata=GetAuthType($data->{authtypecode});
682 my $marcrecordauth=MARC::Record->new();
683 my $field=MARC::Field->new($authtypedata->{auth_tag_to_report},'','',"a"=>"".$record->subfield($data->{tagfield},'a'));
684 map { $field->add_subfields($_->[0]=>$_->[1]) if ($_->[0]=~/[A-z]/ && $_->[0] ne "a" )} $record->field($data->{tagfield})->subfields();
685 $marcrecordauth->insert_fields_ordered($field);
686 my $authid=AddAuthority($marcrecordauth,'',$data->{authtypecode});
687 $countcreated++;
688 $record->field($data->{tagfield})->add_subfields('9'=>$authid);
692 return ($countlinked,$countcreated);
695 # ========================
696 # MAIN
697 #=========================
698 my $input = new CGI;
699 my $error = $input->param('error');
700 my $biblionumber = $input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
701 my $breedingid = $input->param('breedingid');
702 my $z3950 = $input->param('z3950');
703 my $op = $input->param('op');
704 my $mode = $input->param('mode');
705 my $frameworkcode = $input->param('frameworkcode');
706 my $dbh = C4::Context->dbh;
708 $frameworkcode = &GetFrameworkCode($biblionumber)
709 if ( $biblionumber and not($frameworkcode) );
711 $frameworkcode = '' if ( $frameworkcode eq 'Default' );
712 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
714 template_name => "cataloguing/addbiblio.tmpl",
715 query => $input,
716 type => "intranet",
717 authnotrequired => 0,
718 flagsrequired => { editcatalogue => 1 },
722 #Getting the list of all frameworks
723 my $queryfwk = $dbh->prepare("select frameworktext, frameworkcode from biblio_framework");
724 $queryfwk->execute;
725 my %select_fwk;
726 my @select_fwk;
727 my $curfwk;
728 push @select_fwk, "Default";
729 $select_fwk{"Default"} = "Default";
731 while ( my ( $description, $fwk ) = $queryfwk->fetchrow ) {
732 push @select_fwk, $fwk;
733 $select_fwk{$fwk} = $description;
735 $curfwk = $frameworkcode;
736 my $framework = CGI::scrolling_list(
737 -name => 'Frameworks',
738 -id => 'Frameworks',
739 -default => $curfwk,
740 -onchange => 'Changefwk(this);',
741 -values => \@select_fwk,
742 -labels => \%select_fwk,
743 -size => 1,
744 -multiple => 0
746 $template->param( framework => $framework, breedingid => $breedingid );
748 # ++ Global
749 $tagslib = &GetMarcStructure( 1, $frameworkcode );
750 $usedTagsLib = &GetUsedMarcStructure( $frameworkcode );
751 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode);
752 # -- Global
754 my $record = -1;
755 my $encoding = "";
756 my (
757 $biblionumbertagfield,
758 $biblionumbertagsubfield,
759 $biblioitemnumtagfield,
760 $biblioitemnumtagsubfield,
761 $bibitem,
762 $biblioitemnumber
765 if (($biblionumber) && !($breedingid)){
766 $record = GetMarcBiblio($biblionumber);
768 if ($breedingid) {
769 ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
772 $is_a_modif = 0;
774 if ($biblionumber) {
775 $is_a_modif = 1;
777 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
778 ( $biblionumbertagfield, $biblionumbertagsubfield ) =
779 &GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
780 ( $biblioitemnumtagfield, $biblioitemnumtagsubfield ) =
781 &GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
783 # search biblioitems value
784 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
785 $sth->execute($biblionumber);
786 ($biblioitemnumber) = $sth->fetchrow;
789 #-------------------------------------------------------------------------------------
790 if ( $op eq "addbiblio" ) {
791 #-------------------------------------------------------------------------------------
792 # getting html input
793 my @params = $input->param();
794 $record = TransformHtmlToMarc( \@params , $input );
795 # check for a duplicate
796 my ($duplicatebiblionumber,$duplicatetitle) = FindDuplicate($record) if (!$is_a_modif);
797 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
798 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
799 if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
800 my $oldbibnum;
801 my $oldbibitemnum;
802 if (C4::Context->preference("BiblioAddsAuthorities")){
803 my ($countlinked,$countcreated)=BiblioAddAuthorities($record,$frameworkcode);
805 if ( $is_a_modif ) {
806 ModBiblioframework( $biblionumber, $frameworkcode );
807 ModBiblio( $record, $biblionumber, $frameworkcode );
809 else {
810 ( $biblionumber, $oldbibitemnum ) = AddBiblio( $record, $frameworkcode );
813 if ($mode ne "popup"){
814 print $input->redirect(
815 "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode"
817 exit;
818 } else {
819 $template->param(
820 biblionumber => $biblionumber,
821 done =>1,
822 popup =>1
824 $template->param( title => $record->subfield('200',"a") ) if ($record ne "-1" && C4::Context->preference('marcflavour') =~/unimarc/i);
825 $template->param( title => $record->title() ) if ($record ne "-1" && C4::Context->preference('marcflavour') eq "usmarc");
826 $template->param(
827 popup => $mode,
828 itemtype => $frameworkcode,
830 output_html_with_http_headers $input, $cookie, $template->output;
831 exit;
833 } else {
834 # it may be a duplicate, warn the user and do nothing
835 build_tabs ($template, $record, $dbh,$encoding,$input);
836 $template->param(
837 biblionumber => $biblionumber,
838 biblioitemnumber => $biblioitemnumber,
839 duplicatebiblionumber => $duplicatebiblionumber,
840 duplicatebibid => $duplicatebiblionumber,
841 duplicatetitle => $duplicatetitle,
845 elsif ( $op eq "delete" ) {
847 my $error = &DelBiblio($biblionumber);
848 if ($error) {
849 warn "ERROR when DELETING BIBLIO $biblionumber : $error";
850 print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
851 exit;
854 print $input->redirect('/cgi-bin/koha/catalogue/search.pl');
855 exit;
857 } else {
858 #----------------------------------------------------------------------------
859 # If we're in a duplication case, we have to set to "" the biblionumber
860 # as we'll save the biblio as a new one.
861 if ( $op eq "duplicate" ) {
862 $biblionumber = "";
865 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
866 eval {
867 my $uxml = $record->as_xml;
868 MARC::Record::default_record_format("UNIMARC")
869 if ( C4::Context->preference("marcflavour") eq "UNIMARC" );
870 my $urecord = MARC::Record::new_from_xml( $uxml, 'UTF-8' );
871 $record = $urecord;
873 build_tabs( $template, $record, $dbh, $encoding,$input );
874 $template->param(
875 biblionumber => $biblionumber,
876 biblionumbertagfield => $biblionumbertagfield,
877 biblionumbertagsubfield => $biblionumbertagsubfield,
878 biblioitemnumtagfield => $biblioitemnumtagfield,
879 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
880 biblioitemnumber => $biblioitemnumber,
884 $template->param( title => $record->title() ) if ( $record ne "-1" );
885 $template->param(
886 popup => $mode,
887 frameworkcode => $frameworkcode,
888 itemtype => $frameworkcode,
891 output_html_with_http_headers $input, $cookie, $template->output;