Adding a biblio can be executed in a popup now.
[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::Biblio;
27 use C4::Search;
28 use C4::Context;
29 use MARC::Record;
30 use C4::Log;
31 use C4::Koha; # XXX subfield_is_koha_internal_p
32 use Date::Calc qw(Today);
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     #                   $record->insert_fields_ordered($record->field('010'));
109             }           
110         }
111         if ($record->subfield(100,'a')) {
112             my $f100a=$record->subfield(100,'a');
113             my $f100 = $record->field(100);
114             my $f100temp = $f100->as_string;
115             $record->delete_field($f100);
116             if (length($f100temp)>28) {
117                 substr($f100temp,26,2,"50");
118                 $f100->update('a' => $f100temp);
119                 my $f100 = MARC::Field->new('100','','','a' => $f100temp);
120                 $record->insert_fields_ordered($f100);
121             }
122         }
123         if (ref($record) eq undef) {
124             return -1;
125         } else {
126             if (C4::Context->preference("z3950NormalizeAuthor") and C4::Context->preference("z3950AuthorAuthFields")){
127                 my ($tag,$subfield) = GetMarcFromKohaField("biblio.author");
128 #                 my $summary = C4::Context->preference("z3950authortemplate");
129                 my $auth_fields = C4::Context->preference("z3950AuthorAuthFields");
130                 my @auth_fields= split /,/,$auth_fields;
131                 my $field;
132                 #warn $record->as_formatted;
133                 if ($record->field($tag)){
134                     foreach my $tmpfield ($record->field($tag)->subfields){
135 #                        foreach my $subfieldcode ($tmpfield->subfields){
136                         my $subfieldcode=shift @$tmpfield;
137                         my $subfieldvalue=shift @$tmpfield;
138                         if ($field){
139                             $field->add_subfields("$subfieldcode"=>$subfieldvalue) if ($subfieldcode ne $subfield);
140                         } else {
141                             $field=MARC::Field->new($tag,"","",$subfieldcode=>$subfieldvalue) if ($subfieldcode ne $subfield);
142                         }
143                     }
144 #                    warn $field->as_formatted;
145 #                    }
146                 }
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');
154                     if ($title){
155 #                         $field->add_subfields("$subfield"=>"[ ".ucfirst($title).ucfirst($firstname)." ".$number." ]");
156                         $field->add_subfields("$subfield"=>ucfirst($title)." ".ucfirst($firstname)." ".$number);
157                     }else{
158 #                         $field->add_subfields("$subfield"=>"[ ".ucfirst($firstname).", ".ucfirst($lastname)." ]");
159                         $field->add_subfields("$subfield"=>ucfirst($firstname).", ".ucfirst($lastname));
160                     }
161                 }
162                 $record->insert_fields_ordered($field);
163             }
164             return $record,$encoding;
165         }
166     }
167     return -1;
168 }
169
170
171 =item build_authorized_values_list
172
173 =cut
174
175 sub build_authorized_values_list ($$$$$) {
176     my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
177
178     my @authorised_values;
179     my %authorised_lib;
180
181     # builds list, depending on authorised value...
182
183     #---- branch
184     if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
185     my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
186     $sth->execute;
187     push @authorised_values, ""
188         unless ($tagslib->{$tag}->{$subfield}->{mandatory});
189
190     while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
191         push @authorised_values, $branchcode;
192         $authorised_lib{$branchcode}=$branchname;
193     }
194
195     #----- itemtypes
196     } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
197         my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
198         $sth->execute;
199         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
200     
201         while (my ($itemtype,$description) = $sth->fetchrow_array) {
202             push @authorised_values, $itemtype;
203             $authorised_lib{$itemtype}=$description;
204         }
205         $value=$itemtype unless ($value);
206
207     #---- "true" authorised value
208     } else {
209         $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
210
211         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
212     
213         while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
214             push @authorised_values, $value;
215             $authorised_lib{$value}=$lib;
216         }
217     }
218     return CGI::scrolling_list( -name     => 'field_value',
219                 -values   => \@authorised_values,
220                 -default  => $value,
221                 -labels   => \%authorised_lib,
222                 -override => 1,
223                 -size     => 1,
224                 -multiple => 0 );
225 }
226
227 =item create_input
228
229  builds the <input ...> entry for a subfield.
230
231 =cut
232
233 sub create_input () {
234     my ($tag,$subfield,$value,$i,$tabloop,$rec,$authorised_values_sth) = @_;
235     # must be encoded as utf-8 before it reaches the editor
236         #use Encode;
237         #$value = encode('utf-8', $value);
238     $value =~ s/"/&quot;/g;
239     # if there is no value provided but a default value in parameters, get it 
240     unless ($value) {
241         $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
242         # get today date & replace YYYY, MM, DD if provided in the default value
243         my ($year,$month,$day) = Today();
244         $month = sprintf("%02d",$month);
245         $day = sprintf("%02d",$day);
246         $value =~ s/YYYY/$year/g;
247         $value =~ s/MM/$month/g;
248         $value =~ s/DD/$day/g;
249         
250     }
251     my $dbh = C4::Context->dbh;
252     my %subfield_data;
253     $subfield_data{tag}=$tag;
254     $subfield_data{subfield}=$subfield;
255     $subfield_data{marc_lib}="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".substr($tagslib->{$tag}->{$subfield}->{lib},0,15)."</span>";
256     $subfield_data{marc_lib_plain}=$tagslib->{$tag}->{$subfield}->{lib};
257     $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
258     $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
259     $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
260     $subfield_data{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
261     $subfield_data{index} = $i;
262     $subfield_data{visibility} = "display:none" unless (($tagslib->{$tag}->{$subfield}->{hidden}%2==0) or $value ne ''); #check parity
263     # it's an authorised field
264     if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
265         $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
266     # it's a thesaurus / authority field
267     } elsif ($tagslib->{$tag}->{$subfield}->{authtypecode}) {
268         $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>";
269     # it's a plugin field
270     } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
271         # opening plugin. Just check wether we are on a developper computer on a production one
272         # (the cgidir differs)
273         my $cgidir = C4::Context->intranetdir ."/cgi-bin/cataloguing/value_builder";
274         unless (opendir(DIR, "$cgidir")) {
275             $cgidir = C4::Context->intranetdir."/cataloguing/value_builder";
276         }
277         my $plugin=$cgidir."/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
278         do $plugin;
279         my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
280         my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
281         $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";
282     # it's an hidden field
283     } elsif  ($tag eq '') {
284          $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffff00'; \" tabindex=\"1\" type=\"hidden\" name=\"field_value\" value=\"$value\">";
285     } elsif  ($tagslib->{$tag}->{$subfield}->{'hidden'}) {
286         $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\" >";
287     # it's a standard field
288     } else {
289         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")) {
290             $subfield_data{marc_value}="<textarea tabindex=\"1\" name=\"field_value\" cols=\"70\" rows=\"5\" >$value</textarea>";
291         } else {
292             $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\">"; #"
293         }
294     }
295     return \%subfield_data;
296 }
297
298 sub build_tabs ($$$$) {
299     my($template, $record, $dbh,$encoding) = @_;
300     # fill arrays
301     my @loop_data =();
302     my $tag;
303     my $i=0;
304     my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
305         from authorised_values
306         where category=? order by lib");
307     
308     # in this array, we will push all the 10 tabs
309     # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
310     my @BIG_LOOP;
311
312 # loop through each tab 0 through 9
313     for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
314         my @loop_data = ();
315         foreach my $tag (sort(keys (%{$tagslib}))) {
316             my $indicator;
317     # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
318     # if MARC::Record is empty => use tab as master loop.
319             if ($record ne -1 && ($record->field($tag) || $tag eq '000')) {
320                 my @fields;
321                 if ($tag ne '000') {
322                     @fields = $record->field($tag);
323                 } else {
324                     push @fields,$record->leader();
325                 }
326                 foreach my $field (@fields)  {
327                     my @subfields_data;
328                     if ($tag<10) {
329                         my ($value,$subfield);
330                         if ($tag ne '000') {
331                             $value=$field->data();
332                             $subfield="@";
333                         } else {
334                             $value = $field;
335                             $subfield='@';
336                         }
337                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
338                         next if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'biblio.biblionumber');
339                         push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
340                         $i++;
341                     } else {
342                         my @subfields=$field->subfields();
343                         foreach my $subfieldcount (0..$#subfields) {
344                             my $subfield=$subfields[$subfieldcount][0];
345                             my $value=$subfields[$subfieldcount][1];
346                             next if (length $subfield !=1);
347                             next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
348                             push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
349                             $i++;
350                         }
351                     }
352 # now, loop again to add parameter subfield that are not in the MARC::Record
353                     foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
354                         next if (length $subfield !=1);
355                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
356                         next if ($tag<10);
357                         next if (($tagslib->{$tag}->{$subfield}->{hidden}<=-4) or ($tagslib->{$tag}->{$subfield}->{hidden}>=5) ); #check for visibility flag
358                         next if (defined($field->subfield($subfield)));
359                         push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
360                         $i++;
361                     }
362                     if ($#subfields_data >= 0) {
363                         my %tag_data;
364                         $tag_data{tag} = $tag;
365                         $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
366                         $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
367                         $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
368                         $tag_data{subfield_loop} = \@subfields_data;
369                         if ($tag<10) {
370                                                     $tag_data{fixedfield} = 1;
371                                             }
372
373                         push (@loop_data, \%tag_data);
374                     }
375 # If there is more than 1 field, add an empty hidden field as separator.
376                     if ($#fields >=1 && $#loop_data >=0 && $loop_data[$#loop_data]->{'tag'} eq $tag) {
377                         my @subfields_data;
378                         my %tag_data;
379                         push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
380                         $tag_data{tag} = '';
381                         $tag_data{tag_lib} = '';
382                         $tag_data{indicator} = '';
383                         $tag_data{subfield_loop} = \@subfields_data;
384                         if ($tag<10) {
385                                                        $tag_data{fixedfield} = 1;
386                                     }
387                         push (@loop_data, \%tag_data);
388                         $i++;
389                     }
390                 }
391     # if breeding is empty
392             } else {
393                 my @subfields_data;
394                 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
395                     next if (length $subfield !=1);
396                     next if (($tagslib->{$tag}->{$subfield}->{hidden}<=-5) or ($tagslib->{$tag}->{$subfield}->{hidden}>=4) ); #check for visibility flag
397                     next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
398                     push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
399                     $i++;
400                 }
401                 if ($#subfields_data >= 0) {
402                     my %tag_data;
403                     $tag_data{tag} = $tag;
404                     $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
405                     $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
406                     $tag_data{indicator} = $indicator;
407                     $tag_data{subfield_loop} = \@subfields_data;
408                     $tag_data{tagfirstsubfield} = $tag_data{subfield_loop}[0];
409                     if ($tag<10) {
410                         $tag_data{fixedfield} = 1;
411                     }
412                     push (@loop_data, \%tag_data);
413                 }
414             }
415         }
416         if ($#loop_data >=0) {
417             my %big_loop_line;
418             $big_loop_line{number}=$tabloop;
419             $big_loop_line{innerloop}=\@loop_data;
420             push @BIG_LOOP,\%big_loop_line;
421         }
422 #         $template->param($tabloop."XX" =>\@loop_data);
423     }
424     $template->param(BIG_LOOP => \@BIG_LOOP);
425 }
426
427
428
429 sub build_hidden_data () {
430     # build hidden data =>
431     # we store everything, even if we show only requested subfields.
432
433     my @loop_data =();
434     my $i=0;
435     foreach my $tag (keys %{$tagslib}) {
436     my $previous_tag = '';
437
438     # loop through each subfield
439     foreach my $subfield (keys %{$tagslib->{$tag}}) {
440         next if ($subfield eq 'lib');
441         next if ($subfield eq 'tab');
442         next if ($subfield eq 'mandatory');
443         next if ($subfield eq 'repeatable');
444         next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
445         my %subfield_data;
446         $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
447         $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
448         $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
449         $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
450         push(@loop_data, \%subfield_data);
451         $i++
452     }
453     }
454 }
455
456
457 # ======================== 
458 #          MAIN 
459 #=========================
460 my $input = new CGI;
461 my $error = $input->param('error');
462 my $biblionumber=$input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
463 my $breedingid = $input->param('breedingid');
464 my $z3950 = $input->param('z3950');
465 my $op = $input->param('op');
466 my $mode = $input->param('mode');
467 my $frameworkcode = $input->param('frameworkcode');
468 my $dbh = C4::Context->dbh;
469
470 $frameworkcode = &GetFrameworkCode($biblionumber) if ($biblionumber and not ($frameworkcode));
471
472 $frameworkcode='' if ($frameworkcode eq 'Default');
473 my ($template, $loggedinuser, $cookie)
474     = get_template_and_user({template_name => "cataloguing/addbiblio.tmpl",
475                  query => $input,
476                  type => "intranet",
477                  authnotrequired => 0,
478                  flagsrequired => {editcatalogue => 1},
479                  debug => 1,
480                  });
481
482 #Getting the list of all frameworks
483 my $queryfwk =$dbh->prepare("select frameworktext, frameworkcode from biblio_framework");
484 $queryfwk->execute;
485 my %select_fwk;
486 my @select_fwk;
487 my $curfwk;
488 push @select_fwk,"Default";
489 $select_fwk{"Default"} = "Default";
490 while (my ($description, $fwk) =$queryfwk->fetchrow) {
491     push @select_fwk, $fwk;
492     $select_fwk{$fwk} = $description;
493 }
494 $curfwk=$frameworkcode;
495 my $framework=CGI::scrolling_list( 
496             -name     => 'Frameworks',
497             -id => 'Frameworks',
498             -default => $curfwk,
499             -OnChange => 'Changefwk(this);',
500             -values   => \@select_fwk,
501             -labels   => \%select_fwk,
502             -size     => 1,
503             -multiple => 0 );
504 $template->param( framework => $framework, breedingid => $breedingid);
505
506 $tagslib = &GetMarcStructure(1,$frameworkcode);
507 my $record=-1;
508 my $encoding="";
509 $record = GetMarcBiblio( $biblionumber ) if ($biblionumber);
510 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
511
512 # warn "biblionumber : $biblionumber = ".$record->as_formatted;
513 $is_a_modif=0;
514 my ($biblionumtagfield,$biblionumtagsubfield);
515 my ($biblioitemnumtagfield,$biblioitemnumtagsubfield,$bibitem,$biblioitemnumber);
516 if ($biblionumber) {
517     $is_a_modif=1;
518     # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
519     ($biblionumtagfield,$biblionumtagsubfield) = &GetMarcFromKohaField("biblio.biblionumber",$frameworkcode);
520     ($biblioitemnumtagfield,$biblioitemnumtagsubfield) = &GetMarcFromKohaField("biblioitems.biblioitemnumber",$frameworkcode);
521     # search biblioitems value
522     my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
523     $sth->execute($biblionumber);
524     ($biblioitemnumber) = $sth->fetchrow;
525 }
526 #-------------------------------------------------------------------------------------
527 if ($op eq "addbiblio") {
528 #-------------------------------------------------------------------------------------
529     # rebuild
530     my @tags = $input->param('tag');
531     my @subfields = $input->param('subfield');
532     my @values = $input->param('field_value');
533     # build indicator hash.
534     my @ind_tag = $input->param('ind_tag');
535     my @indicator = $input->param('indicator');
536     if (C4::Context->preference('TemplateEncoding') eq "iso-8859-1") {
537         $record = TransformHtmlToMarc($dbh,\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
538     } else {
539         my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
540         $record=MARC::Record->new_from_xml($xml,C4::Context->preference('TemplateEncoding'),C4::Context->preference('marcflavour'));
541 #         warn "MARC :".$record->as_formatted;
542 #         die;
543     }
544     # check for a duplicate
545     my ($duplicatebiblionumber,$duplicatetitle) = FindDuplicate($record) if (!$is_a_modif);
546     my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
547     # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
548     if (!$duplicatebiblionumber or $confirm_not_duplicate) {
549         # MARC::Record built => now, record in DB
550         my $oldbibnum;
551         my $oldbibitemnum;
552         if ($is_a_modif) {
553             ModBiblioframework($biblionumber,$frameworkcode);
554             ModBiblio($record,$biblionumber,$frameworkcode);
555         }
556         else {
557             ($biblionumber,$oldbibitemnum) = AddBiblio($record,$frameworkcode);
558         }
559     # now, redirect to additem page
560         if ($mode ne "popup"){
561         print $input->redirect("/cgi-bin/koha/cataloguing/additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
562         exit;
563         } else {
564           $template->param(
565             biblionumber => $biblionumber,
566             done         =>1,
567             popup        =>1
568           );
569           $template->param( title => $record->subfield('200',"a") ) if ($record ne "-1" && C4::Context->preference('marcflavour') =~/unimarc/i);
570           $template->param( title => $record->title() ) if ($record ne "-1" && C4::Context->preference('marcflavour') eq "usmarc");
571           $template->param(
572                   popup => $mode,
573                   itemtype => $frameworkcode, # HINT: if the library has itemtype = framework, itemtype is auto filled !
574                   );
575           output_html_with_http_headers $input, $cookie, $template->output;
576           exit;     
577         }        
578     } else {
579     # it may be a duplicate, warn the user and do nothing
580         build_tabs ($template, $record, $dbh,$encoding);
581         build_hidden_data;
582         $template->param(
583             biblionumber             => $biblionumber,
584             biblionumtagfield        => $biblionumtagfield,
585             biblionumtagsubfield     => $biblionumtagsubfield,
586             biblioitemnumtagfield    => $biblioitemnumtagfield,
587             biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
588             biblioitemnumber         => $biblioitemnumber,
589             duplicatebiblionumber    => $duplicatebiblionumber,
590             duplicatebibid           => $duplicatebiblionumber,
591             duplicatetitle           => $duplicatetitle,
592         );
593     }
594 #--------------------------------------------------------------------------
595 } elsif ($op eq "addfield") {
596 #--------------------------------------------------------------------------
597     my $addedfield = $input->param('addfield_field');
598     my $cntrepeatfield=$input->param('repeat_field');
599     $cntrepeatfield=1 unless ($cntrepeatfield);
600     my $tagaddfield_subfield = $input->param('addfield_subfield');
601     my @tags = $input->param('tag');
602     my @subfields = $input->param('subfield');
603     my @values = $input->param('field_value');
604     # build indicator hash.
605     my @ind_tag = $input->param('ind_tag');
606     my @indicator = $input->param('indicator');
607     my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
608     my $record;
609     if (C4::Context->preference('TemplateEncoding') eq "iso-8859-1") {
610         my %indicators;
611         for (my $i=0;$i<=$#ind_tag;$i++) {
612             $indicators{$ind_tag[$i]} = $indicator[$i];
613         }
614         $record = TransformHtmlToMarc($dbh,\@tags,\@subfields,\@values,%indicators);
615     } else {
616         my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
617         $record=MARC::Record->new_from_xml($xml,C4::Context->preference('TemplateEncoding'),C4::Context->preference('marcflavour'));
618     }
619     for (my $i=1;$i<=$cntrepeatfield;$i++){
620         my $field = MARC::Field->new("$addedfield",'','',"$tagaddfield_subfield" => "");
621         $record->append_fields($field);
622     }
623     #warn "result : ".$record->as_formatted;
624     build_tabs ($template, $record, $dbh,$encoding);
625     build_hidden_data;
626     $template->param(
627         biblionumber             => $biblionumber,
628         biblionumtagfield        => $biblionumtagfield,
629         biblionumtagsubfield     => $biblionumtagsubfield,
630         biblioitemnumtagfield    => $biblioitemnumtagfield,
631         biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
632         biblioitemnumber         => $biblioitemnumber );
633 } elsif ($op eq "delete") {
634 #-----------------------------------------------------------------------------
635     my $error = &DelBiblio($biblionumber);
636     if ($error) {
637         warn "ERROR when DELETING BIBLIO $biblionumber : $error";
638         print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
639     } else {
640     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=/cgi-bin/koha/catalogue/search.pl?type=intranet\"></html>";
641     }
642     exit;
643 #----------------------------------------------------------------------------
644 } else {
645 #----------------------------------------------------------------------------
646     # If we're in a duplication case, we have to set to "" the biblionumber
647     # as we'll save the biblio as a new one.
648     if ($op eq "duplicate")
649     {
650         $biblionumber= "";
651     }
652     #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
653     eval {
654         my $uxml = $record->as_xml;
655         MARC::Record::default_record_format("UNIMARC") if (C4::Context->preference("marcflavour") eq "UNIMARC");
656         my $urecord = MARC::Record::new_from_xml($uxml, 'UTF-8');
657         $record = $urecord;
658     };
659     build_tabs ($template, $record, $dbh,$encoding);
660     build_hidden_data;
661     $template->param(
662         biblionumber             => $biblionumber,
663         biblionumtagfield        => $biblionumtagfield,
664         biblionumtagsubfield     => $biblionumtagsubfield,
665         biblioitemnumtagfield    => $biblioitemnumtagfield,
666         biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
667         biblioitemnumber         => $biblioitemnumber,
668         );
669 }
670 $template->param( title => $record->title() ) if ($record ne "-1");
671 $template->param(
672         popup => $mode,
673         frameworkcode => $frameworkcode,
674         itemtype => $frameworkcode, # HINT: if the library has itemtype = framework, itemtype is auto filled !
675         hide_marc => C4::Context->preference('hide_marc'),
676         intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
677         intranetstylesheet => C4::Context->preference("intranetstylesheet"),
678         IntranetNav => C4::Context->preference("IntranetNav"),
679         advancedMARCEditor => C4::Context->preference("advancedMARCEditor"),
680         );
681 output_html_with_http_headers $input, $cookie, $template->output;