Clean up before final commits
[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::Biblio;
29 use C4::Context;
30 use C4::Koha; # XXX subfield_is_koha_internal_p
31 use Encode;
32
33 use vars qw( $tagslib);
34 use vars qw( $authorised_values_sth);
35 use vars qw( $is_a_modif );
36 my $input = new CGI;
37 my $z3950 = $input->param('z3950');
38 my $logstatus=C4::Context->preference('Activate_log');
39 my $xml;
40 my $itemtype; # created here because it can be used in build_authorized_values_list sub
41
42
43
44
45
46 =item build_authorized_values_list
47
48 =cut
49
50 sub build_authorized_values_list ($$$$$) {
51         my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
52
53         my @authorised_values;
54         my %authorised_lib;
55
56         # builds list, depending on authorised value...
57
58         #---- branch
59         if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
60         my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
61         $sth->execute;
62         push @authorised_values, ""
63                 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
64
65         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
66                 push @authorised_values, $branchcode;
67                 $authorised_lib{$branchcode}=$branchname;
68         }
69
70         #----- itemtypes
71         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
72                 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
73                 $sth->execute;
74                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
75         
76                 while (my ($itemtype,$description) = $sth->fetchrow_array) {
77                         push @authorised_values, $itemtype;
78                         $authorised_lib{$itemtype}=$description;
79                 }
80                 $value=$itemtype unless ($value);
81
82         #---- "true" authorised value
83         } else {
84                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
85
86                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
87         
88                 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
89                         push @authorised_values, $value;
90                         $authorised_lib{$value}=$lib;
91                 }
92     }
93     return CGI::scrolling_list( -name     => 'field_value',
94                                 -values   => \@authorised_values,
95                                 -default  => $value,
96                                 -labels   => \%authorised_lib,
97                                 -override => 1,
98                                 -size     => 1,
99                                 -multiple => 0 );
100 }
101
102
103 =item create_input
104  builds the <input ...> entry for a subfield.
105 =cut
106 sub create_input () {
107         my ($tag,$subfield,$value,$i,$tabloop,$rec,$authorised_values_sth,$id) = @_;    
108         my $dbh=C4::Context->dbh;
109         $value =~ s/"/&quot;/g;
110         my %subfield_data;
111         $subfield_data{id}=$id;
112         $subfield_data{tag}=$tag;
113         $subfield_data{subfield}=$subfield;
114         $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
115         $subfield_data{marc_lib_plain}=$tagslib->{$tag}->{$subfield}->{lib};
116         $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
117         $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
118         $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
119         $subfield_data{index} = $i;
120         $subfield_data{visibility} = "display:none" if (substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) gt "0") ; #check parity
121         # it's an authorised field
122         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
123                 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
124         # it's linking authority field to another authority
125         } elsif ($tagslib->{$tag}->{$subfield}->{link}) {
126                 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffffff;'\" tabindex=\"1\" type=\"text\" name=\"field_value\" id=\"field_value$id\" value=\"$value\" size=\"40\" maxlength=\"255\" DISABLE READONLY> <a  style=\"cursor: help;\" href=\"javascript:Dopop('../authorities/auth_linker.pl?index=$id',$id);\">...</a>";
127         
128                 # it's a plugin field
129         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
130                 # opening plugin. Just check wether we are on a developper computer on a production one
131                 # (the cgidir differs)
132                 my $cgidir = C4::Context->intranetdir ."/cgi-bin/value_builder";
133                 unless (opendir(DIR, "$cgidir")) {
134                         $cgidir = C4::Context->intranetdir."/value_builder";
135                 } 
136                 my $plugin=$cgidir."/".$tagslib->{$tag}->{$subfield}->{'value_builder'}; 
137                 require $plugin;
138                 my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
139                 my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
140                 $subfield_data{marc_value}="<input tabindex=\"1\" type=\"text\"  name=\"field_value\" id=\"field_value$id\"  value=\"$value\" size=\"40\" maxlength=\"255\" DISABLE READONLY OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i); \"> <a  style=\"cursor: help;\" href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
141         # it's an hidden field
142         } elsif  ($tag eq '') {
143                 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffffff'; \" tabindex=\"1\" type=\"hidden\" name=\"field_value\" id=\"field_value$id\"  value=\"$value\">";
144         } elsif  (substr($tagslib->{$tag}->{$subfield}->{'hidden'},2,1) gt "1") {
145
146                 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffffff'; \" tabindex=\"1\" type=\"text\" name=\"field_value\" id=\"field_value$id\"   value=\"$value\" size=\"40\" maxlength=\"255\" >";
147         # it's a standard field
148         } else {
149                 if (length($value) >100) {
150                         $subfield_data{marc_value}="<textarea tabindex=\"1\" name=\"field_value\" id=\"field_value$id\"  cols=\"40\" rows=\"5\" >$value</textarea>";
151                 } else {
152                         $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffffff'; \" tabindex=\"1\" type=\"text\" name=\"field_value\" id=\"field_value$id\"  value=\"$value\" size=\"50\">"; #"
153                 }
154         }
155         return \%subfield_data;
156 }
157
158 sub build_tabs  ($$$;$){
159     my($template, $xmlhash, $dbh,$addedfield) = @_;
160     # fill arrays
161     my @loop_data =();
162     my $tag;
163     my $i=0;
164 my $id=100;
165         my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
166                 from authorised_values
167                 where category=? order by lib");
168 my $author;
169 my $controlfields;
170 my $leader;
171 if ($xmlhash){
172  $author=$xmlhash->{'datafield'};
173  $controlfields=$xmlhash->{'controlfield'};
174  $leader=$xmlhash->{'leader'};
175 }
176     my @BIG_LOOP;
177 my %built;
178 # loop through each tab 0 through 9
179         for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
180                 my @loop_data = ();
181                 foreach my $tag (sort(keys (%{$tagslib}))) {
182                         my $indicator;
183                                 # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
184                                 # if MARC::Record is empty => use tab as master loop.
185         if ($xmlhash) {
186                         ####
187                 
188                         my %tagdefined;
189                         my %definedsubfields;
190                         my $hiddenrequired;
191                         my ($ind1,$ind2);
192                         
193                  if ($tag>9){
194                         foreach my $data (@$author){
195                                         $hiddenrequired=0;
196                                         my @subfields_data;
197                                         undef %definedsubfields;
198                                  if ($data->{'tag'} eq $tag){
199                                         $tagdefined{$tag}=1 ;
200                                            if ($built{$tag}==1){
201                                                 $hiddenrequired=1;
202                                             }
203                                             $ind1="  ";
204                                               $ind2="  ";               
205                                               foreach my $subfieldcode ( $data->{'subfield'}){
206                                                  foreach my $code ( @$subfieldcode){    
207                                                         next if ($tagslib->{$tag}->{$code->{'code'}}->{tab} ne $tabloop);                                               
208                                                         my $subfield=$code->{'code'}  ;
209                                                         my $value=$code->{'content'};
210                                                         $definedsubfields{$tag.$subfield}=1 ;
211                                                          $built{$tag}=1;
212                                                         push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$xmlhash,$authorised_values_sth,$id)) ;
213                                                         $i++ ;
214                                                 }
215                                               } ##each subfield
216                                             $ind1=$data->{'ind1'};
217                                             $ind2=      $data->{'ind2'};
218                                           
219                                         if ($hiddenrequired && $#loop_data >=0 && $loop_data[$#loop_data]->{'tag'} eq $tag) {
220                                                 my @hiddensubfields_data;
221                                                 my %tag_data;
222                                                 push(@hiddensubfields_data, &create_input('','','',$i,$tabloop,$xmlhash,$authorised_values_sth,$id));
223                                                 $tag_data{tag} = '';
224                                                 $tag_data{tag_lib} = '';
225                                                 $tag_data{indicator} = '';
226                                                 $tag_data{subfield_loop} = \@hiddensubfields_data;
227                                                 push (@loop_data, \%tag_data);
228                                                 $i++;
229                                         }
230                                         # now, loop again to add parameter subfield that are not in the MARC::Record
231                                         
232                                         foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
233                                                 next if (length $subfield !=1);
234                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
235                                                 next if ((substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) gt "1")  ); #check for visibility flag
236                                                 next if ($definedsubfields{$tag.$subfield} );
237                                                 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$xmlhash,$authorised_values_sth,$id));
238                                                 $definedsubfields{$tag.$subfield}=1;
239                                                 $i++;
240                                         }
241                                         if ($#subfields_data >= 0) {
242                                                 my %tag_data;
243                                                 $tag_data{tag} = $tag;
244                                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
245                                                 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
246                                                 $tag_data{indicator} = $ind1.$ind2 if ($tag>=10);
247                                                 $tag_data{subfield_loop} = \@subfields_data;
248                                                 push (@loop_data, \%tag_data);
249                                                 
250                                         }
251                                         $id++;
252                                      }## if tag matches
253                         
254                         }#eachdata
255                 }else{ ## tag <10
256                                 if ($tag eq "000" || $tag eq "LDR"){
257                                         my $subfield="@";
258                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
259                                         my @subfields_data;
260                                         my $value=$leader->[0] if $leader->[0];
261                                         $tagdefined{$tag}=1 ;
262                                         push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$xmlhash,$authorised_values_sth,$id));                                    
263                                         $i++;
264                                         if ($#subfields_data >= 0) {
265                                                 my %tag_data;
266                                                 $tag_data{tag} = $tag;
267                                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
268                                                 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
269                                                 $tag_data{subfield_loop} = \@subfields_data;
270                                                                         $tag_data{fixedfield} = 1;
271                                                 push (@loop_data, \%tag_data);
272                                         }
273                                  }else{
274                                  foreach my $control (@$controlfields){
275                                         my $subfield="@";
276                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
277                                         my @subfields_data;
278                                         if ($control->{'tag'} eq $tag){
279                                         $hiddenrequired=0;
280                                         $tagdefined{$tag}=1 ;
281                                          if ($built{$tag}==1){$hiddenrequired=1;}
282                                         my $value=$control->{'content'} ;
283                                         $definedsubfields{$tag.'@'}=1;
284                                         push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$xmlhash,$authorised_values_sth,$id));                                    
285                                         $i++;
286                                         
287                                            $built{$tag}=1;
288                                         if ($hiddenrequired && $#loop_data >=0 && $loop_data[$#loop_data]->{'tag'} eq $tag) {
289                                                 my @hiddensubfields_data;
290                                                 my %tag_data;
291                                                 push(@hiddensubfields_data, &create_input('','','',$i,$tabloop,$xmlhash,$authorised_values_sth,$id));
292                                                 $tag_data{tag} = '';
293                                                 $tag_data{tag_lib} = '';
294                                                 $tag_data{subfield_loop} = \@hiddensubfields_data;
295                                                 $tag_data{fixedfield} = 1;
296                                                 push (@loop_data, \%tag_data);
297                                                 $i++;
298                                         }
299                                         if ($#subfields_data >= 0) {
300                                                 my %tag_data;
301                                                 $tag_data{tag} = $tag;
302                                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
303                                                 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
304                                                 $tag_data{subfield_loop} = \@subfields_data;
305                                                 $tag_data{fixedfield} = 1;
306                                                 push (@loop_data, \%tag_data);
307                                         }
308                                         $id++;
309                                         }## tag matches
310                                  }# each control
311                                }
312                         }##tag >9
313
314
315                         ##### Any remaining tag
316                                 my @subfields_data;
317                                 # now, loop again to add parameter subfield that are not in the MARC::Record
318                                         foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
319                                                 next if ($tagdefined{$tag} );
320                                                 next if (length $subfield !=1);
321                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
322                                                 next if ((substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) gt "1")  ); #check for visibility flag
323                                                 
324                                                 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$xmlhash,$authorised_values_sth,$id));
325                                                 $tagdefined{$tag.$subfield}=1;
326                                                 $i++;
327                                         }
328                                         if ($#subfields_data >= 0) {
329                                                 my %tag_data;
330                                                 $tag_data{tag} = $tag;
331                                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
332                                                 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
333                                                 $tag_data{indicator} = $ind1.$ind2 if ($tag>=10);
334                                                 $tag_data{subfield_loop} = \@subfields_data;
335                                                 if ($tag<10) {
336                                                                         $tag_data{fixedfield} = 1;
337                                                                         }
338
339                                                 push (@loop_data, \%tag_data);
340                                         }
341
342                                         
343                                         if ($addedfield eq $tag) {
344                                                 my %tag_data;
345                                                 my @subfields_data;
346                                                 $id++;
347                                                 $tagdefined{$tag}=1 ;
348                                                 foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
349                                                 next if (length $subfield !=1);
350                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
351                                                 next if ((substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) gt "1")  ); #check for visibility flag
352                                                 $addedfield=""; 
353                                                 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$xmlhash,$authorised_values_sth,$id));
354                                                 $i++;
355                                                         }
356                                                 if ($#subfields_data >= 0) {
357                                                 $tag_data{tag} = $tag;
358                                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
359                                                 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
360                                                 $tag_data{indicator} = ' ' if ($tag>=10);
361                                                 $tag_data{subfield_loop} = \@subfields_data;
362                                                         if ($tag<10) {
363                                                                                 $tag_data{fixedfield} = 1;
364                                                                                 }
365                                                 push (@loop_data, \%tag_data);
366                                                                                         
367                                                 }
368                                 
369                                         }
370                                 
371         # if breeding is empty
372                         } else {
373                                 my @subfields_data;
374                                 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
375                                         next if (length $subfield !=1);
376                                         next if ((substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) gt "1")  ); #check for visibility flag
377                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
378                                         push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$xmlhash,$authorised_values_sth,$id));
379                                         $i++;
380                                 }
381                                 if ($#subfields_data >= 0) {
382                                         my %tag_data;
383                                         $tag_data{tag} = $tag;
384                                         $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
385                                         $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
386                                         $tag_data{indicator} = $indicator;
387                                         $tag_data{subfield_loop} = \@subfields_data;
388                                         $tag_data{tagfirstsubfield} = $tag_data{subfield_loop}[0];
389                                         if ($tag<10) {
390                                                 $tag_data{fixedfield} = 1;
391                                         }
392                                         push (@loop_data, \%tag_data);
393                                 }
394                         }
395                 $id++;
396         }
397         if ($#loop_data >=0) {
398             my %big_loop_line;
399             $big_loop_line{number}=$tabloop;
400             $big_loop_line{innerloop}=\@loop_data;
401             push @BIG_LOOP,\%big_loop_line;
402             }   
403 #               $template->param($tabloop."XX" =>\@loop_data);
404                 $template->param(BIG_LOOP => \@BIG_LOOP);
405 }## tab loop
406 }
407
408
409 sub build_hidden_data () {
410     # build hidden data =>
411     # we store everything, even if we show only requested subfields.
412
413     my @loop_data =();
414     my $i=0;
415     foreach my $tag (keys %{$tagslib}) {
416         my $previous_tag = '';
417
418         # loop through each subfield
419         foreach my $subfield (keys %{$tagslib->{$tag}}) {
420             next if ($subfield eq 'lib');
421             next if ($subfield eq 'tab');
422             next if ($subfield eq 'mandatory');
423                 next if ($subfield eq 'repeatable');
424             next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
425             my %subfield_data;
426             $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
427             $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
428             $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
429             $subfield_data{marc_value}="<input type=\"hidden\"  name=\"field_value[]\">";
430             push(@loop_data, \%subfield_data);
431             $i++
432         }
433     }
434 }
435
436 # ======================== 
437 #          MAIN 
438 #=========================
439 my $input = new CGI;
440 my $error = $input->param('error');
441 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
442 my $z3950 = $input->param('z3950');
443 my $op = $input->param('op');
444 my $nonav = $input->param('nonav');
445 my $myindex = $input->param('index');
446 my $linkid=$input->param('linkid');
447 my $authtypecode = $input->param('authtypecode');
448
449 my $dbh = C4::Context->dbh;
450 $authtypecode = &AUTHfind_authtypecode($dbh,$authid) if !$authtypecode;
451
452
453 my ($template, $loggedinuser, $cookie)
454     = get_template_and_user({template_name => "authorities/authorities.tmpl",
455                              query => $input,
456                              type => "intranet",
457                              authnotrequired => 0,
458                              flagsrequired => {editcatalogue => 1},
459                              debug => 1,
460                              });
461 $template->param(nonav   => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
462 $tagslib = AUTHgettagslib($dbh,1,$authtypecode);
463
464 my $xmlhash;
465 my $xml;
466 $xmlhash = XMLgetauthorityhash($dbh,$authid) if ($authid);
467
468
469 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
470 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
471 $is_a_modif=0;
472 if ($authid) {
473         $is_a_modif=1;
474         ($oldauthnumtagfield,$oldauthnumtagsubfield) = MARCfind_marc_from_kohafield("auth_authid","authorities");
475         ($oldauthtypetagfield,$oldauthtypetagsubfield) = MARCfind_marc_from_kohafield("auth_authtypecode","authorities");
476 }
477
478 #------------------------------------------------------------------------------------------------------------------------------
479 if ($op eq "add") {
480 #------------------------------------------------------------------------------------------------------------------------------
481
482         # rebuild
483         my @tags = $input->param('tag');
484         my @subfields = $input->param('subfield');
485         my @values = $input->param('field_value');
486         # build indicator hash.
487         my @ind_tag = $input->param('ind_tag');
488         my @indicator = $input->param('indicator');
489 ## check for malformed xml -- non UTF-8 like (MARC8) will break xml without warning
490 ### This usually happens with data coming from other Z3950 servers
491 ## Slows the saving process so comment out at your own risk
492 eval{
493  $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);        
494 };
495  if ($@){
496 warn $@;
497  $template->param(error             =>1,xmlerror=>1,);
498 goto FINAL;
499   };    # check for a duplicate
500   my $xmlhash=XML_xml2hash_onerecord($xml);
501         my ($duplicateauthid,$duplicateauthvalue) = C4::AuthoritiesMarc::FindDuplicateauth($xmlhash,$authtypecode) if ($op eq "add") && (!$is_a_modif);
502 #warn "duplicate:$duplicateauthid,$duplicateauthvalue"; 
503         my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
504         # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
505         if (!$duplicateauthid or $confirm_not_duplicate) {
506 # warn "noduplicate";
507                 if ($is_a_modif ) {     
508                         $authid=AUTHmodauthority($dbh,$authid,$xmlhash,$authtypecode,1);                
509                 } else {
510                 ($authid) = AUTHaddauthority($dbh,$xmlhash,$authid,$authtypecode);
511
512                 }
513         # now, redirect to detail page
514                 if ($nonav){
515 #warn ($myindex,$nonav);
516                 print $input->redirect("auth_finder.pl?index=$myindex&nonav=$nonav&authtypecode=$authtypecode");
517                 }else{
518                 print $input->redirect("detail.pl?nonav=$nonav&authid=$authid");
519                 }
520                 exit;
521         } else {
522 FINAL:
523 #warn "duplicate";
524         # it may be a duplicate, warn the user and do nothing
525                 build_tabs ($template, $xmlhash, $dbh);
526                 build_hidden_data;
527                 $template->param(authid =>$authid,
528                         duplicateauthid                         => $duplicateauthid,
529                         duplicateauthvalue                              => $duplicateauthvalue,
530                          );
531         }
532 #------------------------------------------------------------------------------------------------------------------------------
533 } elsif ($op eq "addfield") {
534 #------------------------------------------------------------------------------------------------------------------------------
535         my $addedfield = $input->param('addfield_field');
536         my @tags = $input->param('tag');
537         my @subfields = $input->param('subfield');
538         my @values = $input->param('field_value');
539         # build indicator hash.
540         my @ind_tag = $input->param('ind_tag');
541         my @indicator = $input->param('indicator');
542         my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
543         my $xmlhash=XML_xml2hash_onerecord($xml);
544         # adding an empty field
545         build_tabs ($template, $xmlhash, $dbh,$addedfield);
546         build_hidden_data;
547         $template->param(
548                 authid                       => $authid,);
549
550 } elsif ($op eq "delete") {
551 #------------------------------------------------------------------------------------------------------------------------------
552         &AUTHdelauthority($dbh,$authid);
553         if ($nonav){
554         print $input->redirect("auth_finder.pl");
555         }else{
556         print $input->redirect("authorities-home.pl?authid=0");
557         }
558                 exit;
559 } else {
560 if ($op eq "duplicate")
561         {
562                 $authid = "";
563         }
564         build_tabs ($template, $xmlhash, $dbh);
565         build_hidden_data;
566         $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
567                 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
568                 authid                      => $authid , authtypecode=>$authtypecode,   );
569 }
570
571 $template->param(
572         authid                       => $authid,
573         authtypecode => $authtypecode,
574         linkid=>$linkid,
575                         intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
576                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
577                 IntranetNav => C4::Context->preference("IntranetNav"),
578                 advancedMARCEditor => C4::Context->preference("advancedMARCEditor"),
579                 );
580
581 my $authtypes = getauthtypes;
582 my @authtypesloop;
583 foreach my $thisauthtype (keys %$authtypes) {
584         my $selected = 1 if $thisauthtype eq $authtypecode;
585         my %row =(value => $thisauthtype,
586                                 selected => $selected,
587                                 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
588                         );
589         push @authtypesloop, \%row;
590 }
591
592 $template->param(authtypesloop => \@authtypesloop,
593                                 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
594                                 nonav=>$nonav,);
595 output_html_with_http_headers $input, $cookie, $template->output;