added ability for the popup to close and the opening page to refresh once subscriptio...
[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                                 -tabindex=>'',
132                                 -multiple => 0 );
133 }
134
135
136 =item create_input
137  builds the <input ...> entry for a subfield.
138 =cut
139 sub create_input () {
140         my ($tag,$subfield,$value,$i,$tabloop,$rec,$authorised_values_sth) = @_;
141         # must be encoded as utf-8 before it reaches the editor
142        my $dbh=C4::Context->dbh;
143         $value =~ s/"/&quot;/g;
144         my %subfield_data;
145         $subfield_data{tag}=$tag;
146         $subfield_data{subfield}=$subfield;
147         $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
148         $subfield_data{marc_lib_plain}=$tagslib->{$tag}->{$subfield}->{lib};
149         $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
150         $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
151         $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
152         $subfield_data{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
153         $subfield_data{index} = $i;
154         $subfield_data{visibility} = "display:none" if (substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) gt "0") ; #check parity
155         # it's an authorised field
156         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
157                 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
158         # it's a thesaurus / authority field
159         } elsif ($tagslib->{$tag}->{$subfield}->{link}) {
160                 $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         
162                 # it's a plugin field
163         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
164                 # opening plugin. Just check wether we are on a developper computer on a production one
165                 # (the cgidir differs)
166                 my $cgidir = C4::Context->intranetdir ."/cgi-bin/value_builder";
167                 unless (opendir(DIR, "$cgidir")) {
168                         $cgidir = C4::Context->intranetdir."/value_builder";
169                 } 
170                 my $plugin=$cgidir."/".$tagslib->{$tag}->{$subfield}->{'value_builder'}; 
171                 require $plugin;
172                 my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
173                 my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
174                 $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";
175         # it's an hidden field
176         } elsif  ($tag eq '') {
177                 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffffff'; \" tabindex=\"1\" type=\"hidden\" name=\"field_value\" value=\"$value\">";
178         } elsif  (substr($tagslib->{$tag}->{$subfield}->{'hidden'},2,1) gt "1") {
179
180                 $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\" >";
181         # it's a standard field
182         } else {
183                 if (length($value) >100) {
184                         $subfield_data{marc_value}="<textarea tabindex=\"1\" name=\"field_value\" cols=\"40\" rows=\"5\" >$value</textarea>";
185                 } else {
186                         $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\">"; #"
187                 }
188         }
189         return \%subfield_data;
190 }
191
192 sub build_tabs ($$$$) {
193     my($template, $record, $dbh,$encoding) = @_;
194     # fill arrays
195     my @loop_data =();
196     my $tag;
197     my $i=0;
198         my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
199                 from authorised_values
200                 where category=? order by lib");
201
202 # loop through each tab 0 through 9
203         for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
204                 my @loop_data = ();
205                 foreach my $tag (sort(keys (%{$tagslib}))) {
206                         my $indicator;
207         # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
208         # if MARC::Record is empty => use tab as master loop.
209                         if ($record ne -1 && ($record->field($tag) || $tag eq '000')) {
210                                 my @fields;
211                                 if ($tag ne '000') {
212                                         @fields = $record->field($tag);
213                                 } else {
214                                         push @fields,$record->leader();
215                                 }
216                                 foreach my $field (@fields)  {
217                                         my @subfields_data;
218                                         if ($tag<10) {
219                                                 my ($value,$subfield);
220                                                 if ($tag ne '000') {
221                                                         $value=$field->data();
222                                                         $subfield="@";
223                                                 } else {
224                                                         $value = $field;
225                                                         $subfield='@';
226                                                 }
227                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
228                                         #       next if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'auth_header.authid');
229                                                 push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
230                                                 $i++;
231                                         } else {
232                                                 my @subfields=$field->subfields();
233                                                 foreach my $subfieldcount (0..$#subfields) {
234                                                         my $subfield=$subfields[$subfieldcount][0];
235                                                         my $value=$subfields[$subfieldcount][1];
236                                                         next if (length $subfield !=1);
237                                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
238                                                         push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
239                                                         $i++;
240                                                 }
241                                         }
242 # now, loop again to add parameter subfield that are not in the MARC::Record
243                                         foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
244                                                 next if (length $subfield !=1);
245                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
246                                                 next if ($tag<10);
247                                                 next if ((substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) gt "1")  ); #check for visibility flag
248                                                 next if (defined($field->subfield($subfield)));
249                                                 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
250                                                 $i++;
251                                         }
252                                         if ($#subfields_data >= 0) {
253                                                 my %tag_data;
254                                                 $tag_data{tag} = $tag;
255                                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
256                                                 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
257                                                 $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
258                                                 $tag_data{subfield_loop} = \@subfields_data;
259                                                 if ($tag<10) {
260                                                         $tag_data{fixedfield} = 1;
261                                                 }
262
263                                                 push (@loop_data, \%tag_data);
264                                         }
265 # If there is more than 1 field, add an empty hidden field as separator.
266                                         if ($#fields >=1 && $#loop_data >=0 && $loop_data[$#loop_data]->{'tag'} eq $tag) {
267                                                 my @subfields_data;
268                                                 my %tag_data;
269                                                 push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
270                                                 $tag_data{tag} = '';
271                                                 $tag_data{tag_lib} = '';
272                                                 $tag_data{indicator} = '';
273                                                 $tag_data{subfield_loop} = \@subfields_data;
274                                                 if ($tag<10) {
275                                                         $tag_data{fixedfield} = 1;
276                                                 }
277                                                 push (@loop_data, \%tag_data);
278                                                 $i++;
279                                         }
280                                 }
281         
282                         } else {
283                                 my @subfields_data;
284                                 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
285                                         next if (length $subfield !=1);
286                                         next if ((substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) gt "1")  ); #check for visibility flag
287                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
288                                         push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
289                                         $i++;
290                                 }
291                                 if ($#subfields_data >= 0) {
292                                         my %tag_data;
293                                         $tag_data{tag} = $tag;
294                                         $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
295                                         $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
296                                         $tag_data{indicator} = $indicator;
297                                         $tag_data{subfield_loop} = \@subfields_data;
298                                         $tag_data{tagfirstsubfield} = $tag_data{subfield_loop}[0];
299                                         if ($tag<10) {
300                                                 $tag_data{fixedfield} = 1;
301                                         }
302                                         push (@loop_data, \%tag_data);
303                                 }
304                         }
305                 }
306                 $template->param($tabloop."XX" =>\@loop_data);
307         }
308 }
309
310
311 sub build_hidden_data () {
312     # build hidden data =>
313     # we store everything, even if we show only requested subfields.
314
315     my @loop_data =();
316     my $i=0;
317     foreach my $tag (keys %{$tagslib}) {
318         my $previous_tag = '';
319
320         # loop through each subfield
321         foreach my $subfield (keys %{$tagslib->{$tag}}) {
322             next if ($subfield eq 'lib');
323             next if ($subfield eq 'tab');
324             next if ($subfield eq 'mandatory');
325                 next if ($subfield eq 'repeatable');
326             next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
327             my %subfield_data;
328             $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
329             $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
330             $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
331             $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
332             push(@loop_data, \%subfield_data);
333             $i++
334         }
335     }
336 }
337
338 # ======================== 
339 #          MAIN 
340 #=========================
341 my $input = new CGI;
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 $z3950 = $input->param('z3950');
345 my $op = $input->param('op');
346 my $nonav = $input->param('nonav');
347 my $myindex = $input->param('index');
348 my $linkid=$input->param('linkid');
349 my $authtypecode = $input->param('authtypecode');
350
351 my $dbh = C4::Context->dbh;
352 $authtypecode = &AUTHfind_authtypecode($dbh,$authid) if !$authtypecode;
353
354
355 my ($template, $loggedinuser, $cookie)
356     = get_template_and_user({template_name => "authorities/authorities.tmpl",
357                              query => $input,
358                              type => "intranet",
359                              authnotrequired => 0,
360                              flagsrequired => {editcatalogue => 1},
361                              debug => 1,
362                              });
363 $template->param(nonav   => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
364 $tagslib = AUTHgettagslib($dbh,1,$authtypecode);
365 my $record=-1;
366 my $encoding="";
367 $record = AUTHgetauthority($dbh,$authid) if ($authid);
368 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
369 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
370 $is_a_modif=0;
371 if ($authid) {
372         $is_a_modif=1;
373         ($oldauthnumtagfield,$oldauthnumtagsubfield) = &AUTHfind_marc_from_kohafield($dbh,"auth_header.authid",$authtypecode);
374         ($oldauthtypetagfield,$oldauthtypetagsubfield) = &AUTHfind_marc_from_kohafield($dbh,"auth_header.authtypecode",$authtypecode);
375 }
376
377 #------------------------------------------------------------------------------------------------------------------------------
378 if ($op eq "add") {
379 #------------------------------------------------------------------------------------------------------------------------------
380
381         # rebuild
382         my @tags = $input->param('tag');
383         my @subfields = $input->param('subfield');
384         my @values = $input->param('field_value');
385         # build indicator hash.
386         my @ind_tag = $input->param('ind_tag');
387         my @indicator = $input->param('indicator');
388         my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
389         #warn $xml;
390         my $record=MARC::Record->new_from_xml($xml,'UTF-8');
391         $record->encoding('UTF-8');
392         #warn $record->as_formatted;
393         #warn "IN ADDBIB";
394         # check for a duplicate
395         my ($duplicateauthid,$duplicateauthvalue) = C4::AuthoritiesMarc::FindDuplicate($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
396 #warn "duplicate:$duplicateauthid,$duplicateauthvalue"; 
397         my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
398         # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
399         if (!$duplicateauthid or $confirm_not_duplicate) {
400 # warn "noduplicate";
401                 if ($is_a_modif ) {     
402                         $authid=AUTHmodauthority($dbh,$authid,$record,$authtypecode,1);         
403                 } else {
404                 ($authid) = AUTHaddauthority($dbh,$record,$authid,$authtypecode);
405
406                 }
407         # now, redirect to detail page
408                 if ($nonav){
409 #warn ($myindex,$nonav);
410                 print $input->redirect("auth_finder.pl?index=$myindex&nonav=$nonav&authtypecode=$authtypecode");
411                 }else{
412                 print $input->redirect("detail.pl?nonav=$nonav&authid=$authid");
413                 }
414                 exit;
415         } else {
416 #warn "duplicate";
417         # it may be a duplicate, warn the user and do nothing
418                 build_tabs ($template, $record, $dbh,$encoding);
419                 build_hidden_data;
420                 $template->param(authid =>$authid,
421                         duplicateauthid                         => $duplicateauthid,
422                         duplicateauthvalue                              => $duplicateauthvalue,
423                          );
424         }
425 #------------------------------------------------------------------------------------------------------------------------------
426 } elsif ($op eq "addfield") {
427 #------------------------------------------------------------------------------------------------------------------------------
428         my $addedfield = $input->param('addfield_field');
429         my $tagaddfield_subfield = $input->param('addfield_subfield');
430         my @tags = $input->param('tag');
431         my @subfields = $input->param('subfield');
432         my @values = $input->param('field_value');
433         # build indicator hash.
434         my @ind_tag = $input->param('ind_tag');
435         my @indicator = $input->param('indicator');
436         my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
437         my $record=MARC::Record->new_from_xml($xml,'UTF-8');
438         $record->encoding('UTF-8');
439         # adding an empty field
440         my $field = MARC::Field->new("$addedfield",'','','$tagaddfield_subfield' => "");
441         $record->append_fields($field);
442         build_tabs ($template, $record, $dbh,$encoding);
443         build_hidden_data;
444         $template->param(
445                 authid                       => $authid,);
446
447 } elsif ($op eq "delete") {
448 #------------------------------------------------------------------------------------------------------------------------------
449         &AUTHdelauthority($dbh,$authid);
450         if ($nonav){
451         print $input->redirect("auth_finder.pl");
452         }else{
453         print $input->redirect("authorities-home.pl?authid=0");
454         }
455                 exit;
456 } else {
457 if ($op eq "duplicate")
458         {
459                 $authid = "";
460         }
461         build_tabs ($template, $record, $dbh,$encoding);
462         build_hidden_data;
463         $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
464                 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
465                 authid                      => $authid , authtypecode=>$authtypecode,   );
466 }
467
468 #unless ($op) {
469 #       warn "BUILDING";
470 #       build_tabs ($template, $record, $dbh,$encoding);
471 #       build_hidden_data;
472 #}
473 $template->param(
474         authid                       => $authid,
475         authtypecode => $authtypecode,
476         linkid=>$linkid,
477         );
478
479 my $authtypes = getauthtypes;
480 my @authtypesloop;
481 foreach my $thisauthtype (keys %$authtypes) {
482         my $selected = 1 if $thisauthtype eq $authtypecode;
483         my %row =(value => $thisauthtype,
484                                 selected => $selected,
485                                 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
486                         );
487         push @authtypesloop, \%row;
488 }
489
490 $template->param(authtypesloop => \@authtypesloop,
491                                 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
492                                 hide_marc => C4::Context->preference('hide_marc'),
493                                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
494                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
495                 IntranetNav => C4::Context->preference("IntranetNav"),
496                                 );
497 output_html_with_http_headers $input, $cookie, $template->output;