script to edit the item subcategory table.
[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?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 if ($oldbiblionumber) {
344         $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
345         # find framework type
346         $frameworkcode = &MARCfind_frameworkcode($dbh,$bibid) if $bibid;
347 }else {
348         $bibid = $input->param('bibid');
349         $frameworkcode = &MARCfind_frameworkcode($dbh,$bibid) if $bibid;
350 }
351 my ($template, $loggedinuser, $cookie)
352     = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
353                              query => $input,
354                              type => "intranet",
355                              authnotrequired => 0,
356                              flagsrequired => {editcatalogue => 1},
357                              debug => 1,
358                              });
359
360 $tagslib = &MARCgettagslib($dbh,1,$frameworkcode);
361 my $record=-1;
362 my $encoding="";
363 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
364 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
365
366 $is_a_modif=0;
367 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
368 my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
369 if ($bibid) {
370         $is_a_modif=1;
371         # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
372         ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber",$frameworkcode);
373         ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber",$frameworkcode);
374         # search biblioitems value
375         my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
376         $sth->execute($oldbiblionumber);
377         ($oldbiblioitemnumber) = $sth->fetchrow;
378 }
379 #------------------------------------------------------------------------------------------------------------------------------
380 if ($op eq "addbiblio") {
381 #------------------------------------------------------------------------------------------------------------------------------
382         # rebuild
383         my @tags = $input->param('tag');
384         my @subfields = $input->param('subfield');
385         my @values = $input->param('field_value');
386         # build indicator hash.
387         my @ind_tag = $input->param('ind_tag');
388         my @indicator = $input->param('indicator');
389         my %indicators;
390         for (my $i=0;$i<=$#ind_tag;$i++) {
391                 $indicators{$ind_tag[$i]} = $indicator[$i];
392         }
393         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
394         # check for a duplicate
395         my ($duplicatebiblionumber,$duplicatebibid,$duplicatetitle) = FindDuplicate($record) if ($op eq "addbiblio") && (!$is_a_modif);
396         my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
397         # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
398         if (!$duplicatebiblionumber or $confirm_not_duplicate) {
399                 # MARC::Record built => now, record in DB
400                 my $oldbibnum;
401                 my $oldbibitemnum;
402                 if ($is_a_modif) {
403                         NEWmodbiblio($dbh,$record,$bibid,$frameworkcode);
404                 } else {
405                         ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record,$frameworkcode);
406                 }
407         # now, redirect to additem page
408                 print $input->redirect("additem.pl?bibid=$bibid&frameworkcode=$frameworkcode");
409                 exit;
410         } else {
411         # it may be a duplicate, warn the user and do nothing
412                 build_tabs ($template, $record, $dbh,$encoding);
413                 build_hidden_data;
414                 $template->param(
415                         oldbiblionumber             => $oldbiblionumber,
416                         bibid                       => $bibid,
417                         oldbiblionumtagfield        => $oldbiblionumtagfield,
418                         oldbiblionumtagsubfield     => $oldbiblionumtagsubfield,
419                         oldbiblioitemnumtagfield    => $oldbiblioitemnumtagfield,
420                         oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
421                         oldbiblioitemnumber         => $oldbiblioitemnumber,
422                         duplicatebiblionumber           => $duplicatebiblionumber,
423                         duplicatebibid                          => $duplicatebibid,
424                         duplicatetitle                          => $duplicatetitle,
425                          );
426         }
427 #------------------------------------------------------------------------------------------------------------------------------
428 } elsif ($op eq "addfield") {
429 #------------------------------------------------------------------------------------------------------------------------------
430         my $addedfield = $input->param('addfield_field');
431         my @tags = $input->param('tag');
432         my @subfields = $input->param('subfield');
433         my @values = $input->param('field_value');
434         # build indicator hash.
435         my @ind_tag = $input->param('ind_tag');
436         my @indicator = $input->param('indicator');
437         splice(@tags,$addedfield,0,$tags[$addedfield]);
438         splice(@subfields,$addedfield,0,$subfields[$addedfield]);
439         splice(@values,$addedfield,0,$values[$addedfield]);
440         splice(@ind_tag,$addedfield,0,$ind_tag[$addedfield]);
441         my %indicators;
442         for (my $i=0;$i<=$#ind_tag;$i++) {
443                 $indicators{$ind_tag[$i]} = $indicator[$i];
444         }
445 # search the part of the array to duplicate.
446         my $start=0;
447         my $end=0;
448         my $started;
449         for (my $i=$#tags;$i>0;$i--) {
450                 $end=$i if ($end eq 0 && $tags[$i] == $addedfield);
451                 $start=$i if ($end>0 && $tags[$i] eq $addedfield);
452                 last if ($end>0 && $tags[$i] ne $addedfield);
453         }
454         # add an empty line in all arrays. This forces a new field in MARC::Record.
455         splice(@tags,$end+1,0,'');
456         splice(@subfields,$end+1,0,'');
457         splice(@values,$end+1,0,'');
458         splice(@ind_tag,$end+1,0,'');
459         splice(@indicator,$end+1,0,'');
460 # then duplicate the field.
461         splice(@tags,$end+2,0,@tags[$start..$end]);
462         splice(@subfields,$end+2,0,@subfields[$start..$end]);
463         splice(@values,$end+2,0,@values[$start..$end]);
464         splice(@ind_tag,$end+2,0,@ind_tag[$start..$end]);
465         splice(@indicator,$end+2,0,@indicator[$start..$end]);
466
467         my %indicators;
468         for (my $i=0;$i<=$#ind_tag;$i++) {
469                 $indicators{$ind_tag[$i]} = $indicator[$i];
470         }
471         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
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;