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( $is_a_modif );
39 ($indicators, $value) = find_value($tag, $subfield, $record);
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) = @_;
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,@$subfield[1];
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).
83 sub MARCfindbreeding {
85 my $sth = $dbh->prepare("select file,marc from marc_breeding where id=?");
87 my ($file,$marc) = $sth->fetchrow;
89 my $record = MARC::File::USMARC::decode($marc);
90 if (ref($record) eq undef) {
100 =item build_authorized_values_list
104 sub build_authorized_values_list ($$$$$) {
105 my($tag, $subfield, $value, $dbh, $authorised_values_sth) = @_;
107 my @authorised_values;
110 # builds list, depending on authorised value...
113 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
114 my $sth=$dbh->prepare("select branchcode,branchname from branches");
116 push @authorised_values, ""
117 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
119 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
120 push @authorised_values, $branchcode;
121 $authorised_lib{$branchcode}=$branchname;
125 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
126 my $sth=$dbh->prepare("select itemtype,description from itemtypes");
128 push @authorised_values, ""
129 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
131 while (my ($itemtype,$description) = $sth->fetchrow_array) {
132 push @authorised_values, $itemtype;
133 $authorised_lib{$itemtype}=$description;
136 #---- "true" authorised value
138 $authorised_values_sth->execute
139 ($tagslib->{$tag}->{$subfield}->{authorised_value});
141 push @authorised_values, ""
142 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
144 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
145 push @authorised_values, $value;
146 $authorised_lib{$value}=$lib;
149 return CGI::scrolling_list( -name => 'field_value',
150 -values => \@authorised_values,
152 -labels => \%authorised_lib,
157 sub build_tabs ($$$) {
158 my($template, $record, $dbh) = @_;
164 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
165 from authorised_values
166 where category=? order by authorised_value");
168 # loop through each tab 0 through 9
169 for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
170 # my @fields = $record->fields();
172 foreach my $tag (sort(keys (%{$tagslib}))) {
173 my $previous_tag = '';
177 # loop through each subfield
178 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
179 next if subfield_is_koha_internal_p($subfield);
180 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
181 # if breeding is not empty
183 my ($x,@value) = find_value($tag,$subfield,$record);
184 foreach my $value (@value) {
186 $subfield_data{tag}=$tag;
187 $subfield_data{subfield}=$subfield;
188 $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
189 $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
190 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
191 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
192 $value=char_decode($value) unless ($is_a_modif);
193 $indicator = $x if $x; #XXX
194 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
195 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh, $authorised_values_sth);
196 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
197 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" size=47 maxlength=255> <a href=\"javascript:Dopop('../thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">...</a>"; #"
198 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
199 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
201 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
202 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
203 $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";
205 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
207 push(@subfields_data, \%subfield_data);
210 # if breeding is empty
213 ($x,$value) = find_value($tag,$subfield,$record) if ($record ne -1);
214 $value=char_decode($value) unless ($is_a_modif);
216 $subfield_data{tag}=$tag;
217 $subfield_data{subfield}=$subfield;
218 $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
219 $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
220 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
221 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
222 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
223 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh, $authorised_values_sth);
224 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
225 $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>";
226 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
227 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
229 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
230 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
231 $subfield_data{marc_value}="<input type=\"text\" name=\"field_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";
233 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" size=50 maxlength=255>";
235 push(@subfields_data, \%subfield_data);
239 if ($#subfields_data >= 0) {
241 $tag_data{tag} = $tag;
242 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
243 $tag_data{indicator} = $indicator;
244 $tag_data{subfield_loop} = \@subfields_data;
245 push (@loop_data, \%tag_data);
248 $template->param($tabloop."XX" =>\@loop_data);
253 sub build_hidden_data () {
254 # build hidden data =>
255 # we store everything, even if we show only requested subfields.
259 foreach my $tag (keys %{$tagslib}) {
260 my $previous_tag = '';
262 # loop through each subfield
263 foreach my $subfield (keys %{$tagslib->{$tag}}) {
264 next if ($subfield eq 'lib');
265 next if ($subfield eq 'tab');
266 next if ($subfield eq 'mandatory');
267 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
269 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
270 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
271 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
272 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
273 push(@loop_data, \%subfield_data);
281 my $error = $input->param('error');
282 my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio.
283 my $breedingid = $input->param('breedingid');
284 my $op = $input->param('op');
285 my $dbh = C4::Context->dbh;
287 if ($oldbiblionumber) {
288 $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
290 $bibid = $input->param('bibid');
292 my ($template, $loggedinuser, $cookie)
293 = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
296 authnotrequired => 0,
297 flagsrequired => {catalogue => 1},
301 $tagslib = &MARCgettagslib($dbh,1);
303 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
304 $record = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
306 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
307 my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
310 # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
311 ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber");
312 ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber");
313 # search biblioitems value
314 my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
315 $sth->execute($oldbiblionumber);
316 ($oldbiblioitemnumber) = $sth->fetchrow;
318 #------------------------------------------------------------------------------------------------------------------------------
319 if ($op eq "addbiblio") {
320 #------------------------------------------------------------------------------------------------------------------------------
322 my @tags = $input->param('tag');
323 my @subfields = $input->param('subfield');
324 my @values = $input->param('field_value');
325 # build indicator hash.
326 my @ind_tag = $input->param('ind_tag');
327 my @indicator = $input->param('indicator');
329 for (my $i=0;$i<=$#ind_tag;$i++) {
330 $indicators{$ind_tag[$i]} = $indicator[$i];
332 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
333 # MARC::Record built => now, record in DB
337 NEWmodbiblio($dbh,$record,$bibid);
339 ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record);
341 # now, redirect to additem page
342 print $input->redirect("additem.pl?bibid=$bibid");
344 #------------------------------------------------------------------------------------------------------------------------------
346 #------------------------------------------------------------------------------------------------------------------------------
347 build_tabs ($template, $record, $dbh);
350 oldbiblionumber => $oldbiblionumber,
352 oldbiblionumtagfield => $oldbiblionumtagfield,
353 oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
354 oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
355 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
356 oldbiblioitemnumber => $oldbiblioitemnumber );
358 output_html_with_http_headers $input, $cookie, $template->output;