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