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;
35 use vars qw( $tagslib);
36 use vars qw( $authorised_values_sth);
37 use vars qw( $is_a_modif );
39 my $itemtype; # created here because it can be used in build_authorized_values_list sub
43 ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
45 Find the given $subfield in the given $tag in the given
46 MARC::Record $record. If the subfield is found, returns
47 the (indicators, value) pair; otherwise, (undef, undef) is
53 my ($tagfield,$insubfield,$record,$encoding) = @_;
57 if ($record->field($tagfield)) {
58 push @result, $record->field($tagfield)->data();
63 foreach my $field ($record->field($tagfield)) {
64 my @subfields = $field->subfields();
65 foreach my $subfield (@subfields) {
66 if (@$subfield[0] eq $insubfield) {
67 push @result,char_decode(@$subfield[1],$encoding);
68 $indicator = $field->indicator(1).$field->indicator(2);
73 return($indicator,@result);
77 =item MARCfindbreeding
79 $record = MARCfindbreeding($dbh, $breedingid);
81 Look up the breeding farm with database handle $dbh, for the
82 record with id $breedingid. If found, returns the decoded
83 MARC::Record; otherwise, -1 is returned (FIXME).
84 Returns as second parameter the character encoding.
88 sub MARCfindbreeding {
90 my $sth = $dbh->prepare("select file,marc,encoding from marc_breeding where id=?");
92 my ($file,$marc,$encoding) = $sth->fetchrow;
94 my $record = MARC::File::USMARC::decode($marc);
95 if (ref($record) eq undef) {
98 return $record,$encoding;
105 =item build_authorized_values_list
109 sub build_authorized_values_list ($$$$$) {
110 my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
112 my @authorised_values;
115 # builds list, depending on authorised value...
118 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
119 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
121 push @authorised_values, ""
122 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
124 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
125 push @authorised_values, $branchcode;
126 $authorised_lib{$branchcode}=$branchname;
130 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
131 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
133 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
135 while (my ($itemtype,$description) = $sth->fetchrow_array) {
136 push @authorised_values, $itemtype;
137 $authorised_lib{$itemtype}=$description;
139 $value=$itemtype unless ($value);
141 #---- "true" authorised value
143 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
145 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
147 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
148 push @authorised_values, $value;
149 $authorised_lib{$value}=$lib;
152 return CGI::scrolling_list( -name => 'field_value',
153 -values => \@authorised_values,
155 -labels => \%authorised_lib,
162 builds the <input ...> entry for a subfield.
164 sub create_input () {
165 my ($tag,$subfield,$value,$i,$tabloop,$rec,$authorised_values_sth) = @_;
166 $value =~ s/"/"/g;
167 my $dbh = C4::Context->dbh;
169 $subfield_data{tag}=$tag;
170 $subfield_data{subfield}=$subfield;
171 $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
172 $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
173 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
174 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
175 $subfield_data{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
176 $subfield_data{index} = $i;
177 # it's an authorised field
178 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
179 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
180 # it's a thesaurus / authority field
181 } elsif ($tagslib->{$tag}->{$subfield}->{authtypecode}) {
182 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"67\" maxlength=\"255\" DISABLE READONLY /> <a href=\"javascript:Dopop('../authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$i',$i)\">...</a>";
183 # it's a plugin field
184 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
185 # opening plugin. Just check wether we are on a developper computer on a production one
186 # (the cgidir differs)
187 my $cgidir = C4::Context->intranetdir ."/cgi-bin/value_builder";
188 unless (opendir(DIR, "$cgidir")) {
189 $cgidir = C4::Context->intranetdir."/value_builder";
191 my $plugin=$cgidir."/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
193 my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
194 my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
195 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"67\" maxlength=\"255\" OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i)\" /> <a href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
196 # it's an hidden field
197 } elsif ($tag eq '') {
198 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value\" value=\"$value\" />";
199 } elsif ($tagslib->{$tag}->{$subfield}->{'hidden'}) {
200 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"70\" maxlength=\"255\" DISABLE READONLY />";
201 # it's a standard field
203 if (length($value) >100) {
204 $subfield_data{marc_value}="<textarea name=\"field_value\" cols=\"70\" rows=\"5\" >$value</textarea>";
206 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"70\" />"; #"
209 return \%subfield_data;
212 sub build_tabs ($$$$) {
213 my($template, $record, $dbh,$encoding) = @_;
218 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
219 from authorised_values
220 where category=? order by lib");
222 # loop through each tab 0 through 9
223 for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
225 foreach my $tag (sort(keys (%{$tagslib}))) {
227 # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
228 # if MARC::Record is empty => use tab as master loop.
229 if ($record ne -1 && ($record->field($tag) || $tag eq '000')) {
232 @fields = $record->field($tag);
234 push @fields,$record->leader();
236 foreach my $field (@fields) {
239 my ($value,$subfield);
241 $value=$field->data();
247 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
248 next if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'biblio.biblionumber');
249 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$tabloop,$record,$authorised_values_sth));
252 my @subfields=$field->subfields();
253 foreach my $subfieldcount (0..$#subfields) {
254 my $subfield=$subfields[$subfieldcount][0];
255 my $value=$subfields[$subfieldcount][1];
256 next if (length $subfield !=1);
257 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
258 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$tabloop,$record,$authorised_values_sth));
262 # now, loop again to add parameter subfield that are not in the MARC::Record
263 foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
264 next if (length $subfield !=1);
265 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
267 next if (defined($field->subfield($subfield)));
268 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
271 if ($#subfields_data >= 0) {
273 $tag_data{tag} = $tag;
274 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
275 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
276 $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
277 $tag_data{subfield_loop} = \@subfields_data;
278 push (@loop_data, \%tag_data);
280 # If there is more than 1 field, add an empty hidden field as separator.
281 if ($#fields >=1 && $#loop_data >=0 && $loop_data[$#loop_data]->{'tag'} eq $tag) {
284 push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
286 $tag_data{tag_lib} = '';
287 $tag_data{indicator} = '';
288 $tag_data{subfield_loop} = \@subfields_data;
289 push (@loop_data, \%tag_data);
293 # if breeding is empty
296 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
297 next if (length $subfield !=1);
298 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
299 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
302 if ($#subfields_data >= 0) {
304 $tag_data{tag} = $tag;
305 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
306 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
307 $tag_data{indicator} = $indicator;
308 $tag_data{subfield_loop} = \@subfields_data;
309 push (@loop_data, \%tag_data);
313 $template->param($tabloop."XX" =>\@loop_data);
318 sub build_hidden_data () {
319 # build hidden data =>
320 # we store everything, even if we show only requested subfields.
324 foreach my $tag (keys %{$tagslib}) {
325 my $previous_tag = '';
327 # loop through each subfield
328 foreach my $subfield (keys %{$tagslib->{$tag}}) {
329 next if ($subfield eq 'lib');
330 next if ($subfield eq 'tab');
331 next if ($subfield eq 'mandatory');
332 next if ($subfield eq 'repeatable');
333 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
335 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
336 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
337 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
338 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
339 push(@loop_data, \%subfield_data);
346 # ========================
348 #=========================
350 my $error = $input->param('error');
351 my $biblionumber=$input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
352 my $breedingid = $input->param('breedingid');
353 my $z3950 = $input->param('z3950');
354 my $op = $input->param('op');
355 my $frameworkcode = $input->param('frameworkcode');
356 my $dbh = C4::Context->dbh;
358 $frameworkcode = &MARCfind_frameworkcode($dbh,$biblionumber) if ($biblionumber and not ($frameworkcode));
359 $frameworkcode='' if ($frameworkcode eq 'Default');
360 my ($template, $loggedinuser, $cookie)
361 = get_template_and_user({template_name => "cataloguing/addbiblio.tmpl",
364 authnotrequired => 0,
365 flagsrequired => {editcatalogue => 1},
369 #Getting the list of all frameworks
370 my $queryfwk =$dbh->prepare("select frameworktext, frameworkcode from biblio_framework");
375 push @select_fwk,"Default";
376 $select_fwk{"Default"} = "Default";
377 while (my ($description, $fwk) =$queryfwk->fetchrow) {
378 push @select_fwk, $fwk;
379 $select_fwk{$fwk} = $description;
381 $curfwk=$frameworkcode;
382 my $framework=CGI::scrolling_list( -name => 'Frameworks',
385 -OnChange => 'Changefwk(this);',
386 -values => \@select_fwk,
387 -labels => \%select_fwk,
390 $template->param( framework => $framework);
392 $tagslib = &MARCgettagslib($dbh,1,$frameworkcode);
395 $record = MARCgetbiblio($dbh,$biblionumber) if ($biblionumber);
396 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
399 my ($biblionumtagfield,$biblionumtagsubfield);
400 my ($biblioitemnumtagfield,$biblioitemnumtagsubfield,$bibitem,$biblioitemnumber);
403 # if it's a modif, retrieve bibli and biblioitem numbers for the future modification of old-DB.
404 ($biblionumtagfield,$biblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber",$frameworkcode);
405 ($biblioitemnumtagfield,$biblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber",$frameworkcode);
406 # search biblioitems value
407 my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
408 $sth->execute($biblionumber);
409 ($biblioitemnumber) = $sth->fetchrow;
411 #------------------------------------------------------------------------------------------------------------------------------
412 if ($op eq "addbiblio") {
413 #------------------------------------------------------------------------------------------------------------------------------
415 my @tags = $input->param('tag');
416 my @subfields = $input->param('subfield');
417 my @values = $input->param('field_value');
418 # build indicator hash.
419 my @ind_tag = $input->param('ind_tag');
420 my @indicator = $input->param('indicator');
421 my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
422 my $record=MARC::Record::new_from_xml($xml);
423 # check for a duplicate
424 my ($duplicatebiblionumber,$duplicatebibid,$duplicatetitle) = FindDuplicate($record) if ($op eq "addbiblio") && (!$is_a_modif);
425 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
426 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
427 if (!$duplicatebiblionumber or $confirm_not_duplicate) {
428 # MARC::Record built => now, record in DB
430 warn "ITS A MODIF : .$biblionumber";
431 NEWmodbiblioframework($dbh,$biblionumber,$frameworkcode);
432 NEWmodbiblio($dbh,$record,$biblionumber,$frameworkcode);
434 my $biblioitemnumber;
435 ($biblionumber,$biblioitemnumber) = NEWnewbiblio($dbh,$record,$frameworkcode);
436 logaction($loggedinuser,"acqui.simple","add",$biblionumber,"record : ".$record->as_formatted) if (C4::Context->preference("Activate_Log"));
438 # now, redirect to additem page
439 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
442 # it may be a duplicate, warn the user and do nothing
443 build_tabs ($template, $record, $dbh,$encoding);
446 biblionumber => $biblionumber,
447 biblionumtagfield => $biblionumtagfield,
448 biblionumtagsubfield => $biblionumtagsubfield,
449 biblioitemnumtagfield => $biblioitemnumtagfield,
450 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
451 biblioitemnumber => $biblioitemnumber,
452 duplicatebiblionumber => $duplicatebiblionumber,
453 duplicatetitle => $duplicatetitle,
456 #------------------------------------------------------------------------------------------------------------------------------
457 } elsif ($op eq "addfield") {
458 #------------------------------------------------------------------------------------------------------------------------------
459 my $addedfield = $input->param('addfield_field');
460 my @tags = $input->param('tag');
461 my @subfields = $input->param('subfield');
462 my @values = $input->param('field_value');
463 # build indicator hash.
464 my @ind_tag = $input->param('ind_tag');
465 my @indicator = $input->param('indicator');
466 my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
467 my $record=MARC::Record::new_from_xml($xml);
468 # adding an empty field
469 my $field = MARC::Field->new("$addedfield",'','','a'=> "");
470 $record->append_fields($field);
471 build_tabs ($template, $record, $dbh,$encoding);
474 biblionumber => $biblionumber,
475 biblionumtagfield => $biblionumtagfield,
476 biblionumtagsubfield => $biblionumtagsubfield,
477 biblioitemnumtagfield => $biblioitemnumtagfield,
478 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
479 biblioitemnumber => $biblioitemnumber );
480 } elsif ($op eq "delete") {
481 #------------------------------------------------------------------------------------------------------------------------------
482 &NEWdelbiblio($dbh,$biblionumber);
483 logaction($loggedinuser,"acqui.simple","del",$biblionumber,"") if (logstatus);
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")
498 build_tabs ($template, $record, $dbh,$encoding);
501 biblionumber => $biblionumber,
502 biblionumtagfield => $biblionumtagfield,
503 biblionumtagsubfield => $biblionumtagsubfield,
504 biblioitemnumtagfield => $biblioitemnumtagfield,
505 biblioitemnumtagsubfield => $biblioitemnumtagsubfield,
506 biblioitemnumber => $biblioitemnumber,
510 frameworkcode => $frameworkcode,
511 itemtype => $frameworkcode, # HINT: if the library has itemtype = framework, itemtype is auto filled !
512 hide_marc => C4::Context->preference('hide_marc'),
514 output_html_with_http_headers $input, $cookie, $template->output;