last commits before 1.9.1
[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                                 foreach my $value (@value) {
185                                         my %subfield_data;
186                                         $subfield_data{tag}=$tag;
187                                         $subfield_data{subfield}=$subfield;
188                                         $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
189                                         $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
190                                         $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
191                                         $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
192                                         $value=char_decode($value) unless ($is_a_modif);
193                                         $indicator = $x if $x; #XXX
194                                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
195                                                 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh, $authorised_values_sth);
196                                         } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
197                                                 $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>"; #"
198                                         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
199                                                 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
200                                                 require $plugin;
201                                                 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
202                                                 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
203                                                 $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";
204                                         } else {
205                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
206                                         }
207                                         push(@subfields_data, \%subfield_data);
208                                         $i++;
209                                 }
210                 # if breeding is empty
211                         } else {
212                                 my ($x,$value);
213                                 ($x,$value) = find_value($tag,$subfield,$record) if ($record ne -1);
214                                 $value=char_decode($value) unless ($is_a_modif);
215                                         my %subfield_data;
216                                         $subfield_data{tag}=$tag;
217                                         $subfield_data{subfield}=$subfield;
218                                         $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
219                                         $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
220                                         $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
221                                         $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
222                                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
223                                                 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh, $authorised_values_sth);
224                                         } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
225                                                 $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>";
226                                         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
227                                                 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
228                                                 require $plugin;
229                                                 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
230                                                 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
231                                                 $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";
232                                         } else {
233                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" size=50 maxlength=255>";
234                                         }
235                                         push(@subfields_data, \%subfield_data);
236                                         $i++;
237                                 }
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} = $indicator;
244                                 $tag_data{subfield_loop} = \@subfields_data;
245                                 push (@loop_data, \%tag_data);
246                         }
247                 }
248                 $template->param($tabloop."XX" =>\@loop_data);
249         }
250 }
251
252
253 sub build_hidden_data () {
254     # build hidden data =>
255     # we store everything, even if we show only requested subfields.
256
257     my @loop_data =();
258     my $i=0;
259     foreach my $tag (keys %{$tagslib}) {
260         my $previous_tag = '';
261
262         # loop through each subfield
263         foreach my $subfield (keys %{$tagslib->{$tag}}) {
264             next if ($subfield eq 'lib');
265             next if ($subfield eq 'tab');
266             next if ($subfield eq 'mandatory');
267             next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
268             my %subfield_data;
269             $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
270             $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
271             $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
272             $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
273             push(@loop_data, \%subfield_data);
274             $i++
275         }
276     }
277 }
278
279
280 my $input = new CGI;
281 my $error = $input->param('error');
282 my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio.
283 my $breedingid = $input->param('breedingid');
284 my $op = $input->param('op');
285 my $dbh = C4::Context->dbh;
286 my $bibid;
287 if ($oldbiblionumber) {
288         $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
289 }else {
290         $bibid = $input->param('bibid');
291 }
292 my ($template, $loggedinuser, $cookie)
293     = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
294                              query => $input,
295                              type => "intranet",
296                              authnotrequired => 0,
297                              flagsrequired => {catalogue => 1},
298                              debug => 1,
299                              });
300
301 $tagslib = &MARCgettagslib($dbh,1);
302 my $record=-1;
303 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
304 $record = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
305 $is_a_modif=0;
306 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
307 my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
308 if ($bibid) {
309         $is_a_modif=1;
310         # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
311         ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber");
312         ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber");
313         # search biblioitems value
314         my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
315         $sth->execute($oldbiblionumber);
316         ($oldbiblioitemnumber) = $sth->fetchrow;
317 }
318 #------------------------------------------------------------------------------------------------------------------------------
319 if ($op eq "addbiblio") {
320 #------------------------------------------------------------------------------------------------------------------------------
321         # rebuild
322         my @tags = $input->param('tag');
323         my @subfields = $input->param('subfield');
324         my @values = $input->param('field_value');
325         # build indicator hash.
326         my @ind_tag = $input->param('ind_tag');
327         my @indicator = $input->param('indicator');
328         my %indicators;
329         for (my $i=0;$i<=$#ind_tag;$i++) {
330                 $indicators{$ind_tag[$i]} = $indicator[$i];
331         }
332         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
333 # MARC::Record built => now, record in DB
334         my $oldbibnum;
335         my $oldbibitemnum;
336         if ($is_a_modif) {
337                  NEWmodbiblio($dbh,$record,$bibid);
338         } else {
339                 ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record);
340         }
341 # now, redirect to additem page
342         print $input->redirect("additem.pl?bibid=$bibid");
343         exit;
344 #------------------------------------------------------------------------------------------------------------------------------
345 } else {
346 #------------------------------------------------------------------------------------------------------------------------------
347         build_tabs ($template, $record, $dbh);
348         build_hidden_data;
349         $template->param(
350                 oldbiblionumber             => $oldbiblionumber,
351                 bibid                       => $bibid,
352                 oldbiblionumtagfield        => $oldbiblionumtagfield,
353                 oldbiblionumtagsubfield     => $oldbiblionumtagsubfield,
354                 oldbiblioitemnumtagfield    => $oldbiblioitemnumtagfield,
355                 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
356                 oldbiblioitemnumber         => $oldbiblioitemnumber );
357 }
358 output_html_with_http_headers $input, $cookie, $template->output;