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 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\" maxlength=\"255\">"; #"
202 return \%subfield_data;
205 sub build_tabs ($$$$) {
206 my($template, $record, $dbh,$encoding) = @_;
212 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
213 from authorised_values
214 where category=? order by lib");
216 # loop through each tab 0 through 9
217 for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
219 foreach my $tag (sort(keys (%{$tagslib}))) {
221 # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
222 # if MARC::Record is empty => use tab as master loop.
223 if ($record ne -1 && $record->field($tag)) {
224 my @fields = $record->field($tag);
225 foreach my $field (@fields) {
228 my $value=$field->data();
230 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
231 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$tabloop,$record,$authorised_values_sth));
234 my @subfields=$field->subfields();
235 foreach my $subfieldcount (0..$#subfields) {
236 my $subfield=$subfields[$subfieldcount][0];
237 my $value=$subfields[$subfieldcount][1];
238 next if (length $subfield !=1);
239 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
240 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$tabloop,$record,$authorised_values_sth));
244 # now, loop again to add parameter subfield that are not in the MARC::Record
245 foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
246 next if (length $subfield !=1);
247 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
249 next if (defined($field->subfield($subfield)));
250 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
253 if ($#subfields_data >= 0) {
255 $tag_data{tag} = $tag;
256 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
257 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
258 $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
259 $tag_data{subfield_loop} = \@subfields_data;
260 push (@loop_data, \%tag_data);
262 # If there is more than 1 field, add an empty hidden field as separator.
266 push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
268 $tag_data{tag_lib} = '';
269 $tag_data{indicator} = '';
270 $tag_data{subfield_loop} = \@subfields_data;
271 push (@loop_data, \%tag_data);
275 # if breeding is empty
278 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
279 next if (length $subfield !=1);
280 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
281 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
284 if ($#subfields_data >= 0) {
286 $tag_data{tag} = $tag;
287 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
288 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
289 $tag_data{indicator} = $indicator;
290 $tag_data{subfield_loop} = \@subfields_data;
291 push (@loop_data, \%tag_data);
295 $template->param($tabloop."XX" =>\@loop_data);
300 sub build_hidden_data () {
301 # build hidden data =>
302 # we store everything, even if we show only requested subfields.
306 foreach my $tag (keys %{$tagslib}) {
307 my $previous_tag = '';
309 # loop through each subfield
310 foreach my $subfield (keys %{$tagslib->{$tag}}) {
311 next if ($subfield eq 'lib');
312 next if ($subfield eq 'tab');
313 next if ($subfield eq 'mandatory');
314 next if ($subfield eq 'repeatable');
315 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
317 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
318 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
319 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
320 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
321 push(@loop_data, \%subfield_data);
328 # ========================
330 #=========================
332 my $error = $input->param('error');
333 my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio.
334 my $breedingid = $input->param('breedingid');
335 my $z3950 = $input->param('z3950');
336 my $op = $input->param('op');
337 my $frameworkcode = $input->param('frameworkcode');
338 my $dbh = C4::Context->dbh;
340 if ($oldbiblionumber) {
341 $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
342 # find framework type
343 $frameworkcode = &MARCfind_frameworkcode($dbh,$bibid) if $bibid;
345 $bibid = $input->param('bibid');
346 $frameworkcode = &MARCfind_frameworkcode($dbh,$bibid) if $bibid;
348 my ($template, $loggedinuser, $cookie)
349 = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
352 authnotrequired => 0,
353 flagsrequired => {editcatalogue => 1},
357 $tagslib = &MARCgettagslib($dbh,1,$frameworkcode);
360 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
361 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
364 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
365 my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
368 # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
369 ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber",$frameworkcode);
370 ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber",$frameworkcode);
371 # search biblioitems value
372 my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
373 $sth->execute($oldbiblionumber);
374 ($oldbiblioitemnumber) = $sth->fetchrow;
376 #------------------------------------------------------------------------------------------------------------------------------
377 if ($op eq "addbiblio") {
378 #------------------------------------------------------------------------------------------------------------------------------
380 my @tags = $input->param('tag');
381 my @subfields = $input->param('subfield');
382 my @values = $input->param('field_value');
383 # build indicator hash.
384 my @ind_tag = $input->param('ind_tag');
385 my @indicator = $input->param('indicator');
387 for (my $i=0;$i<=$#ind_tag;$i++) {
388 $indicators{$ind_tag[$i]} = $indicator[$i];
390 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
391 # check for a duplicate
392 my ($duplicatebiblionumber,$duplicatebibid,$duplicatetitle) = FindDuplicate($record) if ($op eq "addbiblio") && (!$is_a_modif);
393 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
394 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
395 if (!$duplicatebiblionumber or $confirm_not_duplicate) {
396 # MARC::Record built => now, record in DB
400 NEWmodbiblio($dbh,$record,$bibid,$frameworkcode);
402 ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record,$frameworkcode);
404 # now, redirect to additem page
405 print $input->redirect("additem.pl?bibid=$bibid&frameworkcode=$frameworkcode");
408 # it may be a duplicate, warn the user and do nothing
409 build_tabs ($template, $record, $dbh,$encoding);
412 oldbiblionumber => $oldbiblionumber,
414 oldbiblionumtagfield => $oldbiblionumtagfield,
415 oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
416 oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
417 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
418 oldbiblioitemnumber => $oldbiblioitemnumber,
419 duplicatebiblionumber => $duplicatebiblionumber,
420 duplicatebibid => $duplicatebibid,
421 duplicatetitle => $duplicatetitle,
424 #------------------------------------------------------------------------------------------------------------------------------
425 } elsif ($op eq "addfield") {
426 #------------------------------------------------------------------------------------------------------------------------------
427 my $addedfield = $input->param('addfield_field');
428 my @tags = $input->param('tag');
429 my @subfields = $input->param('subfield');
430 my @values = $input->param('field_value');
431 # build indicator hash.
432 my @ind_tag = $input->param('ind_tag');
433 my @indicator = $input->param('indicator');
434 splice(@tags,$addedfield,0,$tags[$addedfield]);
435 splice(@subfields,$addedfield,0,$subfields[$addedfield]);
436 splice(@values,$addedfield,0,$values[$addedfield]);
437 splice(@ind_tag,$addedfield,0,$ind_tag[$addedfield]);
439 for (my $i=0;$i<=$#ind_tag;$i++) {
440 $indicators{$ind_tag[$i]} = $indicator[$i];
442 # search the part of the array to duplicate.
446 for (my $i=$#tags;$i>0;$i--) {
447 $end=$i if ($end eq 0 && $tags[$i] == $addedfield);
448 $start=$i if ($end>0 && $tags[$i] eq $addedfield);
449 last if ($end>0 && $tags[$i] ne $addedfield);
451 # add an empty line in all arrays. This forces a new field in MARC::Record.
452 splice(@tags,$end+1,0,'');
453 splice(@subfields,$end+1,0,'');
454 splice(@values,$end+1,0,'');
455 splice(@ind_tag,$end+1,0,'');
456 splice(@indicator,$end+1,0,'');
457 # then duplicate the field.
458 splice(@tags,$end+2,0,@tags[$start..$end]);
459 splice(@subfields,$end+2,0,@subfields[$start..$end]);
460 splice(@values,$end+2,0,@values[$start..$end]);
461 splice(@ind_tag,$end+2,0,@ind_tag[$start..$end]);
462 splice(@indicator,$end+2,0,@indicator[$start..$end]);
465 for (my $i=0;$i<=$#ind_tag;$i++) {
466 $indicators{$ind_tag[$i]} = $indicator[$i];
468 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
469 build_tabs ($template, $record, $dbh,$encoding);
472 oldbiblionumber => $oldbiblionumber,
474 oldbiblionumtagfield => $oldbiblionumtagfield,
475 oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
476 oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
477 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
478 oldbiblioitemnumber => $oldbiblioitemnumber );
479 } elsif ($op eq "delete") {
480 #------------------------------------------------------------------------------------------------------------------------------
481 &NEWdelbiblio($dbh,$bibid);
482 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=/cgi-bin/koha/search.marc/search.pl?type=intranet\"></html>";
484 #------------------------------------------------------------------------------------------------------------------------------
485 #------------------------------------------------------------------------------------------------------------------------------
487 #------------------------------------------------------------------------------------------------------------------------------
488 build_tabs ($template, $record, $dbh,$encoding);
491 oldbiblionumber => $oldbiblionumber,
493 oldbiblionumtagfield => $oldbiblionumtagfield,
494 oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
495 oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
496 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
497 oldbiblioitemnumber => $oldbiblioitemnumber,
501 frameworkcode => $frameworkcode,
502 itemtype => $frameworkcode # HINT: if the library has itemtype = framework, itemtype is auto filled !
504 output_html_with_http_headers $input, $cookie, $template->output;