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
31 use C4::Koha; # XXX subfield_is_koha_internal_p
33 use MARC::File::USMARC;
36 use vars qw( $tagslib);
37 use vars qw( $authorised_values_sth);
38 use vars qw( $is_a_modif );
40 my $itemtype; # created here because it can be used in build_authorized_values_list sub
44 ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
46 Find the given $subfield in the given $tag in the given
47 MARC::Record $record. If the subfield is found, returns
48 the (indicators, value) pair; otherwise, (undef, undef) is
54 my ($tagfield,$insubfield,$record,$encoding) = @_;
58 if ($record->field($tagfield)) {
59 push @result, $record->field($tagfield)->data();
64 foreach my $field ($record->field($tagfield)) {
65 my @subfields = $field->subfields();
66 foreach my $subfield (@subfields) {
67 if (@$subfield[0] eq $insubfield) {
68 push @result,@$subfield[1];
69 $indicator = $field->indicator(1).$field->indicator(2);
74 return($indicator,@result);
78 =item MARCfindbreeding
80 $record = MARCfindbreeding($dbh, $breedingid);
82 Look up the breeding farm with database handle $dbh, for the
83 record with id $breedingid. If found, returns the decoded
84 MARC::Record; otherwise, -1 is returned (FIXME).
85 Returns as second parameter the character encoding.
89 sub MARCfindbreeding {
91 my $sth = $dbh->prepare("select file,marc,encoding from marc_breeding where id=?");
93 my ($file,$marc,$encoding) = $sth->fetchrow;
95 my $record = MARC::File::USMARC::decode($marc);
96 if (ref($record) eq undef) {
99 return $record,$encoding;
106 =item build_authorized_values_list
110 sub build_authorized_values_list ($$$$$) {
111 my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
113 my @authorised_values;
116 # builds list, depending on authorised value...
119 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
120 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
122 push @authorised_values, ""
123 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
125 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
126 push @authorised_values, $branchcode;
127 $authorised_lib{$branchcode}=$branchname;
131 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
132 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
134 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
136 while (my ($itemtype,$description) = $sth->fetchrow_array) {
137 push @authorised_values, $itemtype;
138 $authorised_lib{$itemtype}=$description;
140 $value=$itemtype unless ($value);
142 #---- "true" authorised value
144 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
146 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
148 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
149 push @authorised_values, $value;
150 $authorised_lib{$value}=$lib;
153 return CGI::scrolling_list( -name => 'field_value',
154 -values => \@authorised_values,
156 -labels => \%authorised_lib,
163 builds the <input ...> entry for a subfield.
165 sub create_input () {
166 my ($tag,$subfield,$value,$i,$tabloop,$rec,$authorised_values_sth) = @_;
167 $value =~ s/"/"/g;
168 my $dbh = C4::Context->dbh;
170 $subfield_data{tag}=$tag;
171 $subfield_data{subfield}=$subfield;
172 $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
173 $subfield_data{marc_lib_plain}=$tagslib->{$tag}->{$subfield}->{lib};
174 $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
175 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
176 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
177 $subfield_data{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
178 $subfield_data{index} = $i;
179 $subfield_data{visibility} = "display:none" unless (($tagslib->{$tag}->{$subfield}->{hidden}%2==0) or $value ne ''); #check parity
180 # it's an authorised field
181 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
182 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
183 # it's a thesaurus / authority field
184 } elsif ($tagslib->{$tag}->{$subfield}->{authtypecode}) {
185 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffff00;'\"\" tabindex=\"1\" type=\"text\" name=\"field_value\" value=\"$value\" size=\"70\" maxlength=\"255\" DISABLE READONLY> <a style=\"cursor: help;\" href=\"javascript:Dopop('../authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$i',$i)\">...</a>";
186 # it's a plugin field
187 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
188 # opening plugin. Just check wether we are on a developper computer on a production one
189 # (the cgidir differs)
190 my $cgidir = C4::Context->intranetdir ."/cgi-bin/value_builder";
191 unless (opendir(DIR, "$cgidir")) {
192 $cgidir = C4::Context->intranetdir."/value_builder";
194 my $plugin=$cgidir."/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
196 my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
197 my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
198 $subfield_data{marc_value}="<input tabindex=\"1\" type=\"text\" name=\"field_value\" value=\"$value\" size=\"70\" maxlength=\"255\" OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i); \"> <a style=\"cursor: help;\" href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
199 # it's an hidden field
200 } elsif ($tag eq '') {
201 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffff00'; \" tabindex=\"1\" type=\"hidden\" name=\"field_value\" value=\"$value\">";
202 } elsif ($tagslib->{$tag}->{$subfield}->{'hidden'}) {
203 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffff00'; \" tabindex=\"1\" type=\"text\" name=\"field_value\" value=\"$value\" size=\"70\" maxlength=\"255\" >";
204 # it's a standard field
206 if (length($value) >100) {
207 $subfield_data{marc_value}="<textarea name=\"field_value\" cols=\"70\" rows=\"5\" >$value</textarea>";
209 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffff00'; \" tabindex=\"1\" type=\"text\" name=\"field_value\" value=\"$value\" size=\"70\">"; #"
212 return \%subfield_data;
215 sub build_tabs ($$$$) {
216 my($template, $record, $dbh,$encoding) = @_;
221 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
222 from authorised_values
223 where category=? order by lib");
225 # loop through each tab 0 through 9
226 for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
228 foreach my $tag (sort(keys (%{$tagslib}))) {
230 # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
231 # if MARC::Record is empty => use tab as master loop.
232 if ($record ne -1 && ($record->field($tag) || $tag eq '000')) {
235 @fields = $record->field($tag);
237 push @fields,$record->leader();
239 foreach my $field (@fields) {
242 my ($value,$subfield);
244 $value=$field->data();
250 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
251 next if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'biblio.biblionumber');
252 push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
255 my @subfields=$field->subfields();
256 foreach my $subfieldcount (0..$#subfields) {
257 my $subfield=$subfields[$subfieldcount][0];
258 my $value=$subfields[$subfieldcount][1];
259 next if (length $subfield !=1);
260 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
261 push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
265 # now, loop again to add parameter subfield that are not in the MARC::Record
266 foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
267 next if (length $subfield !=1);
268 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
270 next if (($tagslib->{$tag}->{$subfield}->{hidden}<=-4) or ($tagslib->{$tag}->{$subfield}->{hidden}>=5) ); #check for visibility flag
271 next if (defined($field->subfield($subfield)));
272 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
275 if ($#subfields_data >= 0) {
277 $tag_data{tag} = $tag;
278 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
279 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
280 $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
281 $tag_data{subfield_loop} = \@subfields_data;
283 $tag_data{fixedfield} = 1;
286 push (@loop_data, \%tag_data);
288 # If there is more than 1 field, add an empty hidden field as separator.
289 if ($#fields >=1 && $#loop_data >=0 && $loop_data[$#loop_data]->{'tag'} eq $tag) {
292 push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
294 $tag_data{tag_lib} = '';
295 $tag_data{indicator} = '';
296 $tag_data{subfield_loop} = \@subfields_data;
298 $tag_data{fixedfield} = 1;
300 push (@loop_data, \%tag_data);
304 # if breeding is empty
307 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
308 next if (length $subfield !=1);
309 next if (($tagslib->{$tag}->{$subfield}->{hidden}<=-5) or ($tagslib->{$tag}->{$subfield}->{hidden}>=4) ); #check for visibility flag
310 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
311 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
314 if ($#subfields_data >= 0) {
316 $tag_data{tag} = $tag;
317 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
318 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
319 $tag_data{indicator} = $indicator;
320 $tag_data{subfield_loop} = \@subfields_data;
321 $tag_data{tagfirstsubfield} = $tag_data{subfield_loop}[0];
323 $tag_data{fixedfield} = 1;
325 push (@loop_data, \%tag_data);
329 $template->param($tabloop."XX" =>\@loop_data);
334 sub build_hidden_data () {
335 # build hidden data =>
336 # we store everything, even if we show only requested subfields.
340 foreach my $tag (keys %{$tagslib}) {
341 my $previous_tag = '';
343 # loop through each subfield
344 foreach my $subfield (keys %{$tagslib->{$tag}}) {
345 next if ($subfield eq 'lib');
346 next if ($subfield eq 'tab');
347 next if ($subfield eq 'mandatory');
348 next if ($subfield eq 'repeatable');
349 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
351 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
352 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
353 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
354 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
355 push(@loop_data, \%subfield_data);
362 # ========================
364 #=========================
366 my $error = $input->param('error');
367 my $biblionumber=$input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
369 $biblionumber=$input->param('oldbiblionumber');
371 my $breedingid = $input->param('breedingid');
372 my $z3950 = $input->param('z3950');
373 my $op = $input->param('op');
374 my $frameworkcode = $input->param('frameworkcode');
375 my $dbh = C4::Context->dbh;
377 $frameworkcode = &MARCfind_frameworkcode($dbh,$biblionumber) if ($biblionumber and not ($frameworkcode));
378 $frameworkcode='' if ($frameworkcode eq 'Default');
379 my ($template, $loggedinuser, $cookie)
380 = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
383 authnotrequired => 0,
384 flagsrequired => {editcatalogue => 1},
388 #Getting the list of all frameworks
389 my $queryfwk =$dbh->prepare("select frameworktext, frameworkcode from biblio_framework");
394 push @select_fwk,"Default";
395 $select_fwk{"Default"} = "Default";
396 while (my ($description, $fwk) =$queryfwk->fetchrow) {
397 push @select_fwk, $fwk;
398 $select_fwk{$fwk} = $description;
400 $curfwk=$frameworkcode;
401 my $framework=CGI::scrolling_list( -name => 'Frameworks',
404 -OnChange => 'Changefwk(this);',
405 -values => \@select_fwk,
406 -labels => \%select_fwk,
409 $template->param( framework => $framework);
411 $tagslib = &MARCgettagslib($dbh,1,$frameworkcode);
414 $record = MARCgetbiblio($dbh,$biblionumber) if ($biblionumber);
415 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
418 my ($biblionumtagfield,$biblionumtagsubfield);
419 my ($biblioitemnumtagfield,$biblioitemnumtagsubfield,$bibitem,$biblioitemnumber);
422 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
423 ($biblionumtagfield,$biblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber",$frameworkcode);
424 ($biblioitemnumtagfield,$biblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber",$frameworkcode);
425 # search biblioitems value
426 my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
427 $sth->execute($biblionumber);
428 ($biblioitemnumber) = $sth->fetchrow;
430 #------------------------------------------------------------------------------------------------------------------------------
431 if ($op eq "addbiblio") {
432 #------------------------------------------------------------------------------------------------------------------------------
434 my @tags = $input->param('tag');
435 my @subfields = $input->param('subfield');
436 my @values = $input->param('field_value');
437 # build indicator hash.
438 my @ind_tag = $input->param('ind_tag');
439 my @indicator = $input->param('indicator');
440 my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
441 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
442 # check for a duplicate
443 my ($duplicatebiblionumber,$duplicatebibid,$duplicatetitle) = FindDuplicate($record) if ($op eq "addbiblio") && (!$is_a_modif);
444 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
445 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
446 if (!$duplicatebiblionumber or $confirm_not_duplicate) {
447 # MARC::Record built => now, record in DB
449 warn "ITS A MODIF : .$biblionumber";
450 NEWmodbiblioframework($dbh,$biblionumber,$frameworkcode);
451 NEWmodbiblio($dbh,$record,$biblionumber,$frameworkcode);
453 my $biblioitemnumber;
454 ($biblionumber,$biblioitemnumber) = NEWnewbiblio($dbh,$record,$frameworkcode);
455 logaction($loggedinuser,"acqui.simple","add",$biblionumber,"record : ".$record->as_formatted) if (C4::Context->preference("Activate_Log"));
457 # now, redirect to additem page
458 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
461 # it may be a duplicate, warn the user and do nothing
462 build_tabs ($template, $record, $dbh,$encoding);
465 biblionumber => $biblionumber,
466 biblionumtagfield => $biblionumtagfield,
467 biblionumtagsubfield => $biblionumtagsubfield,
468 biblioitemnumtagfield => $biblioitemnumtagfield,
469 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
470 biblioitemnumber => $biblioitemnumber,
471 duplicatebiblionumber => $duplicatebiblionumber,
472 duplicatetitle => $duplicatetitle,
475 #------------------------------------------------------------------------------------------------------------------------------
476 } elsif ($op eq "addfield") {
477 #------------------------------------------------------------------------------------------------------------------------------
478 my $addedfield = $input->param('addfield_field');
479 my $tagaddfield_subfield = $input->param('addfield_subfield');
480 my @tags = $input->param('tag');
481 my @subfields = $input->param('subfield');
482 my @values = $input->param('field_value');
483 my @ind_tag = $input->param('ind_tag');
484 my @indicator = $input->param('indicator');
485 my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
486 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
487 # adding an empty field
488 my $field = MARC::Field->new("$addedfield",'','','$tagaddfield_subfield' => "");
489 $record->append_fields($field);
490 build_tabs ($template, $record, $dbh,$encoding);
493 biblionumber => $biblionumber,
494 biblionumtagfield => $biblionumtagfield,
495 biblionumtagsubfield => $biblionumtagsubfield,
496 biblioitemnumtagfield => $biblioitemnumtagfield,
497 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
498 biblioitemnumber => $biblioitemnumber );
499 } elsif ($op eq "delete") {
500 #------------------------------------------------------------------------------------------------------------------------------
501 &NEWdelbiblio($dbh,$biblionumber);
502 logaction($loggedinuser,"acqui.simple","del",$biblionumber,"") if (logstatus);
504 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=/cgi-bin/koha/search.marc/search.pl?type=intranet\"></html>";
506 #------------------------------------------------------------------------------------------------------------------------------
507 #------------------------------------------------------------------------------------------------------------------------------
509 #------------------------------------------------------------------------------------------------------------------------------
510 # If we're in a duplication case, we have to set to "" the bibid and biblionumber
511 # as we'll save the biblio as a new one.
512 if ($op eq "duplicate")
516 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
518 my $uxml = $record->as_xml;
519 my $urecord = MARC::Record::new_from_xml($uxml, 'UTF-8');
522 build_tabs ($template, $record, $dbh,$encoding);
525 biblionumber => $biblionumber,
526 biblionumtagfield => $biblionumtagfield,
527 biblionumtagsubfield => $biblionumtagsubfield,
528 biblioitemnumtagfield => $biblioitemnumtagfield,
529 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
530 biblioitemnumber => $biblioitemnumber,
534 frameworkcode => $frameworkcode,
535 itemtype => $frameworkcode, # HINT: if the library has itemtype = framework, itemtype is auto filled !
536 hide_marc => C4::Context->preference('hide_marc'),
538 output_html_with_http_headers $input, $cookie, $template->output;