Fix some bugs :
[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( $is_a_modif );
35
36
37 =item find_value
38
39     ($indicators, $value) = find_value($tag, $subfield, $record);
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) = @_;
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,@$subfield[1];
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
81 =cut
82
83 sub MARCfindbreeding {
84         my ($dbh,$id) = @_;
85         my $sth = $dbh->prepare("select file,marc from marc_breeding where id=?");
86         $sth->execute($id);
87         my ($file,$marc) = $sth->fetchrow;
88         if ($marc) {
89                 my $record = MARC::File::USMARC::decode($marc);
90                 if (ref($record) eq undef) {
91                         return -1;
92                 } else {
93                         return $record;
94                 }
95         }
96         return -1;
97 }
98
99
100 =item build_authorized_values_list
101
102 =cut
103
104 sub build_authorized_values_list ($$$$$) {
105     my($tag, $subfield, $value, $dbh, $authorised_values_sth) = @_;
106
107     my @authorised_values;
108     my %authorised_lib;
109
110     # builds list, depending on authorised value...
111
112     #---- branch
113     if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
114         my $sth=$dbh->prepare("select branchcode,branchname from branches");
115         $sth->execute;
116         push @authorised_values, ""
117                 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
118
119         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
120             push @authorised_values, $branchcode;
121             $authorised_lib{$branchcode}=$branchname;
122         }
123
124     #----- itemtypes
125     } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
126         my $sth=$dbh->prepare("select itemtype,description from itemtypes");
127         $sth->execute;
128         push @authorised_values, ""
129                 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
130
131         while (my ($itemtype,$description) = $sth->fetchrow_array) {
132             push @authorised_values, $itemtype;
133             $authorised_lib{$itemtype}=$description;
134         }
135
136     #---- "true" authorised value
137     } else {
138         $authorised_values_sth->execute
139                 ($tagslib->{$tag}->{$subfield}->{authorised_value});
140
141         push @authorised_values, ""
142                 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
143
144         while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
145             push @authorised_values, $value;
146             $authorised_lib{$value}=$lib;
147         }
148     }
149     return CGI::scrolling_list( -name     => 'field_value',
150                                 -values   => \@authorised_values,
151                                 -default  => $value,
152                                 -labels   => \%authorised_lib,
153                                 -size     => 1,
154                                 -multiple => 0 );
155 }
156
157 sub build_tabs ($$$) {
158     my($template, $record, $dbh) = @_;
159
160     # fill arrays
161     my @loop_data =();
162     my $tag;
163     my $i=0;
164     my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
165         from authorised_values
166         where category=? order by authorised_value");
167
168     # loop through each tab 0 through 9
169     for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
170     #   my @fields = $record->fields();
171         my @loop_data = ();
172         foreach my $tag (sort(keys (%{$tagslib}))) {
173                 my $previous_tag = '';
174                 my @subfields_data;
175                 my $indicator;
176
177                 # loop through each subfield
178                 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
179                         next if subfield_is_koha_internal_p($subfield);
180                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
181                         # if breeding is not empty
182                         if ($record ne -1) {
183                                 my ($x,@value) = find_value($tag,$subfield,$record);
184                                 push (@value,"") if ($#value eq -1);
185                                 foreach my $value (@value) {
186                                         my %subfield_data;
187                                         $subfield_data{tag}=$tag;
188                                         $subfield_data{subfield}=$subfield;
189                                         $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
190                                         $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
191                                         $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
192                                         $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
193                                         $value=char_decode($value) unless ($is_a_modif);
194                                         $indicator = $x if $x; #XXX
195                                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
196                                                 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh, $authorised_values_sth);
197                                         } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
198                                                 $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>"; #"
199                                         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
200                                                 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
201                                                 require $plugin;
202                                                 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
203                                                 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
204                                                 $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";
205                                         } else {
206                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
207                                         }
208                                         push(@subfields_data, \%subfield_data);
209                                         $i++;
210                                 }
211                 # if breeding is empty
212                         } else {
213                                 my ($x,$value);
214                                 ($x,$value) = find_value($tag,$subfield,$record) if ($record ne -1);
215                                 $value=char_decode($value) unless ($is_a_modif);
216                                         my %subfield_data;
217                                         $subfield_data{tag}=$tag;
218                                         $subfield_data{subfield}=$subfield;
219                                         $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
220                                         $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
221                                         $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
222                                         $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
223                                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
224                                                 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh, $authorised_values_sth);
225                                         } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
226                                                 $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>";
227                                         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
228                                                 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
229                                                 require $plugin;
230                                                 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
231                                                 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
232                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_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";
233                                         } else {
234                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" size=50 maxlength=255>";
235                                         }
236                                         push(@subfields_data, \%subfield_data);
237                                         $i++;
238                                 }
239                         }
240                         if ($#subfields_data >= 0) {
241                                 my %tag_data;
242                                 $tag_data{tag} = $tag;
243                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
244                                 $tag_data{indicator} = $indicator;
245                                 $tag_data{subfield_loop} = \@subfields_data;
246                                 push (@loop_data, \%tag_data);
247                         }
248                 }
249                 $template->param($tabloop."XX" =>\@loop_data);
250         }
251 }
252
253
254 sub build_hidden_data () {
255     # build hidden data =>
256     # we store everything, even if we show only requested subfields.
257
258     my @loop_data =();
259     my $i=0;
260     foreach my $tag (keys %{$tagslib}) {
261         my $previous_tag = '';
262
263         # loop through each subfield
264         foreach my $subfield (keys %{$tagslib->{$tag}}) {
265             next if ($subfield eq 'lib');
266             next if ($subfield eq 'tab');
267             next if ($subfield eq 'mandatory');
268             next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
269             my %subfield_data;
270             $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
271             $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
272             $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
273             $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
274             push(@loop_data, \%subfield_data);
275             $i++
276         }
277     }
278 }
279
280
281 my $input = new CGI;
282 my $error = $input->param('error');
283 my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio.
284 my $breedingid = $input->param('breedingid');
285 my $op = $input->param('op');
286 my $dbh = C4::Context->dbh;
287 my $bibid;
288 if ($oldbiblionumber) {
289         $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
290 }else {
291         $bibid = $input->param('bibid');
292 }
293 my ($template, $loggedinuser, $cookie)
294     = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
295                              query => $input,
296                              type => "intranet",
297                              authnotrequired => 0,
298                              flagsrequired => {catalogue => 1},
299                              debug => 1,
300                              });
301
302 $tagslib = &MARCgettagslib($dbh,1);
303 my $record=-1;
304 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
305 $record = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
306 $is_a_modif=0;
307 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
308 my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
309 if ($bibid) {
310         $is_a_modif=1;
311         # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
312         ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber");
313         ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber");
314         # search biblioitems value
315         my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
316         $sth->execute($oldbiblionumber);
317         ($oldbiblioitemnumber) = $sth->fetchrow;
318 }
319 #------------------------------------------------------------------------------------------------------------------------------
320 if ($op eq "addbiblio") {
321 #------------------------------------------------------------------------------------------------------------------------------
322         # rebuild
323         my @tags = $input->param('tag');
324         my @subfields = $input->param('subfield');
325         my @values = $input->param('field_value');
326         # build indicator hash.
327         my @ind_tag = $input->param('ind_tag');
328         my @indicator = $input->param('indicator');
329         my %indicators;
330         for (my $i=0;$i<=$#ind_tag;$i++) {
331                 $indicators{$ind_tag[$i]} = $indicator[$i];
332         }
333         warn "MARChtml";
334         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
335         warn "MARChtml2";
336 # MARC::Record built => now, record in DB
337         my $oldbibnum;
338         my $oldbibitemnum;
339         if ($is_a_modif) {
340                  NEWmodbiblio($dbh,$record,$bibid);
341         } else {
342                 ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record);
343         }
344 # now, redirect to additem page
345         print $input->redirect("additem.pl?bibid=$bibid");
346         exit;
347 #------------------------------------------------------------------------------------------------------------------------------
348 } else {
349 #------------------------------------------------------------------------------------------------------------------------------
350         build_tabs ($template, $record, $dbh);
351         build_hidden_data;
352         $template->param(
353                 oldbiblionumber             => $oldbiblionumber,
354                 bibid                       => $bibid,
355                 oldbiblionumtagfield        => $oldbiblionumtagfield,
356                 oldbiblionumtagsubfield     => $oldbiblionumtagsubfield,
357                 oldbiblioitemnumtagfield    => $oldbiblioitemnumtagfield,
358                 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
359                 oldbiblioitemnumber         => $oldbiblioitemnumber );
360 }
361 output_html_with_http_headers $input, $cookie, $template->output;