Finalized XML version for intranet
[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 ($authidtagfield,$authidtagsubfield)=MARCfind_marc_from_kohafield("authid","authorities");
166         my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
167                 from authorised_values
168                 where category=? order by lib");
169 my $author;
170 my $controlfields;
171 my $leader;
172 if ($xmlhash){
173  $author=$xmlhash->{'datafield'};
174  $controlfields=$xmlhash->{'controlfield'};
175  $leader=$xmlhash->{'leader'};
176 }
177     my @BIG_LOOP;
178 my %built;
179 # loop through each tab 0 through 9
180         for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
181                 my @loop_data = ();
182                 foreach my $tag (sort(keys (%{$tagslib}))) {
183                         my $indicator;
184                                 # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
185                                 # if MARC::Record is empty => use tab as master loop.
186         if ($xmlhash) {
187                         ####
188                 
189                         my %tagdefined;
190                         my %definedsubfields;
191                         my $hiddenrequired;
192                         my ($ind1,$ind2);
193                         
194                  if ($tag>9){
195                         next if ($tag eq $authidtagfield); #we do not want authid to duplicate
196
197                         foreach my $data (@$author){                                                    
198                                         $hiddenrequired=0;
199                                         my @subfields_data;
200                                         undef %definedsubfields;
201                                  if ($data->{'tag'} eq $tag){
202                                         $tagdefined{$tag}=1 ;
203                                            if ($built{$tag}==1){
204                                                 $hiddenrequired=1;
205                                             }
206                                             $ind1="  ";
207                                               $ind2="  ";               
208                                               foreach my $subfieldcode ( $data->{'subfield'}){
209                                                  foreach my $code ( @$subfieldcode){    
210                                                         next if ($tagslib->{$tag}->{$code->{'code'}}->{tab} ne $tabloop);                                               
211                                                         my $subfield=$code->{'code'}  ;
212                                                         my $value=$code->{'content'};
213                                                         $definedsubfields{$tag.$subfield}=1 ;
214                                                          $built{$tag}=1;
215                                                         push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$xmlhash,$authorised_values_sth,$id)) ;
216                                                         $i++ ;
217                                                 }
218                                               } ##each subfield
219                                             $ind1=$data->{'ind1'};
220                                             $ind2=      $data->{'ind2'};
221                                           
222                                         if ($hiddenrequired && $#loop_data >=0 && $loop_data[$#loop_data]->{'tag'} eq $tag) {
223                                                 my @hiddensubfields_data;
224                                                 my %tag_data;
225                                                 push(@hiddensubfields_data, &create_input('','','',$i,$tabloop,$xmlhash,$authorised_values_sth,$id));
226                                                 $tag_data{tag} = '';
227                                                 $tag_data{tag_lib} = '';
228                                                 $tag_data{indicator} = '';
229                                                 $tag_data{subfield_loop} = \@hiddensubfields_data;
230                                                 push (@loop_data, \%tag_data);
231                                                 $i++;
232                                         }
233                                         # now, loop again to add parameter subfield that are not in the MARC::Record
234                                         
235                                         foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
236                                                 next if (length $subfield !=1);
237                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
238                                                 next if ((substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) >1)  ); #check for visibility flag
239                                                 next if ($definedsubfields{$tag.$subfield} );
240                                                 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$xmlhash,$authorised_values_sth,$id));
241                                                 $definedsubfields{$tag.$subfield}=1;
242                                                 $i++;
243                                         }
244                                         if ($#subfields_data >= 0) {
245                                                 my %tag_data;
246                                                 $tag_data{tag} = $tag;
247                                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
248                                                 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
249                                                 $tag_data{indicator} = $ind1.$ind2 if ($tag>=10);
250                                                 $tag_data{subfield_loop} = \@subfields_data;
251                                                 push (@loop_data, \%tag_data);
252                                                 
253                                         }
254                                         $id++;
255                                      }## if tag matches
256                         
257                         }#eachdata
258                 }else{ ## tag <10
259                         next if ($tag eq $authidtagfield); #we do not want authid to duplicate
260
261                                 if ($tag eq "000" || $tag eq "LDR"){
262                                         my $subfield="@";
263                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
264                                         my @subfields_data;
265                                         my $value=$leader->[0] if $leader->[0];
266                                         $tagdefined{$tag}=1 ;
267                                         push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$xmlhash,$authorised_values_sth,$id));                                    
268                                         $i++;
269                                         if ($#subfields_data >= 0) {
270                                                 my %tag_data;
271                                                 $tag_data{tag} = $tag;
272                                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
273                                                 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
274                                                 $tag_data{subfield_loop} = \@subfields_data;
275                                                                         $tag_data{fixedfield} = 1;
276                                                 push (@loop_data, \%tag_data);
277                                         }
278                                  }else{
279                                  foreach my $control (@$controlfields){
280                                         my $subfield="@";
281                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
282                                         next if ($tagslib->{$tag} eq $authidtagfield);
283                                         my @subfields_data;
284                                         if ($control->{'tag'} eq $tag){
285                                                 $hiddenrequired=0;
286                                                 $tagdefined{$tag}=1;
287                                                  if ($built{$tag}==1){$hiddenrequired=1;}
288                                                 my $value=$control->{'content'} ;
289                                                 $definedsubfields{$tag.'@'}=1;
290                                                 push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$xmlhash,$authorised_values_sth,$id));                                    
291                                                 $i++;
292                                         
293                                                 $built{$tag}=1;
294                                         ###hiddenrequired
295                                         if ($#subfields_data >= 0) {
296                                                 my %tag_data;
297                                                 $tag_data{tag} = $tag;
298                                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
299                                                 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
300                                                 $tag_data{subfield_loop} = \@subfields_data;
301                                                 $tag_data{fixedfield} = 1;
302                                                 push (@loop_data, \%tag_data);
303                                         }
304                                         $id++;
305                                         }## tag matches
306                                  }# each control
307                                }
308                         }##tag >9
309
310
311                         ##### Any remaining tag
312                                 my @subfields_data;
313                                 # now, loop again to add parameter subfield that are not in the MARC::Record
314                                         foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
315                                                 next if ($tagdefined{$tag} );
316                                                 next if (length $subfield !=1);
317                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
318                                                 next if ((substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) > 1) ); #check for visibility flag
319                                                 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$xmlhash,$authorised_values_sth,$id));
320                                                 $tagdefined{$tag.$subfield}=1;
321                                                 $i++;
322                                         }
323                                         if ($#subfields_data >= 0) {
324                                                 my %tag_data;
325                                                 $tag_data{tag} = $tag;
326                                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
327                                                 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
328                                                 $tag_data{indicator} = $ind1.$ind2 if ($tag>=10);
329                                                 $tag_data{subfield_loop} = \@subfields_data;
330                                                 if ($tag<10) {
331                                                                         $tag_data{fixedfield} = 1;
332                                                                         }
333
334                                                 push (@loop_data, \%tag_data);
335                                         }
336
337                                         
338                                         if ($addedfield eq $tag) {
339                                                 my %tag_data;
340                                                 my @subfields_data;
341                                                 $id++;
342                                                 $tagdefined{$tag}=1 ;
343                                                 foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
344                                                 next if (length $subfield !=1);
345                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
346                                                 next if ((substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) >1)  ); #check for visibility flag
347                                                 $addedfield=""; 
348                                                 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$xmlhash,$authorised_values_sth,$id));
349                                                 $i++;
350                                                         }
351                                                 if ($#subfields_data >= 0) {
352                                                 $tag_data{tag} = $tag;
353                                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
354                                                 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
355                                                 $tag_data{indicator} = ' ' if ($tag>=10);
356                                                 $tag_data{subfield_loop} = \@subfields_data;
357                                                         if ($tag<10) {
358                                                                                 $tag_data{fixedfield} = 1;
359                                                                                 }
360                                                 push (@loop_data, \%tag_data);
361                                                                                         
362                                                 }
363                                 
364                                         }
365                                 
366         # if breeding is empty
367                         } else {
368                                 my @subfields_data;
369                                 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
370                                         next if (length $subfield !=1);
371                                         next if ((substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) >1)  ); #check for visibility flag
372                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
373                                         push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$xmlhash,$authorised_values_sth,$id));
374                                         $i++;
375                                 }
376                                 if ($#subfields_data >= 0) {
377                                         my %tag_data;
378                                         $tag_data{tag} = $tag;
379                                         $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
380                                         $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
381                                         $tag_data{indicator} = $indicator;
382                                         $tag_data{subfield_loop} = \@subfields_data;
383                                         $tag_data{tagfirstsubfield} = $tag_data{subfield_loop}[0];
384                                         if ($tag<10) {
385                                                 $tag_data{fixedfield} = 1;
386                                         }
387                                         push (@loop_data, \%tag_data);
388                                 }
389                         }
390                 $id++;
391         }
392         if ($#loop_data >=0) {
393             my %big_loop_line;
394             $big_loop_line{number}=$tabloop;
395             $big_loop_line{innerloop}=\@loop_data;
396             push @BIG_LOOP,\%big_loop_line;
397             }   
398 #               $template->param($tabloop."XX" =>\@loop_data);
399                 $template->param(BIG_LOOP => \@BIG_LOOP);
400 }## tab loop
401 }
402
403
404 sub build_hidden_data () {
405     # build hidden data =>
406     # we store everything, even if we show only requested subfields.
407
408     my @loop_data =();
409     my $i=0;
410     foreach my $tag (keys %{$tagslib}) {
411         my $previous_tag = '';
412
413         # loop through each subfield
414         foreach my $subfield (keys %{$tagslib->{$tag}}) {
415             next if ($subfield eq 'lib');
416             next if ($subfield eq 'tab');
417             next if ($subfield eq 'mandatory');
418                 next if ($subfield eq 'repeatable');
419             next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
420             my %subfield_data;
421             $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
422             $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
423             $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
424             $subfield_data{marc_value}="<input type=\"hidden\"  name=\"field_value[]\">";
425             push(@loop_data, \%subfield_data);
426             $i++
427         }
428     }
429 }
430
431 # ======================== 
432 #          MAIN 
433 #=========================
434 my $input = new CGI;
435 my $error = $input->param('error');
436 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
437 my $z3950 = $input->param('z3950');
438 my $op = $input->param('op');
439 my $nonav = $input->param('nonav');
440 my $myindex = $input->param('index');
441 my $linkid=$input->param('linkid');
442 my $authtypecode = $input->param('authtypecode');
443
444 my $dbh = C4::Context->dbh;
445 $authtypecode = &AUTHfind_authtypecode($dbh,$authid) if !$authtypecode;
446
447
448 my ($template, $loggedinuser, $cookie)
449     = get_template_and_user({template_name => "authorities/authorities.tmpl",
450                              query => $input,
451                              type => "intranet",
452                              authnotrequired => 0,
453                              flagsrequired => {editcatalogue => 1},
454                              debug => 1,
455                              });
456 $template->param(nonav   => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
457 $tagslib = AUTHgettagslib($dbh,1,$authtypecode);
458
459 my $xmlhash;
460 my $xml;
461 $xmlhash = XMLgetauthorityhash($dbh,$authid) if ($authid);
462
463
464 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
465 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
466 $is_a_modif=0;
467 if ($authid) {
468         $is_a_modif=1;
469         ($oldauthnumtagfield,$oldauthnumtagsubfield) = MARCfind_marc_from_kohafield("authid","authorities");
470         ($oldauthtypetagfield,$oldauthtypetagsubfield) = MARCfind_marc_from_kohafield("authtypecode","authorities");
471 }
472
473 #------------------------------------------------------------------------------------------------------------------------------
474 if ($op eq "add") {
475 #------------------------------------------------------------------------------------------------------------------------------
476
477         # rebuild
478         my @tags = $input->param('tag');
479         my @subfields = $input->param('subfield');
480         my @values = $input->param('field_value');
481         # build indicator hash.
482         my @ind_tag = $input->param('ind_tag');
483         my @indicator = $input->param('indicator');
484 ## check for malformed xml -- non UTF-8 like (MARC8) will break xml without warning
485 ### This usually happens with data coming from other Z3950 servers
486 ## Slows the saving process so comment out at your own risk
487 eval{
488  $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);        
489 };
490
491  if ($@){
492 warn $@;
493  $template->param(error             =>1,xmlerror=>1,);
494 goto FINAL;
495   };    # check for a duplicate
496 ###Authorities need the XML header unlike biblios
497 $xml='<?xml version="1.0" encoding="UTF-8"?>'.$xml;
498   my $xmlhash=XML_xml2hash_onerecord($xml);
499         my ($duplicateauthid,$duplicateauthvalue) = C4::AuthoritiesMarc::FindDuplicateauth($xmlhash,$authtypecode) if ($op eq "add") && (!$is_a_modif);
500 #warn "duplicate:$duplicateauthid,$duplicateauthvalue"; 
501         my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
502         # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
503         if (!$duplicateauthid or $confirm_not_duplicate) {
504 # warn "noduplicate";
505                 if ($is_a_modif ) {     
506                         $authid=AUTHmodauthority($dbh,$authid,$xmlhash,$authtypecode);
507                 } else {
508                 $authid = AUTHaddauthority($dbh,$xmlhash,'',$authtypecode);
509
510                 }
511         # now, redirect to detail page
512                 if ($nonav){
513 #warn ($myindex,$nonav);
514                 print $input->redirect("auth_finder.pl?index=$myindex&nonav=$nonav&authtypecode=$authtypecode");
515                 }else{
516                 print $input->redirect("detail.pl?nonav=$nonav&authid=$authid");
517                 }
518                 exit;
519         } else {
520 FINAL:
521 #warn "duplicate";
522         # it may be a duplicate, warn the user and do nothing
523                 build_tabs ($template, $xmlhash, $dbh);
524                 build_hidden_data;
525                 $template->param(authid =>$authid,
526                         duplicateauthid                         => $duplicateauthid,
527                         duplicateauthvalue                              => $duplicateauthvalue,
528                          );
529         }
530 #------------------------------------------------------------------------------------------------------------------------------
531 } elsif ($op eq "addfield") {
532 #------------------------------------------------------------------------------------------------------------------------------
533         my $addedfield = $input->param('addfield_field');
534         my @tags = $input->param('tag');
535         my @subfields = $input->param('subfield');
536         my @values = $input->param('field_value');
537         # build indicator hash.
538         my @ind_tag = $input->param('ind_tag');
539         my @indicator = $input->param('indicator');
540         my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
541         $xml='<?xml version="1.0" encoding="UTF-8"?>'.$xml;
542         my $xmlhash=XML_xml2hash_onerecord($xml);
543         # adding an empty field
544         build_tabs ($template, $xmlhash, $dbh,$addedfield);
545         build_hidden_data;
546         $template->param(
547                 authid                       => $authid,);
548
549 } elsif ($op eq "delete") {
550 #------------------------------------------------------------------------------------------------------------------------------
551         &AUTHdelauthority($dbh,$authid);
552         if ($nonav){
553         print $input->redirect("auth_finder.pl");
554         }else{
555         print $input->redirect("authorities-home.pl?authid=0");
556         }
557                 exit;
558 } else {
559 if ($op eq "duplicate")
560         {
561                 $authid = "";
562         }
563         build_tabs ($template, $xmlhash, $dbh);
564         build_hidden_data;
565         $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
566                 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
567                 authid                      => $authid , authtypecode=>$authtypecode,   );
568 }
569
570 $template->param(
571         authid                       => $authid,
572         authtypecode => $authtypecode,
573         linkid=>$linkid,
574                         intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
575                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
576                 IntranetNav => C4::Context->preference("IntranetNav"),
577                 advancedMARCEditor => C4::Context->preference("advancedMARCEditor"),
578                 );
579
580 my $authtypes = getauthtypes;
581 my @authtypesloop;
582 foreach my $thisauthtype (keys %$authtypes) {
583         my $selected = 1 if $thisauthtype eq $authtypecode;
584         my %row =(value => $thisauthtype,
585                                 selected => $selected,
586                                 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
587                         );
588         push @authtypesloop, \%row;
589 }
590
591 $template->param(authtypesloop => \@authtypesloop,
592                                 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
593                                 nonav=>$nonav,);
594 output_html_with_http_headers $input, $cookie, $template->output;