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?category=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$i',$i)\">...</a>";
180 # it's a plugin field
181 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
182 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
184 my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
185 my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
186 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"47\" maxlength=\"255\" OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i)\"> <a href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
187 # it's an hidden field
188 } elsif ($tag eq '') {
189 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value\" value=\"$value\">";
190 } elsif ($tagslib->{$tag}->{$subfield}->{'hidden'}) {
191 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\" maxlength=\"255\" DISABLE READONLY>";
192 # it's a standard field
194 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\" maxlength=\"255\">"; #"
196 return \%subfield_data;
199 sub build_tabs ($$$$) {
200 my($template, $record, $dbh,$encoding) = @_;
206 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
207 from authorised_values
208 where category=? order by lib");
210 # loop through each tab 0 through 9
211 for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
213 foreach my $tag (sort(keys (%{$tagslib}))) {
215 # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
216 # if MARC::Record is empty => use tab as master loop.
217 if ($record ne -1 && $record->field($tag)) {
218 my @fields = $record->field($tag);
219 foreach my $field (@fields) {
222 my $value=$field->data();
224 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
225 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$tabloop,$record,$authorised_values_sth));
228 my @subfields=$field->subfields();
229 foreach my $subfieldcount (0..$#subfields) {
230 my $subfield=$subfields[$subfieldcount][0];
231 my $value=$subfields[$subfieldcount][1];
232 next if (length $subfield !=1);
233 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
234 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$tabloop,$record,$authorised_values_sth));
238 # now, loop again to add parameter subfield that are not in the MARC::Record
239 foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
240 next if (length $subfield !=1);
241 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
243 next if (defined($field->subfield($subfield)));
244 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
247 if ($#subfields_data >= 0) {
249 $tag_data{tag} = $tag;
250 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
251 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
252 $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
253 $tag_data{subfield_loop} = \@subfields_data;
254 push (@loop_data, \%tag_data);
256 # If there is more than 1 field, add an empty hidden field as separator.
260 push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
262 $tag_data{tag_lib} = '';
263 $tag_data{indicator} = '';
264 $tag_data{subfield_loop} = \@subfields_data;
265 push (@loop_data, \%tag_data);
269 # if breeding is empty
272 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
273 next if (length $subfield !=1);
274 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
275 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
278 if ($#subfields_data >= 0) {
280 $tag_data{tag} = $tag;
281 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
282 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
283 $tag_data{indicator} = $indicator;
284 $tag_data{subfield_loop} = \@subfields_data;
285 push (@loop_data, \%tag_data);
289 $template->param($tabloop."XX" =>\@loop_data);
294 sub build_hidden_data () {
295 # build hidden data =>
296 # we store everything, even if we show only requested subfields.
300 foreach my $tag (keys %{$tagslib}) {
301 my $previous_tag = '';
303 # loop through each subfield
304 foreach my $subfield (keys %{$tagslib->{$tag}}) {
305 next if ($subfield eq 'lib');
306 next if ($subfield eq 'tab');
307 next if ($subfield eq 'mandatory');
308 next if ($subfield eq 'repeatable');
309 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
311 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
312 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
313 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
314 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
315 push(@loop_data, \%subfield_data);
322 # ========================
324 #=========================
326 my $error = $input->param('error');
327 my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio.
328 my $breedingid = $input->param('breedingid');
329 my $z3950 = $input->param('z3950');
330 my $op = $input->param('op');
331 my $frameworkcode = $input->param('frameworkcode');
332 my $dbh = C4::Context->dbh;
334 if ($oldbiblionumber) {
335 $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
336 # find framework type
337 $frameworkcode = &MARCfind_frameworkcode($dbh,$bibid) if $bibid;
339 $bibid = $input->param('bibid');
340 $frameworkcode = &MARCfind_frameworkcode($dbh,$bibid) if $bibid;
342 my ($template, $loggedinuser, $cookie)
343 = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
346 authnotrequired => 0,
347 flagsrequired => {editcatalogue => 1},
351 $tagslib = &MARCgettagslib($dbh,1,$frameworkcode);
354 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
355 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
358 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
359 my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
362 # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
363 ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber",$frameworkcode);
364 ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber",$frameworkcode);
365 # search biblioitems value
366 my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
367 $sth->execute($oldbiblionumber);
368 ($oldbiblioitemnumber) = $sth->fetchrow;
370 #------------------------------------------------------------------------------------------------------------------------------
371 if ($op eq "addbiblio") {
372 #------------------------------------------------------------------------------------------------------------------------------
374 my @tags = $input->param('tag');
375 my @subfields = $input->param('subfield');
376 my @values = $input->param('field_value');
377 # build indicator hash.
378 my @ind_tag = $input->param('ind_tag');
379 my @indicator = $input->param('indicator');
381 for (my $i=0;$i<=$#ind_tag;$i++) {
382 $indicators{$ind_tag[$i]} = $indicator[$i];
384 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
385 # check for a duplicate
386 my ($duplicatebiblionumber,$duplicatebibid,$duplicatetitle) = FindDuplicate($record) if ($op eq "addbiblio") && (!$is_a_modif);
387 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
388 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
389 if (!$duplicatebiblionumber or $confirm_not_duplicate) {
390 # MARC::Record built => now, record in DB
394 NEWmodbiblio($dbh,$record,$bibid,$frameworkcode);
396 ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record,$frameworkcode);
398 # now, redirect to additem page
399 print $input->redirect("additem.pl?bibid=$bibid&frameworkcode=$frameworkcode");
402 # it may be a duplicate, warn the user and do nothing
403 build_tabs ($template, $record, $dbh,$encoding);
406 oldbiblionumber => $oldbiblionumber,
408 oldbiblionumtagfield => $oldbiblionumtagfield,
409 oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
410 oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
411 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
412 oldbiblioitemnumber => $oldbiblioitemnumber,
413 duplicatebiblionumber => $duplicatebiblionumber,
414 duplicatebibid => $duplicatebibid,
415 duplicatetitle => $duplicatetitle,
418 #------------------------------------------------------------------------------------------------------------------------------
419 } elsif ($op eq "addfield") {
420 #------------------------------------------------------------------------------------------------------------------------------
421 my $addedfield = $input->param('addfield_field');
422 my @tags = $input->param('tag');
423 my @subfields = $input->param('subfield');
424 my @values = $input->param('field_value');
425 # build indicator hash.
426 my @ind_tag = $input->param('ind_tag');
427 my @indicator = $input->param('indicator');
428 splice(@tags,$addedfield,0,$tags[$addedfield]);
429 splice(@subfields,$addedfield,0,$subfields[$addedfield]);
430 splice(@values,$addedfield,0,$values[$addedfield]);
431 splice(@ind_tag,$addedfield,0,$ind_tag[$addedfield]);
433 for (my $i=0;$i<=$#ind_tag;$i++) {
434 $indicators{$ind_tag[$i]} = $indicator[$i];
436 # search the part of the array to duplicate.
440 for (my $i=$#tags;$i>0;$i--) {
441 $end=$i if ($end eq 0 && $tags[$i] == $addedfield);
442 $start=$i if ($end>0 && $tags[$i] eq $addedfield);
443 last if ($end>0 && $tags[$i] ne $addedfield);
445 # add an empty line in all arrays. This forces a new field in MARC::Record.
446 splice(@tags,$end+1,0,'');
447 splice(@subfields,$end+1,0,'');
448 splice(@values,$end+1,0,'');
449 splice(@ind_tag,$end+1,0,'');
450 splice(@indicator,$end+1,0,'');
451 # then duplicate the field.
452 splice(@tags,$end+2,0,@tags[$start..$end]);
453 splice(@subfields,$end+2,0,@subfields[$start..$end]);
454 splice(@values,$end+2,0,@values[$start..$end]);
455 splice(@ind_tag,$end+2,0,@ind_tag[$start..$end]);
456 splice(@indicator,$end+2,0,@indicator[$start..$end]);
459 for (my $i=0;$i<=$#ind_tag;$i++) {
460 $indicators{$ind_tag[$i]} = $indicator[$i];
462 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
463 build_tabs ($template, $record, $dbh,$encoding);
466 oldbiblionumber => $oldbiblionumber,
468 oldbiblionumtagfield => $oldbiblionumtagfield,
469 oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
470 oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
471 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
472 oldbiblioitemnumber => $oldbiblioitemnumber );
473 } elsif ($op eq "delete") {
474 #------------------------------------------------------------------------------------------------------------------------------
475 &NEWdelbiblio($dbh,$bibid);
476 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=/cgi-bin/koha/search.marc/search.pl?type=intranet\"></html>";
478 #------------------------------------------------------------------------------------------------------------------------------
479 #------------------------------------------------------------------------------------------------------------------------------
481 #------------------------------------------------------------------------------------------------------------------------------
482 build_tabs ($template, $record, $dbh,$encoding);
485 oldbiblionumber => $oldbiblionumber,
487 oldbiblionumtagfield => $oldbiblionumtagfield,
488 oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
489 oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
490 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
491 oldbiblioitemnumber => $oldbiblioitemnumber,
495 frameworkcode => $frameworkcode,
496 itemtype => $frameworkcode # HINT: if the library has itemtype = framework, itemtype is auto filled !
498 output_html_with_http_headers $input, $cookie, $template->output;