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