Improve SQL formatting (following coding guidelines)
[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::Interface::CGI::Output;
27 use C4::AuthoritiesMarc;
28 use C4::Context;
29 use C4::Koha; # XXX subfield_is_koha_internal_p
30 use HTML::Template;
31 use MARC::File::USMARC;
32 use MARC::File::XML;
33 use C4::Biblio;
34 use vars qw( $tagslib);
35 use vars qw( $authorised_values_sth);
36 use vars qw( $is_a_modif );
37 my $input = new CGI;
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
41
42 =item find_value
43
44     ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
45
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
49 returned.
50
51 =cut
52
53 sub find_value {
54         my ($tagfield,$insubfield,$record,$encoding) = @_;
55         my @result;
56         my $indicator;
57         if ($tagfield <10) {
58                 if ($record->field($tagfield)) {
59                         push @result, $record->field($tagfield)->data();
60                 } else {
61                         push @result,"";
62                 }
63         } else {
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);
70                                 }
71                         }
72                 }
73         }
74         return($indicator,@result);
75 }
76
77
78 =item build_authorized_values_list
79
80 =cut
81
82 sub build_authorized_values_list ($$$$$) {
83         my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
84
85         my @authorised_values;
86         my %authorised_lib;
87
88         # builds list, depending on authorised value...
89
90         #---- branch
91         if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
92         my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
93         $sth->execute;
94         push @authorised_values, ""
95                 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
96
97         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
98                 push @authorised_values, $branchcode;
99                 $authorised_lib{$branchcode}=$branchname;
100         }
101
102         #----- itemtypes
103         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
104                 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
105                 $sth->execute;
106                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
107         
108                 while (my ($itemtype,$description) = $sth->fetchrow_array) {
109                         push @authorised_values, $itemtype;
110                         $authorised_lib{$itemtype}=$description;
111                 }
112                 $value=$itemtype unless ($value);
113
114         #---- "true" authorised value
115         } else {
116                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
117
118                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
119         
120                 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
121                         push @authorised_values, $value;
122                         $authorised_lib{$value}=$lib;
123                 }
124     }
125     return CGI::scrolling_list( -name     => 'field_value',
126                                 -values   => \@authorised_values,
127                                 -default  => $value,
128                                 -labels   => \%authorised_lib,
129                                 -override => 1,
130                                 -size     => 1,
131                                 -multiple => 0 );
132 }
133
134
135 =item create_input
136  builds the <input ...> entry for a subfield.
137 =cut
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/"/&quot;/g;
143         my %subfield_data;
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>";
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/value_builder";
166                 unless (opendir(DIR, "$cgidir")) {
167                         $cgidir = C4::Context->intranetdir."/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 $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');
349
350 my $dbh = C4::Context->dbh;
351 $authtypecode = &AUTHfind_authtypecode($dbh,$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 => {editcatalogue => 1},
360                              debug => 1,
361                              });
362 $template->param(nonav   => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
363 $tagslib = AUTHgettagslib($dbh,1,$authtypecode);
364 my $record=-1;
365 my $encoding="";
366 $record = AUTHgetauthority($dbh,$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) = &AUTHfind_marc_from_kohafield($dbh,"auth_header.authid",$authtypecode);
373         ($oldauthtypetagfield,$oldauthtypetagsubfield) = &AUTHfind_marc_from_kohafield($dbh,"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 = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
388         #warn $xml;
389         my $record=MARC::Record->new_from_xml($xml,'UTF-8');
390         $record->encoding('UTF-8');
391         #warn $record->as_formatted;
392         #warn "IN ADDBIB";
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";
400                 if ($is_a_modif ) {     
401                         $authid=AUTHmodauthority($dbh,$authid,$record,$authtypecode,1);         
402                 } else {
403                 ($authid) = AUTHaddauthority($dbh,$record,$authid,$authtypecode);
404
405                 }
406         # now, redirect to detail page
407                 if ($nonav){
408 #warn ($myindex,$nonav);
409                 print $input->redirect("auth_finder.pl?index=$myindex&nonav=$nonav&authtypecode=$authtypecode");
410                 }else{
411                 print $input->redirect("detail.pl?nonav=$nonav&authid=$authid");
412                 }
413                 exit;
414         } else {
415 #warn "duplicate";
416         # it may be a duplicate, warn the user and do nothing
417                 build_tabs ($template, $record, $dbh,$encoding);
418                 build_hidden_data;
419                 $template->param(authid =>$authid,
420                         duplicateauthid                         => $duplicateauthid,
421                         duplicateauthvalue                              => $duplicateauthvalue,
422                          );
423         }
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);
442         build_hidden_data;
443         $template->param(
444                 authid                       => $authid,);
445
446 } elsif ($op eq "delete") {
447 #------------------------------------------------------------------------------------------------------------------------------
448         &AUTHdelauthority($dbh,$authid);
449         if ($nonav){
450         print $input->redirect("auth_finder.pl");
451         }else{
452         print $input->redirect("authorities-home.pl?authid=0");
453         }
454                 exit;
455 } else {
456 if ($op eq "duplicate")
457         {
458                 $authid = "";
459         }
460         build_tabs ($template, $record, $dbh,$encoding);
461         build_hidden_data;
462         $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
463                 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
464                 authid                      => $authid , authtypecode=>$authtypecode,   );
465 }
466
467 #unless ($op) {
468 #       warn "BUILDING";
469 #       build_tabs ($template, $record, $dbh,$encoding);
470 #       build_hidden_data;
471 #}
472 $template->param(
473         authid                       => $authid,
474         authtypecode => $authtypecode,
475         linkid=>$linkid,
476         );
477
478 my $authtypes = getauthtypes;
479 my @authtypesloop;
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'},
485                         );
486         push @authtypesloop, \%row;
487 }
488
489 $template->param(authtypesloop => \@authtypesloop,
490                                 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
491                                 nonav=>$nonav,);
492 output_html_with_http_headers $input, $cookie, $template->output;