various bugfixes on parameters modules + adding default NoZebraIndexes systempreferen...
[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                                                 
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) = GetMarcFromKohaField("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     # if there is no value provided but a default value in parameters, get it 
243     unless ($value) {
244         $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
245         # get today date & replace YYYY, MM, DD if provided in the default value
246         my ($year,$month,$day) = Today();
247         $month = sprintf("%02d",$month);
248         $day = sprintf("%02d",$day);
249         $value =~ s/YYYY/$year/g;
250         $value =~ s/MM/$month/g;
251         $value =~ s/DD/$day/g;
252         
253     }
254     my $dbh = C4::Context->dbh;
255     my %subfield_data;
256     $subfield_data{tag}=$tag;
257     $subfield_data{subfield}=$subfield;
258     $subfield_data{marc_lib}="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".substr($tagslib->{$tag}->{$subfield}->{lib},0,15)."</span>";
259     $subfield_data{marc_lib_plain}=$tagslib->{$tag}->{$subfield}->{lib};
260     $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
261     $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
262     $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
263     $subfield_data{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
264     $subfield_data{index} = $i;
265     $subfield_data{visibility} = "display:none" unless (($tagslib->{$tag}->{$subfield}->{hidden}%2==0) or $value ne ''); #check parity
266     # it's an authorised field
267     if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
268         $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
269     # it's a thesaurus / authority field
270     } elsif ($tagslib->{$tag}->{$subfield}->{authtypecode}) {
271         $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>";
272     # it's a plugin field
273     } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
274         # opening plugin. Just check wether we are on a developper computer on a production one
275         # (the cgidir differs)
276         my $cgidir = C4::Context->intranetdir ."/cgi-bin/cataloguing/value_builder";
277         unless (opendir(DIR, "$cgidir")) {
278             $cgidir = C4::Context->intranetdir."/cataloguing/value_builder";
279         }
280         my $plugin=$cgidir."/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
281         do $plugin;
282         my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
283         my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
284         $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";
285     # it's an hidden field
286     } elsif  ($tag eq '') {
287          $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffff00'; \" tabindex=\"1\" type=\"hidden\" name=\"field_value\" value=\"$value\">";
288     } elsif  ($tagslib->{$tag}->{$subfield}->{'hidden'}) {
289         $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\" >";
290     # it's a standard field
291     } else {
292         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")) {
293             $subfield_data{marc_value}="<textarea tabindex=\"1\" name=\"field_value\" cols=\"70\" rows=\"5\" >$value</textarea>";
294         } else {
295             $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\">"; #"
296         }
297     }
298     return \%subfield_data;
299 }
300
301 sub build_tabs ($$$$) {
302     my($template, $record, $dbh,$encoding) = @_;
303     # fill arrays
304     my @loop_data =();
305     my $tag;
306     my $i=0;
307     my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
308         from authorised_values
309         where category=? order by lib");
310     
311     # in this array, we will push all the 10 tabs
312     # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
313     my @BIG_LOOP;
314
315 # loop through each tab 0 through 9
316     for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
317         my @loop_data = ();
318         foreach my $tag (sort(keys (%{$tagslib}))) {
319             my $indicator;
320     # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
321     # if MARC::Record is empty => use tab as master loop.
322             if ($record ne -1 && ($record->field($tag) || $tag eq '000')) {
323                 my @fields;
324                 if ($tag ne '000') {
325                     @fields = $record->field($tag);
326                 } else {
327                     push @fields,$record->leader();
328                 }
329                 foreach my $field (@fields)  {
330                     my @subfields_data;
331                     if ($tag<10) {
332                         my ($value,$subfield);
333                         if ($tag ne '000') {
334                             $value=$field->data();
335                             $subfield="@";
336                         } else {
337                             $value = $field;
338                             $subfield='@';
339                         }
340                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
341                         next if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'biblio.biblionumber');
342                         push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
343                         $i++;
344                     } else {
345                         my @subfields=$field->subfields();
346                         foreach my $subfieldcount (0..$#subfields) {
347                             my $subfield=$subfields[$subfieldcount][0];
348                             my $value=$subfields[$subfieldcount][1];
349                             next if (length $subfield !=1);
350                             next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
351                             push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
352                             $i++;
353                         }
354                     }
355 # now, loop again to add parameter subfield that are not in the MARC::Record
356                     foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
357                         next if (length $subfield !=1);
358                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
359                         next if ($tag<10);
360                         next if (($tagslib->{$tag}->{$subfield}->{hidden}<=-4) or ($tagslib->{$tag}->{$subfield}->{hidden}>=5) ); #check for visibility flag
361                         next if (defined($field->subfield($subfield)));
362                         push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
363                         $i++;
364                     }
365                     if ($#subfields_data >= 0) {
366                         my %tag_data;
367                         $tag_data{tag} = $tag;
368                         $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
369                         $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
370                         $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
371                         $tag_data{subfield_loop} = \@subfields_data;
372                         if ($tag<10) {
373                                                     $tag_data{fixedfield} = 1;
374                                             }
375
376                         push (@loop_data, \%tag_data);
377                     }
378 # If there is more than 1 field, add an empty hidden field as separator.
379                     if ($#fields >=1 && $#loop_data >=0 && $loop_data[$#loop_data]->{'tag'} eq $tag) {
380                         my @subfields_data;
381                         my %tag_data;
382                         push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
383                         $tag_data{tag} = '';
384                         $tag_data{tag_lib} = '';
385                         $tag_data{indicator} = '';
386                         $tag_data{subfield_loop} = \@subfields_data;
387                         if ($tag<10) {
388                                                        $tag_data{fixedfield} = 1;
389                                     }
390                         push (@loop_data, \%tag_data);
391                         $i++;
392                     }
393                 }
394     # if breeding is empty
395             } else {
396                 my @subfields_data;
397                 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
398                     next if (length $subfield !=1);
399                     next if (($tagslib->{$tag}->{$subfield}->{hidden}<=-5) or ($tagslib->{$tag}->{$subfield}->{hidden}>=4) ); #check for visibility flag
400                     next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
401                     push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
402                     $i++;
403                 }
404                 if ($#subfields_data >= 0) {
405                     my %tag_data;
406                     $tag_data{tag} = $tag;
407                     $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
408                     $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
409                     $tag_data{indicator} = $indicator;
410                     $tag_data{subfield_loop} = \@subfields_data;
411                     $tag_data{tagfirstsubfield} = $tag_data{subfield_loop}[0];
412                     if ($tag<10) {
413                         $tag_data{fixedfield} = 1;
414                     }
415                     push (@loop_data, \%tag_data);
416                 }
417             }
418         }
419         if ($#loop_data >=0) {
420             my %big_loop_line;
421             $big_loop_line{number}=$tabloop;
422             $big_loop_line{innerloop}=\@loop_data;
423             push @BIG_LOOP,\%big_loop_line;
424         }
425 #         $template->param($tabloop."XX" =>\@loop_data);
426     }
427     $template->param(BIG_LOOP => \@BIG_LOOP);
428 }
429
430
431
432 sub build_hidden_data () {
433     # build hidden data =>
434     # we store everything, even if we show only requested subfields.
435
436     my @loop_data =();
437     my $i=0;
438     foreach my $tag (keys %{$tagslib}) {
439     my $previous_tag = '';
440
441     # loop through each subfield
442     foreach my $subfield (keys %{$tagslib->{$tag}}) {
443         next if ($subfield eq 'lib');
444         next if ($subfield eq 'tab');
445         next if ($subfield eq 'mandatory');
446         next if ($subfield eq 'repeatable');
447         next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
448         my %subfield_data;
449         $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
450         $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
451         $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
452         $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
453         push(@loop_data, \%subfield_data);
454         $i++
455     }
456     }
457 }
458
459
460 # ======================== 
461 #          MAIN 
462 #=========================
463 my $input = new CGI;
464 my $error = $input->param('error');
465 my $biblionumber=$input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
466 my $breedingid = $input->param('breedingid');
467 my $z3950 = $input->param('z3950');
468 my $op = $input->param('op');
469 my $frameworkcode = $input->param('frameworkcode');
470 my $dbh = C4::Context->dbh;
471
472 $frameworkcode = &GetFrameworkCode($biblionumber) if ($biblionumber and not ($frameworkcode));
473
474 $frameworkcode='' if ($frameworkcode eq 'Default');
475 my ($template, $loggedinuser, $cookie)
476     = get_template_and_user({template_name => "cataloguing/addbiblio.tmpl",
477                  query => $input,
478                  type => "intranet",
479                  authnotrequired => 0,
480                  flagsrequired => {editcatalogue => 1},
481                  debug => 1,
482                  });
483
484 #Getting the list of all frameworks
485 my $queryfwk =$dbh->prepare("select frameworktext, frameworkcode from biblio_framework");
486 $queryfwk->execute;
487 my %select_fwk;
488 my @select_fwk;
489 my $curfwk;
490 push @select_fwk,"Default";
491 $select_fwk{"Default"} = "Default";
492 while (my ($description, $fwk) =$queryfwk->fetchrow) {
493     push @select_fwk, $fwk;
494     $select_fwk{$fwk} = $description;
495 }
496 $curfwk=$frameworkcode;
497 my $framework=CGI::scrolling_list( 
498             -name     => 'Frameworks',
499             -id => 'Frameworks',
500             -default => $curfwk,
501             -OnChange => 'Changefwk(this);',
502             -values   => \@select_fwk,
503             -labels   => \%select_fwk,
504             -size     => 1,
505             -multiple => 0 );
506 $template->param( framework => $framework, breedingid => $breedingid);
507
508 $tagslib = &GetMarcStructure($dbh,1,$frameworkcode);
509 my $record=-1;
510 my $encoding="";
511 $record = GetMarcBiblio( $biblionumber ) if ($biblionumber);
512 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
513
514 # warn "biblionumber : $biblionumber = ".$record->as_formatted;
515 $is_a_modif=0;
516 my ($biblionumtagfield,$biblionumtagsubfield);
517 my ($biblioitemnumtagfield,$biblioitemnumtagsubfield,$bibitem,$biblioitemnumber);
518 if ($biblionumber) {
519     $is_a_modif=1;
520     # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
521     ($biblionumtagfield,$biblionumtagsubfield) = &GetMarcFromKohaField("biblio.biblionumber",$frameworkcode);
522     ($biblioitemnumtagfield,$biblioitemnumtagsubfield) = &GetMarcFromKohaField("biblioitems.biblioitemnumber",$frameworkcode);
523     # search biblioitems value
524     my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
525     $sth->execute($biblionumber);
526     ($biblioitemnumber) = $sth->fetchrow;
527 }
528 #-------------------------------------------------------------------------------------
529 if ($op eq "addbiblio") {
530 #-------------------------------------------------------------------------------------
531     # rebuild
532     my @tags = $input->param('tag');
533     my @subfields = $input->param('subfield');
534     my @values = $input->param('field_value');
535     # build indicator hash.
536     my @ind_tag = $input->param('ind_tag');
537     my @indicator = $input->param('indicator');
538     if (C4::Context->preference('TemplateEncoding') eq "iso-8859-1") {
539         $record = TransformHtmlToMarc($dbh,\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
540     } else {
541         my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
542         $record=MARC::Record->new_from_xml($xml,C4::Context->preference('TemplateEncoding'),C4::Context->preference('marcflavour'));
543 #         warn "MARC :".$record->as_formatted;
544 #         die;
545     }
546     # check for a duplicate
547     my ($duplicatebiblionumber,$duplicatebiblionumber,$duplicatetitle) = FindDuplicate($record) if ($op eq "addbiblio") && (!$is_a_modif);
548     my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
549     # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
550     if (!$duplicatebiblionumber or $confirm_not_duplicate) {
551         # MARC::Record built => now, record in DB
552         my $oldbibnum;
553         my $oldbibitemnum;
554         if ($is_a_modif) {
555             ModBiblioframework($biblionumber,$frameworkcode);
556             ModBiblio($record,$biblionumber,$frameworkcode);
557         }
558         else {
559             ($biblionumber,$oldbibitemnum) = AddBiblio($record,$frameworkcode);
560         }
561     # now, redirect to additem page
562         print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
563         exit;
564     } else {
565     # it may be a duplicate, warn the user and do nothing
566         build_tabs ($template, $record, $dbh,$encoding);
567         build_hidden_data;
568         $template->param(
569             biblionumber             => $biblionumber,
570             biblionumtagfield        => $biblionumtagfield,
571             biblionumtagsubfield     => $biblionumtagsubfield,
572             biblioitemnumtagfield    => $biblioitemnumtagfield,
573             biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
574             biblioitemnumber         => $biblioitemnumber,
575             duplicatebiblionumber    => $duplicatebiblionumber,
576             duplicatebibid           => $duplicatebiblionumber,
577             duplicatetitle           => $duplicatetitle,
578         );
579     }
580 #--------------------------------------------------------------------------
581 } elsif ($op eq "addfield") {
582 #--------------------------------------------------------------------------
583     my $addedfield = $input->param('addfield_field');
584     my $cntrepeatfield=$input->param('repeat_field');
585     $cntrepeatfield=1 unless ($cntrepeatfield);
586     my $tagaddfield_subfield = $input->param('addfield_subfield');
587     my @tags = $input->param('tag');
588     my @subfields = $input->param('subfield');
589     my @values = $input->param('field_value');
590     # build indicator hash.
591     my @ind_tag = $input->param('ind_tag');
592     my @indicator = $input->param('indicator');
593     my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
594     my $record;
595     if (C4::Context->preference('TemplateEncoding') eq "iso-8859-1") {
596         my %indicators;
597         for (my $i=0;$i<=$#ind_tag;$i++) {
598             $indicators{$ind_tag[$i]} = $indicator[$i];
599         }
600         $record = TransformHtmlToMarc($dbh,\@tags,\@subfields,\@values,%indicators);
601     } else {
602         my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
603         $record=MARC::Record->new_from_xml($xml,C4::Context->preference('TemplateEncoding'),C4::Context->preference('marcflavour'));
604     }
605     for (my $i=1;$i<=$cntrepeatfield;$i++){
606         my $field = MARC::Field->new("$addedfield",'','',"$tagaddfield_subfield" => "");
607         $record->append_fields($field);
608     }
609     #warn "result : ".$record->as_formatted;
610     build_tabs ($template, $record, $dbh,$encoding);
611     build_hidden_data;
612     $template->param(
613         biblionumber             => $biblionumber,
614         biblionumtagfield        => $biblionumtagfield,
615         biblionumtagsubfield     => $biblionumtagsubfield,
616         biblioitemnumtagfield    => $biblioitemnumtagfield,
617         biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
618         biblioitemnumber         => $biblioitemnumber );
619 } elsif ($op eq "delete") {
620 #-----------------------------------------------------------------------------
621     my $error = &DelBiblio($biblionumber);
622     if ($error) {
623         warn "ERROR when DELETING BIBLIO $biblionumber : $error";
624         print "Content-Type: text/html\n\n<html><body><h1>ERROR when DELETING BIBLIO $biblionumber : $error</h1></body></html>";
625     } else {
626     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=/cgi-bin/koha/catalogue/search.pl?type=intranet\"></html>";
627     }
628     exit;
629 #----------------------------------------------------------------------------
630 } else {
631 #----------------------------------------------------------------------------
632     # If we're in a duplication case, we have to set to "" the biblionumber
633     # as we'll save the biblio as a new one.
634     if ($op eq "duplicate")
635     {
636         $biblionumber= "";
637     }
638     #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
639     eval {
640         my $uxml = $record->as_xml;
641         MARC::Record::default_record_format("UNIMARC") if (C4::Context->preference("marcflavour") eq "UNIMARC");
642         my $urecord = MARC::Record::new_from_xml($uxml, 'UTF-8');
643         $record = $urecord;
644     };
645     build_tabs ($template, $record, $dbh,$encoding);
646     build_hidden_data;
647     $template->param(
648         biblionumber             => $biblionumber,
649         biblionumtagfield        => $biblionumtagfield,
650         biblionumtagsubfield     => $biblionumtagsubfield,
651         biblioitemnumtagfield    => $biblioitemnumtagfield,
652         biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
653         biblioitemnumber         => $biblioitemnumber,
654         );
655 }
656 $template->param( title => $record->title() ) if ($record ne "-1");
657 $template->param(
658         frameworkcode => $frameworkcode,
659         itemtype => $frameworkcode, # HINT: if the library has itemtype = framework, itemtype is auto filled !
660         hide_marc => C4::Context->preference('hide_marc'),
661         intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
662         intranetstylesheet => C4::Context->preference("intranetstylesheet"),
663         IntranetNav => C4::Context->preference("IntranetNav"),
664         advancedMARCEditor => C4::Context->preference("advancedMARCEditor"),
665         );
666 output_html_with_http_headers $input, $cookie, $template->output;