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