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