rel_3_0 moved to HEAD
[koha.git] / cataloguing / addbiblio.pl
1 #!/usr/bin/perl 
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use CGI;
24 use C4::Auth;
25 use C4::Output;
26 use C4::Interface::CGI::Output;
27 use C4::Biblio;
28 use C4::Search;
29 use C4::Context;
30 use MARC::Record;
31 use C4::Log;
32 use C4::Koha; # XXX subfield_is_koha_internal_p
33
34 use MARC::File::USMARC;
35 use MARC::File::XML;
36 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
37     MARC::File::XML->default_record_format( 'UNIMARC' );
38 }
39
40 use vars qw( $tagslib);
41 use vars qw( $authorised_values_sth);
42 use vars qw( $is_a_modif );
43
44 my $itemtype; # created here because it can be used in build_authorized_values_list sub
45
46 =item find_value
47
48     ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
49
50 Find the given $subfield in the given $tag in the given
51 MARC::Record $record.  If the subfield is found, returns
52 the (indicators, value) pair; otherwise, (undef, undef) is
53 returned.
54
55 =cut
56
57 sub find_value {
58     my ($tagfield,$insubfield,$record,$encoding) = @_;
59     my @result;
60     my $indicator;
61     if ($tagfield <10) {
62         if ($record->field($tagfield)) {
63             push @result, $record->field($tagfield)->data();
64         } else {
65             push @result,"";
66         }
67     } else {
68         foreach my $field ($record->field($tagfield)) {
69             my @subfields = $field->subfields();
70             foreach my $subfield (@subfields) {
71                 if (@$subfield[0] eq $insubfield) {
72                     push @result,@$subfield[1];
73                     $indicator = $field->indicator(1).$field->indicator(2);
74                 }
75             }
76         }
77     }
78     return($indicator,@result);
79 }
80
81
82 =item MARCfindbreeding
83
84     $record = MARCfindbreeding($dbh, $breedingid);
85
86 Look up the breeding farm with database handle $dbh, for the
87 record with id $breedingid.  If found, returns the decoded
88 MARC::Record; otherwise, -1 is returned (FIXME).
89 Returns as second parameter the character encoding.
90
91 =cut
92
93 sub MARCfindbreeding {
94     my ($dbh,$id) = @_;
95     my $sth = $dbh->prepare("select file,marc,encoding from marc_breeding where id=?");
96     $sth->execute($id);
97     my ($file,$marc,$encoding) = $sth->fetchrow;
98     if ($marc) {
99         my $record = MARC::Record->new_from_usmarc($marc);
100         if ($record->field('010')){ 
101         foreach my $field ($record->field('010'))
102                 {
103                         foreach my $subfield ($field->subfield('a')){
104                                 my $newisbn = $field->subfield('a');
105                                 $newisbn =~ s/-//g;
106                                 $field->update( 'a' => $newisbn );
107                                 
108                                                 
109                         }
110 #                       $record->insert_fields_ordered($record->field('010'));
111                 }               
112         }
113         
114     if ($record->subfield(100,'a')){
115         my $f100a=$record->subfield(100,'a');
116         my $f100 = $record->field(100);
117         my $f100temp = $f100->as_string;
118         $record->delete_field($f100);
119         if (length($f100temp)>28){
120         substr($f100temp,26,2,"50");
121         $f100->update('a' => $f100temp);
122         my $f100 = MARC::Field->new('100','','','a' => $f100temp);
123         $record->insert_fields_ordered($f100);
124         }
125     }
126         if (ref($record) eq undef) {
127             return -1;
128         } else {
129             if (C4::Context->preference("z3950NormalizeAuthor") and C4::Context->preference("z3950AuthorAuthFields")){
130                 my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,"biblio.author");
131 #                 my $summary = C4::Context->preference("z3950authortemplate");
132                 my $auth_fields = C4::Context->preference("z3950AuthorAuthFields");
133                 my @auth_fields= split /,/,$auth_fields;
134                 my $field;
135                 #warn $record->as_formatted;
136                 if ($record->field($tag)){
137                     foreach my $tmpfield ($record->field($tag)->subfields){
138 #                        foreach my $subfieldcode ($tmpfield->subfields){
139                         my $subfieldcode=shift @$tmpfield;
140                         my $subfieldvalue=shift @$tmpfield;
141                         if ($field){
142                             $field->add_subfields("$subfieldcode"=>$subfieldvalue) if ($subfieldcode ne $subfield);
143                         } else {
144                             $field=MARC::Field->new($tag,"","",$subfieldcode=>$subfieldvalue) if ($subfieldcode ne $subfield);
145                         }
146                     }
147 #                    warn $field->as_formatted;
148 #                    }
149                 }
150                 $record->delete_field($record->field($tag));
151                 foreach my $fieldtag (@auth_fields){
152                     next unless ($record->field($fieldtag));
153                     my $lastname = $record->field($fieldtag)->subfield('a');
154                     my $firstname= $record->field($fieldtag)->subfield('b');
155                     my $title = $record->field($fieldtag)->subfield('c');
156                     my $number= $record->field($fieldtag)->subfield('d');
157                     if ($title){
158 #                         $field->add_subfields("$subfield"=>"[ ".ucfirst($title).ucfirst($firstname)." ".$number." ]");
159                         $field->add_subfields("$subfield"=>ucfirst($title)." ".ucfirst($firstname)." ".$number);
160                     }else{
161 #                         $field->add_subfields("$subfield"=>"[ ".ucfirst($firstname).", ".ucfirst($lastname)." ]");
162                         $field->add_subfields("$subfield"=>ucfirst($firstname).", ".ucfirst($lastname));
163                     }
164                 }
165                 $record->insert_fields_ordered($field);
166             }
167             return $record,$encoding;
168         }
169     }
170     return -1;
171 }
172
173
174 =item build_authorized_values_list
175
176 =cut
177
178 sub build_authorized_values_list ($$$$$) {
179     my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
180
181     my @authorised_values;
182     my %authorised_lib;
183
184     # builds list, depending on authorised value...
185
186     #---- branch
187     if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
188     my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
189     $sth->execute;
190     push @authorised_values, ""
191         unless ($tagslib->{$tag}->{$subfield}->{mandatory});
192
193     while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
194         push @authorised_values, $branchcode;
195         $authorised_lib{$branchcode}=$branchname;
196     }
197
198     #----- itemtypes
199     } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
200         my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
201         $sth->execute;
202         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
203     
204         while (my ($itemtype,$description) = $sth->fetchrow_array) {
205             push @authorised_values, $itemtype;
206             $authorised_lib{$itemtype}=$description;
207         }
208         $value=$itemtype unless ($value);
209
210     #---- "true" authorised value
211     } else {
212         $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
213
214         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
215     
216         while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
217             push @authorised_values, $value;
218             $authorised_lib{$value}=$lib;
219         }
220     }
221     return CGI::scrolling_list( -name     => 'field_value',
222                 -values   => \@authorised_values,
223                 -default  => $value,
224                 -labels   => \%authorised_lib,
225                 -override => 1,
226                 -size     => 1,
227                 -multiple => 0 );
228 }
229
230 =item create_input
231
232  builds the <input ...> entry for a subfield.
233
234 =cut
235
236 sub create_input () {
237     my ($tag,$subfield,$value,$i,$tabloop,$rec,$authorised_values_sth) = @_;
238     # must be encoded as utf-8 before it reaches the editor
239         #use Encode;
240         #$value = encode('utf-8', $value);
241     $value =~ s/"/&quot;/g;
242     my $dbh = C4::Context->dbh;
243     my %subfield_data;
244     $subfield_data{tag}=$tag;
245     $subfield_data{subfield}=$subfield;
246     $subfield_data{marc_lib}="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".substr($tagslib->{$tag}->{$subfield}->{lib},0,15)."</span>";
247     $subfield_data{marc_lib_plain}=$tagslib->{$tag}->{$subfield}->{lib};
248     $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
249     $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
250     $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
251     $subfield_data{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
252     $subfield_data{index} = $i;
253     $subfield_data{visibility} = "display:none" unless (($tagslib->{$tag}->{$subfield}->{hidden}%2==0) or $value ne ''); #check parity
254     # it's an authorised field
255     if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
256         $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
257     # it's a thesaurus / authority field
258     } elsif ($tagslib->{$tag}->{$subfield}->{authtypecode}) {
259         $subfield_data{marc_value}="<input type=\"text\" onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffff00;'\"\" tabindex=\"1\" type=\"text\" name=\"field_value\" value=\"$value\" size=\"70\" maxlength=\"255\" DISABLE READONLY> <a  style=\"cursor: help;\" href=\"javascript:Dopop('../authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$i',$i)\">...</a>";
260     # it's a plugin field
261     } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
262         # opening plugin. Just check wether we are on a developper computer on a production one
263         # (the cgidir differs)
264         my $cgidir = C4::Context->intranetdir ."/cgi-bin/cataloguing/value_builder";
265         unless (opendir(DIR, "$cgidir")) {
266             $cgidir = C4::Context->intranetdir."/cataloguing/value_builder";
267         }
268         my $plugin=$cgidir."/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
269         do $plugin;
270         my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
271         my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
272         $subfield_data{marc_value}="<input tabindex=\"1\" type=\"text\" name=\"field_value\"  value=\"$value\" size=\"70\" maxlength=\"255\" OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i); \"> <a  style=\"cursor: help;\" href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
273     # it's an hidden field
274     } elsif  ($tag eq '') {
275          $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffff00'; \" tabindex=\"1\" type=\"hidden\" name=\"field_value\" value=\"$value\">";
276     } elsif  ($tagslib->{$tag}->{$subfield}->{'hidden'}) {
277         $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffff00'; \" tabindex=\"1\" type=\"text\" name=\"field_value\" value=\"$value\" size=\"70\" maxlength=\"255\" >";
278     # it's a standard field
279     } else {
280         if (length($value) >100 or (C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >=300 and $tag <400 && $subfield eq 'a') or ($tag >=500 and $tag <600 && C4::Context->preference("marcflavour") eq "MARC21")) {
281             $subfield_data{marc_value}="<textarea tabindex=\"1\" name=\"field_value\" cols=\"70\" rows=\"5\" >$value</textarea>";
282         } else {
283             $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffff00'; \" tabindex=\"1\" type=\"text\" name=\"field_value\" value=\"$value\" size=\"70\">"; #"
284         }
285     }
286     return \%subfield_data;
287 }
288
289 sub build_tabs ($$$$) {
290     my($template, $record, $dbh,$encoding) = @_;
291     # fill arrays
292     my @loop_data =();
293     my $tag;
294     my $i=0;
295     my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
296         from authorised_values
297         where category=? order by lib");
298     
299     # in this array, we will push all the 10 tabs
300     # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
301     my @BIG_LOOP;
302
303 # loop through each tab 0 through 9
304     for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
305         my @loop_data = ();
306         foreach my $tag (sort(keys (%{$tagslib}))) {
307             my $indicator;
308     # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
309     # if MARC::Record is empty => use tab as master loop.
310             if ($record ne -1 && ($record->field($tag) || $tag eq '000')) {
311                 my @fields;
312                 if ($tag ne '000') {
313                     @fields = $record->field($tag);
314                 } else {
315                     push @fields,$record->leader();
316                 }
317                 foreach my $field (@fields)  {
318                     my @subfields_data;
319                     if ($tag<10) {
320                         my ($value,$subfield);
321                         if ($tag ne '000') {
322                             $value=$field->data();
323                             $subfield="@";
324                         } else {
325                             $value = $field;
326                             $subfield='@';
327                         }
328                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
329                         next if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'biblio.biblionumber');
330                         push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
331                         $i++;
332                     } else {
333                         my @subfields=$field->subfields();
334                         foreach my $subfieldcount (0..$#subfields) {
335                             my $subfield=$subfields[$subfieldcount][0];
336                             my $value=$subfields[$subfieldcount][1];
337                             next if (length $subfield !=1);
338                             next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
339                             push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
340                             $i++;
341                         }
342                     }
343 # now, loop again to add parameter subfield that are not in the MARC::Record
344                     foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
345                         next if (length $subfield !=1);
346                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
347                         next if ($tag<10);
348                         next if (($tagslib->{$tag}->{$subfield}->{hidden}<=-4) or ($tagslib->{$tag}->{$subfield}->{hidden}>=5) ); #check for visibility flag
349                         next if (defined($field->subfield($subfield)));
350                         push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
351                         $i++;
352                     }
353                     if ($#subfields_data >= 0) {
354                         my %tag_data;
355                         $tag_data{tag} = $tag;
356                         $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
357                         $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
358                         $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
359                         $tag_data{subfield_loop} = \@subfields_data;
360                         if ($tag<10) {
361                                                     $tag_data{fixedfield} = 1;
362                                             }
363
364                         push (@loop_data, \%tag_data);
365                     }
366 # If there is more than 1 field, add an empty hidden field as separator.
367                     if ($#fields >=1 && $#loop_data >=0 && $loop_data[$#loop_data]->{'tag'} eq $tag) {
368                         my @subfields_data;
369                         my %tag_data;
370                         push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
371                         $tag_data{tag} = '';
372                         $tag_data{tag_lib} = '';
373                         $tag_data{indicator} = '';
374                         $tag_data{subfield_loop} = \@subfields_data;
375                         if ($tag<10) {
376                                                        $tag_data{fixedfield} = 1;
377                                     }
378                         push (@loop_data, \%tag_data);
379                         $i++;
380                     }
381                 }
382     # if breeding is empty
383             } else {
384                 my @subfields_data;
385                 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
386                     next if (length $subfield !=1);
387                     next if (($tagslib->{$tag}->{$subfield}->{hidden}<=-5) or ($tagslib->{$tag}->{$subfield}->{hidden}>=4) ); #check for visibility flag
388                     next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
389                     push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
390                     $i++;
391                 }
392                 if ($#subfields_data >= 0) {
393                     my %tag_data;
394                     $tag_data{tag} = $tag;
395                     $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
396                     $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
397                     $tag_data{indicator} = $indicator;
398                     $tag_data{subfield_loop} = \@subfields_data;
399                     $tag_data{tagfirstsubfield} = $tag_data{subfield_loop}[0];
400                     if ($tag<10) {
401                         $tag_data{fixedfield} = 1;
402                     }
403                     push (@loop_data, \%tag_data);
404                 }
405             }
406         }
407         if ($#loop_data >=0) {
408             my %big_loop_line;
409             $big_loop_line{number}=$tabloop;
410             $big_loop_line{innerloop}=\@loop_data;
411             push @BIG_LOOP,\%big_loop_line;
412         }
413 #         $template->param($tabloop."XX" =>\@loop_data);
414     }
415     $template->param(BIG_LOOP => \@BIG_LOOP);
416 }
417
418
419
420 sub build_hidden_data () {
421     # build hidden data =>
422     # we store everything, even if we show only requested subfields.
423
424     my @loop_data =();
425     my $i=0;
426     foreach my $tag (keys %{$tagslib}) {
427     my $previous_tag = '';
428
429     # loop through each subfield
430     foreach my $subfield (keys %{$tagslib->{$tag}}) {
431         next if ($subfield eq 'lib');
432         next if ($subfield eq 'tab');
433         next if ($subfield eq 'mandatory');
434         next if ($subfield eq 'repeatable');
435         next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
436         my %subfield_data;
437         $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
438         $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
439         $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
440         $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
441         push(@loop_data, \%subfield_data);
442         $i++
443     }
444     }
445 }
446
447
448 # ======================== 
449 #          MAIN 
450 #=========================
451 my $input = new CGI;
452 my $error = $input->param('error');
453 my $biblionumber=$input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
454 my $breedingid = $input->param('breedingid');
455 my $z3950 = $input->param('z3950');
456 my $op = $input->param('op');
457 my $frameworkcode = $input->param('frameworkcode');
458 my $dbh = C4::Context->dbh;
459
460 $frameworkcode = &MARCfind_frameworkcode($biblionumber) if ($biblionumber and not ($frameworkcode));
461
462 $frameworkcode='' if ($frameworkcode eq 'Default');
463 my ($template, $loggedinuser, $cookie)
464     = get_template_and_user({template_name => "cataloguing/addbiblio.tmpl",
465                  query => $input,
466                  type => "intranet",
467                  authnotrequired => 0,
468                  flagsrequired => {editcatalogue => 1},
469                  debug => 1,
470                  });
471
472 #Getting the list of all frameworks
473 my $queryfwk =$dbh->prepare("select frameworktext, frameworkcode from biblio_framework");
474 $queryfwk->execute;
475 my %select_fwk;
476 my @select_fwk;
477 my $curfwk;
478 push @select_fwk,"Default";
479 $select_fwk{"Default"} = "Default";
480 while (my ($description, $fwk) =$queryfwk->fetchrow) {
481     push @select_fwk, $fwk;
482     $select_fwk{$fwk} = $description;
483 }
484 $curfwk=$frameworkcode;
485 my $framework=CGI::scrolling_list( 
486             -name     => 'Frameworks',
487             -id => 'Frameworks',
488             -default => $curfwk,
489             -OnChange => 'Changefwk(this);',
490             -values   => \@select_fwk,
491             -labels   => \%select_fwk,
492             -size     => 1,
493             -multiple => 0 );
494 $template->param( framework => $framework, breedingid => $breedingid);
495
496 $tagslib = &MARCgettagslib($dbh,1,$frameworkcode);
497 my $record=-1;
498 my $encoding="";
499 $record = GetMarcBiblio( $biblionumber ) if ($biblionumber);
500 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
501
502 # warn "biblionumber : $biblionumber = ".$record->as_formatted;
503 $is_a_modif=0;
504 my ($biblionumtagfield,$biblionumtagsubfield);
505 my ($biblioitemnumtagfield,$biblioitemnumtagsubfield,$bibitem,$biblioitemnumber);
506 if ($biblionumber) {
507     $is_a_modif=1;
508     # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
509     ($biblionumtagfield,$biblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber",$frameworkcode);
510     ($biblioitemnumtagfield,$biblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber",$frameworkcode);
511     # search biblioitems value
512     my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
513     $sth->execute($biblionumber);
514     ($biblioitemnumber) = $sth->fetchrow;
515 }
516 #-------------------------------------------------------------------------------------
517 if ($op eq "addbiblio") {
518 #-------------------------------------------------------------------------------------
519     # rebuild
520     my @tags = $input->param('tag');
521     my @subfields = $input->param('subfield');
522     my @values = $input->param('field_value');
523     # build indicator hash.
524     my @ind_tag = $input->param('ind_tag');
525     my @indicator = $input->param('indicator');
526     if (C4::Context->preference('TemplateEncoding') eq "iso-8859-1") {
527         $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
528     } else {
529         my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
530         $record=MARC::Record->new_from_xml($xml,C4::Context->preference('TemplateEncoding'),C4::Context->preference('marcflavour'));
531 #         warn "MARC :".$record->as_formatted;
532 #         die;
533     }
534     # check for a duplicate
535     my ($duplicatebiblionumber,$duplicatebiblionumber,$duplicatetitle) = FindDuplicate($record) if ($op eq "addbiblio") && (!$is_a_modif);
536     my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
537     # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
538     if (!$duplicatebiblionumber or $confirm_not_duplicate) {
539         # MARC::Record built => now, record in DB
540         my $oldbibnum;
541         my $oldbibitemnum;
542         if ($is_a_modif) {
543             ModBiblioframework($biblionumber,$frameworkcode);
544             ModBiblio($record,$biblionumber,$frameworkcode);
545         }
546         else {
547             ($biblionumber,$oldbibitemnum) = AddBiblio($record,$frameworkcode);
548         }
549     # now, redirect to additem page
550         print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
551         exit;
552     } else {
553     # it may be a duplicate, warn the user and do nothing
554         build_tabs ($template, $record, $dbh,$encoding);
555         build_hidden_data;
556         $template->param(
557             biblionumber             => $biblionumber,
558             biblionumtagfield        => $biblionumtagfield,
559             biblionumtagsubfield     => $biblionumtagsubfield,
560             biblioitemnumtagfield    => $biblioitemnumtagfield,
561             biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
562             biblioitemnumber         => $biblioitemnumber,
563             duplicatebiblionumber    => $duplicatebiblionumber,
564             duplicatebibid           => $duplicatebiblionumber,
565             duplicatetitle           => $duplicatetitle,
566         );
567     }
568 #--------------------------------------------------------------------------
569 } elsif ($op eq "addfield") {
570 #--------------------------------------------------------------------------
571     my $addedfield = $input->param('addfield_field');
572     my $cntrepeatfield=$input->param('repeat_field');
573     $cntrepeatfield=1 unless ($cntrepeatfield);
574     my $tagaddfield_subfield = $input->param('addfield_subfield');
575     my @tags = $input->param('tag');
576     my @subfields = $input->param('subfield');
577     my @values = $input->param('field_value');
578     # build indicator hash.
579     my @ind_tag = $input->param('ind_tag');
580     my @indicator = $input->param('indicator');
581     my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
582     my $record;
583     if (C4::Context->preference('TemplateEncoding') eq "iso-8859-1") {
584         my %indicators;
585         for (my $i=0;$i<=$#ind_tag;$i++) {
586             $indicators{$ind_tag[$i]} = $indicator[$i];
587         }
588         $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
589     } else {
590         my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
591         $record=MARC::Record->new_from_xml($xml,C4::Context->preference('TemplateEncoding'),C4::Context->preference('marcflavour'));
592     }
593     for (my $i=1;$i<=$cntrepeatfield;$i++){
594         my $field = MARC::Field->new("$addedfield",'','',"$tagaddfield_subfield" => "");
595         $record->append_fields($field);
596     }
597     #warn "result : ".$record->as_formatted;
598     build_tabs ($template, $record, $dbh,$encoding);
599     build_hidden_data;
600     $template->param(
601         biblionumber             => $biblionumber,
602         biblionumtagfield        => $biblionumtagfield,
603         biblionumtagsubfield     => $biblionumtagsubfield,
604         biblioitemnumtagfield    => $biblioitemnumtagfield,
605         biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
606         biblioitemnumber         => $biblioitemnumber );
607 } elsif ($op eq "delete") {
608 #-----------------------------------------------------------------------------
609     my $error = &DelBiblio($biblionumber);
610     if ($error) {
611         warn "ERROR when DELETING BIBLIO $biblionumber : $error";
612         print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
613     } else {
614     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=/cgi-bin/koha/catalogue/search.pl?type=intranet\"></html>";
615     }
616     exit;
617 #----------------------------------------------------------------------------
618 } else {
619 #----------------------------------------------------------------------------
620     # If we're in a duplication case, we have to set to "" the biblionumber
621     # as we'll save the biblio as a new one.
622     if ($op eq "duplicate")
623     {
624         $biblionumber= "";
625     }
626     #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
627     eval {
628         my $uxml = $record->as_xml;
629         MARC::Record::default_record_format("UNIMARC") if (C4::Context->preference("marcflavour") eq "UNIMARC");
630         my $urecord = MARC::Record::new_from_xml($uxml, 'UTF-8');
631         $record = $urecord;
632     };
633     build_tabs ($template, $record, $dbh,$encoding);
634     build_hidden_data;
635     $template->param(
636         biblionumber             => $biblionumber,
637         biblionumtagfield        => $biblionumtagfield,
638         biblionumtagsubfield     => $biblionumtagsubfield,
639         biblioitemnumtagfield    => $biblioitemnumtagfield,
640         biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
641         biblioitemnumber         => $biblioitemnumber,
642         );
643 }
644 $template->param( title => $record->title() ) if ($record ne "-1");
645 $template->param(
646         frameworkcode => $frameworkcode,
647         itemtype => $frameworkcode, # HINT: if the library has itemtype = framework, itemtype is auto filled !
648         hide_marc => C4::Context->preference('hide_marc'),
649         intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
650         intranetstylesheet => C4::Context->preference("intranetstylesheet"),
651         IntranetNav => C4::Context->preference("IntranetNav"),
652         advancedMARCEditor => C4::Context->preference("advancedMARCEditor"),
653         );
654 output_html_with_http_headers $input, $cookie, $template->output;