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