missing comma :-/
[koha.git] / authorities / authorities.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
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
12 # version.
13 #
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.
17 #
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
21
22 use strict;
23 use CGI;
24 use C4::Auth;
25 use C4::Output;
26 use C4::AuthoritiesMarc;
27 use C4::Context;
28 use C4::Koha; # XXX subfield_is_koha_internal_p
29
30 use MARC::File::USMARC;
31 use MARC::File::XML;
32 use C4::Biblio;
33 use vars qw( $tagslib);
34 use vars qw( $authorised_values_sth);
35 use vars qw( $is_a_modif );
36
37 my $itemtype; # created here because it can be used in build_authorized_values_list sub
38
39 =item find_value
40
41     ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
42
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
46 returned.
47
48 =cut
49
50 sub find_value {
51         my ($tagfield,$insubfield,$record,$encoding) = @_;
52         my @result;
53         my $indicator;
54         if ($tagfield <10) {
55                 if ($record->field($tagfield)) {
56                         push @result, $record->field($tagfield)->data();
57                 } else {
58                         push @result,"";
59                 }
60         } else {
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);
67                                 }
68                         }
69                 }
70         }
71         return($indicator,@result);
72 }
73
74
75 =item build_authorized_values_list
76
77 =cut
78
79 sub build_authorized_values_list ($$$$$) {
80         my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
81
82         my @authorised_values;
83         my %authorised_lib;
84
85         # builds list, depending on authorised value...
86
87         #---- branch
88         if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
89         my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
90         $sth->execute;
91         push @authorised_values, ""
92                 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
93
94         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
95                 push @authorised_values, $branchcode;
96                 $authorised_lib{$branchcode}=$branchname;
97         }
98
99         #----- itemtypes
100         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
101                 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
102                 $sth->execute;
103                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
104         
105                 while (my ($itemtype,$description) = $sth->fetchrow_array) {
106                         push @authorised_values, $itemtype;
107                         $authorised_lib{$itemtype}=$description;
108                 }
109                 $value=$itemtype unless ($value);
110
111         #---- "true" authorised value
112         } else {
113                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
114
115                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
116         
117                 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
118                         push @authorised_values, $value;
119                         $authorised_lib{$value}=$lib;
120                 }
121     }
122     return CGI::scrolling_list( -name     => 'field_value',
123                                 -values   => \@authorised_values,
124                                 -default  => $value,
125                                 -labels   => \%authorised_lib,
126                                 -override => 1,
127                                 -size     => 1,
128                                 -tabindex=>'',
129                                 -multiple => 0 );
130 }
131
132
133 =item create_input
134  builds the <input ...> entry for a subfield.
135 =cut
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/"/&quot;/g;
141         my %subfield_data;
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>";
160         
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";
168                 } 
169                 my $plugin=$cgidir."/".$tagslib->{$tag}->{$subfield}->{'value_builder'}; 
170                 require $plugin;
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") {
178
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
181         } else {
182                 if (length($value) >100) {
183                         $subfield_data{marc_value}="<textarea tabindex=\"1\" name=\"field_value\" cols=\"40\" rows=\"5\" >$value</textarea>";
184                 } else {
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\">"; #"
186                 }
187         }
188         return \%subfield_data;
189 }
190
191 sub build_tabs ($$$$) {
192     my($template, $record, $dbh,$encoding) = @_;
193     # fill arrays
194     my @loop_data =();
195     my $tag;
196     my $i=0;
197         my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
198                 from authorised_values
199                 where category=? order by lib");
200
201 # loop through each tab 0 through 9
202         for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
203                 my @loop_data = ();
204                 foreach my $tag (sort(keys (%{$tagslib}))) {
205                         my $indicator;
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')) {
209                                 my @fields;
210                                 if ($tag ne '000') {
211                                         @fields = $record->field($tag);
212                                 } else {
213                                         push @fields,$record->leader();
214                                 }
215                                 foreach my $field (@fields)  {
216                                         my @subfields_data;
217                                         if ($tag<10) {
218                                                 my ($value,$subfield);
219                                                 if ($tag ne '000') {
220                                                         $value=$field->data();
221                                                         $subfield="@";
222                                                 } else {
223                                                         $value = $field;
224                                                         $subfield='@';
225                                                 }
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));
229                                                 $i++;
230                                         } else {
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));
238                                                         $i++;
239                                                 }
240                                         }
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);
245                                                 next if ($tag<10);
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));
249                                                 $i++;
250                                         }
251                                         if ($#subfields_data >= 0) {
252                                                 my %tag_data;
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;
258                                                 if ($tag<10) {
259                                                         $tag_data{fixedfield} = 1;
260                                                 }
261
262                                                 push (@loop_data, \%tag_data);
263                                         }
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) {
266                                                 my @subfields_data;
267                                                 my %tag_data;
268                                                 push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
269                                                 $tag_data{tag} = '';
270                                                 $tag_data{tag_lib} = '';
271                                                 $tag_data{indicator} = '';
272                                                 $tag_data{subfield_loop} = \@subfields_data;
273                                                 if ($tag<10) {
274                                                         $tag_data{fixedfield} = 1;
275                                                 }
276                                                 push (@loop_data, \%tag_data);
277                                                 $i++;
278                                         }
279                                 }
280         
281                         } else {
282                                 my @subfields_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));
288                                         $i++;
289                                 }
290                                 if ($#subfields_data >= 0) {
291                                         my %tag_data;
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];
298                                         if ($tag<10) {
299                                                 $tag_data{fixedfield} = 1;
300                                         }
301                                         push (@loop_data, \%tag_data);
302                                 }
303                         }
304                 }
305                 $template->param($tabloop."XX" =>\@loop_data);
306         }
307 }
308
309
310 sub build_hidden_data () {
311     # build hidden data =>
312     # we store everything, even if we show only requested subfields.
313
314     my @loop_data =();
315     my $i=0;
316     foreach my $tag (keys %{$tagslib}) {
317         my $previous_tag = '';
318
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");
326             my %subfield_data;
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);
332             $i++
333         }
334     }
335 }
336
337 # ======================== 
338 #          MAIN 
339 #=========================
340 my $input = new CGI;
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');
349
350 my $dbh = C4::Context->dbh;
351 $authtypecode = &GetAuthTypeCode($authid) if !$authtypecode;
352
353
354 my ($template, $loggedinuser, $cookie)
355     = get_template_and_user({template_name => "authorities/authorities.tmpl",
356                              query => $input,
357                              type => "intranet",
358                              authnotrequired => 0,
359                              flagsrequired => {editauthorities => 1},
360                              debug => 1,
361                              });
362 $template->param(nonav   => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
363 $tagslib = GetTagsLabels(1,$authtypecode);
364 my $record=-1;
365 my $encoding="";
366 $record = GetAuthority($authid) if ($authid);
367 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
368 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
369 $is_a_modif=0;
370 if ($authid) {
371   $is_a_modif=1;
372   ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
373   ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
374 }
375
376 #------------------------------------------------------------------------------------------------------------------------------
377 if ($op eq "add") {
378 #------------------------------------------------------------------------------------------------------------------------------
379
380         # rebuild
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;
389 #       warn $xml;
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";
399           if ($is_a_modif ) {   
400             $authid=ModAuthority($authid,$record,$authtypecode,1);              
401           } else {
402             ($authid) = AddAuthority($record,$authid,$authtypecode);
403           }
404           print $input->redirect("detail.pl?authid=$authid");
405           exit;
406         } else {
407         # it may be a duplicate, warn the user and do nothing
408             build_tabs ($template, $record, $dbh,$encoding);
409             build_hidden_data;
410             $template->param(authid =>$authid,
411                             duplicateauthid     => $duplicateauthid,
412                             duplicateauthvalue  => $duplicateauthvalue,
413                             );
414         }
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);
433         build_hidden_data;
434         $template->param(
435                 authid => $authid,);
436
437 } elsif ($op eq "delete") {
438 #------------------------------------------------------------------------------------------------------------------------------
439         &AUTHdelauthority($authid);
440         if ($nonav){
441         print $input->redirect("auth_finder.pl");
442         }else{
443         print $input->redirect("authorities-home.pl?authid=0");
444         }
445                 exit;
446 } else {
447 if ($op eq "duplicate")
448         {
449                 $authid = "";
450         }
451         build_tabs ($template, $record, $dbh,$encoding);
452         build_hidden_data;
453         $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
454                 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
455                 authid                      => $authid , authtypecode=>$authtypecode,   );
456 }
457
458 $template->param(
459         authid                       => $authid,
460         authtypecode => $authtypecode,
461         linkid=>$linkid,
462         );
463
464 my $authtypes = getauthtypes;
465 my @authtypesloop;
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'},
471                         );
472         push @authtypesloop, \%row;
473 }
474
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"),
481                                 );
482 output_html_with_http_headers $input, $cookie, $template->output;