* bugfixes
[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 CGI;
23 use strict;
24 use C4::Output;
25 use C4::Biblio;
26 use C4::Context;
27 use HTML::Template;
28 use MARC::File::USMARC;
29
30 sub find_value {
31         my ($tagfield,$insubfield,$record) = @_;
32 #       warn "$tagfield / $insubfield // ";
33         my $result;
34         my $indicator;
35         foreach my $field ($record->field($tagfield)) {
36                 my @subfields = $field->subfields();
37                 foreach my $subfield (@subfields) {
38                         if (@$subfield[0] eq $insubfield) {
39                                 $result .= @$subfield[1];
40                                 $indicator = $field->indicator(1).$field->indicator(2);
41                         }
42                 }
43         }
44         return($indicator,$result);
45 }
46
47 sub MARCfindbreeding {
48         my ($dbh,$isbn) = @_;
49         my $sth = $dbh->prepare("select file,marc from marc_breeding where isbn=?");
50         $sth->execute($isbn);
51         my ($file,$marc) = $sth->fetchrow;
52         if ($marc) {
53                 my $record = MARC::File::USMARC::decode($marc);
54                 if (ref($record) eq undef) {
55                         return -1;
56                 } else {
57                         return $record;
58                 }
59         }
60         return -1;
61
62 }
63 my $input = new CGI;
64 my $error = $input->param('error');
65 my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio.
66 my $isbn = $input->param('isbn');
67 my $op = $input->param('op');
68 my $dbh = C4::Context->dbh;
69 my $bibid;
70 if ($oldbiblionumber) {
71         $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
72 }else {
73         $bibid = $input->param('bibid');
74 }
75 my $template;
76
77 my $tagslib = &MARCgettagslib($dbh,1);
78 my $record=-1;
79 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
80 $record = MARCfindbreeding($dbh,$isbn) if ($isbn);
81 my $is_a_modif=0;
82 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
83 if ($bibid) {
84         $is_a_modif=1;
85 ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber");
86 warn "==>$oldbiblionumtagfield,$oldbiblionumtagsubfield";
87
88 }
89 #------------------------------------------------------------------------------------------------------------------------------
90 if ($op eq "addbiblio") {
91 #------------------------------------------------------------------------------------------------------------------------------
92         # rebuild
93         my @tags = $input->param('tag');
94         my @subfields = $input->param('subfield');
95         my @values = $input->param('field_value');
96         # build indicator hash.
97         my @ind_tag = $input->param('ind_tag');
98         my @indicator = $input->param('indicator');
99         my %indicators;
100         for (my $i=0;$i<=$#ind_tag;$i++) {
101                 $indicators{$ind_tag[$i]} = $indicator[$i];
102         }
103         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
104 # MARC::Record builded => now, record in DB
105         my $oldbibnum;
106         my $oldbibitemnum;
107         if ($is_a_modif) {
108                  NEWmodbiblio($dbh,$record,$bibid);
109         } else {
110                 ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record);
111         }
112 # now, redirect to additem page
113         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=additem.pl?bibid=$bibid\"></html>";
114         exit;
115 #------------------------------------------------------------------------------------------------------------------------------
116 } else {
117 #------------------------------------------------------------------------------------------------------------------------------
118         $template = gettemplate("acqui.simple/addbiblio.tmpl");
119         # fill arrays
120         my @loop_data =();
121         my $tag;
122         my $i=0;
123         my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by authorised_value");
124 # loop through each tab 0 through 9
125         for (my $tabloop = 0; $tabloop<=9;$tabloop++) {
126         #       my @fields = $record->fields();
127                 my @loop_data =();
128                 foreach my $tag (sort(keys (%{$tagslib}))) {
129                         my $previous_tag = '';
130                         my @subfields_data;
131                         my $indicator;
132 # loop through each subfield
133                         foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
134                                 next if ($subfield eq 'lib'); # skip lib and tabs, which are koha internal
135                                 next if ($subfield eq 'tab');
136                                 next if ($tagslib->{$tag}->{$subfield}->{tab}  ne $tabloop);
137                                 my %subfield_data;
138                                 $subfield_data{tag}=$tag;
139                                 $subfield_data{subfield}=$subfield;
140                                 $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
141                                 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
142                                 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
143                                 # if breeding is not empty
144                                 if ($record ne -1) {
145                                         my ($x,$value) = find_value($tag,$subfield,$record);
146                                         $indicator = $x if $x;
147                                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
148                                                 my @authorised_values;
149                                                 my %authorised_lib;
150                                                 # builds list, depending on authorised value...
151                                                 #---- branch
152                                                 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
153                                                         my $sth=$dbh->prepare("select branchcode,branchname from branches");
154                                                         $sth->execute;
155                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
156                                                         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
157                                                                 push @authorised_values, $branchcode;
158                                                                 $authorised_lib{$branchcode}=$branchname;
159                                                         }
160                                                 #----- itemtypes
161                                                 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
162                                                         my $sth=$dbh->prepare("select itemtype,description from itemtypes");
163                                                         $sth->execute;
164                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
165                                                         while (my ($itemtype,$description) = $sth->fetchrow_array) {
166                                                                 push @authorised_values, $itemtype;
167                                                                 $authorised_lib{$itemtype}=$description;
168                                                         }
169                                                 #---- "true" authorised value
170                                                 } else {
171                                                         $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
172                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
173                                                         while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
174                                                                 push @authorised_values, $value;
175                                                                 $authorised_lib{$value}=$lib;
176                                                         }
177                                                 }
178                                                 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
179                                                                                                                                                                         -values=> \@authorised_values,
180                                                                                                                                                                         -default=>"$value",
181                                                                                                                                                                         -labels => \%authorised_lib,
182                                                                                                                                                                         -size=>1,
183                                                                                                                                                                         -multiple=>0,
184                                                                                                                                                                         );
185                                         } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
186                                                 $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>"; #"
187                                         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
188                                                 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
189                                                 require $plugin;
190                                                 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
191                                                 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
192                                                 $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";
193                                         } else {
194                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
195                                         }
196                                 # if breeding is empty
197                                 } else {
198                                         my ($x,$value);
199                                         ($x,$value) = find_value($tag,$subfield,$record) if ($record ne -1);
200                                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
201                                                 my @authorised_values;
202                                                 my %authorised_lib;
203                                                 # builds list, depending on authorised value...
204                                                 #---- branch
205                                                 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
206                                                         my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchcode");
207                                                         $sth->execute;
208                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
209                                                         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
210                                                                 push @authorised_values, $branchcode;
211                                                                 $authorised_lib{$branchcode}=$branchname;
212                                                         }
213                                                 #----- itemtypes
214                                                 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
215                                                         my $sth=$dbh->prepare("select itemtype,description from itemtypes order by itemtype");
216                                                         $sth->execute;
217                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
218                                                         while (my ($itemtype,$description) = $sth->fetchrow_array) {
219                                                                 push @authorised_values, $itemtype;
220                                                                 $authorised_lib{$itemtype}=$description;
221                                                         }
222                                                 #---- "true" authorised value
223                                                 } else {
224                                                         $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
225                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
226                                                         while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
227                                                                 push @authorised_values, $value;
228                                                                 $authorised_lib{$value}=$lib;
229                                                         }
230                                                 }
231                                                 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
232                                                                                                                                                                         -values=> \@authorised_values,
233                                                                                                                                                                         -default=>"$value",
234                                                                                                                                                                         -labels => \%authorised_lib,
235                                                                                                                                                                         -size=>1,
236                                                                                                                                                                         -multiple=>0,
237                                                                                                                                                                         );
238                                         } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
239                                                 $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>";
240                                         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
241                                                 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
242                                                 require $plugin;
243                                                 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
244                                                 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
245                                                 $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";
246                                         } else {
247                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" size=50 maxlength=255>";
248                                         }
249                                 }
250                                 push(@subfields_data, \%subfield_data);
251                                 $i++;
252                         }
253                         if ($#subfields_data>=0) {
254                                 my %tag_data;
255                                 $tag_data{tag}=$tag;
256                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
257                                 $tag_data{indicator} = $indicator;
258                                 $tag_data{subfield_loop} = \@subfields_data;
259                                 push (@loop_data, \%tag_data);
260                         }
261                 }
262                 $template->param($tabloop."XX" =>\@loop_data);
263         }
264         # now, build hidden datas => we store everything, even if we show only requested subfields.
265         my @loop_data =();
266         my $i=0;
267         foreach my $tag (keys %{$tagslib}) {
268                 my $previous_tag = '';
269         # loop through each subfield
270                 foreach my $subfield (keys %{$tagslib->{$tag}}) {
271                         next if ($subfield eq 'lib');
272                         next if ($subfield eq 'tab');
273                         next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
274                         my %subfield_data;
275                         $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
276                         $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
277                         $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
278                         $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
279                         push(@loop_data, \%subfield_data);
280                         $i++
281                 }
282         }
283         $template->param(
284                                                         oldbiblionumber => $oldbiblionumber,
285                                                         bibid => $bibid,
286                                                         oldbiblionumtagfield => $oldbiblionumtagfield,
287                                                         oldbiblionumtagsubfield => $oldbiblionumtagsubfield);
288 }
289 print "Content-Type: text/html\n\n", $template->output;