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;
34 use vars qw( $tagslib);
35 use vars qw( $authorised_values_sth);
36 use vars qw( $is_a_modif );
38 my $z3950 = $input->param('z3950');
39 my $logstatus=C4::Context->preference('Activate_log');
40 my $itemtype; # created here because it can be used in build_authorized_values_list sub
44 ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
46 Find the given $subfield in the given $tag in the given
47 MARC::Record $record. If the subfield is found, returns
48 the (indicators, value) pair; otherwise, (undef, undef) is
54 my ($tagfield,$insubfield,$record,$encoding) = @_;
58 if ($record->field($tagfield)) {
59 push @result, $record->field($tagfield)->data();
64 foreach my $field ($record->field($tagfield)) {
65 my @subfields = $field->subfields();
66 foreach my $subfield (@subfields) {
67 if (@$subfield[0] eq $insubfield) {
68 push @result,@$subfield[1];
69 $indicator = $field->indicator(1).$field->indicator(2);
74 return($indicator,@result);
78 =item build_authorized_values_list
82 sub build_authorized_values_list ($$$$$) {
83 my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
85 my @authorised_values;
88 # builds list, depending on authorised value...
91 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
92 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
94 push @authorised_values, ""
95 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
97 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
98 push @authorised_values, $branchcode;
99 $authorised_lib{$branchcode}=$branchname;
103 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
104 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
106 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
108 while (my ($itemtype,$description) = $sth->fetchrow_array) {
109 push @authorised_values, $itemtype;
110 $authorised_lib{$itemtype}=$description;
112 $value=$itemtype unless ($value);
114 #---- "true" authorised value
116 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
118 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
120 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
121 push @authorised_values, $value;
122 $authorised_lib{$value}=$lib;
125 return CGI::scrolling_list( -name => 'field_value',
126 -values => \@authorised_values,
128 -labels => \%authorised_lib,
136 builds the <input ...> entry for a subfield.
138 sub create_input () {
139 my ($tag,$subfield,$value,$i,$tabloop,$rec,$authorised_values_sth) = @_;
140 # must be encoded as utf-8 before it reaches the editor
141 my $dbh=C4::Context->dbh;
142 $value =~ s/"/"/g;
144 $subfield_data{tag}=$tag;
145 $subfield_data{subfield}=$subfield;
146 $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
147 $subfield_data{marc_lib_plain}=$tagslib->{$tag}->{$subfield}->{lib};
148 $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
149 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
150 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
151 $subfield_data{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
152 $subfield_data{index} = $i;
153 $subfield_data{visibility} = "display:none" if (substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) gt "0") ; #check parity
154 # it's an authorised field
155 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
156 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
157 # it's a thesaurus / authority field
158 } elsif ($tagslib->{$tag}->{$subfield}->{link}) {
159 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffffff;'\" tabindex=\"1\" type=\"text\" name=\"field_value\" value=\"$value\" size=\"40\" maxlength=\"255\" DISABLE READONLY> <a style=\"cursor: help;\" href=\"javascript:Dopop('../authorities/auth_linker.pl?index=$i',$i)\">...</a>";
161 # it's a plugin field
162 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
163 # opening plugin. Just check wether we are on a developper computer on a production one
164 # (the cgidir differs)
165 my $cgidir = C4::Context->intranetdir ."/cgi-bin/value_builder";
166 unless (opendir(DIR, "$cgidir")) {
167 $cgidir = C4::Context->intranetdir."/value_builder";
169 my $plugin=$cgidir."/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
171 my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
172 my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
173 $subfield_data{marc_value}="<input tabindex=\"1\" type=\"text\" name=\"field_value\" value=\"$value\" size=\"40\" maxlength=\"255\" OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i); \"> <a style=\"cursor: help;\" href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
174 # it's an hidden field
175 } elsif ($tag eq '') {
176 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffffff'; \" tabindex=\"1\" type=\"hidden\" name=\"field_value\" value=\"$value\">";
177 } elsif (substr($tagslib->{$tag}->{$subfield}->{'hidden'},2,1) gt "1") {
179 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffffff'; \" tabindex=\"1\" type=\"text\" name=\"field_value\" value=\"$value\" size=\"40\" maxlength=\"255\" >";
180 # it's a standard field
182 if (length($value) >100) {
183 $subfield_data{marc_value}="<textarea tabindex=\"1\" name=\"field_value\" cols=\"40\" rows=\"5\" >$value</textarea>";
185 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffffff'; \" tabindex=\"1\" type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\">"; #"
188 return \%subfield_data;
191 sub build_tabs ($$$$) {
192 my($template, $record, $dbh,$encoding) = @_;
197 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
198 from authorised_values
199 where category=? order by lib");
201 # loop through each tab 0 through 9
202 for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
204 foreach my $tag (sort(keys (%{$tagslib}))) {
206 # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
207 # if MARC::Record is empty => use tab as master loop.
208 if ($record ne -1 && ($record->field($tag) || $tag eq '000')) {
211 @fields = $record->field($tag);
213 push @fields,$record->leader();
215 foreach my $field (@fields) {
218 my ($value,$subfield);
220 $value=$field->data();
226 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
227 # next if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'auth_header.authid');
228 push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
231 my @subfields=$field->subfields();
232 foreach my $subfieldcount (0..$#subfields) {
233 my $subfield=$subfields[$subfieldcount][0];
234 my $value=$subfields[$subfieldcount][1];
235 next if (length $subfield !=1);
236 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
237 push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
241 # now, loop again to add parameter subfield that are not in the MARC::Record
242 foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
243 next if (length $subfield !=1);
244 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
246 next if ((substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) gt "1") ); #check for visibility flag
247 next if (defined($field->subfield($subfield)));
248 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
251 if ($#subfields_data >= 0) {
253 $tag_data{tag} = $tag;
254 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
255 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
256 $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
257 $tag_data{subfield_loop} = \@subfields_data;
259 $tag_data{fixedfield} = 1;
262 push (@loop_data, \%tag_data);
264 # If there is more than 1 field, add an empty hidden field as separator.
265 if ($#fields >=1 && $#loop_data >=0 && $loop_data[$#loop_data]->{'tag'} eq $tag) {
268 push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
270 $tag_data{tag_lib} = '';
271 $tag_data{indicator} = '';
272 $tag_data{subfield_loop} = \@subfields_data;
274 $tag_data{fixedfield} = 1;
276 push (@loop_data, \%tag_data);
283 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
284 next if (length $subfield !=1);
285 next if ((substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) gt "1") ); #check for visibility flag
286 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
287 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
290 if ($#subfields_data >= 0) {
292 $tag_data{tag} = $tag;
293 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
294 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
295 $tag_data{indicator} = $indicator;
296 $tag_data{subfield_loop} = \@subfields_data;
297 $tag_data{tagfirstsubfield} = $tag_data{subfield_loop}[0];
299 $tag_data{fixedfield} = 1;
301 push (@loop_data, \%tag_data);
305 $template->param($tabloop."XX" =>\@loop_data);
310 sub build_hidden_data () {
311 # build hidden data =>
312 # we store everything, even if we show only requested subfields.
316 foreach my $tag (keys %{$tagslib}) {
317 my $previous_tag = '';
319 # loop through each subfield
320 foreach my $subfield (keys %{$tagslib->{$tag}}) {
321 next if ($subfield eq 'lib');
322 next if ($subfield eq 'tab');
323 next if ($subfield eq 'mandatory');
324 next if ($subfield eq 'repeatable');
325 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1");
327 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
328 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
329 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
330 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
331 push(@loop_data, \%subfield_data);
337 # ========================
339 #=========================
341 my $error = $input->param('error');
342 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
343 my $z3950 = $input->param('z3950');
344 my $op = $input->param('op');
345 my $nonav = $input->param('nonav');
346 my $myindex = $input->param('index');
347 my $linkid=$input->param('linkid');
348 my $authtypecode = $input->param('authtypecode');
350 my $dbh = C4::Context->dbh;
351 $authtypecode = &AUTHfind_authtypecode($dbh,$authid) if !$authtypecode;
354 my ($template, $loggedinuser, $cookie)
355 = get_template_and_user({template_name => "authorities/authorities.tmpl",
358 authnotrequired => 0,
359 flagsrequired => {editcatalogue => 1},
362 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
363 $tagslib = AUTHgettagslib($dbh,1,$authtypecode);
366 $record = AUTHgetauthority($dbh,$authid) if ($authid);
367 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
368 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
372 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &AUTHfind_marc_from_kohafield($dbh,"auth_header.authid",$authtypecode);
373 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &AUTHfind_marc_from_kohafield($dbh,"auth_header.authtypecode",$authtypecode);
376 #------------------------------------------------------------------------------------------------------------------------------
378 #------------------------------------------------------------------------------------------------------------------------------
381 my @tags = $input->param('tag');
382 my @subfields = $input->param('subfield');
383 my @values = $input->param('field_value');
384 # build indicator hash.
385 my @ind_tag = $input->param('ind_tag');
386 my @indicator = $input->param('indicator');
387 my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
389 my $record=MARC::Record->new_from_xml($xml,'UTF-8');
390 $record->encoding('UTF-8');
391 #warn $record->as_formatted;
393 # check for a duplicate
394 my ($duplicateauthid,$duplicateauthvalue) = C4::AuthoritiesMarc::FindDuplicate($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
395 #warn "duplicate:$duplicateauthid,$duplicateauthvalue";
396 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
397 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
398 if (!$duplicateauthid or $confirm_not_duplicate) {
399 # warn "noduplicate";
401 $authid=AUTHmodauthority($dbh,$authid,$record,$authtypecode,1);
403 ($authid) = AUTHaddauthority($dbh,$record,$authid,$authtypecode);
406 # now, redirect to detail page
408 #warn ($myindex,$nonav);
409 print $input->redirect("auth_finder.pl?index=$myindex&nonav=$nonav&authtypecode=$authtypecode");
411 print $input->redirect("detail.pl?nonav=$nonav&authid=$authid");
416 # it may be a duplicate, warn the user and do nothing
417 build_tabs ($template, $record, $dbh,$encoding);
419 $template->param(authid =>$authid,
420 duplicateauthid => $duplicateauthid,
421 duplicateauthvalue => $duplicateauthvalue,
424 #------------------------------------------------------------------------------------------------------------------------------
425 } elsif ($op eq "addfield") {
426 #------------------------------------------------------------------------------------------------------------------------------
427 my $addedfield = $input->param('addfield_field');
428 my $tagaddfield_subfield = $input->param('addfield_subfield');
429 my @tags = $input->param('tag');
430 my @subfields = $input->param('subfield');
431 my @values = $input->param('field_value');
432 # build indicator hash.
433 my @ind_tag = $input->param('ind_tag');
434 my @indicator = $input->param('indicator');
435 my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
436 my $record=MARC::Record->new_from_xml($xml,'UTF-8');
437 $record->encoding('UTF-8');
438 # adding an empty field
439 my $field = MARC::Field->new("$addedfield",'','','$tagaddfield_subfield' => "");
440 $record->append_fields($field);
441 build_tabs ($template, $record, $dbh,$encoding);
446 } elsif ($op eq "delete") {
447 #------------------------------------------------------------------------------------------------------------------------------
448 &AUTHdelauthority($dbh,$authid);
450 print $input->redirect("auth_finder.pl");
452 print $input->redirect("authorities-home.pl?authid=0");
456 if ($op eq "duplicate")
460 build_tabs ($template, $record, $dbh,$encoding);
462 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
463 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
464 authid => $authid , authtypecode=>$authtypecode, );
469 # build_tabs ($template, $record, $dbh,$encoding);
474 authtypecode => $authtypecode,
478 my $authtypes = getauthtypes;
480 foreach my $thisauthtype (keys %$authtypes) {
481 my $selected = 1 if $thisauthtype eq $authtypecode;
482 my %row =(value => $thisauthtype,
483 selected => $selected,
484 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
486 push @authtypesloop, \%row;
489 $template->param(authtypesloop => \@authtypesloop,
490 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
492 output_html_with_http_headers $input, $cookie, $template->output;