New scripts for translation into Chinese and other languages where English
[koha.git] / acqui.simple / 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::Context;
29 use C4::Koha; # XXX subfield_is_koha_internal_p
30 use HTML::Template;
31 use MARC::File::USMARC;
32
33 use vars qw( $tagslib);
34 use vars qw( $authorised_values_sth);
35 use vars qw( $is_a_modif );
36
37 =item find_value
38
39     ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
40
41 Find the given $subfield in the given $tag in the given
42 MARC::Record $record.  If the subfield is found, returns
43 the (indicators, value) pair; otherwise, (undef, undef) is
44 returned.
45
46 =cut
47
48 sub find_value {
49         my ($tagfield,$insubfield,$record,$encoding) = @_;
50         my @result;
51         my $indicator;
52         if ($tagfield <10) {
53                 if ($record->field($tagfield)) {
54                         push @result, $record->field($tagfield)->data();
55                 } else {
56                         push @result,"";
57                 }
58         } else {
59                 foreach my $field ($record->field($tagfield)) {
60                         my @subfields = $field->subfields();
61                         foreach my $subfield (@subfields) {
62                                 if (@$subfield[0] eq $insubfield) {
63                                         push @result,char_decode(@$subfield[1],$encoding);
64                                         $indicator = $field->indicator(1).$field->indicator(2);
65                                 }
66                         }
67                 }
68         }
69         return($indicator,@result);
70 }
71
72
73 =item MARCfindbreeding
74
75     $record = MARCfindbreeding($dbh, $breedingid);
76
77 Look up the breeding farm with database handle $dbh, for the
78 record with id $breedingid.  If found, returns the decoded
79 MARC::Record; otherwise, -1 is returned (FIXME).
80 Returns as second parameter the character encoding.
81
82 =cut
83
84 sub MARCfindbreeding {
85         my ($dbh,$id) = @_;
86         my $sth = $dbh->prepare("select file,marc,encoding from marc_breeding where id=?");
87         $sth->execute($id);
88         my ($file,$marc,$encoding) = $sth->fetchrow;
89         if ($marc) {
90                 my $record = MARC::File::USMARC::decode($marc);
91                 if (ref($record) eq undef) {
92                         return -1;
93                 } else {
94                         return $record,$encoding;
95                 }
96         }
97         return -1;
98 }
99
100
101 =item build_authorized_values_list
102
103 =cut
104
105 sub build_authorized_values_list ($$$$$) {
106     my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
107
108     my @authorised_values;
109     my %authorised_lib;
110
111     # builds list, depending on authorised value...
112
113     #---- branch
114     if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
115         my $sth=$dbh->prepare("select branchcode,branchname from branches");
116         $sth->execute;
117         push @authorised_values, ""
118                 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
119
120         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
121             push @authorised_values, $branchcode;
122             $authorised_lib{$branchcode}=$branchname;
123         }
124
125     #----- itemtypes
126     } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
127         my $sth=$dbh->prepare("select itemtype,description from itemtypes");
128         $sth->execute;
129         push @authorised_values, ""
130                 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
131
132         while (my ($itemtype,$description) = $sth->fetchrow_array) {
133             push @authorised_values, $itemtype;
134             $authorised_lib{$itemtype}=$description;
135         }
136
137     #---- "true" authorised value
138     } else {
139         $authorised_values_sth->execute
140                 ($tagslib->{$tag}->{$subfield}->{authorised_value});
141
142         push @authorised_values, ""
143                 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
144
145         while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
146             push @authorised_values, $value;
147             $authorised_lib{$value}=$lib;
148         }
149     }
150     return CGI::scrolling_list( -name     => 'field_value',
151                                 -values   => \@authorised_values,
152                                 -default  => $value,
153                                 -labels   => \%authorised_lib,
154                                 -size     => 1,
155                                 -multiple => 0 );
156 }
157
158 =item create_input
159  builds the <input ...> entry for a subfield.
160 =cut
161 sub create_input () {
162         my ($tag,$subfield,$value,$i,$tabloop,$rec,$authorised_values_sth) = @_;
163         $value =~ s/"/&quot;/g;
164         my $dbh = C4::Context->dbh;
165         my %subfield_data;
166         $subfield_data{tag}=$tag;
167         $subfield_data{subfield}=$subfield;
168         $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
169         $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
170         $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
171         $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
172         $subfield_data{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
173         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
174                 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
175         } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
176                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  size=47 maxlength=255 DISABLE READONLY> <a href=\"javascript:Dopop('../thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">...</a>";
177         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
178                 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
179                 require $plugin;
180                 my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
181                 my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
182                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  value=\"$value\" DISABLE READONLY size=47 maxlength=255 OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i)\"> <a href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
183         } elsif  ($tag eq '') {
184                 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value\" size=50 maxlength=255>"; #"
185         } else {
186                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>"; #"
187         }
188         return \%subfield_data;
189 }
190
191 sub build_tabs ($$$$) {
192     my($template, $record, $dbh,$encoding) = @_;
193
194     # fill arrays
195     my @loop_data =();
196     my $tag;
197     my $i=0;
198         my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
199                 from authorised_values
200                 where category=? order by lib");
201
202 # loop through each tab 0 through 9
203         for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
204                 my @loop_data = ();
205                 foreach my $tag (sort(keys (%{$tagslib}))) {
206                         my $indicator;
207         # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
208         # if MARC::Record is empty => use tab as master loop.
209                         if ($record ne -1 && $record->field($tag)) {
210                                 my @fields = $record->field($tag);
211                                 foreach my $field (@fields)  {
212                                         my @subfields_data;
213                                         if ($tag<10) {
214                                                 my $value=$field->data();
215                                                 my $subfield="@";
216                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
217                                                 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$tabloop,$record,$authorised_values_sth));
218                                                 $i++;
219                                         } else {
220                                                 my @subfields=$field->subfields();
221                                                 foreach my $subfieldcount (0..$#subfields) {
222                                                         my $subfield=$subfields[$subfieldcount][0];
223                                                         my $value=$subfields[$subfieldcount][1];
224                                                         next if subfield_is_koha_internal_p($subfield);
225                                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
226                                                         push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$tabloop,$record,$authorised_values_sth));
227                                                         $i++;
228                                                 }
229                                         }
230 # now, loop again to add parameter subfield that are not in the MARC::Record
231                                         foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
232                                                 next if subfield_is_koha_internal_p($subfield);
233                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
234                                                 next if ($tag<10);
235                                                 next if (defined($record->field($tag)->subfield($subfield)));
236                                                 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
237                                                 $i++;
238                                         }
239                                         if ($#subfields_data >= 0) {
240                                                 my %tag_data;
241                                                 $tag_data{tag} = $tag;
242                                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
243                                                 $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
244                                                 $tag_data{subfield_loop} = \@subfields_data;
245                                                 push (@loop_data, \%tag_data);
246                                         }
247 # If there is more than 1 field, add an empty hidden field as separator.
248                                         if ($#fields >=1) {
249                                                 my @subfields_data;
250                                                 my %tag_data;
251                                                 push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
252                                                 $tag_data{tag} = '';
253                                                 $tag_data{tag_lib} = '';
254                                                 $tag_data{indicator} = '';
255                                                 $tag_data{subfield_loop} = \@subfields_data;
256                                                 push (@loop_data, \%tag_data);
257                                                 $i++;
258                                         }
259                                 }
260         # if breeding is empty
261                         } else {
262                                 my @subfields_data;
263                                 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
264                                         next if subfield_is_koha_internal_p($subfield);
265                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
266                                         push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
267                                         $i++;
268                                 }
269                                 if ($#subfields_data >= 0) {
270                                         my %tag_data;
271                                         $tag_data{tag} = $tag;
272                                         $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
273                                         $tag_data{indicator} = $indicator;
274                                         $tag_data{subfield_loop} = \@subfields_data;
275                                         push (@loop_data, \%tag_data);
276                                 }
277                         }
278                 }
279                 $template->param($tabloop."XX" =>\@loop_data);
280         }
281 }
282
283
284 sub build_hidden_data () {
285     # build hidden data =>
286     # we store everything, even if we show only requested subfields.
287
288     my @loop_data =();
289     my $i=0;
290     foreach my $tag (keys %{$tagslib}) {
291         my $previous_tag = '';
292
293         # loop through each subfield
294         foreach my $subfield (keys %{$tagslib->{$tag}}) {
295             next if ($subfield eq 'lib');
296             next if ($subfield eq 'tab');
297             next if ($subfield eq 'mandatory');
298             next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
299             my %subfield_data;
300             $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
301             $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
302             $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
303             $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
304             push(@loop_data, \%subfield_data);
305             $i++
306         }
307     }
308 }
309
310 my $input = new CGI;
311 my $error = $input->param('error');
312 my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio.
313 my $breedingid = $input->param('breedingid');
314 my $z3950 = $input->param('z3950');
315 my $op = $input->param('op');
316 my $dbh = C4::Context->dbh;
317 my $bibid;
318 if ($oldbiblionumber) {
319         $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
320 }else {
321         $bibid = $input->param('bibid');
322 }
323 my ($template, $loggedinuser, $cookie)
324     = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
325                              query => $input,
326                              type => "intranet",
327                              authnotrequired => 0,
328                              flagsrequired => {editcatalogue => 1},
329                              debug => 1,
330                              });
331
332 $tagslib = &MARCgettagslib($dbh,1);
333 my $record=-1;
334 my $encoding="";
335 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
336 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
337
338 $is_a_modif=0;
339 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
340 my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
341 if ($bibid) {
342         $is_a_modif=1;
343         # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
344         ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber");
345         ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber");
346         # search biblioitems value
347         my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
348         $sth->execute($oldbiblionumber);
349         ($oldbiblioitemnumber) = $sth->fetchrow;
350 }
351 #------------------------------------------------------------------------------------------------------------------------------
352 if ($op eq "addbiblio") {
353 #------------------------------------------------------------------------------------------------------------------------------
354         # rebuild
355         my @tags = $input->param('tag');
356         my @subfields = $input->param('subfield');
357         my @values = $input->param('field_value');
358         # build indicator hash.
359         my @ind_tag = $input->param('ind_tag');
360         my @indicator = $input->param('indicator');
361         my %indicators;
362         for (my $i=0;$i<=$#ind_tag;$i++) {
363                 $indicators{$ind_tag[$i]} = $indicator[$i];
364         }
365         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
366 # MARC::Record built => now, record in DB
367         my $oldbibnum;
368         my $oldbibitemnum;
369         if ($is_a_modif) {
370                  NEWmodbiblio($dbh,$record,$bibid);
371         } else {
372                 ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record);
373         }
374 # now, redirect to additem page
375         print $input->redirect("additem.pl?bibid=$bibid");
376         exit;
377 #------------------------------------------------------------------------------------------------------------------------------
378 } elsif ($op eq "addfield") {
379 #------------------------------------------------------------------------------------------------------------------------------
380         my $addedfield = $input->param('addfield_field');
381         my @tags = $input->param('tag');
382         my @subfields = $input->param('subfield');
383         my @values = $input->param('field_value');
384         # build indicator hash.
385         my @ind_tag = $input->param('ind_tag');
386         my @indicator = $input->param('indicator');
387         splice(@tags,$addedfield,0,$tags[$addedfield]);
388         splice(@subfields,$addedfield,0,$subfields[$addedfield]);
389         splice(@values,$addedfield,0,$values[$addedfield]);
390         splice(@ind_tag,$addedfield,0,$ind_tag[$addedfield]);
391         my %indicators;
392         for (my $i=0;$i<=$#ind_tag;$i++) {
393                 $indicators{$ind_tag[$i]} = $indicator[$i];
394         }
395 # search the part of the array to duplicate.
396         my $start=0;
397         my $end=0;
398         my $started;
399         for (my $i=0;$i<=$#tags;$i++) {
400                 $start=$i if ($start eq 0 && $tags[$i] == $addedfield);
401                 $end=$i if ($start>0 && $tags[$i] eq $addedfield);
402                 last if ($start>0 && $tags[$i] ne $addedfield);
403         }
404 # add an empty line in all arrays. This forces a new field in MARC::Record.
405         splice(@tags,$end+1,0,'');
406         splice(@subfields,$end+1,0,'');
407         splice(@values,$end+1,0,'');
408         splice(@ind_tag,$end+1,0,'');
409         splice(@indicator,$end+1,0,'');
410 # then duplicate the field.
411         splice(@tags,$end+2,0,@tags[$start..$end]);
412         splice(@subfields,$end+2,0,@subfields[$start..$end]);
413         splice(@values,$end+2,0,@values[$start..$end]);
414         splice(@ind_tag,$end+2,0,@ind_tag[$start..$end]);
415         splice(@indicator,$end+2,0,@indicator[$start..$end]);
416
417         my %indicators;
418         for (my $i=0;$i<=$#ind_tag;$i++) {
419                 $indicators{$ind_tag[$i]} = $indicator[$i];
420         }
421         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
422         build_tabs ($template, $record, $dbh,$encoding);
423         build_hidden_data;
424         $template->param(
425                 oldbiblionumber             => $oldbiblionumber,
426                 bibid                       => $bibid,
427                 oldbiblionumtagfield        => $oldbiblionumtagfield,
428                 oldbiblionumtagsubfield     => $oldbiblionumtagsubfield,
429                 oldbiblioitemnumtagfield    => $oldbiblioitemnumtagfield,
430                 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
431                 oldbiblioitemnumber         => $oldbiblioitemnumber );
432 } elsif ($op eq "delete") {
433 #------------------------------------------------------------------------------------------------------------------------------
434         &NEWdelbiblio($dbh,$bibid);
435         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=/cgi-bin/koha/search.marc/search.pl?type=intranet\"></html>";
436         exit;
437 #------------------------------------------------------------------------------------------------------------------------------#------------------------------------------------------------------------------------------------------------------------------
438 } else {
439 #------------------------------------------------------------------------------------------------------------------------------
440         build_tabs ($template, $record, $dbh,$encoding);
441         build_hidden_data;
442         $template->param(
443                 oldbiblionumber             => $oldbiblionumber,
444                 bibid                       => $bibid,
445                 oldbiblionumtagfield        => $oldbiblionumtagfield,
446                 oldbiblionumtagsubfield     => $oldbiblionumtagsubfield,
447                 oldbiblioitemnumtagfield    => $oldbiblioitemnumtagfield,
448                 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
449                 oldbiblioitemnumber         => $oldbiblioitemnumber );
450 }
451 output_html_with_http_headers $input, $cookie, $template->output;