templating this file and modify it so that all the budgets are on the screen
[koha.git] / acqui.simple / addbiblio.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 #
6 # TODO
7 #
8 # Add info on biblioitems and items already entered as you enter new ones
9 #
10
11
12 # Copyright 2000-2002 Katipo Communications
13 #
14 # This file is part of Koha.
15 #
16 # Koha is free software; you can redistribute it and/or modify it under the
17 # terms of the GNU General Public License as published by the Free Software
18 # Foundation; either version 2 of the License, or (at your option) any later
19 # version.
20 #
21 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
22 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
23 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License along with
26 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
27 # Suite 330, Boston, MA  02111-1307 USA
28
29 use CGI;
30 use strict;
31 use C4::Output;
32 use C4::Biblio;
33 use C4::Context;
34 use HTML::Template;
35 use MARC::File::USMARC;
36
37 sub find_value {
38         my ($tagfield,$insubfield,$record) = @_;
39         my $result;
40         my $indicator;
41         foreach my $field ($record->field($tagfield)) {
42                 my @subfields = $field->subfields();
43                 foreach my $subfield (@subfields) {
44                         if (@$subfield[0] eq $insubfield) {
45                                 $result .= @$subfield[1];
46                                 $indicator = $field->indicator(1).$field->indicator(2);
47                         }
48                 }
49         }
50         return($indicator,$result);
51 }
52
53 sub MARCfindbreeding {
54         my ($dbh,$isbn) = @_;
55         my $sth = $dbh->prepare("select file,marc from marc_breeding where isbn=?");
56         $sth->execute($isbn);
57         my ($file,$marc) = $sth->fetchrow;
58         if ($marc) {
59                 my $record = MARC::File::USMARC::decode($marc);
60                 if (ref($record) eq undef) {
61                         warn "not a MARC record : $marc";
62                         return -1;
63                 } else {
64                         return $record;
65                 }
66         }
67         return -1;
68
69 }
70 my $input = new CGI;
71 my $error = $input->param('error');
72 my $oldbiblionumber=$input->param('bib'); # if bib exists, it's a modif, not a new biblio.
73 my $isbn = $input->param('isbn');
74 my $op = $input->param('op');
75 my $dbh = C4::Context->dbh;
76 my $bibid;
77 if ($oldbiblionumber) {;
78         $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber)
79 }else {
80         $bibid = $input->param('bibid');
81 }
82 my $template;
83
84 my $tagslib = &MARCgettagslib($dbh,1);
85
86 my $record = MARCgetbiblio($dbh,$bibid) if ($oldbiblionumber);
87 my $record = MARCfindbreeding($dbh,$isbn) if ($isbn);
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 #       warn "record ".$record->as_formatted();
105 # MARC::Record builded => now, record in DB
106         my ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record);
107         my $authorised_values_sth = $dbh->prepare("select authorised_value from authorised_values where category=?");
108 # build item screen. There is no item for instance.
109         my @loop_data =();
110         my $i=0;
111         foreach my $tag (keys %{$tagslib}) {
112                 my $previous_tag = '';
113         # loop through each subfield
114                 foreach my $subfield (keys %{$tagslib->{$tag}}) {
115                         next if ($subfield eq 'lib');
116                         next if ($subfield eq 'tab');
117                         next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "10");
118                         $i++;
119                         my %subfield_data;
120                         $subfield_data{tag}=$tag;
121                         $subfield_data{subfield}=$subfield;
122                         $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
123                         $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
124                         $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
125                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
126                                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
127                                 my @authorised_values;
128                                 push @authorised_values, "" unless ($subfield_data{mandatory});
129                                 while ((my $value) = $authorised_values_sth->fetchrow_array) {
130                                         push @authorised_values, $value;
131                                 }
132                                 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
133                                                                                                                                                         -values=> \@authorised_values,
134                                                                                                                                                         -size=>1,
135                                                                                                                                                         -multiple=>0,
136                                                                                                                                                         );
137                         } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
138                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" DISABLE READONLY> <a href=\"javascript:Dopop('../thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">...</a>";
139                         } else {
140                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
141                         }
142                         push(@loop_data, \%subfield_data);
143                 }
144         }
145         $template = gettemplate("acqui.simple/addbiblio2.tmpl");
146         $template->param(bibid => $bibid,
147                                                         item => \@loop_data);
148 #------------------------------------------------------------------------------------------------------------------------------
149 } elsif ($op eq "additem") {
150 #------------------------------------------------------------------------------------------------------------------------------
151         my @tags = $input->param('tag');
152         my @subfields = $input->param('subfield');
153         my @values = $input->param('field_value');
154 #       my @ind_tag = $input->param('ind_tag');
155 #       my @indicator = $input->param('indicator');
156 #       my %indicators;
157 #       for (my $i=0;$i<=$#ind_tag;$i++) {
158 #               $indicators{$ind_tag[$i]} = $indicator[$i];
159 #       }
160         my %indicators;
161         $indicators{995}='  ';
162         warn "REs : $tags[0] - $tags[1] - $subfields[0] - $subfields[1] - $values[0] - $values[1]";
163         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
164         my ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewitem($dbh,$record,$bibid);
165         # now, build existiing item list
166         my $temp = MARCgetbiblio($dbh,$bibid);
167         my @fields = $temp->fields();
168         my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
169         my @big_array;
170         foreach my $field (@fields) {
171                 my @subf=$field->subfields;
172                 my %this_row;
173         # loop through each subfield
174                 for my $i (0..$#subf) {
175                         next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne 10);
176                         $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib};
177                         $this_row{$subf[$i][0]} =$subf[$i][1];
178                 }
179                 if (%this_row) {
180                         push(@big_array, \%this_row);
181                 }
182         }
183         #fill big_row with missing datas
184         foreach my $subfield_code  (keys(%witness)) {
185                 for (my $i=0;$i<=$#big_array;$i++) {
186                         $big_array[$i]{$subfield_code}="&nbsp;" unless ($big_array[$i]{$subfield_code});
187                 }
188         }
189         # now, construct template !
190         my @item_value_loop;
191         my @header_value_loop;
192         for (my $i=0;$i<=$#big_array; $i++) {
193                 my $items_data;
194                 foreach my $subfield_code (keys(%witness)) {
195                         $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
196                 }
197                 my %row_data;
198                 $row_data{item_value} = $items_data;
199                 push(@item_value_loop,\%row_data);
200         }
201         foreach my $subfield_code (keys(%witness)) {
202                 my %header_value;
203                 $header_value{header_value} = $witness{$subfield_code};
204                 push(@header_value_loop, \%header_value);
205         }
206
207 # next item form
208         my @loop_data =();
209         my $i=0;
210         foreach my $tag (keys %{$tagslib}) {
211                 my $previous_tag = '';
212         # loop through each subfield
213                 foreach my $subfield (keys %{$tagslib->{$tag}}) {
214                         next if ($subfield eq 'lib');
215                         next if ($subfield eq 'tab');
216                         next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "10");
217                         my %subfield_data;
218                         $subfield_data{tag}=$tag;
219                         $subfield_data{subfield}=$subfield;
220                         $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
221                         $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
222                         $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
223                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
224                         push(@loop_data, \%subfield_data);
225                         $i++
226                 }
227         }
228         $template = gettemplate("acqui.simple/addbiblio2.tmpl");
229         $template->param(item_loop => \@item_value_loop,
230                                                         item_header_loop => \@header_value_loop,
231                                                         bibid => $bibid,
232                                                         item => \@loop_data);
233 #------------------------------------------------------------------------------------------------------------------------------
234 } else {
235 #------------------------------------------------------------------------------------------------------------------------------
236         $template = gettemplate("acqui.simple/addbiblio.tmpl");
237         # fill arrays
238         my @loop_data =();
239         my $tag;
240         my $i=0;
241         my $authorised_values_sth = $dbh->prepare("select authorised_value from authorised_values where category=?");
242 # loop through each tab 0 through 9
243         for (my $tabloop = 0; $tabloop<=9;$tabloop++) {
244         #       my @fields = $record->fields();
245                 my @loop_data =();
246                 foreach my $tag (keys %{$tagslib}) {
247                         my $previous_tag = '';
248                         my @subfields_data;
249                         my $indicator;
250 # loop through each subfield
251                         foreach my $subfield (keys %{$tagslib->{$tag}}) {
252                                 next if ($subfield eq 'lib'); # skip lib and tabs, which are koha internal
253                                 next if ($subfield eq 'tab');
254                                 next if ($tagslib->{$tag}->{$subfield}->{tab}  ne $tabloop);
255                                 my %subfield_data;
256                                 $subfield_data{tag}=$tag;
257                                 $subfield_data{subfield}=$subfield;
258                                 $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
259                                 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
260                                 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
261                                 # if breeding is not empty
262                                 if ($record ne -1) {
263                                         my ($x,$value) = find_value($tag,$subfield,$record);
264                                         $indicator = $x if $x;
265                                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
266                                                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
267                                                 my @authorised_values;
268                                                 push @authorised_values, "" unless ($subfield_data{mandatory});
269                                                 while ((my $value) = $authorised_values_sth->fetchrow_array) {
270                                                         push @authorised_values, $value;
271                                                 }
272                                                 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
273                                                                                                                                                                         -values=> \@authorised_values,
274                                                                                                                                                                         -default=>"$value",
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\" 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                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" DISABLE READONLY> <a href=\"javascript:Dopop('../value_builder/$tagslib->{$tag}->{$subfield}->{value_builder}?index=$i',$i)\">...</a>";
282                                         } else {
283                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\">";
284                                         }
285                                 # if breeding is empty
286                                 } else {
287                                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
288                                                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
289                                                 my @authorised_values;
290                                                 push @authorised_values, "" unless ($subfield_data{mandatory});
291                                                 while ((my $value) = $authorised_values_sth->fetchrow_array) {
292                                                         push @authorised_values, $value;
293                                                 }
294                                                 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
295                                                                                                                                                                         -values=> \@authorised_values,
296                                                                                                                                                                         -size=>1,
297                                                                                                                                                                         -multiple=>0,
298                                                                                                                                                                         );
299                                         } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
300                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" DISABLE READONLY> <a href=\"javascript:Dopop('../thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">...</a>";
301                                         } elsif ($tagslib->{$tag}->{$subfield}->{value_builder}) {
302                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" DISABLE READONLY> <a href=\"javascript:Dopop('../value_builder/$tagslib->{$tag}->{$subfield}->{value_builder}?index=$i',$i)\">...</a>";
303                                         } else {
304                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
305                                         }
306                                 }
307                                 push(@subfields_data, \%subfield_data);
308                                 $i++;
309                         }
310                         if ($#subfields_data>=0) {
311                                 my %tag_data;
312                                 $tag_data{tag}=$tag;
313                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
314                                 $tag_data{indicator} = $indicator;
315                                 $tag_data{subfield_loop} = \@subfields_data;
316                                 push (@loop_data, \%tag_data);
317                         }
318                 }
319                 $template->param($tabloop."XX" =>\@loop_data);
320         }
321         # now, build hidden datas => we store everything, even if we show only requested subfields.
322         my @loop_data =();
323         my $i=0;
324         foreach my $tag (keys %{$tagslib}) {
325                 my $previous_tag = '';
326         # loop through each subfield
327                 foreach my $subfield (keys %{$tagslib->{$tag}}) {
328                         next if ($subfield eq 'lib');
329                         next if ($subfield eq 'tab');
330                         next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
331                         my %subfield_data;
332                         $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
333                         $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
334                         $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
335                         $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
336                         push(@loop_data, \%subfield_data);
337                         $i++
338                 }
339         }
340         $template->param(
341                                                         biblionumber => $oldbiblionumber,
342                                                         bibid => $bibid);
343 }
344 print "Content-Type: text/html\n\n", $template->output;