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;
29 use C4::SearchMarc; # also includes Biblio.pm, SearchMarc is used to FindDuplicate
32 use C4::Koha; # XXX subfield_is_koha_internal_p
34 use MARC::File::USMARC;
37 use vars qw( $tagslib);
38 use vars qw( $authorised_values_sth);
39 use vars qw( $is_a_modif );
41 my $itemtype; # created here because it can be used in build_authorized_values_list sub
45 ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
47 Find the given $subfield in the given $tag in the given
48 MARC::Record $record. If the subfield is found, returns
49 the (indicators, value) pair; otherwise, (undef, undef) is
55 my ($tagfield,$insubfield,$record,$encoding) = @_;
59 if ($record->field($tagfield)) {
60 push @result, $record->field($tagfield)->data();
65 foreach my $field ($record->field($tagfield)) {
66 my @subfields = $field->subfields();
67 foreach my $subfield (@subfields) {
68 if (@$subfield[0] eq $insubfield) {
69 push @result,@$subfield[1];
70 $indicator = $field->indicator(1).$field->indicator(2);
75 return($indicator,@result);
79 =item MARCfindbreeding
81 $record = MARCfindbreeding($dbh, $breedingid);
83 Look up the breeding farm with database handle $dbh, for the
84 record with id $breedingid. If found, returns the decoded
85 MARC::Record; otherwise, -1 is returned (FIXME).
86 Returns as second parameter the character encoding.
90 sub MARCfindbreeding {
92 my $sth = $dbh->prepare("select file,marc,encoding from marc_breeding where id=?");
94 my ($file,$marc,$encoding) = $sth->fetchrow;
96 my $record = MARC::File::USMARC::decode($marc);
97 if (ref($record) eq undef) {
100 return $record,$encoding;
107 =item build_authorized_values_list
111 sub build_authorized_values_list ($$$$$) {
112 my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
114 my @authorised_values;
117 # builds list, depending on authorised value...
120 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
121 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
123 push @authorised_values, ""
124 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
126 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
127 push @authorised_values, $branchcode;
128 $authorised_lib{$branchcode}=$branchname;
132 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
133 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
135 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
137 while (my ($itemtype,$description) = $sth->fetchrow_array) {
138 push @authorised_values, $itemtype;
139 $authorised_lib{$itemtype}=$description;
141 $value=$itemtype unless ($value);
143 #---- "true" authorised value
145 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
147 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
149 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
150 push @authorised_values, $value;
151 $authorised_lib{$value}=$lib;
154 return CGI::scrolling_list( -name => 'field_value',
155 -values => \@authorised_values,
157 -labels => \%authorised_lib,
164 builds the <input ...> entry for a subfield.
166 sub create_input () {
167 my ($tag,$subfield,$value,$i,$tabloop,$rec,$authorised_values_sth) = @_;
168 $value =~ s/"/"/g;
169 my $dbh = C4::Context->dbh;
171 $subfield_data{tag}=$tag;
172 $subfield_data{subfield}=$subfield;
173 $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
174 $subfield_data{marc_lib_plain}=$tagslib->{$tag}->{$subfield}->{lib};
175 $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
176 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
177 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
178 $subfield_data{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
179 $subfield_data{index} = $i;
180 $subfield_data{visibility} = "display:none" unless (($tagslib->{$tag}->{$subfield}->{hidden}%2==0) or $value ne ''); #check parity
181 # it's an authorised field
182 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
183 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
184 # it's a thesaurus / authority field
185 } elsif ($tagslib->{$tag}->{$subfield}->{authtypecode}) {
186 $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>";
187 # it's a plugin field
188 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
189 # opening plugin. Just check wether we are on a developper computer on a production one
190 # (the cgidir differs)
191 my $cgidir = C4::Context->intranetdir ."/cgi-bin/value_builder";
192 unless (opendir(DIR, "$cgidir")) {
193 $cgidir = C4::Context->intranetdir."/value_builder";
195 my $plugin=$cgidir."/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
197 my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
198 my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
199 $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";
200 # it's an hidden field
201 } elsif ($tag eq '') {
202 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffff00'; \" tabindex=\"1\" type=\"hidden\" name=\"field_value\" value=\"$value\">";
203 } elsif ($tagslib->{$tag}->{$subfield}->{'hidden'}) {
204 $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\" >";
205 # it's a standard field
207 if (length($value) >100) {
208 $subfield_data{marc_value}="<textarea name=\"field_value\" cols=\"70\" rows=\"5\" >$value</textarea>";
210 $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\">"; #"
213 return \%subfield_data;
216 sub build_tabs ($$$$) {
217 my($template, $record, $dbh,$encoding) = @_;
222 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
223 from authorised_values
224 where category=? order by lib");
226 # loop through each tab 0 through 9
227 for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
229 foreach my $tag (sort(keys (%{$tagslib}))) {
231 # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
232 # if MARC::Record is empty => use tab as master loop.
233 if ($record ne -1 && ($record->field($tag) || $tag eq '000')) {
236 @fields = $record->field($tag);
238 push @fields,$record->leader();
240 foreach my $field (@fields) {
243 my ($value,$subfield);
245 $value=$field->data();
251 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
252 next if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'biblio.biblionumber');
253 push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
256 my @subfields=$field->subfields();
257 foreach my $subfieldcount (0..$#subfields) {
258 my $subfield=$subfields[$subfieldcount][0];
259 my $value=$subfields[$subfieldcount][1];
260 next if (length $subfield !=1);
261 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
262 push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
266 # now, loop again to add parameter subfield that are not in the MARC::Record
267 foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
268 next if (length $subfield !=1);
269 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
271 next if (($tagslib->{$tag}->{$subfield}->{hidden}<=-4) or ($tagslib->{$tag}->{$subfield}->{hidden}>=5) ); #check for visibility flag
272 next if (defined($field->subfield($subfield)));
273 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
276 if ($#subfields_data >= 0) {
278 $tag_data{tag} = $tag;
279 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
280 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
281 $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
282 $tag_data{subfield_loop} = \@subfields_data;
284 $tag_data{fixedfield} = 1;
287 push (@loop_data, \%tag_data);
289 # If there is more than 1 field, add an empty hidden field as separator.
290 if ($#fields >=1 && $#loop_data >=0 && $loop_data[$#loop_data]->{'tag'} eq $tag) {
293 push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
295 $tag_data{tag_lib} = '';
296 $tag_data{indicator} = '';
297 $tag_data{subfield_loop} = \@subfields_data;
299 $tag_data{fixedfield} = 1;
301 push (@loop_data, \%tag_data);
305 # if breeding is empty
308 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
309 next if (length $subfield !=1);
310 next if (($tagslib->{$tag}->{$subfield}->{hidden}<=-5) or ($tagslib->{$tag}->{$subfield}->{hidden}>=4) ); #check for visibility flag
311 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
312 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
315 if ($#subfields_data >= 0) {
317 $tag_data{tag} = $tag;
318 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
319 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
320 $tag_data{indicator} = $indicator;
321 $tag_data{subfield_loop} = \@subfields_data;
322 $tag_data{tagfirstsubfield} = $tag_data{subfield_loop}[0];
324 $tag_data{fixedfield} = 1;
326 push (@loop_data, \%tag_data);
330 $template->param($tabloop."XX" =>\@loop_data);
335 sub build_hidden_data () {
336 # build hidden data =>
337 # we store everything, even if we show only requested subfields.
341 foreach my $tag (keys %{$tagslib}) {
342 my $previous_tag = '';
344 # loop through each subfield
345 foreach my $subfield (keys %{$tagslib->{$tag}}) {
346 next if ($subfield eq 'lib');
347 next if ($subfield eq 'tab');
348 next if ($subfield eq 'mandatory');
349 next if ($subfield eq 'repeatable');
350 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
352 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
353 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
354 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
355 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
356 push(@loop_data, \%subfield_data);
363 # ========================
365 #=========================
367 my $error = $input->param('error');
368 my $biblionumber=$input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
370 $biblionumber=$input->param('oldbiblionumber');
371 warn "OLDBIBLIONUMBER".$biblionumber;
373 my $breedingid = $input->param('breedingid');
374 my $z3950 = $input->param('z3950');
375 my $op = $input->param('op');
376 my $frameworkcode = $input->param('frameworkcode');
377 my $dbh = C4::Context->dbh;
379 $frameworkcode = &MARCfind_frameworkcode($dbh,$biblionumber) if ($biblionumber and not ($frameworkcode));
380 $frameworkcode='' if ($frameworkcode eq 'Default');
381 my ($template, $loggedinuser, $cookie)
382 = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
385 authnotrequired => 0,
386 flagsrequired => {editcatalogue => 1},
390 #Getting the list of all frameworks
391 my $queryfwk =$dbh->prepare("select frameworktext, frameworkcode from biblio_framework");
396 push @select_fwk,"Default";
397 $select_fwk{"Default"} = "Default";
398 while (my ($description, $fwk) =$queryfwk->fetchrow) {
399 push @select_fwk, $fwk;
400 $select_fwk{$fwk} = $description;
402 $curfwk=$frameworkcode;
403 my $framework=CGI::scrolling_list( -name => 'Frameworks',
406 -OnChange => 'Changefwk(this);',
407 -values => \@select_fwk,
408 -labels => \%select_fwk,
411 $template->param( framework => $framework);
413 $tagslib = &MARCgettagslib($dbh,1,$frameworkcode);
416 #$record = MARCgetbiblio($dbh,$biblionumber) if ($biblionumber);
417 $record=get_record($biblionumber) if ($biblionumber);
419 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
422 my ($biblionumtagfield,$biblionumtagsubfield);
423 my ($biblioitemnumtagfield,$biblioitemnumtagsubfield,$bibitem,$biblioitemnumber);
426 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
427 ($biblionumtagfield,$biblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber",$frameworkcode);
428 ($biblioitemnumtagfield,$biblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber",$frameworkcode);
429 # search biblioitems value
430 my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
431 $sth->execute($biblionumber);
432 ($biblioitemnumber) = $sth->fetchrow;
434 #------------------------------------------------------------------------------------------------------------------------------
435 if ($op eq "addbiblio") {
436 #------------------------------------------------------------------------------------------------------------------------------
438 my @tags = $input->param('tag');
439 my @subfields = $input->param('subfield');
440 my @values = $input->param('field_value');
441 # build indicator hash.
442 my @ind_tag = $input->param('ind_tag');
443 my @indicator = $input->param('indicator');
444 my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
445 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
446 # check for a duplicate
447 my ($duplicatebiblionumber,$duplicatebibid,$duplicatetitle) = FindDuplicate($record) if ($op eq "addbiblio") && (!$is_a_modif);
448 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
449 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
450 if (!$duplicatebiblionumber or $confirm_not_duplicate) {
451 # MARC::Record built => now, record in DB
453 warn "CONFIRM ITS A MODIF : .$biblionumber";
454 NEWmodbiblioframework($dbh,$biblionumber,$frameworkcode);
455 NEWmodbiblio($dbh,$record,$biblionumber,$frameworkcode);
457 my $biblioitemnumber;
458 ($biblionumber,$biblioitemnumber) = NEWnewbiblio($dbh,$record,$frameworkcode);
459 logaction($loggedinuser,"acqui.simple","add",$biblionumber,"record : ".$record->as_formatted) if (C4::Context->preference("Activate_Log"));
461 # now, redirect to additem page
462 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
465 # it may be a duplicate, warn the user and do nothing
466 build_tabs ($template, $record, $dbh,$encoding);
469 oldbiblionumber => $biblionumber,
470 biblionumber => $biblionumber,
471 biblionumtagfield => $biblionumtagfield,
472 biblionumtagsubfield => $biblionumtagsubfield,
473 biblioitemnumtagfield => $biblioitemnumtagfield,
474 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
475 biblioitemnumber => $biblioitemnumber,
476 duplicatebiblionumber => $duplicatebiblionumber,
477 duplicatetitle => $duplicatetitle,
480 #------------------------------------------------------------------------------------------------------------------------------
481 } elsif ($op eq "addfield") {
482 #------------------------------------------------------------------------------------------------------------------------------
483 my $addedfield = $input->param('addfield_field');
484 my $tagaddfield_subfield = $input->param('addfield_subfield');
485 my @tags = $input->param('tag');
486 my @subfields = $input->param('subfield');
487 my @values = $input->param('field_value');
488 my @ind_tag = $input->param('ind_tag');
489 my @indicator = $input->param('indicator');
490 my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
491 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
492 # adding an empty field
493 my $field = MARC::Field->new("$addedfield",'','','$tagaddfield_subfield' => "");
494 $record->append_fields($field);
495 build_tabs ($template, $record, $dbh,$encoding);
498 oldbiblionumber => $biblionumber,
499 biblionumber => $biblionumber,
500 biblionumtagfield => $biblionumtagfield,
501 biblionumtagsubfield => $biblionumtagsubfield,
502 biblioitemnumtagfield => $biblioitemnumtagfield,
503 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
504 biblioitemnumber => $biblioitemnumber );
505 } elsif ($op eq "delete") {
506 #------------------------------------------------------------------------------------------------------------------------------
507 &NEWdelbiblio($dbh,$biblionumber);
508 logaction($loggedinuser,"acqui.simple","del",$biblionumber,"") if (logstatus);
510 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=/cgi-bin/koha/search.marc/search.pl?type=intranet\"></html>";
512 #------------------------------------------------------------------------------------------------------------------------------
513 #------------------------------------------------------------------------------------------------------------------------------
515 #------------------------------------------------------------------------------------------------------------------------------
516 # If we're in a duplication case, we have to set to "" the bibid and biblionumber
517 # as we'll save the biblio as a new one.
518 if ($op eq "duplicate")
522 #FIXME: it's kind of silly to go from MARC::Record to MARC::File::XML and then back again just to fix the encoding
524 my $uxml = $record->as_xml;
525 my $urecord = MARC::Record::new_from_xml($uxml, 'UTF-8');
528 build_tabs ($template, $record, $dbh,$encoding);
531 oldbiblionumber => $biblionumber,
532 biblionumber => $biblionumber,
533 biblionumtagfield => $biblionumtagfield,
534 biblionumtagsubfield => $biblionumtagsubfield,
535 biblioitemnumtagfield => $biblioitemnumtagfield,
536 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
537 biblioitemnumber => $biblioitemnumber,
541 frameworkcode => $frameworkcode,
542 itemtype => $frameworkcode, # HINT: if the library has itemtype = framework, itemtype is auto filled !
543 hide_marc => C4::Context->preference('hide_marc'),
545 output_html_with_http_headers $input, $cookie, $template->output;