Road to 1.3.2
[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,$subfield,$record) = @_;
39         my $result;
40         foreach my $field ($record->field($tagfield)) {
41                 my @subfields = $field->subfields();
42                 foreach my $subfield (@subfields) {
43                         if (@$subfield[0] eq $subfield) {
44                                 $result .= @$subfield[1];
45                         }
46                 }
47         }
48 }
49
50 sub MARCfindbreeding {
51         my ($dbh,$isbn) = @_;
52         my $sth = $dbh->prepare("select file,marc from marc_breeding where isbn=?");
53         $sth->execute($isbn);
54         my ($file,$marc) = $sth->fetchrow;
55         if ($marc) {
56                 my $record = MARC::File::USMARC::decode($marc);
57                 if (ref($record) eq undef) {
58                         warn "not a MARC record !";
59                         return -1;
60                 } else {
61                         return $record;
62                 }
63         }
64         warn "not MARC";
65         return -1;
66
67 }
68 my $input = new CGI;
69 my $error = $input->param('error');
70 my $oldbiblionumber=$input->param('bib'); # if bib exists, it's a modif, not a new biblio.
71 my $isbn = $input->param('isbn');
72 my $op = $input->param('op');
73 my $dbh = C4::Context->dbh;
74 my $bibid;
75 if ($oldbiblionumber) {;
76         $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber)
77 }else {
78         $bibid = $input->param('bibid');
79 }
80 my $template;
81
82 my $tagslib = &MARCgettagslib($dbh,1);
83
84 my $record = MARCgetbiblio($dbh,$bibid) if ($oldbiblionumber);
85 my $record = MARCfindbreeding($dbh,$isbn) if ($isbn);
86
87 #------------------------------------------------------------------------------------------------------------------------------
88 if ($op eq "addbiblio") {
89 #------------------------------------------------------------------------------------------------------------------------------
90         # rebuild
91         my @tags = $input->param('tag[]');
92         my @subfields = $input->param('subfield[]');
93         my @values = $input->param('value[]');
94         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values);
95 # MARC::Record builded => now, record in DB
96         my ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record);
97 # build item screen. There is no item for instance.
98         my @loop_data =();
99         my $i=0;
100         foreach my $tag (keys %{$tagslib}) {
101                 my $previous_tag = '';
102         # loop through each subfield
103                 foreach my $subfield (keys %{$tagslib->{$tag}}) {
104                         next if ($subfield eq 'lib');
105                         next if ($subfield eq 'tab');
106                         next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "10");
107                         $i++;
108                         my %subfield_data;
109                         $subfield_data{tag}=$tag;
110                         $subfield_data{subfield}=$subfield;
111                         $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
112                         $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
113                         $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
114                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
115                         push(@loop_data, \%subfield_data);
116                 }
117         }
118         $template = gettemplate("acqui.simple/addbiblio2.tmpl");
119         $template->param(bibid => $bibid,
120                                                         item => \@loop_data);
121 #------------------------------------------------------------------------------------------------------------------------------
122 } elsif ($op eq "additem") {
123 #------------------------------------------------------------------------------------------------------------------------------
124         my @tags = $input->param('tag[]');
125         my @subfields = $input->param('subfield[]');
126         my @values = $input->param('value[]');
127         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values);
128         my ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewitem($dbh,$record,$bibid);
129         # now, build existiing item list
130         my $temp = MARCgetbiblio($dbh,$bibid);
131         my @fields = $temp->fields();
132         my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
133         my @big_array;
134         foreach my $field (@fields) {
135                 my @subf=$field->subfields;
136                 my %this_row;
137         # loop through each subfield
138                 for my $i (0..$#subf) {
139                         next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne 10);
140                         $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib};
141                         $this_row{$subf[$i][0]} =$subf[$i][1];
142                 }
143                 if (%this_row) {
144                         push(@big_array, \%this_row);
145                 }
146         }
147         #fill big_row with missing datas
148         foreach my $subfield_code  (keys(%witness)) {
149                 for (my $i=0;$i<=$#big_array;$i++) {
150                         $big_array[$i]{$subfield_code}="&nbsp;" unless ($big_array[$i]{$subfield_code});
151                 }
152         }
153         # now, construct template !
154         my @item_value_loop;
155         my @header_value_loop;
156         for (my $i=0;$i<=$#big_array; $i++) {
157                 my $items_data;
158                 foreach my $subfield_code (keys(%witness)) {
159                         $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
160                 }
161                 my %row_data;
162                 $row_data{item_value} = $items_data;
163                 push(@item_value_loop,\%row_data);
164         }
165         foreach my $subfield_code (keys(%witness)) {
166                 my %header_value;
167                 $header_value{header_value} = $witness{$subfield_code};
168                 push(@header_value_loop, \%header_value);
169         }
170
171 # next item form
172         my @loop_data =();
173         my $i=0;
174         foreach my $tag (keys %{$tagslib}) {
175                 my $previous_tag = '';
176         # loop through each subfield
177                 foreach my $subfield (keys %{$tagslib->{$tag}}) {
178                         next if ($subfield eq 'lib');
179                         next if ($subfield eq 'tab');
180                         next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "10");
181                         my %subfield_data;
182                         $subfield_data{tag}=$tag;
183                         $subfield_data{subfield}=$subfield;
184                         $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
185                         $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
186                         $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
187                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
188                         push(@loop_data, \%subfield_data);
189                         $i++
190                 }
191         }
192         $template = gettemplate("acqui.simple/addbiblio2.tmpl");
193         $template->param(item_loop => \@item_value_loop,
194                                                         item_header_loop => \@header_value_loop,
195                                                         bibid => $bibid,
196                                                         item => \@loop_data);
197 #------------------------------------------------------------------------------------------------------------------------------
198 } else {
199 #------------------------------------------------------------------------------------------------------------------------------
200         $template = gettemplate("acqui.simple/addbiblio.tmpl");
201         # fill arrays
202         my @loop_data =();
203         my $tag;
204         my $i=0;
205         my $authorised_values_sth = $dbh->prepare("select authorised_value from authorised_values where category=?");
206         # loop through each tab 0 through 9
207         for (my $tabloop = 0; $tabloop<=9;$tabloop++) {
208         # loop through each tag
209         #       my @fields = $record->fields();
210                 my @loop_data =();
211                 foreach my $tag (keys %{$tagslib}) {
212                         my $previous_tag = '';
213                         my @subfields_data;
214         # loop through each subfield
215                         foreach my $subfield (keys %{$tagslib->{$tag}}) {
216                                 next if ($subfield eq 'lib');
217                                 next if ($subfield eq 'tab');
218                                 next if ($tagslib->{$tag}->{$subfield}->{tab}  ne $tabloop);
219                                 my %subfield_data;
220                                 $subfield_data{tag}=$tag;
221                                 $subfield_data{subfield}=$subfield;
222                                 $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
223                                 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
224                                 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
225                                 if ($record ne -1) {
226                                         my $value = find_value($tag,$subfield,$record);
227                                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
228                                                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
229                                                 my @authorised_values;
230                                                 push @authorised_values, "" unless ($subfield_data{mandatory});
231                                                 while ((my $value) = $authorised_values_sth->fetchrow_array) {
232                                                         push @authorised_values, $value;
233                                                 }
234                                                 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
235                                                                                                                                                                         -values=> \@authorised_values,
236                                                                                                                                                                         -default=>"$value",
237                                                                                                                                                                         -size=>1,
238                                                                                                                                                                         -multiple=>0,
239                                                                                                                                                                         );
240                                         } else {
241                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\">";
242                                         }
243                                 } else {
244                                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
245                                                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
246                                                 my @authorised_values;
247                                                 push @authorised_values, "" unless ($subfield_data{mandatory});
248                                                 while ((my $value) = $authorised_values_sth->fetchrow_array) {
249                                                         push @authorised_values, $value;
250                                                 }
251                                                 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
252                                                                                                                                                                         -values=> \@authorised_values,
253                                                                                                                                                                         -size=>1,
254                                                                                                                                                                         -multiple=>0,
255                                                                                                                                                                         );
256                                         } else {
257                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
258                                         }
259                                 }
260                                 push(@subfields_data, \%subfield_data);
261                                 $i++;
262                         }
263                         if ($#subfields_data>=0) {
264                                 my %tag_data;
265                                 $tag_data{tag}=$tag.' -'. $tagslib->{$tag}->{lib};
266                                 $tag_data{subfield} = \@subfields_data;
267                                 push (@loop_data, \%tag_data);
268                         }
269                 }
270                 $template->param($tabloop."XX" =>\@loop_data);
271         }
272         # now, build hidden datas => we store everything, even if we show only requested subfields.
273         my @loop_data =();
274         my $i=0;
275         foreach my $tag (keys %{$tagslib}) {
276                 my $previous_tag = '';
277         # loop through each subfield
278                 foreach my $subfield (keys %{$tagslib->{$tag}}) {
279                         next if ($subfield eq 'lib');
280                         next if ($subfield eq 'tab');
281                         next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
282                         my %subfield_data;
283                         $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
284                         $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
285                         $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
286                         $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value\">";
287                         push(@loop_data, \%subfield_data);
288                         $i++
289                 }
290         }
291         $template->param(
292                                                         biblionumber => $oldbiblionumber,
293                                                         bibid => $bibid);
294 }
295 print "Content-Type: text/html\n\n", $template->output;