character encoding ISO646 => 8859-1, first draft
[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::Biblio;
27 use C4::Context;
28 use HTML::Template;
29 use MARC::File::USMARC;
30
31 sub find_value {
32         my ($tagfield,$insubfield,$record) = @_;
33 #       warn "$tagfield / $insubfield // ";
34         my $result;
35         my $indicator;
36         foreach my $field ($record->field($tagfield)) {
37                 my @subfields = $field->subfields();
38                 foreach my $subfield (@subfields) {
39                         if (@$subfield[0] eq $insubfield) {
40                                 $result .= @$subfield[1];
41                                 $indicator = $field->indicator(1).$field->indicator(2);
42                         }
43                 }
44         }
45         return($indicator,$result);
46 }
47
48 sub MARCfindbreeding {
49         my ($dbh,$isbn) = @_;
50         my $sth = $dbh->prepare("select file,marc from marc_breeding where isbn=?");
51         $sth->execute($isbn);
52         my ($file,$marc) = $sth->fetchrow;
53 #       $marc = char_decode($marc);
54         if ($marc) {
55                 my $record = MARC::File::USMARC::decode($marc);
56                 if (ref($record) eq undef) {
57                         return -1;
58                 } else {
59                         return $record;
60                 }
61         }
62         return -1;
63 }
64
65 # some special chars in ISO 2709 (ISO 6630 and ISO 646 set)
66
67 my $IS3 = '\x1d' ;              # IS3 : record end
68 my $IS2 = '\x1e' ;              # IS2 : field end
69 my $IS1 = '\x1f' ;              # IS1 : begin subfield
70 my $NSB = '\x88' ;              # NSB : begin Non Sorting Block
71 my $NSE = '\x89' ;              # NSE : Non Sorting Block end
72
73 sub char_decode {
74         # converts ISO 5426 coded string to ISO 8859-1
75         # sloppy code : should be improved in next issue
76         my ($string) = @_ ;
77         $_ = $string ;
78         if(/[\xc1-\xff]/) {
79                 s/\xe1/Æ/gm ;
80                 s/\xe2/Ð/gm ;
81                 s/\xe9/Ø/gm ;
82                 s/\xec/þ/gm ;
83                 s/\xf1/æ/gm ;
84                 s/\xf3/ð/gm ;
85                 s/\xf9/ø/gm ;
86                 s/\xfb/ß/gm ;
87                 s/\xc1\x61/à/gm ;
88                 s/\xc1\x65/è/gm ;
89                 s/\xc1\x69/ì/gm ;
90                 s/\xc1\x6f/ò/gm ;
91                 s/\xc1\x75/ù/gm ;
92                 s/\xc1\x41/À/gm ;
93                 s/\xc1\x45/È/gm ;
94                 s/\xc1\x49/Ì/gm ;
95                 s/\xc1\x4f/Ò/gm ;
96                 s/\xc1\x55/Ù/gm ;
97                 s/\xc2\x41/Á/gm ;
98                 s/\xc2\x45/É/gm ;
99                 s/\xc2\x49/Í/gm ;
100                 s/\xc2\x4f/Ó/gm ;
101                 s/\xc2\x55/Ú/gm ;
102                 s/\xc2\x59/Ý/gm ;
103                 s/\xc2\x61/á/gm ;
104                 s/\xc2\x65/é/gm ;
105                 s/\xc2\x69/í/gm ;
106                 s/\xc2\x6f/ó/gm ;
107                 s/\xc2\x75/ú/gm ;
108                 s/\xc2\x79/ý/gm ;
109                 s/\xc3\x41/Â/gm ;
110                 s/\xc3\x45/Ê/gm ;
111                 s/\xc3\x49/Î/gm ;
112                 s/\xc3\x4f/Ô/gm ;
113                 s/\xc3\x55/Û/gm ;
114                 s/\xc3\x61/â/gm ;
115                 s/\xc3\x65/ê/gm ;
116                 s/\xc3\x69/î/gm ;
117                 s/\xc3\x6f/ô/gm ;
118                 s/\xc3\x75/û/gm ;
119                 s/\xc4\x41/Ã/gm ;
120                 s/\xc4\x4e/Ñ/gm ;
121                 s/\xc4\x4f/Õ/gm ;
122                 s/\xc4\x61/ã/gm ;
123                 s/\xc4\x6e/ñ/gm ;
124                 s/\xc4\x6f/õ/gm ;
125                 s/\xc8\x45/Ë/gm ;
126                 s/\xc8\x49/Ï/gm ;
127                 s/\xc8\x65/ë/gm ;
128                 s/\xc8\x69/ï/gm ;
129                 s/\xc8\x76/ÿ/gm ;
130                 s/\xc9\x41/Ä/gm ;
131                 s/\xc9\x4f/Ö/gm ;
132                 s/\xc9\x55/Ü/gm ;
133                 s/\xc9\x61/ä/gm ;
134                 s/\xc9\x6f/ö/gm ;
135                 s/\xc9\x75/ü/gm ;
136                 s/\xca\x41/Å/gm ;
137                 s/\xca\x61/å/gm ;
138                 s/\xd0\x43/Ç/gm ;
139                 s/\xd0\x63/ç/gm ;
140         }
141         # this handles non-sorting blocks (if implementation requires this)
142         $string = nsb_clean($_) ;
143         return($string) ;
144 }
145
146 sub nsb_clean {
147         # handles non sorting blocks
148         my ($string) = @_ ;
149         $_ = $string ;
150         s/$NSB/(/gm ;
151         s/[ ]{0,1}$NSE/) /gm ;
152         $string = $_ ;
153         return($string) ;
154 }
155
156 my $input = new CGI;
157 my $error = $input->param('error');
158 my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio.
159 my $isbn = $input->param('isbn');
160 my $op = $input->param('op');
161 my $dbh = C4::Context->dbh;
162 my $bibid;
163 if ($oldbiblionumber) {
164         $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
165 }else {
166         $bibid = $input->param('bibid');
167 }
168 my ($template, $loggedinuser, $cookie)
169     = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
170                              query => $input,
171                              type => "intranet",
172                              authnotrequired => 0,
173                              flagsrequired => {catalogue => 1},
174                              debug => 1,
175                              });
176
177 my $tagslib = &MARCgettagslib($dbh,1);
178 my $record=-1;
179 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
180 #warn "1= ".$record->as_formatted;
181 $record = MARCfindbreeding($dbh,$isbn) if ($isbn);
182 my $is_a_modif=0;
183 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
184 my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
185 if ($bibid) {
186         $is_a_modif=1;
187         # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
188         ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber");
189         ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber");
190         # search biblioitems value
191         my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
192         $sth->execute($oldbiblionumber);
193         ($oldbiblioitemnumber) = $sth->fetchrow;
194 }
195 #------------------------------------------------------------------------------------------------------------------------------
196 if ($op eq "addbiblio") {
197 #------------------------------------------------------------------------------------------------------------------------------
198         # rebuild
199         my @tags = $input->param('tag');
200         my @subfields = $input->param('subfield');
201         my @values = $input->param('field_value');
202         # build indicator hash.
203         my @ind_tag = $input->param('ind_tag');
204         my @indicator = $input->param('indicator');
205         my %indicators;
206         for (my $i=0;$i<=$#ind_tag;$i++) {
207                 $indicators{$ind_tag[$i]} = $indicator[$i];
208         }
209         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
210 # MARC::Record builded => now, record in DB
211         my $oldbibnum;
212         my $oldbibitemnum;
213         if ($is_a_modif) {
214                  NEWmodbiblio($dbh,$record,$bibid);
215         } else {
216                 ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record);
217         }
218 # now, redirect to additem page
219         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=additem.pl?bibid=$bibid\"></html>";
220         exit;
221 #------------------------------------------------------------------------------------------------------------------------------
222 } else {
223 #------------------------------------------------------------------------------------------------------------------------------
224         # fill arrays
225         my @loop_data =();
226         my $tag;
227         my $i=0;
228         my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by authorised_value");
229 # loop through each tab 0 through 9
230         for (my $tabloop = 0; $tabloop<=9;$tabloop++) {
231         #       my @fields = $record->fields();
232                 my @loop_data =();
233                 foreach my $tag (sort(keys (%{$tagslib}))) {
234                         my $previous_tag = '';
235                         my @subfields_data;
236                         my $indicator;
237 # loop through each subfield
238                         foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
239                                 next if ($subfield eq 'lib'); # skip lib and tabs, which are koha internal
240                                 next if ($subfield eq 'tab');
241                                 next if ($tagslib->{$tag}->{$subfield}->{tab}  ne $tabloop);
242                                 my %subfield_data;
243                                 $subfield_data{tag}=$tag;
244                                 $subfield_data{subfield}=$subfield;
245                                 $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
246                                 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
247                                 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
248                                 # if breeding is not empty
249                                 if ($record ne -1) {
250                                         my ($x,$value) = find_value($tag,$subfield,$record);
251                                         $value=char_decode($value);
252                                         $indicator = $x if $x;
253                                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
254                                                 my @authorised_values;
255                                                 my %authorised_lib;
256                                                 # builds list, depending on authorised value...
257                                                 #---- branch
258                                                 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
259                                                         my $sth=$dbh->prepare("select branchcode,branchname from branches");
260                                                         $sth->execute;
261                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
262                                                         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
263                                                                 push @authorised_values, $branchcode;
264                                                                 $authorised_lib{$branchcode}=$branchname;
265                                                         }
266                                                 #----- itemtypes
267                                                 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
268                                                         my $sth=$dbh->prepare("select itemtype,description from itemtypes");
269                                                         $sth->execute;
270                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
271                                                         while (my ($itemtype,$description) = $sth->fetchrow_array) {
272                                                                 push @authorised_values, $itemtype;
273                                                                 $authorised_lib{$itemtype}=$description;
274                                                         }
275                                                 #---- "true" authorised value
276                                                 } else {
277                                                         $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
278                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
279                                                         while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
280                                                                 push @authorised_values, $value;
281                                                                 $authorised_lib{$value}=$lib;
282                                                         }
283                                                 }
284                                                 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
285                                                                                                                                                                         -values=> \@authorised_values,
286                                                                                                                                                                         -default=>"$value",
287                                                                                                                                                                         -labels => \%authorised_lib,
288                                                                                                                                                                         -size=>1,
289                                                                                                                                                                         -multiple=>0,
290                                                                                                                                                                         );
291                                         } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
292                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  size=47 maxlength=255> <a href=\"javascript:Dopop('../thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">...</a>"; #"
293                                         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
294                                                 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
295                                                 require $plugin;
296                                                 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
297                                                 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
298                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  value=\"$value\" 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";
299                                         } else {
300                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
301                                         }
302                                 # if breeding is empty
303                                 } else {
304                                         my ($x,$value);
305                                         ($x,$value) = find_value($tag,$subfield,$record) if ($record ne -1);
306                                         $value=char_decode($value);
307                                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
308                                                 my @authorised_values;
309                                                 my %authorised_lib;
310                                                 # builds list, depending on authorised value...
311                                                 #---- branch
312                                                 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
313                                                         my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchcode");
314                                                         $sth->execute;
315                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
316                                                         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
317                                                                 push @authorised_values, $branchcode;
318                                                                 $authorised_lib{$branchcode}=$branchname;
319                                                         }
320                                                 #----- itemtypes
321                                                 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
322                                                         my $sth=$dbh->prepare("select itemtype,description from itemtypes order by itemtype");
323                                                         $sth->execute;
324                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
325                                                         while (my ($itemtype,$description) = $sth->fetchrow_array) {
326                                                                 push @authorised_values, $itemtype;
327                                                                 $authorised_lib{$itemtype}=$description;
328                                                         }
329                                                 #---- "true" authorised value
330                                                 } else {
331                                                         $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
332                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
333                                                         while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
334                                                                 push @authorised_values, $value;
335                                                                 $authorised_lib{$value}=$lib;
336                                                         }
337                                                 }
338                                                 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
339                                                                                                                                                                         -values=> \@authorised_values,
340                                                                                                                                                                         -default=>"$value",
341                                                                                                                                                                         -labels => \%authorised_lib,
342                                                                                                                                                                         -size=>1,
343                                                                                                                                                                         -multiple=>0,
344                                                                                                                                                                         );
345                                         } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
346                                                 $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>";
347                                         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
348                                                 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
349                                                 require $plugin;
350                                                 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
351                                                 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
352                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  size=47 maxlength=255 DISABLE READONLY OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i)\"> <a href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
353                                         } else {
354                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" size=50 maxlength=255>";
355                                         }
356                                 }
357                                 push(@subfields_data, \%subfield_data);
358                                 $i++;
359                         }
360                         if ($#subfields_data>=0) {
361                                 my %tag_data;
362                                 $tag_data{tag}=$tag;
363                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
364                                 $tag_data{indicator} = $indicator;
365                                 $tag_data{subfield_loop} = \@subfields_data;
366                                 push (@loop_data, \%tag_data);
367                         }
368                 }
369                 $template->param($tabloop."XX" =>\@loop_data);
370         }
371         # now, build hidden datas => we store everything, even if we show only requested subfields.
372         my @loop_data =();
373         my $i=0;
374         foreach my $tag (keys %{$tagslib}) {
375                 my $previous_tag = '';
376         # loop through each subfield
377                 foreach my $subfield (keys %{$tagslib->{$tag}}) {
378                         next if ($subfield eq 'lib');
379                         next if ($subfield eq 'tab');
380                         next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
381                         my %subfield_data;
382                         $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
383                         $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
384                         $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
385                         $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
386                         push(@loop_data, \%subfield_data);
387                         $i++
388                 }
389         }
390         $template->param(
391                                                         oldbiblionumber => $oldbiblionumber,
392                                                         bibid => $bibid,
393                                                         oldbiblionumtagfield => $oldbiblionumtagfield,
394                                                         oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
395                                                         oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
396                                                         oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
397                                                         oldbiblioitemnumber => $oldbiblioitemnumber);
398 }
399 print $input->header(-cookie => $cookie),$template->output;