5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
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
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.
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
26 use C4::Interface::CGI::Output;
28 use C4::SearchMarc; # also includes Biblio.pm, SearchMarc is used to FindDuplicate
30 use C4::Koha; # XXX subfield_is_koha_internal_p
32 use MARC::File::USMARC;
34 use vars qw( $tagslib);
35 use vars qw( $authorised_values_sth);
36 use vars qw( $is_a_modif );
38 my $itemtype; # created here because it can be used in build_authorized_values_list sub
42 ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
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
52 my ($tagfield,$insubfield,$record,$encoding) = @_;
56 if ($record->field($tagfield)) {
57 push @result, $record->field($tagfield)->data();
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);
72 return($indicator,@result);
76 =item MARCfindbreeding
78 $record = MARCfindbreeding($dbh, $breedingid);
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.
87 sub MARCfindbreeding {
89 my $sth = $dbh->prepare("select file,marc,encoding from marc_breeding where id=?");
91 my ($file,$marc,$encoding) = $sth->fetchrow;
93 my $record = MARC::File::USMARC::decode($marc);
94 if (ref($record) eq undef) {
97 return $record,$encoding;
104 =item build_authorized_values_list
108 sub build_authorized_values_list ($$$$$) {
109 my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
111 my @authorised_values;
114 # builds list, depending on authorised value...
117 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
118 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
120 push @authorised_values, ""
121 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
123 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
124 push @authorised_values, $branchcode;
125 $authorised_lib{$branchcode}=$branchname;
129 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
130 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
132 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
134 while (my ($itemtype,$description) = $sth->fetchrow_array) {
135 push @authorised_values, $itemtype;
136 $authorised_lib{$itemtype}=$description;
138 $value=$itemtype unless ($value);
140 #---- "true" authorised value
142 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
144 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
146 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
147 push @authorised_values, $value;
148 $authorised_lib{$value}=$lib;
151 return CGI::scrolling_list( -name => 'field_value',
152 -values => \@authorised_values,
154 -labels => \%authorised_lib,
160 builds the <input ...> entry for a subfield.
162 sub create_input () {
163 my ($tag,$subfield,$value,$i,$tabloop,$rec,$authorised_values_sth) = @_;
164 $value =~ s/"/"/g;
165 my $dbh = C4::Context->dbh;
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";
188 my $plugin=$cgidir."/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
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
200 if (length($value) >200) {
201 $subfield_data{marc_value}="<textarea name=\"fieldvalue\" cols=\"50\" rows=\"5\" >$value</textarea>";
203 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\">"; #"
206 return \%subfield_data;
209 sub build_tabs ($$$$) {
210 my($template, $record, $dbh,$encoding) = @_;
215 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
216 from authorised_values
217 where category=? order by lib");
219 # loop through each tab 0 through 9
220 for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
222 foreach my $tag (sort(keys (%{$tagslib}))) {
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) {
231 my $value=$field->data();
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));
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));
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);
252 next if (defined($field->subfield($subfield)));
253 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
256 if ($#subfields_data >= 0) {
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);
265 # If there is more than 1 field, add an empty hidden field as separator.
269 push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
271 $tag_data{tag_lib} = '';
272 $tag_data{indicator} = '';
273 $tag_data{subfield_loop} = \@subfields_data;
274 push (@loop_data, \%tag_data);
278 # if breeding is empty
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));
287 if ($#subfields_data >= 0) {
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);
298 $template->param($tabloop."XX" =>\@loop_data);
303 sub build_hidden_data () {
304 # build hidden data =>
305 # we store everything, even if we show only requested subfields.
309 foreach my $tag (keys %{$tagslib}) {
310 my $previous_tag = '';
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");
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);
331 # ========================
333 #=========================
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;
343 if ($oldbiblionumber) {
344 $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
345 # find framework type
346 $frameworkcode = &MARCfind_frameworkcode($dbh,$bibid) if $bibid;
348 $bibid = $input->param('bibid');
349 $frameworkcode = &MARCfind_frameworkcode($dbh,$bibid) if $bibid;
351 my ($template, $loggedinuser, $cookie)
352 = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
355 authnotrequired => 0,
356 flagsrequired => {editcatalogue => 1},
360 $tagslib = &MARCgettagslib($dbh,1,$frameworkcode);
363 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
364 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
367 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
368 my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
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;
379 #------------------------------------------------------------------------------------------------------------------------------
380 if ($op eq "addbiblio") {
381 #------------------------------------------------------------------------------------------------------------------------------
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');
390 for (my $i=0;$i<=$#ind_tag;$i++) {
391 $indicators{$ind_tag[$i]} = $indicator[$i];
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
403 NEWmodbiblio($dbh,$record,$bibid,$frameworkcode);
405 ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record,$frameworkcode);
407 # now, redirect to additem page
408 print $input->redirect("additem.pl?bibid=$bibid&frameworkcode=$frameworkcode");
411 # it may be a duplicate, warn the user and do nothing
412 build_tabs ($template, $record, $dbh,$encoding);
415 oldbiblionumber => $oldbiblionumber,
417 oldbiblionumtagfield => $oldbiblionumtagfield,
418 oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
419 oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
420 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
421 oldbiblioitemnumber => $oldbiblioitemnumber,
422 duplicatebiblionumber => $duplicatebiblionumber,
423 duplicatebibid => $duplicatebibid,
424 duplicatetitle => $duplicatetitle,
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]);
442 for (my $i=0;$i<=$#ind_tag;$i++) {
443 $indicators{$ind_tag[$i]} = $indicator[$i];
445 # search the part of the array to duplicate.
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);
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]);
468 for (my $i=0;$i<=$#ind_tag;$i++) {
469 $indicators{$ind_tag[$i]} = $indicator[$i];
471 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
472 build_tabs ($template, $record, $dbh,$encoding);
475 oldbiblionumber => $oldbiblionumber,
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>";
487 #------------------------------------------------------------------------------------------------------------------------------
488 #------------------------------------------------------------------------------------------------------------------------------
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")
496 $oldbiblionumber= "";
499 build_tabs ($template, $record, $dbh,$encoding);
502 oldbiblionumber => $oldbiblionumber,
504 oldbiblionumtagfield => $oldbiblionumtagfield,
505 oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
506 oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
507 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
508 oldbiblioitemnumber => $oldbiblioitemnumber,
512 frameworkcode => $frameworkcode,
513 itemtype => $frameworkcode # HINT: if the library has itemtype = framework, itemtype is auto filled !
515 output_html_with_http_headers $input, $cookie, $template->output;