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