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