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) = @_;
50 # warn "$tagfield / $insubfield // ";
53 foreach my $field ($record->field($tagfield)) {
54 my @subfields = $field->subfields();
55 foreach my $subfield (@subfields) {
56 if (@$subfield[0] eq $insubfield) {
57 $result .= @$subfield[1];
58 $indicator = $field->indicator(1).$field->indicator(2);
62 return($indicator,$result);
68 $record = MARCfindbreeding($dbh, $breedingid);
70 Look up the breeding farm with database handle $dbh, for the
71 record with id $breedingid. If found, returns the decoded
72 MARC::Record; otherwise, -1 is returned (FIXME).
76 sub MARCfindbreeding {
78 my $sth = $dbh->prepare("select file,marc from marc_breeding where id=?");
80 my ($file,$marc) = $sth->fetchrow;
82 my $record = MARC::File::USMARC::decode($marc);
83 if (ref($record) eq undef) {
93 =item build_authorized_values_list
97 sub build_authorized_values_list ($$$$$) {
98 my($tag, $subfield, $value, $dbh, $authorised_values_sth) = @_;
100 my @authorised_values;
103 # builds list, depending on authorised value...
106 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
107 my $sth=$dbh->prepare("select branchcode,branchname from branches");
109 push @authorised_values, ""
110 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
112 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
113 push @authorised_values, $branchcode;
114 $authorised_lib{$branchcode}=$branchname;
118 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
119 my $sth=$dbh->prepare("select itemtype,description from itemtypes");
121 push @authorised_values, ""
122 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
124 while (my ($itemtype,$description) = $sth->fetchrow_array) {
125 push @authorised_values, $itemtype;
126 $authorised_lib{$itemtype}=$description;
129 #---- "true" authorised value
131 $authorised_values_sth->execute
132 ($tagslib->{$tag}->{$subfield}->{authorised_value});
134 push @authorised_values, ""
135 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
137 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
138 push @authorised_values, $value;
139 $authorised_lib{$value}=$lib;
142 return CGI::scrolling_list( -name => 'field_value',
143 -values => \@authorised_values,
145 -labels => \%authorised_lib,
150 sub build_tabs ($$$) {
151 my($template, $record, $dbh) = @_;
157 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
158 from authorised_values
159 where category=? order by authorised_value");
161 # loop through each tab 0 through 9
162 for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
163 # my @fields = $record->fields();
165 foreach my $tag (sort(keys (%{$tagslib}))) {
166 my $previous_tag = '';
170 # loop through each subfield
171 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
172 next if subfield_is_koha_internal_p($subfield);
173 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
175 $subfield_data{tag}=$tag;
176 $subfield_data{subfield}=$subfield;
177 $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
178 $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
179 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
180 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
181 # if breeding is not empty
183 my ($x,$value) = find_value($tag,$subfield,$record);
184 $value=char_decode($value) unless ($is_a_modif);
185 $indicator = $x if $x; #XXX
186 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
187 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh, $authorised_values_sth);
188 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
189 $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>"; #"
190 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
191 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
193 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
194 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
195 $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";
197 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
200 # if breeding is empty
203 ($x,$value) = find_value($tag,$subfield,$record) if ($record ne -1);
204 $value=char_decode($value) unless ($is_a_modif);
205 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
206 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh, $authorised_values_sth);
207 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
208 $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>";
209 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
210 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
212 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
213 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
214 $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";
216 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" size=50 maxlength=255>";
219 push(@subfields_data, \%subfield_data);
222 if ($#subfields_data >= 0) {
224 $tag_data{tag} = $tag;
225 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
226 $tag_data{indicator} = $indicator;
227 $tag_data{subfield_loop} = \@subfields_data;
228 push (@loop_data, \%tag_data);
231 $template->param($tabloop."XX" =>\@loop_data);
236 sub build_hidden_data () {
237 # build hidden data =>
238 # we store everything, even if we show only requested subfields.
242 foreach my $tag (keys %{$tagslib}) {
243 my $previous_tag = '';
245 # loop through each subfield
246 foreach my $subfield (keys %{$tagslib->{$tag}}) {
247 next if ($subfield eq 'lib');
248 next if ($subfield eq 'tab');
249 next if ($subfield eq 'mandatory');
250 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
252 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
253 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
254 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
255 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
256 push(@loop_data, \%subfield_data);
264 my $error = $input->param('error');
265 my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio.
266 my $breedingid = $input->param('breedingid');
267 my $op = $input->param('op');
268 my $dbh = C4::Context->dbh;
270 if ($oldbiblionumber) {
271 $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
273 $bibid = $input->param('bibid');
275 my ($template, $loggedinuser, $cookie)
276 = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
279 authnotrequired => 0,
280 flagsrequired => {catalogue => 1},
284 $tagslib = &MARCgettagslib($dbh,1);
286 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
287 #warn "1= ".$record->as_formatted;
288 $record = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
290 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
291 my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
294 # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
295 ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber");
296 ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber");
297 # search biblioitems value
298 my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
299 $sth->execute($oldbiblionumber);
300 ($oldbiblioitemnumber) = $sth->fetchrow;
302 #------------------------------------------------------------------------------------------------------------------------------
303 if ($op eq "addbiblio") {
304 #------------------------------------------------------------------------------------------------------------------------------
306 my @tags = $input->param('tag');
307 my @subfields = $input->param('subfield');
308 my @values = $input->param('field_value');
309 # build indicator hash.
310 my @ind_tag = $input->param('ind_tag');
311 my @indicator = $input->param('indicator');
313 for (my $i=0;$i<=$#ind_tag;$i++) {
314 $indicators{$ind_tag[$i]} = $indicator[$i];
316 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
317 # MARC::Record built => now, record in DB
321 NEWmodbiblio($dbh,$record,$bibid);
323 ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record);
325 # now, redirect to additem page
326 print $input->redirect("additem.pl?bibid=$bibid");
328 #------------------------------------------------------------------------------------------------------------------------------
330 #------------------------------------------------------------------------------------------------------------------------------
331 build_tabs ($template, $record, $dbh);
334 oldbiblionumber => $oldbiblionumber,
336 oldbiblionumtagfield => $oldbiblionumtagfield,
337 oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
338 oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
339 oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
340 oldbiblioitemnumber => $oldbiblioitemnumber );
342 output_html_with_http_headers $input, $cookie, $template->output;