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) = @_;
167 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
168 from authorised_values
169 where category=? order by lib");
172 foreach my $tag (sort(keys (%{$tagslib}))) {
174 # warn "TAG : $tag => ".$tagslib->{$tag}->{lib}."//";
175 # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
176 # if MARC::Record is empty => use tab as master loop.
177 if ($record ne -1 && $record->field($tag)) {
178 my @fields = $record->field($tag);
179 foreach my $field (@fields) {
182 my $value=$field->data();
184 next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
185 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$record,$authorised_values_sth));
188 my @subfields=$field->subfields();
189 foreach my $subfieldcount (0..$#subfields) {
190 my $subfield=$subfields[$subfieldcount][0];
191 my $value=$subfields[$subfieldcount][1];
192 next if (length $subfield !=1);
193 next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
194 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$record,$authorised_values_sth));
198 # now, loop again to add parameter subfield that are not in the MARC::Record
199 foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
200 next if (length $subfield !=1);
201 next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
203 next if (defined($record->field($tag)->subfield($subfield)));
204 push(@subfields_data, &create_input($tag,$subfield,'',$i,$record,$authorised_values_sth));
207 if ($#subfields_data >= 0) {
209 $tag_data{tag} = $tag;
210 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
211 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
212 $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
213 $tag_data{subfield_loop} = \@subfields_data;
214 push (@loop_data, \%tag_data);
216 # If there is more than 1 field, add an empty hidden field as separator.
220 push(@subfields_data, &create_input('','','',$i,$record,$authorised_values_sth));
222 $tag_data{tag_lib} = '';
223 $tag_data{indicator} = '';
224 $tag_data{subfield_loop} = \@subfields_data;
225 push (@loop_data, \%tag_data);
229 # if breeding is empty
232 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
233 next if (length $subfield !=1);
234 next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
235 push(@subfields_data, &create_input($tag,$subfield,'',$i,$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{repeatable} = $tagslib->{$tag}->{repeatable};
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 # check for a duplicate
338 my ($duplicateauthid,$duplicateauthvalue) = FindDuplicate($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
339 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
340 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
341 if (!$duplicateauthid or $confirm_not_duplicate) {
343 AUTHmodauthority($dbh,$authid,$record,$authtypecode);
345 ($authid) = AUTHaddauthority($dbh,$record,$authid,$authtypecode);
347 # now, redirect to detail page
348 print $input->redirect("detail.pl?authid=$authid");
351 # it may be a duplicate, warn the user and do nothing
352 build_tabs ($template, $record, $dbh,$encoding);
355 duplicateauthid => $duplicateauthid,
356 duplicateauthvalue => $duplicateauthvalue,
359 #------------------------------------------------------------------------------------------------------------------------------
360 } elsif ($op eq "addfield") {
361 #------------------------------------------------------------------------------------------------------------------------------
362 my $addedfield = $input->param('addfield_field');
363 my @tags = $input->param('tag');
364 my @subfields = $input->param('subfield');
365 my @values = $input->param('field_value');
366 # build indicator hash.
367 my @ind_tag = $input->param('ind_tag');
368 my @indicator = $input->param('indicator');
370 for (my $i=0;$i<=$#ind_tag;$i++) {
371 $indicators{$ind_tag[$i]} = $indicator[$i];
373 my $record = AUTHhtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
374 my $field = MARC::Field->new("$addedfield",'','','a'=> "");
375 $record->append_fields($field);
377 build_tabs ($template, $record, $dbh,$encoding);
381 # oldbiblionumtagfield => $oldbiblionumtagfield,
382 # oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
383 # oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
384 # oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
385 # oldbiblioitemnumber => $oldbiblioitemnumber );
386 } elsif ($op eq "delete") {
387 #------------------------------------------------------------------------------------------------------------------------------
388 &AUTHdelauthority($dbh,$authid);
390 build_tabs ($template, $record, $dbh,$encoding);
396 build_tabs ($template, $record, $dbh,$encoding);
401 # oldbiblionumtagfield => $oldbiblionumtagfield,
402 # oldbiblionumtagsubfield => $oldbiblionumtagsubfield,
403 # oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield,
404 # # oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
405 # oldbiblioitemnumber => $oldbiblioitemnumber,
406 authtypecode => $authtypecode,
409 my $authtypes = getauthtypes;
411 foreach my $thisauthtype (keys %$authtypes) {
412 my $selected = 1 if $thisauthtype eq $authtypecode;
413 my %row =(value => $thisauthtype,
414 selected => $selected,
415 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
417 push @authtypesloop, \%row;
420 $template->param(authtypesloop => \@authtypesloop,
421 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
423 output_html_with_http_headers $input, $cookie, $template->output;