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::Koha; # XXX subfield_is_koha_internal_p
31 use MARC::File::USMARC;
33 use vars qw( $tagslib);
34 use vars qw( $authorised_values_sth);
35 use vars qw( $is_a_modif );
39 ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
41 Find the given $subfield in the given $tag in the given
42 MARC::Record $record. If the subfield is found, returns
43 the (indicators, value) pair; otherwise, (undef, undef) is
49 my ($tagfield,$insubfield,$record,$encoding) = @_;
53 if ($record->field($tagfield)) {
54 push @result, $record->field($tagfield)->data();
59 foreach my $field ($record->field($tagfield)) {
60 my @subfields = $field->subfields();
61 foreach my $subfield (@subfields) {
62 if (@$subfield[0] eq $insubfield) {
63 push @result,char_decode(@$subfield[1],$encoding);
64 $indicator = $field->indicator(1).$field->indicator(2);
69 return($indicator,@result);
73 =item MARCfindbreeding
75 $record = MARCfindbreeding($dbh, $breedingid);
77 Look up the breeding farm with database handle $dbh, for the
78 record with id $breedingid. If found, returns the decoded
79 MARC::Record; otherwise, -1 is returned (FIXME).
80 Returns as second parameter the character encoding.
84 sub MARCfindbreeding {
86 my $sth = $dbh->prepare("select file,marc,encoding from marc_breeding where id=?");
88 my ($file,$marc,$encoding) = $sth->fetchrow;
90 my $record = MARC::File::USMARC::decode($marc);
91 if (ref($record) eq undef) {
94 return $record,$encoding;
101 =item build_authorized_values_list
105 sub build_authorized_values_list ($$$$$) {
106 my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
108 my @authorised_values;
111 # builds list, depending on authorised value...
114 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
115 my $sth=$dbh->prepare("select branchcode,branchname from branches");
117 push @authorised_values, ""
118 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
120 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
121 push @authorised_values, $branchcode;
122 $authorised_lib{$branchcode}=$branchname;
126 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
127 my $sth=$dbh->prepare("select itemtype,description from itemtypes");
129 push @authorised_values, ""
130 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
132 while (my ($itemtype,$description) = $sth->fetchrow_array) {
133 push @authorised_values, $itemtype;
134 $authorised_lib{$itemtype}=$description;
137 #---- "true" authorised value
139 $authorised_values_sth->execute
140 ($tagslib->{$tag}->{$subfield}->{authorised_value});
142 push @authorised_values, ""
143 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
145 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
146 push @authorised_values, $value;
147 $authorised_lib{$value}=$lib;
150 return CGI::scrolling_list( -name => 'field_value',
151 -values => \@authorised_values,
153 -labels => \%authorised_lib,
159 builds the <input ...> entry for a subfield.
161 sub create_input () {
162 my ($tag,$subfield,$value,$i,$tabloop,$rec,$authorised_values_sth) = @_;
163 my $dbh = C4::Context->dbh;
165 $subfield_data{tag}=$tag;
166 $subfield_data{subfield}=$subfield;
167 $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
168 $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
169 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
170 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
171 $subfield_data{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
172 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
173 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
174 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
175 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" size=47 maxlength=255 DISABLE READONLY> <a href=\"javascript:Dopop('../thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">...</a>";
176 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
177 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
179 my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
180 my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
181 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" DISABLE READONLY 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";
182 } elsif ($tag eq '') {
183 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value\" size=50 maxlength=255>"; #"
185 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>"; #"
187 return \%subfield_data;
190 sub build_tabs ($$$$) {
191 my($template, $record, $dbh,$encoding) = @_;
197 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
198 from authorised_values
199 where category=? order by authorised_value");
201 # loop through each tab 0 through 9
202 for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
204 foreach my $tag (sort(keys (%{$tagslib}))) {
206 # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
207 # if MARC::Record is empty => use tab as master loop.
208 if ($record ne -1 && $record->field($tag)) {
209 my @fields = $record->field($tag);
210 foreach my $field (@fields) {
213 my $value=$field->data();
215 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$tabloop,$record,$authorised_values_sth))
216 unless ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
219 my @subfields=$field->subfields();
220 foreach my $subfieldcount (0..$#subfields) {
221 my $subfield=$subfields[$subfieldcount][0];
222 my $value=$subfields[$subfieldcount][1];
223 next if subfield_is_koha_internal_p($subfield);
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));
229 # now, loop again to add parameter subfield that are not in the MARC::Record
230 foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
231 next if subfield_is_koha_internal_p($subfield);
232 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
233 next if ($tag > 10 && defined($record->field($tag)->subfield($subfield)));
234 next if ($tag < 10 && defined($record->field($tag)->data()));
235 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
238 if ($#subfields_data >= 0) {
240 $tag_data{tag} = $tag;
241 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
242 $tag_data{indicator} = $indicator;
243 $tag_data{subfield_loop} = \@subfields_data;
244 push (@loop_data, \%tag_data);
246 # If there is more than 1 field, add an empty hidden field as separator.
250 push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
252 $tag_data{tag_lib} = '';
253 $tag_data{indicator} = '';
254 $tag_data{subfield_loop} = \@subfields_data;
255 push (@loop_data, \%tag_data);
259 # if breeding is empty
262 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
263 next if subfield_is_koha_internal_p($subfield);
264 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
265 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
268 if ($#subfields_data >= 0) {
270 $tag_data{tag} = $tag;
271 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
272 $tag_data{indicator} = $indicator;
273 $tag_data{subfield_loop} = \@subfields_data;
274 push (@loop_data, \%tag_data);
278 $template->param($tabloop."XX" =>\@loop_data);
283 sub build_hidden_data () {
284 # build hidden data =>
285 # we store everything, even if we show only requested subfields.
289 foreach my $tag (keys %{$tagslib}) {
290 my $previous_tag = '';
292 # loop through each subfield
293 foreach my $subfield (keys %{$tagslib->{$tag}}) {
294 next if ($subfield eq 'lib');
295 next if ($subfield eq 'tab');
296 next if ($subfield eq 'mandatory');
297 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
299 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
300 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
301 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
302 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
303 push(@loop_data, \%subfield_data);
310 my $error = $input->param('error');
311 my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio.
312 my $breedingid = $input->param('breedingid');
313 my $z3950 = $input->param('z3950');
314 my $op = $input->param('op');
315 my $dbh = C4::Context->dbh;
317 if ($oldbiblionumber) {
318 $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
320 $bibid = $input->param('bibid');
322 my ($template, $loggedinuser, $cookie)
323 = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
326 authnotrequired => 0,
327 flagsrequired => {catalogue => 1},
331 $tagslib = &MARCgettagslib($dbh,1);
334 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
335 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
338 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
339 my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
342 # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
343 ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber");
344 ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber");
345 # search biblioitems value
346 my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
347 $sth->execute($oldbiblionumber);
348 ($oldbiblioitemnumber) = $sth->fetchrow;
350 #------------------------------------------------------------------------------------------------------------------------------
351 if ($op eq "addbiblio") {
352 #------------------------------------------------------------------------------------------------------------------------------
354 my @tags = $input->param('tag');
355 my @subfields = $input->param('subfield');
356 my @values = $input->param('field_value');
357 # build indicator hash.
358 my @ind_tag = $input->param('ind_tag');
359 my @indicator = $input->param('indicator');
361 for (my $i=0;$i<=$#ind_tag;$i++) {
362 $indicators{$ind_tag[$i]} = $indicator[$i];
364 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
365 # MARC::Record built => now, record in DB
369 NEWmodbiblio($dbh,$record,$bibid);
371 ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record);
373 # now, redirect to additem page
374 print $input->redirect("additem.pl?bibid=$bibid");
376 #------------------------------------------------------------------------------------------------------------------------------
377 } elsif ($op eq "addfield") {
378 #------------------------------------------------------------------------------------------------------------------------------
379 my $addedfield = $input->param('addfield_field');
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');
386 splice(@tags,$addedfield,0,$tags[$addedfield]);
387 splice(@subfields,$addedfield,0,$subfields[$addedfield]);
388 splice(@values,$addedfield,0,$values[$addedfield]);
389 splice(@ind_tag,$addedfield,0,$ind_tag[$addedfield]);
391 for (my $i=0;$i<=$#ind_tag;$i++) {
392 $indicators{$ind_tag[$i]} = $indicator[$i];
394 # search the part of the array to duplicate.
398 for (my $i=0;$i<=$#tags;$i++) {
399 $start=$i if ($start eq 0 && $tags[$i] == $addedfield);
400 $end=$i if ($start>0 && $tags[$i] eq $addedfield);
401 last if ($start>0 && $tags[$i] ne $addedfield);
403 # add an empty line in all arrays. This forces a new field in MARC::Record.
404 splice(@tags,$end+1,0,'');
405 splice(@subfields,$end+1,0,'');
406 splice(@values,$end+1,0,'');
407 splice(@ind_tag,$end+1,0,'');
408 splice(@indicator,$end+1,0,'');
409 # then duplicate the field.
410 splice(@tags,$end+2,0,@tags[$start..$end]);
411 splice(@subfields,$end+2,0,@subfields[$start..$end]);
412 splice(@values,$end+2,0,@values[$start..$end]);
413 splice(@ind_tag,$end+2,0,@ind_tag[$start..$end]);
414 splice(@indicator,$end+2,0,@indicator[$start..$end]);
417 for (my $i=0;$i<=$#ind_tag;$i++) {
418 $indicators{$ind_tag[$i]} = $indicator[$i];
420 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
421 build_tabs ($template, $record, $dbh,$encoding);
424 oldbiblionumber => $oldbiblionumber,
426 oldbiblionumtagfield => $oldbiblionumtagfield,
427 oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
428 oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
429 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
430 oldbiblioitemnumber => $oldbiblioitemnumber );
431 } elsif ($op eq "delete") {
432 #------------------------------------------------------------------------------------------------------------------------------
433 &NEWdelbiblio($dbh,$bibid);
434 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=/cgi-bin/koha/search.marc/search.pl?type=intranet\"></html>";
436 #------------------------------------------------------------------------------------------------------------------------------#------------------------------------------------------------------------------------------------------------------------------
438 #------------------------------------------------------------------------------------------------------------------------------
439 build_tabs ($template, $record, $dbh,$encoding);
442 oldbiblionumber => $oldbiblionumber,
444 oldbiblionumtagfield => $oldbiblionumtagfield,
445 oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
446 oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
447 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
448 oldbiblioitemnumber => $oldbiblioitemnumber );
450 output_html_with_http_headers $input, $cookie, $template->output;