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