road to 1.3.2 : adding a biblio in MARC format.
[srvgit] / 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                         my %subfield_data;
108                         $subfield_data{tag}=$tag;
109                         $subfield_data{subfield}=$subfield;
110                         $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
111                         $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
112                         $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
113                         $subfield_data{marc_value}="<input type=\"text\" name=\"value[]\">";
114                         push(@loop_data, \%subfield_data);
115                         $i++
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=\"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         # loop through each tab 0 through 9
205         for (my $tabloop = 0; $tabloop<=9;$tabloop++) {
206         # loop through each tag
207         #       my @fields = $record->fields();
208                 my @loop_data =();
209                 foreach my $tag (keys %{$tagslib}) {
210                         my $previous_tag = '';
211                         my @subfields_data;
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 $tabloop);
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{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
222                                 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
223                                 if ($record ne -1) {
224                                         my $value ="";# &find_value($tag,$subfield,$record);
225                                         $subfield_data{marc_value}="<input type=\"text\" name=\"value[]\" value=\"$value\">";
226                                 } else {
227                                         $subfield_data{marc_value}="<input type=\"text\" name=\"value[]\">";
228                                 }
229                                 push(@subfields_data, \%subfield_data);
230                         }
231                         if ($#subfields_data>=0) {
232                                 my %tag_data;
233                                 $tag_data{tag}=$tag.' -'. $tagslib->{$tag}->{lib};
234                                 $tag_data{subfield} = \@subfields_data;
235                                 push (@loop_data, \%tag_data);
236                         }
237                 }
238                 $template->param($tabloop."XX" =>\@loop_data);
239         }
240         # now, build hidden datas => we store everything, even if we show only requested subfields.
241         my @loop_data =();
242         my $i=0;
243         foreach my $tag (keys %{$tagslib}) {
244                 my $previous_tag = '';
245         # loop through each subfield
246                 foreach my $subfield (keys %{$tagslib->{$tag}}) {
247                         next if ($subfield eq 'lib');
248                         next if ($subfield eq 'tab');
249                         next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
250                         my %subfield_data;
251                         $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
252                         $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
253                         $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
254                         $subfield_data{marc_value}="<input type=\"hidden\" name=\"value[]\">";
255                         push(@loop_data, \%subfield_data);
256                         $i++
257                 }
258         }
259         $template->param(
260                                                         biblionumber => $oldbiblionumber,
261                                                         bibid => $bibid);
262 }
263 print "Content-Type: text/html\n\n", $template->output;