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