(bugfix for previous commit, that was wrong)
[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( $authorised_values_sth);
35 use vars qw( $is_a_modif );
36
37 my $itemtype; # created here because it can be used in build_authorized_values_list sub
38
39 =item find_value
40
41     ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
42
43 Find the given $subfield in the given $tag in the given
44 MARC::Record $record.  If the subfield is found, returns
45 the (indicators, value) pair; otherwise, (undef, undef) is
46 returned.
47
48 =cut
49
50 sub find_value {
51         my ($tagfield,$insubfield,$record,$encoding) = @_;
52         my @result;
53         my $indicator;
54         if ($tagfield <10) {
55                 if ($record->field($tagfield)) {
56                         push @result, $record->field($tagfield)->data();
57                 } else {
58                         push @result,"";
59                 }
60         } else {
61                 foreach my $field ($record->field($tagfield)) {
62                         my @subfields = $field->subfields();
63                         foreach my $subfield (@subfields) {
64                                 if (@$subfield[0] eq $insubfield) {
65                                         push @result,char_decode(@$subfield[1],$encoding);
66                                         $indicator = $field->indicator(1).$field->indicator(2);
67                                 }
68                         }
69                 }
70         }
71         return($indicator,@result);
72 }
73
74
75 =item MARCfindbreeding
76
77     $record = MARCfindbreeding($dbh, $breedingid);
78
79 Look up the breeding farm with database handle $dbh, for the
80 record with id $breedingid.  If found, returns the decoded
81 MARC::Record; otherwise, -1 is returned (FIXME).
82 Returns as second parameter the character encoding.
83
84 =cut
85
86 sub MARCfindbreeding {
87         my ($dbh,$id) = @_;
88         my $sth = $dbh->prepare("select file,marc,encoding from marc_breeding where id=?");
89         $sth->execute($id);
90         my ($file,$marc,$encoding) = $sth->fetchrow;
91         if ($marc) {
92                 my $record = MARC::File::USMARC::decode($marc);
93                 if (ref($record) eq undef) {
94                         return -1;
95                 } else {
96                         return $record,$encoding;
97                 }
98         }
99         return -1;
100 }
101
102
103 =item build_authorized_values_list
104
105 =cut
106
107 sub build_authorized_values_list ($$$$$) {
108         my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
109
110         my @authorised_values;
111         my %authorised_lib;
112
113         # builds list, depending on authorised value...
114
115         #---- branch
116         if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
117         my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
118         $sth->execute;
119         push @authorised_values, ""
120                 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
121
122         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
123                 push @authorised_values, $branchcode;
124                 $authorised_lib{$branchcode}=$branchname;
125         }
126
127         #----- itemtypes
128         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
129                 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
130                 $sth->execute;
131                 push @authorised_values, "" 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                 $value=$itemtype unless ($value);
138
139         #---- "true" authorised value
140         } else {
141                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
142
143                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
144         
145                 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
146                         push @authorised_values, $value;
147                         $authorised_lib{$value}=$lib;
148                 }
149     }
150     return CGI::scrolling_list( -name     => 'field_value',
151                                 -values   => \@authorised_values,
152                                 -default  => $value,
153                                 -labels   => \%authorised_lib,
154                                 -size     => 1,
155                                 -multiple => 0 );
156 }
157
158 =item create_input
159  builds the <input ...> entry for a subfield.
160 =cut
161 sub create_input () {
162         my ($tag,$subfield,$value,$i,$tabloop,$rec,$authorised_values_sth) = @_;
163         $value =~ s/"/&quot;/g;
164         my $dbh = C4::Context->dbh;
165         my %subfield_data;
166         $subfield_data{tag}=$tag;
167         $subfield_data{subfield}=$subfield;
168         $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
169         $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
170         $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
171         $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
172         $subfield_data{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
173         # it's an authorised field
174         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
175                 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
176         # it's a thesaurus / authority field
177         } elsif ($tagslib->{$tag}->{$subfield}->{authtypecode}) {
178                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=47 maxlength=255 DISABLE READONLY> <a href=\"javascript:Dopop('../authorities/auth_finder.pl?category=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$i',$i)\">...</a>";
179         # it's a plugin field
180         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
181                 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
182                 require $plugin;
183                 my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
184                 my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
185                 $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";
186         # it's an hidden field
187         } elsif  ($tag eq '') {
188                 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value\" value=\"$value\">";
189         } elsif  ($tagslib->{$tag}->{$subfield}->{'hidden'}) {
190                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255 DISABLE READONLY>";
191         # it's a standard field
192         } else {
193                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>"; #"
194         }
195         return \%subfield_data;
196 }
197
198 sub build_tabs ($$$$) {
199     my($template, $record, $dbh,$encoding) = @_;
200
201     # fill arrays
202     my @loop_data =();
203     my $tag;
204     my $i=0;
205         my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
206                 from authorised_values
207                 where category=? order by lib");
208
209 # loop through each tab 0 through 9
210         for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
211                 my @loop_data = ();
212                 foreach my $tag (sort(keys (%{$tagslib}))) {
213                         my $indicator;
214         # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
215         # if MARC::Record is empty => use tab as master loop.
216                         if ($record ne -1 && $record->field($tag)) {
217                                 my @fields = $record->field($tag);
218                                 foreach my $field (@fields)  {
219                                         my @subfields_data;
220                                         if ($tag<10) {
221                                                 my $value=$field->data();
222                                                 my $subfield="@";
223                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
224                                                 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$tabloop,$record,$authorised_values_sth));
225                                                 $i++;
226                                         } else {
227                                                 my @subfields=$field->subfields();
228                                                 foreach my $subfieldcount (0..$#subfields) {
229                                                         my $subfield=$subfields[$subfieldcount][0];
230                                                         my $value=$subfields[$subfieldcount][1];
231                                                         next if (length $subfield !=1);
232                                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
233                                                         push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$tabloop,$record,$authorised_values_sth));
234                                                         $i++;
235                                                 }
236                                         }
237 # now, loop again to add parameter subfield that are not in the MARC::Record
238                                         foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
239                                                 next if (length $subfield !=1);
240                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
241                                                 next if ($tag<10);
242                                                 next if (defined($record->field($tag)->subfield($subfield)));
243                                                 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
244                                                 $i++;
245                                         }
246                                         if ($#subfields_data >= 0) {
247                                                 my %tag_data;
248                                                 $tag_data{tag} = $tag;
249                                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
250                                                 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
251                                                 $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
252                                                 $tag_data{subfield_loop} = \@subfields_data;
253                                                 push (@loop_data, \%tag_data);
254                                         }
255 # If there is more than 1 field, add an empty hidden field as separator.
256                                         if ($#fields >=1) {
257                                                 my @subfields_data;
258                                                 my %tag_data;
259                                                 push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
260                                                 $tag_data{tag} = '';
261                                                 $tag_data{tag_lib} = '';
262                                                 $tag_data{indicator} = '';
263                                                 $tag_data{subfield_loop} = \@subfields_data;
264                                                 push (@loop_data, \%tag_data);
265                                                 $i++;
266                                         }
267                                 }
268         # if breeding is empty
269                         } else {
270                                 my @subfields_data;
271                                 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
272                                         next if (length $subfield !=1);
273                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
274                                         push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
275                                         $i++;
276                                 }
277                                 if ($#subfields_data >= 0) {
278                                         my %tag_data;
279                                         $tag_data{tag} = $tag;
280                                         $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
281                                         $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
282                                         $tag_data{indicator} = $indicator;
283                                         $tag_data{subfield_loop} = \@subfields_data;
284                                         push (@loop_data, \%tag_data);
285                                 }
286                         }
287                 }
288                 $template->param($tabloop."XX" =>\@loop_data);
289         }
290 }
291
292
293 sub build_hidden_data () {
294     # build hidden data =>
295     # we store everything, even if we show only requested subfields.
296
297     my @loop_data =();
298     my $i=0;
299     foreach my $tag (keys %{$tagslib}) {
300         my $previous_tag = '';
301
302         # loop through each subfield
303         foreach my $subfield (keys %{$tagslib->{$tag}}) {
304             next if ($subfield eq 'lib');
305             next if ($subfield eq 'tab');
306             next if ($subfield eq 'mandatory');
307                 next if ($subfield eq 'repeatable');
308             next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
309             my %subfield_data;
310             $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
311             $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
312             $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
313             $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
314             push(@loop_data, \%subfield_data);
315             $i++
316         }
317     }
318 }
319
320
321 # ======================== 
322 #          MAIN 
323 #=========================
324 my $input = new CGI;
325 my $error = $input->param('error');
326 my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio.
327 my $breedingid = $input->param('breedingid');
328 my $z3950 = $input->param('z3950');
329 my $op = $input->param('op');
330 my $frameworkcode = $input->param('frameworkcode');
331 my $dbh = C4::Context->dbh;
332 my $bibid;
333 if ($oldbiblionumber) {
334         $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
335         # find framework type
336         $frameworkcode = &MARCfind_frameworkcode($dbh,$bibid) if $bibid;
337 }else {
338         $bibid = $input->param('bibid');
339         $frameworkcode = &MARCfind_frameworkcode($dbh,$bibid) if $bibid;
340 }
341 my ($template, $loggedinuser, $cookie)
342     = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
343                              query => $input,
344                              type => "intranet",
345                              authnotrequired => 0,
346                              flagsrequired => {editcatalogue => 1},
347                              debug => 1,
348                              });
349
350 $tagslib = &MARCgettagslib($dbh,1,$frameworkcode);
351 my $record=-1;
352 my $encoding="";
353 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
354 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
355
356 $is_a_modif=0;
357 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
358 my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
359 if ($bibid) {
360         $is_a_modif=1;
361         # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
362         ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber",$frameworkcode);
363         ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber",$frameworkcode);
364         # search biblioitems value
365         my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
366         $sth->execute($oldbiblionumber);
367         ($oldbiblioitemnumber) = $sth->fetchrow;
368 }
369 #------------------------------------------------------------------------------------------------------------------------------
370 if ($op eq "addbiblio") {
371 #------------------------------------------------------------------------------------------------------------------------------
372         # rebuild
373         my @tags = $input->param('tag');
374         my @subfields = $input->param('subfield');
375         my @values = $input->param('field_value');
376         # build indicator hash.
377         my @ind_tag = $input->param('ind_tag');
378         my @indicator = $input->param('indicator');
379         my %indicators;
380         for (my $i=0;$i<=$#ind_tag;$i++) {
381                 $indicators{$ind_tag[$i]} = $indicator[$i];
382         }
383         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
384 # MARC::Record built => now, record in DB
385         my $oldbibnum;
386         my $oldbibitemnum;
387         if ($is_a_modif) {
388                  NEWmodbiblio($dbh,$record,$bibid,$frameworkcode);
389         } else {
390                 ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record,$frameworkcode);
391         }
392 # now, redirect to additem page
393         print $input->redirect("additem.pl?bibid=$bibid&frameworkcode=$frameworkcode");
394         exit;
395 #------------------------------------------------------------------------------------------------------------------------------
396 } elsif ($op eq "addfield") {
397 #------------------------------------------------------------------------------------------------------------------------------
398         my $addedfield = $input->param('addfield_field');
399         my @tags = $input->param('tag');
400         my @subfields = $input->param('subfield');
401         my @values = $input->param('field_value');
402         # build indicator hash.
403         my @ind_tag = $input->param('ind_tag');
404         my @indicator = $input->param('indicator');
405         splice(@tags,$addedfield,0,$tags[$addedfield]);
406         splice(@subfields,$addedfield,0,$subfields[$addedfield]);
407         splice(@values,$addedfield,0,$values[$addedfield]);
408         splice(@ind_tag,$addedfield,0,$ind_tag[$addedfield]);
409         my %indicators;
410         for (my $i=0;$i<=$#ind_tag;$i++) {
411                 $indicators{$ind_tag[$i]} = $indicator[$i];
412         }
413 # search the part of the array to duplicate.
414         my $start=0;
415         my $end=0;
416         my $started;
417         for (my $i=$#tags;$i>0;$i--) {
418                 $end=$i if ($end eq 0 && $tags[$i] == $addedfield);
419                 $start=$i if ($end>0 && $tags[$i] eq $addedfield);
420                 last if ($end>0 && $tags[$i] ne $addedfield);
421         }
422         warn "ST : $addedfield => $start / $end";
423         # add an empty line in all arrays. This forces a new field in MARC::Record.
424         splice(@tags,$end+1,0,'');
425         splice(@subfields,$end+1,0,'');
426         splice(@values,$end+1,0,'');
427         splice(@ind_tag,$end+1,0,'');
428         splice(@indicator,$end+1,0,'');
429 # then duplicate the field.
430         splice(@tags,$end+2,0,@tags[$start..$end]);
431         splice(@subfields,$end+2,0,@subfields[$start..$end]);
432         splice(@values,$end+2,0,@values[$start..$end]);
433         splice(@ind_tag,$end+2,0,@ind_tag[$start..$end]);
434         splice(@indicator,$end+2,0,@indicator[$start..$end]);
435
436         my %indicators;
437         for (my $i=0;$i<=$#ind_tag;$i++) {
438                 $indicators{$ind_tag[$i]} = $indicator[$i];
439         }
440         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
441         warn "R=>".$record->as_formatted;
442         build_tabs ($template, $record, $dbh,$encoding);
443         build_hidden_data;
444         $template->param(
445                 oldbiblionumber             => $oldbiblionumber,
446                 bibid                       => $bibid,
447                 oldbiblionumtagfield        => $oldbiblionumtagfield,
448                 oldbiblionumtagsubfield     => $oldbiblionumtagsubfield,
449                 oldbiblioitemnumtagfield    => $oldbiblioitemnumtagfield,
450                 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
451                 oldbiblioitemnumber         => $oldbiblioitemnumber );
452 } elsif ($op eq "delete") {
453 #------------------------------------------------------------------------------------------------------------------------------
454         &NEWdelbiblio($dbh,$bibid);
455         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=/cgi-bin/koha/search.marc/search.pl?type=intranet\"></html>";
456         exit;
457 #------------------------------------------------------------------------------------------------------------------------------
458 #------------------------------------------------------------------------------------------------------------------------------
459 } else {
460 #------------------------------------------------------------------------------------------------------------------------------
461         build_tabs ($template, $record, $dbh,$encoding);
462         build_hidden_data;
463         $template->param(
464                 oldbiblionumber             => $oldbiblionumber,
465                 bibid                       => $bibid,
466                 oldbiblionumtagfield        => $oldbiblionumtagfield,
467                 oldbiblionumtagsubfield     => $oldbiblionumtagsubfield,
468                 oldbiblioitemnumtagfield    => $oldbiblioitemnumtagfield,
469                 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
470                 oldbiblioitemnumber         => $oldbiblioitemnumber );
471 }
472 $template->param(
473                 frameworkcode => $frameworkcode,
474                 itemtype => $frameworkcode # HINT: if the library has itemtype = framework, itemtype is auto filled !
475                 );
476 output_html_with_http_headers $input, $cookie, $template->output;