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::AuthoritiesMarc;
28 use C4::Koha; # XXX subfield_is_koha_internal_p
30 use MARC::File::USMARC;
33 use vars qw( $tagslib);
34 use vars qw( $authorised_values_sth);
35 use vars qw( $is_a_modif );
37 my $itemtype; # created here because it can be used in build_authorized_values_list sub
41 ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
43 Find the given $subfield in the given $tag in the given
44 MARC::Record $record. If the subfield is found, returns
45 the (indicators, value) pair; otherwise, (undef, undef) is
51 my ($tagfield,$insubfield,$record,$encoding) = @_;
55 if ($record->field($tagfield)) {
56 push @result, $record->field($tagfield)->data();
61 foreach my $field ($record->field($tagfield)) {
62 my @subfields = $field->subfields();
63 foreach my $subfield (@subfields) {
64 if (@$subfield[0] eq $insubfield) {
65 push @result,@$subfield[1];
66 $indicator = $field->indicator(1).$field->indicator(2);
71 return($indicator,@result);
75 =item build_authorized_values_list
79 sub build_authorized_values_list ($$$$$) {
80 my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
82 my @authorised_values;
85 # builds list, depending on authorised value...
88 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
89 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
91 push @authorised_values, ""
92 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
94 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
95 push @authorised_values, $branchcode;
96 $authorised_lib{$branchcode}=$branchname;
100 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
101 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
103 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
105 while (my ($itemtype,$description) = $sth->fetchrow_array) {
106 push @authorised_values, $itemtype;
107 $authorised_lib{$itemtype}=$description;
109 $value=$itemtype unless ($value);
111 #---- "true" authorised value
113 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
115 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
117 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
118 push @authorised_values, $value;
119 $authorised_lib{$value}=$lib;
122 return CGI::scrolling_list( -name => 'field_value',
123 -values => \@authorised_values,
125 -labels => \%authorised_lib,
134 builds the <input ...> entry for a subfield.
136 sub create_input () {
137 my ($tag,$subfield,$value,$i,$tabloop,$rec,$authorised_values_sth) = @_;
138 # must be encoded as utf-8 before it reaches the editor
139 my $dbh=C4::Context->dbh;
140 $value =~ s/"/"/g;
142 $subfield_data{tag}=$tag;
143 $subfield_data{subfield}=$subfield;
144 $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
145 $subfield_data{marc_lib_plain}=$tagslib->{$tag}->{$subfield}->{lib};
146 $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
147 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
148 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
149 $subfield_data{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
150 $subfield_data{index} = $i;
151 $subfield_data{visibility} = "display:none" if (substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) gt "0") ; #check parity
152 # it's an authorised field
153 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
154 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
155 # it's a thesaurus / authority field
156 } elsif ($tagslib->{$tag}->{$subfield}->{frameworkcode}) {
157 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"67\" maxlength=\"255\" DISABLE READONLY> <a href=\"javascript:Dopop('../authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{frameworkcode}."&index=$i',$i)\">...</a>";
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/cataloguing/value_builder";
166 unless (opendir(DIR, "$cgidir")) {
167 $cgidir = C4::Context->intranetdir."/cataloguing/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 $z3950 = $input->param('z3950');
342 my $error = $input->param('error');
343 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
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 = &GetAuthTypeCode($authid) if !$authtypecode;
354 my ($template, $loggedinuser, $cookie)
355 = get_template_and_user({template_name => "authorities/authorities.tmpl",
358 authnotrequired => 0,
359 flagsrequired => {editauthorities => 1},
362 $template->param(nonav => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
363 $tagslib = GetTagsLabels(1,$authtypecode);
366 $record = GetAuthority($authid) if ($authid);
367 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
368 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
372 ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
373 ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("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 = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
388 # warn $record->as_formatted;
390 my $record=MARC::Record->new_from_xml($xml,'UTF-8',(C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")));
391 $record->encoding('UTF-8');
392 #warn $record->as_formatted;
393 # check for a duplicate
394 my ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
395 my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
396 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
397 if (!$duplicateauthid or $confirm_not_duplicate) {
398 # warn "noduplicate";
400 $authid=ModAuthority($authid,$record,$authtypecode,1);
402 ($authid) = AddAuthority($record,$authid,$authtypecode);
404 print $input->redirect("detail.pl?authid=$authid");
407 # it may be a duplicate, warn the user and do nothing
408 build_tabs ($template, $record, $dbh,$encoding);
410 $template->param(authid =>$authid,
411 duplicateauthid => $duplicateauthid,
412 duplicateauthvalue => $duplicateauthvalue,
415 #------------------------------------------------------------------------------------------------------------------------------
416 } elsif ($op eq "addfield") {
417 #------------------------------------------------------------------------------------------------------------------------------
418 my $addedfield = $input->param('addfield_field');
419 my $tagaddfield_subfield = $input->param('addfield_subfield');
420 my @tags = $input->param('tag');
421 my @subfields = $input->param('subfield');
422 my @values = $input->param('field_value');
423 # build indicator hash.
424 my @ind_tag = $input->param('ind_tag');
425 my @indicator = $input->param('indicator');
426 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
427 my $record=MARC::Record->new_from_xml($xml,'UTF-8');
428 $record->encoding('UTF-8');
429 # adding an empty field
430 my $field = MARC::Field->new("$addedfield",'','','$tagaddfield_subfield' => "");
431 $record->append_fields($field);
432 build_tabs ($template, $record, $dbh,$encoding);
437 } elsif ($op eq "delete") {
438 #------------------------------------------------------------------------------------------------------------------------------
439 &AUTHdelauthority($authid);
441 print $input->redirect("auth_finder.pl");
443 print $input->redirect("authorities-home.pl?authid=0");
447 if ($op eq "duplicate")
451 build_tabs ($template, $record, $dbh,$encoding);
453 $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
454 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
455 authid => $authid , authtypecode=>$authtypecode, );
460 authtypecode => $authtypecode,
464 my $authtypes = getauthtypes;
466 foreach my $thisauthtype (keys %$authtypes) {
467 my $selected = 1 if $thisauthtype eq $authtypecode;
468 my %row =(value => $thisauthtype,
469 selected => $selected,
470 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
472 push @authtypesloop, \%row;
475 $template->param(authtypesloop => \@authtypesloop,
476 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
477 hide_marc => C4::Context->preference('hide_marc'),
478 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
479 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
480 IntranetNav => C4::Context->preference("IntranetNav"),
482 output_html_with_http_headers $input, $cookie, $template->output;