synch'ing 2.2 and head
[wip/koha-chris_n.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?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$i',$i)\">...</a>";
180         # it's a plugin field
181         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
182                 # opening plugin. Just check wether we are on a developper computer on a production one
183                 # (the cgidir differs)
184                 my $cgidir = C4::Context->intranetdir ."/cgi-bin/value_builder";
185                 unless (opendir(DIR, "$cgidir")) {
186                         $cgidir = C4::Context->intranetdir."/value_builder";
187                 } 
188                 my $plugin=$cgidir."/".$tagslib->{$tag}->{$subfield}->{'value_builder'}; 
189                 require $plugin;
190                 my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
191                 my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
192                 $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";
193         # it's an hidden field
194         } elsif  ($tag eq '') {
195                 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value\" value=\"$value\">";
196         } elsif  ($tagslib->{$tag}->{$subfield}->{'hidden'}) {
197                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\" maxlength=\"255\" DISABLE READONLY>";
198         # it's a standard field
199         } else {
200                 if (length($value) >200) {
201                         $subfield_data{marc_value}="<textarea name=\"fieldvalue\" cols=\"50\" rows=\"5\" >$value</textarea>";
202                 } else {
203                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\">"; #"
204                 }
205         }
206         return \%subfield_data;
207 }
208
209 sub build_tabs ($$$$) {
210     my($template, $record, $dbh,$encoding) = @_;
211     # fill arrays
212     my @loop_data =();
213     my $tag;
214     my $i=0;
215         my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
216                 from authorised_values
217                 where category=? order by lib");
218
219 # loop through each tab 0 through 9
220         for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
221                 my @loop_data = ();
222                 foreach my $tag (sort(keys (%{$tagslib}))) {
223                         my $indicator;
224         # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
225         # if MARC::Record is empty => use tab as master loop.
226                         if ($record ne -1 && $record->field($tag)) {
227                                 my @fields = $record->field($tag);
228                                 foreach my $field (@fields)  {
229                                         my @subfields_data;
230                                         if ($tag<10) {
231                                                 my $value=$field->data();
232                                                 my $subfield="@";
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                                         } else {
237                                                 my @subfields=$field->subfields();
238                                                 foreach my $subfieldcount (0..$#subfields) {
239                                                         my $subfield=$subfields[$subfieldcount][0];
240                                                         my $value=$subfields[$subfieldcount][1];
241                                                         next if (length $subfield !=1);
242                                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
243                                                         push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$tabloop,$record,$authorised_values_sth));
244                                                         $i++;
245                                                 }
246                                         }
247 # now, loop again to add parameter subfield that are not in the MARC::Record
248                                         foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
249                                                 next if (length $subfield !=1);
250                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
251                                                 next if ($tag<10);
252                                                 next if (defined($field->subfield($subfield)));
253                                                 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
254                                                 $i++;
255                                         }
256                                         if ($#subfields_data >= 0) {
257                                                 my %tag_data;
258                                                 $tag_data{tag} = $tag;
259                                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
260                                                 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
261                                                 $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
262                                                 $tag_data{subfield_loop} = \@subfields_data;
263                                                 push (@loop_data, \%tag_data);
264                                         }
265 # If there is more than 1 field, add an empty hidden field as separator.
266                                         if ($#fields >1) {
267                                                 my @subfields_data;
268                                                 my %tag_data;
269                                                 push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
270                                                 $tag_data{tag} = '';
271                                                 $tag_data{tag_lib} = '';
272                                                 $tag_data{indicator} = '';
273                                                 $tag_data{subfield_loop} = \@subfields_data;
274                                                 push (@loop_data, \%tag_data);
275                                                 $i++;
276                                         }
277                                 }
278         # if breeding is empty
279                         } else {
280                                 my @subfields_data;
281                                 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
282                                         next if (length $subfield !=1);
283                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
284                                         push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
285                                         $i++;
286                                 }
287                                 if ($#subfields_data >= 0) {
288                                         my %tag_data;
289                                         $tag_data{tag} = $tag;
290                                         $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
291                                         $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
292                                         $tag_data{indicator} = $indicator;
293                                         $tag_data{subfield_loop} = \@subfields_data;
294                                         push (@loop_data, \%tag_data);
295                                 }
296                         }
297                 }
298                 $template->param($tabloop."XX" =>\@loop_data);
299         }
300 }
301
302
303 sub build_hidden_data () {
304     # build hidden data =>
305     # we store everything, even if we show only requested subfields.
306
307     my @loop_data =();
308     my $i=0;
309     foreach my $tag (keys %{$tagslib}) {
310         my $previous_tag = '';
311
312         # loop through each subfield
313         foreach my $subfield (keys %{$tagslib->{$tag}}) {
314             next if ($subfield eq 'lib');
315             next if ($subfield eq 'tab');
316             next if ($subfield eq 'mandatory');
317                 next if ($subfield eq 'repeatable');
318             next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
319             my %subfield_data;
320             $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
321             $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
322             $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
323             $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
324             push(@loop_data, \%subfield_data);
325             $i++
326         }
327     }
328 }
329
330
331 # ======================== 
332 #          MAIN 
333 #=========================
334 my $input = new CGI;
335 my $error = $input->param('error');
336 my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio.
337 my $breedingid = $input->param('breedingid');
338 my $z3950 = $input->param('z3950');
339 my $op = $input->param('op');
340 my $frameworkcode = $input->param('frameworkcode');
341 my $dbh = C4::Context->dbh;
342 my $bibid;
343
344
345 if ($oldbiblionumber) {
346         $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
347         # find framework type
348         $frameworkcode = &MARCfind_frameworkcode($dbh,$bibid) if ($bibid and not ($frameworkcode));
349 }else {
350         $bibid = $input->param('bibid');
351         $frameworkcode = &MARCfind_frameworkcode($dbh,$bibid) if ($bibid and not ($frameworkcode));
352 }
353 $frameworkcode='' if ($frameworkcode eq 'Default');
354 my ($template, $loggedinuser, $cookie)
355     = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
356                              query => $input,
357                              type => "intranet",
358                              authnotrequired => 0,
359                              flagsrequired => {editcatalogue => 1},
360                              debug => 1,
361                              });
362
363 #Getting the list of all frameworks
364 my $queryfwk =$dbh->prepare("select frameworktext, frameworkcode from biblio_framework");
365 $queryfwk->execute;
366 my %select_fwk;
367 my @select_fwk;
368 my $curfwk;
369 push @select_fwk,"Default";
370 $select_fwk{"Default"} = "Default";
371 while (my ($description, $fwk) =$queryfwk->fetchrow) {
372         push @select_fwk, $fwk;
373         $select_fwk{$fwk} = $description;
374 }
375 $curfwk=$frameworkcode;
376 my $framework=CGI::scrolling_list( -name     => 'Frameworks',
377                         -id => 'Frameworks',
378                         -default => $curfwk,
379                         -OnChange => 'Changefwk(this);',
380                         -values   => \@select_fwk,
381                         -labels   => \%select_fwk,
382                         -size     => 1,
383                         -multiple => 0 );
384 $template->param( framework => $framework);
385
386 $tagslib = &MARCgettagslib($dbh,1,$frameworkcode);
387 my $record=-1;
388 my $encoding="";
389 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
390 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
391
392 $is_a_modif=0;
393 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
394 my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
395 if ($bibid) {
396         $is_a_modif=1;
397         # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
398         ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber",$frameworkcode);
399         ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber",$frameworkcode);
400         # search biblioitems value
401         my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
402         $sth->execute($oldbiblionumber);
403         ($oldbiblioitemnumber) = $sth->fetchrow;
404 }
405 #------------------------------------------------------------------------------------------------------------------------------
406 if ($op eq "addbiblio") {
407 #------------------------------------------------------------------------------------------------------------------------------
408         # rebuild
409         my @tags = $input->param('tag');
410         my @subfields = $input->param('subfield');
411         my @values = $input->param('field_value');
412         # build indicator hash.
413         my @ind_tag = $input->param('ind_tag');
414         my @indicator = $input->param('indicator');
415         my %indicators;
416         for (my $i=0;$i<=$#ind_tag;$i++) {
417                 $indicators{$ind_tag[$i]} = $indicator[$i];
418         }
419         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
420         # check for a duplicate
421         my ($duplicatebiblionumber,$duplicatebibid,$duplicatetitle) = FindDuplicate($record) if ($op eq "addbiblio") && (!$is_a_modif);
422         my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
423         # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
424         if (!$duplicatebiblionumber or $confirm_not_duplicate) {
425                 # MARC::Record built => now, record in DB
426                 my $oldbibnum;
427                 my $oldbibitemnum;
428                 if ($is_a_modif) {
429                         NEWmodbiblioframework($dbh,$bibid,$frameworkcode);
430                         NEWmodbiblio($dbh,$record,$bibid,$frameworkcode);
431                 } else {
432                         ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record,$frameworkcode);
433                 }
434         # now, redirect to additem page
435                 print $input->redirect("additem.pl?bibid=$bibid&frameworkcode=$frameworkcode");
436                 exit;
437         } else {
438         # it may be a duplicate, warn the user and do nothing
439                 build_tabs ($template, $record, $dbh,$encoding);
440                 build_hidden_data;
441                 $template->param(
442                         oldbiblionumber             => $oldbiblionumber,
443                         bibid                       => $bibid,
444                         oldbiblionumtagfield        => $oldbiblionumtagfield,
445                         oldbiblionumtagsubfield     => $oldbiblionumtagsubfield,
446                         oldbiblioitemnumtagfield    => $oldbiblioitemnumtagfield,
447                         oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
448                         oldbiblioitemnumber         => $oldbiblioitemnumber,
449                         duplicatebiblionumber           => $duplicatebiblionumber,
450                         duplicatebibid                          => $duplicatebibid,
451                         duplicatetitle                          => $duplicatetitle,
452                          );
453         }
454 #------------------------------------------------------------------------------------------------------------------------------
455 } elsif ($op eq "addfield") {
456 #------------------------------------------------------------------------------------------------------------------------------
457         my $addedfield = $input->param('addfield_field');
458         my @tags = $input->param('tag');
459         my @subfields = $input->param('subfield');
460         my @values = $input->param('field_value');
461         # build indicator hash.
462         my @ind_tag = $input->param('ind_tag');
463         my @indicator = $input->param('indicator');
464         my %indicators;
465         for (my $i=0;$i<=$#ind_tag;$i++) {
466                 $indicators{$ind_tag[$i]} = $indicator[$i];
467         }
468         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
469         # adding an empty field
470         my $field = MARC::Field->new("$addedfield",'','','a'=> "");
471         $record->append_fields($field);
472         build_tabs ($template, $record, $dbh,$encoding);
473         build_hidden_data;
474         $template->param(
475                 oldbiblionumber             => $oldbiblionumber,
476                 bibid                       => $bibid,
477                 oldbiblionumtagfield        => $oldbiblionumtagfield,
478                 oldbiblionumtagsubfield     => $oldbiblionumtagsubfield,
479                 oldbiblioitemnumtagfield    => $oldbiblioitemnumtagfield,
480                 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
481                 oldbiblioitemnumber         => $oldbiblioitemnumber );
482 } elsif ($op eq "delete") {
483 #------------------------------------------------------------------------------------------------------------------------------
484         &NEWdelbiblio($dbh,$bibid);
485         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=/cgi-bin/koha/search.marc/search.pl?type=intranet\"></html>";
486         exit;
487 #------------------------------------------------------------------------------------------------------------------------------
488 #------------------------------------------------------------------------------------------------------------------------------
489 } else {
490 #------------------------------------------------------------------------------------------------------------------------------
491         # If we're in a duplication case, we have to set to "" the bibid and biblionumber
492         # as we'll save the biblio as a new one.
493         if ($op eq "duplicate")
494         {
495                 $bibid = "";
496                 $oldbiblionumber= "";
497         }
498  
499         build_tabs ($template, $record, $dbh,$encoding);
500         build_hidden_data;
501         $template->param(
502                 oldbiblionumber             => $oldbiblionumber,
503                 bibid                       => $bibid,
504                 oldbiblionumtagfield        => $oldbiblionumtagfield,
505                 oldbiblionumtagsubfield     => $oldbiblionumtagsubfield,
506                 oldbiblioitemnumtagfield    => $oldbiblioitemnumtagfield,
507                 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
508                 oldbiblioitemnumber         => $oldbiblioitemnumber,
509                 );
510 }
511 $template->param(
512                 frameworkcode => $frameworkcode,
513                 itemtype => $frameworkcode # HINT: if the library has itemtype = framework, itemtype is auto filled !
514                 );
515 output_html_with_http_headers $input, $cookie, $template->output;