bugfixes + adding buttons to switch between normal and MARC view of a record
[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 strict;
23 use CGI;
24 use C4::Auth;
25 use C4::Output;
26 use C4::Interface::CGI::Output;
27 use C4::Biblio;
28 use C4::Context;
29 use C4::Koha; # XXX subfield_is_koha_internal_p
30 use HTML::Template;
31 use MARC::File::USMARC;
32
33 use vars qw( $tagslib );
34 use vars qw( $is_a_modif );
35
36
37 =item find_value
38
39     ($indicators, $value) = find_value($tag, $subfield, $record);
40
41 Find the given $subfield in the given $tag in the given
42 MARC::Record $record.  If the subfield is found, returns
43 the (indicators, value) pair; otherwise, (undef, undef) is
44 returned.
45
46 =cut
47
48 sub find_value {
49         my ($tagfield,$insubfield,$record) = @_;
50         my $result;
51         my $indicator;
52         warn "tagfield : $tagfield /".$record->as_formatted;
53         if ($tagfield <10) {
54                 if ($record->field($tagfield)) {
55                         $result = $record->field($tagfield)->data();
56                 } else {
57                         $result="";
58                 }
59         } else {
60                 foreach my $field ($record->field($tagfield)) {
61                         my @subfields = $field->subfields();
62                         foreach my $subfield (@subfields) {
63                                 if (@$subfield[0] eq $insubfield) {
64                                         $result .= @$subfield[1];
65                                         $indicator = $field->indicator(1).$field->indicator(2);
66                                 }
67                         }
68                 }
69         }
70         return($indicator,$result);
71 }
72
73
74 =item find_value
75
76     $record = MARCfindbreeding($dbh, $breedingid);
77
78 Look up the breeding farm with database handle $dbh, for the
79 record with id $breedingid.  If found, returns the decoded
80 MARC::Record; otherwise, -1 is returned (FIXME).
81
82 =cut
83
84 sub MARCfindbreeding {
85         my ($dbh,$id) = @_;
86         my $sth = $dbh->prepare("select file,marc from marc_breeding where id=?");
87         $sth->execute($id);
88         my ($file,$marc) = $sth->fetchrow;
89         if ($marc) {
90                 my $record = MARC::File::USMARC::decode($marc);
91                         warn "==> ".$record->as_formatted();
92                 if (ref($record) eq undef) {
93                         return -1;
94                 } else {
95                         return $record;
96                 }
97         }
98         return -1;
99 }
100
101
102 =item build_authorized_values_list
103
104 =cut
105
106 sub build_authorized_values_list ($$$$$) {
107     my($tag, $subfield, $value, $dbh, $authorised_values_sth) = @_;
108
109     my @authorised_values;
110     my %authorised_lib;
111
112     # builds list, depending on authorised value...
113
114     #---- branch
115     if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
116         my $sth=$dbh->prepare("select branchcode,branchname from branches");
117         $sth->execute;
118         push @authorised_values, ""
119                 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
120
121         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
122             push @authorised_values, $branchcode;
123             $authorised_lib{$branchcode}=$branchname;
124         }
125
126     #----- itemtypes
127     } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
128         my $sth=$dbh->prepare("select itemtype,description from itemtypes");
129         $sth->execute;
130         push @authorised_values, ""
131                 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
132
133         while (my ($itemtype,$description) = $sth->fetchrow_array) {
134             push @authorised_values, $itemtype;
135             $authorised_lib{$itemtype}=$description;
136         }
137
138     #---- "true" authorised value
139     } else {
140         $authorised_values_sth->execute
141                 ($tagslib->{$tag}->{$subfield}->{authorised_value});
142
143         push @authorised_values, ""
144                 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
145
146         while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
147             push @authorised_values, $value;
148             $authorised_lib{$value}=$lib;
149         }
150     }
151     return CGI::scrolling_list( -name     => 'field_value',
152                                 -values   => \@authorised_values,
153                                 -default  => $value,
154                                 -labels   => \%authorised_lib,
155                                 -size     => 1,
156                                 -multiple => 0 );
157 }
158
159 sub build_tabs ($$$) {
160     my($template, $record, $dbh) = @_;
161
162     # fill arrays
163     my @loop_data =();
164     my $tag;
165     my $i=0;
166     my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
167         from authorised_values
168         where category=? order by authorised_value");
169
170     # loop through each tab 0 through 9
171     for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
172     #   my @fields = $record->fields();
173         my @loop_data = ();
174         foreach my $tag (sort(keys (%{$tagslib}))) {
175                 my $previous_tag = '';
176                 my @subfields_data;
177                 my $indicator;
178
179                 # loop through each subfield
180                 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
181                         next if subfield_is_koha_internal_p($subfield);
182                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
183                         my %subfield_data;
184                         $subfield_data{tag}=$tag;
185                         $subfield_data{subfield}=$subfield;
186                         $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
187                         $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
188                         $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
189                         $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
190                         # if breeding is not empty
191                         if ($record ne -1) {
192                                 my ($x,$value) = find_value($tag,$subfield,$record);
193                                 $value=char_decode($value) unless ($is_a_modif);
194                                 $indicator = $x if $x; #XXX
195                                 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
196                                         $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh, $authorised_values_sth);
197                                 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
198                                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  size=47 maxlength=255> <a href=\"javascript:Dopop('../thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">...</a>"; #"
199                                 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
200                                         my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
201                                         require $plugin;
202                                         my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
203                                         my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
204                                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  value=\"$value\" size=47 maxlength=255 OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i)\"> <a href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
205                                 } else {
206                                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
207                                 }
208                 # if breeding is empty
209                         } else {
210                                 my ($x,$value);
211                                 ($x,$value) = find_value($tag,$subfield,$record) if ($record ne -1);
212                                 $value=char_decode($value) unless ($is_a_modif);
213                                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
214                                                 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh, $authorised_values_sth);
215                                         } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
216                                                 $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>";
217                                         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
218                                                 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
219                                                 require $plugin;
220                                                 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
221                                                 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
222                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  DISABLE READONLY size=47 maxlength=255 OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i)\"> <a href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
223                                         } else {
224                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" size=50 maxlength=255>";
225                                         }
226                                 }
227                                 push(@subfields_data, \%subfield_data);
228                                 $i++;
229                         }
230                         if ($#subfields_data >= 0) {
231                                 my %tag_data;
232                                 $tag_data{tag} = $tag;
233                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
234                                 $tag_data{indicator} = $indicator;
235                                 $tag_data{subfield_loop} = \@subfields_data;
236                                 push (@loop_data, \%tag_data);
237                         }
238                 }
239                 $template->param($tabloop."XX" =>\@loop_data);
240         }
241 }
242
243
244 sub build_hidden_data () {
245     # build hidden data =>
246     # we store everything, even if we show only requested subfields.
247
248     my @loop_data =();
249     my $i=0;
250     foreach my $tag (keys %{$tagslib}) {
251         my $previous_tag = '';
252
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 ($subfield eq 'mandatory');
258             next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
259             my %subfield_data;
260             $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
261             $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
262             $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
263             $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
264             push(@loop_data, \%subfield_data);
265             $i++
266         }
267     }
268 }
269
270
271 my $input = new CGI;
272 my $error = $input->param('error');
273 my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio.
274 my $breedingid = $input->param('breedingid');
275 my $op = $input->param('op');
276 my $dbh = C4::Context->dbh;
277 my $bibid;
278 if ($oldbiblionumber) {
279         $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
280 }else {
281         $bibid = $input->param('bibid');
282 }
283 my ($template, $loggedinuser, $cookie)
284     = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
285                              query => $input,
286                              type => "intranet",
287                              authnotrequired => 0,
288                              flagsrequired => {catalogue => 1},
289                              debug => 1,
290                              });
291
292 $tagslib = &MARCgettagslib($dbh,1);
293 my $record=-1;
294 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
295 $record = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
296 $is_a_modif=0;
297 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
298 my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
299 if ($bibid) {
300         $is_a_modif=1;
301         # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
302         ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber");
303         ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber");
304         # search biblioitems value
305         my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
306         $sth->execute($oldbiblionumber);
307         ($oldbiblioitemnumber) = $sth->fetchrow;
308 }
309 #------------------------------------------------------------------------------------------------------------------------------
310 if ($op eq "addbiblio") {
311 #------------------------------------------------------------------------------------------------------------------------------
312         # rebuild
313         my @tags = $input->param('tag');
314         my @subfields = $input->param('subfield');
315         my @values = $input->param('field_value');
316         # build indicator hash.
317         my @ind_tag = $input->param('ind_tag');
318         my @indicator = $input->param('indicator');
319         my %indicators;
320         for (my $i=0;$i<=$#ind_tag;$i++) {
321                 $indicators{$ind_tag[$i]} = $indicator[$i];
322         }
323         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
324 # MARC::Record built => now, record in DB
325         my $oldbibnum;
326         my $oldbibitemnum;
327         if ($is_a_modif) {
328                  NEWmodbiblio($dbh,$record,$bibid);
329         } else {
330                 ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record);
331         }
332 # now, redirect to additem page
333         print $input->redirect("additem.pl?bibid=$bibid");
334         exit;
335 #------------------------------------------------------------------------------------------------------------------------------
336 } else {
337 #------------------------------------------------------------------------------------------------------------------------------
338         build_tabs ($template, $record, $dbh);
339         build_hidden_data;
340         $template->param(
341                 oldbiblionumber             => $oldbiblionumber,
342                 bibid                       => $bibid,
343                 oldbiblionumtagfield        => $oldbiblionumtagfield,
344                 oldbiblionumtagsubfield     => $oldbiblionumtagsubfield,
345                 oldbiblioitemnumtagfield    => $oldbiblioitemnumtagfield,
346                 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
347                 oldbiblioitemnumber         => $oldbiblioitemnumber );
348 }
349 output_html_with_http_headers $input, $cookie, $template->output;