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;
27 use C4::AuthoritiesMarc;
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 build_authorized_values_list
77 sub build_authorized_values_list ($$$$$) {
78 my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
80 my @authorised_values;
83 # builds list, depending on authorised value...
86 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
87 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
89 push @authorised_values, ""
90 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
92 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
93 push @authorised_values, $branchcode;
94 $authorised_lib{$branchcode}=$branchname;
98 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
99 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
101 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
103 while (my ($itemtype,$description) = $sth->fetchrow_array) {
104 push @authorised_values, $itemtype;
105 $authorised_lib{$itemtype}=$description;
107 # $value=$itemtype unless ($value);
109 #---- "true" authorised value
111 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
113 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
115 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
116 push @authorised_values, $value;
117 $authorised_lib{$value}=$lib;
120 return CGI::scrolling_list( -name => 'field_value',
121 -values => \@authorised_values,
123 -labels => \%authorised_lib,
129 builds the <input ...> entry for a subfield.
131 sub create_input () {
132 my ($tag,$subfield,$value,$i,$rec,$authorised_values_sth) = @_;
133 $value =~ s/"/"/g;
134 my $dbh = C4::Context->dbh;
136 $subfield_data{tag}=$tag;
137 $subfield_data{subfield}=$subfield;
138 $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
139 $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
140 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
141 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
142 # $subfield_data{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
143 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
144 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
145 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
146 $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>";
147 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
148 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
150 my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,0);
151 my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,0);
152 $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";
153 } elsif ($tag eq '') {
154 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value\" size=50 maxlength=255>"; #"
156 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>"; #"
158 return \%subfield_data;
161 sub build_tabs ($$$$) {
162 my($template, $record, $dbh,$encoding) = @_;
163 warn "=>".$record->as_formatted;
168 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
169 from authorised_values
170 where category=? order by lib");
173 foreach my $tag (sort(keys (%{$tagslib}))) {
175 # warn "TAG : $tag => ".$tagslib->{$tag}->{lib}."//";
176 # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
177 # if MARC::Record is empty => use tab as master loop.
178 if ($record ne -1 && $record->field($tag)) {
179 my @fields = $record->field($tag);
180 foreach my $field (@fields) {
183 my $value=$field->data();
185 next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
186 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$record,$authorised_values_sth));
189 my @subfields=$field->subfields();
190 foreach my $subfieldcount (0..$#subfields) {
191 my $subfield=$subfields[$subfieldcount][0];
192 my $value=$subfields[$subfieldcount][1];
193 next if (length $subfield !=1);
194 next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
195 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$record,$authorised_values_sth));
199 # now, loop again to add parameter subfield that are not in the MARC::Record
200 foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
201 next if (length $subfield !=1);
202 next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
204 next if (defined($record->field($tag)->subfield($subfield)));
205 push(@subfields_data, &create_input($tag,$subfield,'',$i,$record,$authorised_values_sth));
208 if ($#subfields_data >= 0) {
210 $tag_data{tag} = $tag;
211 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
212 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
213 $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
214 $tag_data{subfield_loop} = \@subfields_data;
215 push (@loop_data, \%tag_data);
217 # If there is more than 1 field, add an empty hidden field as separator.
221 push(@subfields_data, &create_input('','','',$i,$record,$authorised_values_sth));
223 $tag_data{tag_lib} = '';
224 $tag_data{indicator} = '';
225 $tag_data{subfield_loop} = \@subfields_data;
226 push (@loop_data, \%tag_data);
230 # if breeding is empty
233 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
234 next if (length $subfield !=1);
235 next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
236 push(@subfields_data, &create_input($tag,$subfield,'',$i,$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{indicator} = $indicator;
244 $tag_data{subfield_loop} = \@subfields_data;
245 push (@loop_data, \%tag_data);
249 $template->param("0XX" =>\@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 ($subfield eq 'repeatable');
268 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
270 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
271 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
272 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
273 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
274 push(@loop_data, \%subfield_data);
281 # ========================
283 #=========================
285 my $error = $input->param('error');
286 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
287 my $z3950 = $input->param('z3950');
288 my $op = $input->param('op');
290 my $authtypecode = $input->param('authtypecode');
291 my $dbh = C4::Context->dbh;
292 $authtypecode = &AUTHfind_authtypecode($dbh,$authid) if $authid;
293 # warn "authtypecode : $authtypecode && authid = $authid";
294 my ($template, $loggedinuser, $cookie)
295 = get_template_and_user({template_name => "authorities/authorities.tmpl",
298 authnotrequired => 0,
299 flagsrequired => {editcatalogue => 1},
303 $tagslib = AUTHgettagslib($dbh,1,$authtypecode);
306 $record = AUTHgetauthority($dbh,$authid) if ($authid);
309 # my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
310 # my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
313 # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
314 # ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber");
315 # ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber");
316 # # search biblioitems value
317 # my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
318 # $sth->execute($oldbiblionumber);
319 # ($oldbiblioitemnumber) = $sth->fetchrow;
321 #------------------------------------------------------------------------------------------------------------------------------
323 #------------------------------------------------------------------------------------------------------------------------------
325 my @tags = $input->param('tag');
326 my @subfields = $input->param('subfield');
327 my @values = $input->param('field_value');
328 # build indicator hash.
329 my @ind_tag = $input->param('ind_tag');
330 my @indicator = $input->param('indicator');
332 for (my $i=0;$i<=$#ind_tag;$i++) {
333 $indicators{$ind_tag[$i]} = $indicator[$i];
335 my $record = AUTHhtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
336 # MARC::Record built => now, record in DB
337 warn "IN ADD : ".$record->as_formatted();
339 AUTHmodauthority($dbh,$authid,$record,$authtypecode);
341 ($authid) = AUTHaddauthority($dbh,$record,$authid,$authtypecode);
343 print $input->redirect("authorities-home.pl");
344 #------------------------------------------------------------------------------------------------------------------------------
345 } elsif ($op eq "addfield") {
346 #------------------------------------------------------------------------------------------------------------------------------
347 my $addedfield = $input->param('addfield_field');
348 my @tags = $input->param('tag');
349 my @subfields = $input->param('subfield');
350 my @values = $input->param('field_value');
351 # build indicator hash.
352 my @ind_tag = $input->param('ind_tag');
353 my @indicator = $input->param('indicator');
354 splice(@tags,$addedfield,0,$tags[$addedfield]);
355 splice(@subfields,$addedfield,0,$subfields[$addedfield]);
356 splice(@values,$addedfield,0,$values[$addedfield]);
357 splice(@ind_tag,$addedfield,0,$ind_tag[$addedfield]);
359 for (my $i=0;$i<=$#ind_tag;$i++) {
360 $indicators{$ind_tag[$i]} = $indicator[$i];
362 # search the part of the array to duplicate.
366 for (my $i=0;$i<=$#tags;$i++) {
367 $start=$i if ($start eq 0 && $tags[$i] == $addedfield);
368 $end=$i if ($start>0 && $tags[$i] eq $addedfield);
369 last if ($start>0 && $tags[$i] ne $addedfield);
371 # add an empty line in all arrays. This forces a new field in MARC::Record.
372 splice(@tags,$end+1,0,'');
373 splice(@subfields,$end+1,0,'');
374 splice(@values,$end+1,0,'');
375 splice(@ind_tag,$end+1,0,'');
376 splice(@indicator,$end+1,0,'');
377 # then duplicate the field.
378 splice(@tags,$end+2,0,@tags[$start..$end]);
379 splice(@subfields,$end+2,0,@subfields[$start..$end]);
380 splice(@values,$end+2,0,@values[$start..$end]);
381 splice(@ind_tag,$end+2,0,@ind_tag[$start..$end]);
382 splice(@indicator,$end+2,0,@indicator[$start..$end]);
385 for (my $i=0;$i<=$#ind_tag;$i++) {
386 $indicators{$ind_tag[$i]} = $indicator[$i];
388 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
389 build_tabs ($template, $record, $dbh,$encoding);
393 # oldbiblionumtagfield => $oldbiblionumtagfield,
394 # oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
395 # oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
396 # oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
397 # oldbiblioitemnumber => $oldbiblioitemnumber );
398 } elsif ($op eq "delete") {
399 #------------------------------------------------------------------------------------------------------------------------------
400 &AUTHdelauthority($dbh,$authid);
403 build_tabs ($template, $record, $dbh,$encoding);
407 # oldbiblionumtagfield => $oldbiblionumtagfield,
408 # oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
409 # oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
410 # # oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
411 # oldbiblioitemnumber => $oldbiblioitemnumber,
412 authtypecode => $authtypecode,
415 my $authtypes = getauthtypes;
417 foreach my $thisauthtype (keys %$authtypes) {
418 my $selected = 1 if $thisauthtype eq $authtypecode;
419 my %row =(value => $thisauthtype,
420 selected => $selected,
421 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
423 push @authtypesloop, \%row;
426 $template->param(authtypesloop => \@authtypesloop);
427 output_html_with_http_headers $input, $cookie, $template->output;