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 );
37 my $itemtype; # created here because it can be used in build_authorized_values_list sub
41 ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
43 Find the given $subfield in the given $tag in the given
44 MARC::Record $record. If the subfield is found, returns
45 the (indicators, value) pair; otherwise, (undef, undef) is
51 my ($tagfield,$insubfield,$record,$encoding) = @_;
55 if ($record->field($tagfield)) {
56 push @result, $record->field($tagfield)->data();
61 foreach my $field ($record->field($tagfield)) {
62 my @subfields = $field->subfields();
63 foreach my $subfield (@subfields) {
64 if (@$subfield[0] eq $insubfield) {
65 push @result,char_decode(@$subfield[1],$encoding);
66 $indicator = $field->indicator(1).$field->indicator(2);
71 return($indicator,@result);
75 =item MARCfindbreeding
77 $record = MARCfindbreeding($dbh, $breedingid);
79 Look up the breeding farm with database handle $dbh, for the
80 record with id $breedingid. If found, returns the decoded
81 MARC::Record; otherwise, -1 is returned (FIXME).
82 Returns as second parameter the character encoding.
86 sub MARCfindbreeding {
88 my $sth = $dbh->prepare("select file,marc,encoding from marc_breeding where id=?");
90 my ($file,$marc,$encoding) = $sth->fetchrow;
92 my $record = MARC::File::USMARC::decode($marc);
93 if (ref($record) eq undef) {
96 return $record,$encoding;
103 =item build_authorized_values_list
107 sub build_authorized_values_list ($$$$$) {
108 my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
110 my @authorised_values;
113 # builds list, depending on authorised value...
116 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
117 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
119 push @authorised_values, ""
120 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
122 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
123 push @authorised_values, $branchcode;
124 $authorised_lib{$branchcode}=$branchname;
128 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
129 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
131 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
133 while (my ($itemtype,$description) = $sth->fetchrow_array) {
134 push @authorised_values, $itemtype;
135 $authorised_lib{$itemtype}=$description;
137 $value=$itemtype unless ($value);
139 #---- "true" authorised value
141 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
143 push @authorised_values, "" 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 $value =~ s/"/"/g;
164 my $dbh = C4::Context->dbh;
166 $subfield_data{tag}=$tag;
167 $subfield_data{subfield}=$subfield;
168 $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
169 $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
170 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
171 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
172 $subfield_data{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
173 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
174 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
175 } elsif ($tagslib->{$tag}->{$subfield}->{authtypecode}) {
176 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" size=47 maxlength=255 DISABLE READONLY> <a href=\"javascript:Dopop('../authorities/auth_finder.pl?category=$tagslib->{$tag}->{$subfield}->{authtypecode}&index=$i',$i)\">...</a>";
177 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
178 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
180 my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
181 my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
182 $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";
183 } elsif ($tag eq '') {
184 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value\" size=50 maxlength=255>";
186 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>"; #"
188 return \%subfield_data;
191 sub build_tabs ($$$$) {
192 my($template, $record, $dbh,$encoding) = @_;
198 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
199 from authorised_values
200 where category=? order by lib");
202 # loop through each tab 0 through 9
203 for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
205 foreach my $tag (sort(keys (%{$tagslib}))) {
207 # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
208 # if MARC::Record is empty => use tab as master loop.
209 if ($record ne -1 && $record->field($tag)) {
210 my @fields = $record->field($tag);
211 foreach my $field (@fields) {
214 my $value=$field->data();
216 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
217 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$tabloop,$record,$authorised_values_sth));
220 my @subfields=$field->subfields();
221 foreach my $subfieldcount (0..$#subfields) {
222 my $subfield=$subfields[$subfieldcount][0];
223 my $value=$subfields[$subfieldcount][1];
224 next if (length $subfield !=1);
225 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
226 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$tabloop,$record,$authorised_values_sth));
230 # now, loop again to add parameter subfield that are not in the MARC::Record
231 foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
232 next if (length $subfield !=1);
233 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
235 next if (defined($record->field($tag)->subfield($subfield)));
236 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
239 if ($#subfields_data >= 0) {
241 $tag_data{tag} = $tag;
242 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
243 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
244 $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
245 $tag_data{subfield_loop} = \@subfields_data;
246 push (@loop_data, \%tag_data);
248 # If there is more than 1 field, add an empty hidden field as separator.
252 push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
254 $tag_data{tag_lib} = '';
255 $tag_data{indicator} = '';
256 $tag_data{subfield_loop} = \@subfields_data;
257 push (@loop_data, \%tag_data);
261 # if breeding is empty
264 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
265 next if (length $subfield !=1);
266 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
267 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
270 if ($#subfields_data >= 0) {
272 $tag_data{tag} = $tag;
273 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
274 $tag_data{indicator} = $indicator;
275 $tag_data{subfield_loop} = \@subfields_data;
276 push (@loop_data, \%tag_data);
280 $template->param($tabloop."XX" =>\@loop_data);
285 sub build_hidden_data () {
286 # build hidden data =>
287 # we store everything, even if we show only requested subfields.
291 foreach my $tag (keys %{$tagslib}) {
292 my $previous_tag = '';
294 # loop through each subfield
295 foreach my $subfield (keys %{$tagslib->{$tag}}) {
296 next if ($subfield eq 'lib');
297 next if ($subfield eq 'tab');
298 next if ($subfield eq 'mandatory');
299 next if ($subfield eq 'repeatable');
300 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
302 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
303 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
304 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
305 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
306 push(@loop_data, \%subfield_data);
313 # ========================
315 #=========================
317 my $error = $input->param('error');
318 my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio.
319 my $breedingid = $input->param('breedingid');
320 my $z3950 = $input->param('z3950');
321 my $op = $input->param('op');
322 my $frameworkcode = $input->param('frameworkcode');
323 my $dbh = C4::Context->dbh;
325 if ($oldbiblionumber) {
326 $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
327 # find framework type
328 $frameworkcode = &MARCfind_frameworkcode($dbh,$bibid) if $bibid;
330 $bibid = $input->param('bibid');
331 $frameworkcode = &MARCfind_frameworkcode($dbh,$bibid) if $bibid;
333 my ($template, $loggedinuser, $cookie)
334 = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
337 authnotrequired => 0,
338 flagsrequired => {editcatalogue => 1},
342 $tagslib = &MARCgettagslib($dbh,1,$frameworkcode);
345 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
346 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
349 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
350 my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
353 # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
354 ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber");
355 ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber");
356 # search biblioitems value
357 my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
358 $sth->execute($oldbiblionumber);
359 ($oldbiblioitemnumber) = $sth->fetchrow;
361 #------------------------------------------------------------------------------------------------------------------------------
362 if ($op eq "addbiblio") {
363 #------------------------------------------------------------------------------------------------------------------------------
365 my @tags = $input->param('tag');
366 my @subfields = $input->param('subfield');
367 my @values = $input->param('field_value');
368 # build indicator hash.
369 my @ind_tag = $input->param('ind_tag');
370 my @indicator = $input->param('indicator');
372 for (my $i=0;$i<=$#ind_tag;$i++) {
373 $indicators{$ind_tag[$i]} = $indicator[$i];
375 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
376 # MARC::Record built => now, record in DB
380 NEWmodbiblio($dbh,$record,$bibid,$frameworkcode);
382 ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record,$frameworkcode);
384 # now, redirect to additem page
385 print $input->redirect("additem.pl?bibid=$bibid&frameworkcode=$frameworkcode");
387 #------------------------------------------------------------------------------------------------------------------------------
388 } elsif ($op eq "addfield") {
389 #------------------------------------------------------------------------------------------------------------------------------
390 my $addedfield = $input->param('addfield_field');
391 my @tags = $input->param('tag');
392 my @subfields = $input->param('subfield');
393 my @values = $input->param('field_value');
394 # build indicator hash.
395 my @ind_tag = $input->param('ind_tag');
396 my @indicator = $input->param('indicator');
397 splice(@tags,$addedfield,0,$tags[$addedfield]);
398 splice(@subfields,$addedfield,0,$subfields[$addedfield]);
399 splice(@values,$addedfield,0,$values[$addedfield]);
400 splice(@ind_tag,$addedfield,0,$ind_tag[$addedfield]);
402 for (my $i=0;$i<=$#ind_tag;$i++) {
403 $indicators{$ind_tag[$i]} = $indicator[$i];
405 # search the part of the array to duplicate.
409 for (my $i=0;$i<=$#tags;$i++) {
410 $start=$i if ($start eq 0 && $tags[$i] == $addedfield);
411 $end=$i if ($start>0 && $tags[$i] eq $addedfield);
412 last if ($start>0 && $tags[$i] ne $addedfield);
414 # add an empty line in all arrays. This forces a new field in MARC::Record.
415 splice(@tags,$end+1,0,'');
416 splice(@subfields,$end+1,0,'');
417 splice(@values,$end+1,0,'');
418 splice(@ind_tag,$end+1,0,'');
419 splice(@indicator,$end+1,0,'');
420 # then duplicate the field.
421 splice(@tags,$end+2,0,@tags[$start..$end]);
422 splice(@subfields,$end+2,0,@subfields[$start..$end]);
423 splice(@values,$end+2,0,@values[$start..$end]);
424 splice(@ind_tag,$end+2,0,@ind_tag[$start..$end]);
425 splice(@indicator,$end+2,0,@indicator[$start..$end]);
428 for (my $i=0;$i<=$#ind_tag;$i++) {
429 $indicators{$ind_tag[$i]} = $indicator[$i];
431 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
432 build_tabs ($template, $record, $dbh,$encoding);
435 oldbiblionumber => $oldbiblionumber,
437 oldbiblionumtagfield => $oldbiblionumtagfield,
438 oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
439 oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
440 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
441 oldbiblioitemnumber => $oldbiblioitemnumber );
442 } elsif ($op eq "delete") {
443 #------------------------------------------------------------------------------------------------------------------------------
444 &NEWdelbiblio($dbh,$bibid);
445 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=/cgi-bin/koha/search.marc/search.pl?type=intranet\"></html>";
447 #------------------------------------------------------------------------------------------------------------------------------
448 #------------------------------------------------------------------------------------------------------------------------------
450 #------------------------------------------------------------------------------------------------------------------------------
451 build_tabs ($template, $record, $dbh,$encoding);
454 oldbiblionumber => $oldbiblionumber,
456 oldbiblionumtagfield => $oldbiblionumtagfield,
457 oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
458 oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
459 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
460 oldbiblioitemnumber => $oldbiblioitemnumber );
463 frameworkcode => $frameworkcode,
464 itemtype => $frameworkcode # HINT: if the library has itemtype = framework, itemtype is auto filled !
466 output_html_with_http_headers $input, $cookie, $template->output;