Fixing tab display on authorities.tmpl, correcting invalid markup generated by author...
[koha.git] / authorities / authorities.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use CGI;
24 use C4::Auth;
25 use C4::Output;
26 use C4::AuthoritiesMarc;
27 use C4::Context;
28 use C4::Koha; # XXX subfield_is_koha_internal_p
29 use Date::Calc qw(Today);
30 use MARC::File::USMARC;
31 use MARC::File::XML;
32 use C4::Biblio;
33 use vars qw( $tagslib);
34 use vars qw( $authorised_values_sth);
35 use vars qw( $is_a_modif );
36
37 my $itemtype; # created here because it can be used in build_authorized_values_list sub
38 our($authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
39 =item find_value
40
41     ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
42
43 Find the given $subfield in the given $tag in the given
44 MARC::Record $record.  If the subfield is found, returns
45 the (indicators, value) pair; otherwise, (undef, undef) is
46 returned.
47
48 =cut
49
50 sub find_value {
51         my ($tagfield,$insubfield,$record,$encoding) = @_;
52         my @result;
53         my $indicator;
54         if ($tagfield <10) {
55                 if ($record->field($tagfield)) {
56                         push @result, $record->field($tagfield)->data();
57                 } else {
58                         push @result,"";
59                 }
60         } else {
61                 foreach my $field ($record->field($tagfield)) {
62                         my @subfields = $field->subfields();
63                         foreach my $subfield (@subfields) {
64                                 if (@$subfield[0] eq $insubfield) {
65                                 push @result,@$subfield[1];
66                                                         $indicator = $field->indicator(1).$field->indicator(2);
67                                 }
68                         }
69                 }
70         }
71         return($indicator,@result);
72 }
73
74
75 =item build_authorized_values_list
76
77 =cut
78
79 sub build_authorized_values_list ($$$$$$$) {
80     my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
81
82     my @authorised_values;
83     my %authorised_lib;
84
85     # builds list, depending on authorised value...
86
87     #---- branch
88     if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
89         my $sth =
90           $dbh->prepare(
91             "select branchcode,branchname from branches order by branchname");
92         $sth->execute;
93         push @authorised_values, ""
94           unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
95
96         while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
97             push @authorised_values, $branchcode;
98             $authorised_lib{$branchcode} = $branchname;
99         }
100
101         #----- itemtypes
102     }
103     elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
104         my $sth =
105           $dbh->prepare(
106             "select itemtype,description from itemtypes order by description");
107         $sth->execute;
108         push @authorised_values, ""
109           unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
110           
111         my $itemtype;
112         
113         while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
114             push @authorised_values, $itemtype;
115             $authorised_lib{$itemtype} = $description;
116         }
117         $value = $itemtype unless ($value);
118
119         #---- "true" authorised value
120     }
121     else {
122         $authorised_values_sth->execute(
123             $tagslib->{$tag}->{$subfield}->{authorised_value} );
124
125         push @authorised_values, ""
126           unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
127
128         while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
129             push @authorised_values, $value;
130             $authorised_lib{$value} = $lib;
131         }
132     }
133     return CGI::scrolling_list(
134         -name     => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
135         -values   => \@authorised_values,
136         -default  => $value,
137         -labels   => \%authorised_lib,
138         -override => 1,
139         -size     => 1,
140         -multiple => 0,
141         -tabindex => 1,
142         -id       => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
143         -class    => "input_marceditor",
144     );
145 }
146
147
148 =item create_input
149  builds the <input ...> entry for a subfield.
150 =cut
151
152 sub create_input {
153     my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
154     
155     my $index_subfield = CreateKey(); # create a specifique key for each subfield
156
157     $value =~ s/"/&quot;/g;
158
159     # if there is no value provided but a default value in parameters, get it
160     unless ($value) {
161         $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
162
163         # get today date & replace YYYY, MM, DD if provided in the default value
164         my ( $year, $month, $day ) = Today();
165         $month = sprintf( "%02d", $month );
166         $day   = sprintf( "%02d", $day );
167         $value =~ s/YYYY/$year/g;
168         $value =~ s/MM/$month/g;
169         $value =~ s/DD/$day/g;
170     }
171     my $dbh = C4::Context->dbh;
172     my %subfield_data = (
173         tag        => $tag,
174         subfield   => $subfield,
175         marc_lib   => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
176         marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib}, 
177         tag_mandatory  => $tagslib->{$tag}->{mandatory},
178         mandatory      => $tagslib->{$tag}->{$subfield}->{mandatory},
179         repeatable     => $tagslib->{$tag}->{$subfield}->{repeatable},
180         kohafield      => $tagslib->{$tag}->{$subfield}->{kohafield},
181         index          => $index_tag,
182         id             => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
183         value          => $value,
184     );
185     if($subfield eq '@'){
186         $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
187     } else {
188          $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
189     }
190
191     if(exists $mandatory_z3950->{$tag.$subfield}){
192         $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
193     }
194     
195     $subfield_data{visibility} = "display:none;"
196         if (    ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
197             or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
198         );
199     
200     # it's an authorised field
201     if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
202         $subfield_data{marc_value} =
203           build_authorized_values_list( $tag, $subfield, $value, $dbh,
204             $authorised_values_sth,$index_tag,$index_subfield );
205
206     # it's a thesaurus / authority field
207     }
208     elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
209         $subfield_data{marc_value} =
210     "<input type=\"text\"
211                         id=\"".$subfield_data{id}."\"
212                         name=\"".$subfield_data{id}."\"
213       value=\"$value\"
214       class=\"input_marceditor\"
215       tabindex=\"1\"                     
216       disabled=\"disabled\"
217           readonly=\"readonly\" \/>
218       <span class=\"buttonDot\"
219         onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}')\">...</span>
220     ";
221     # it's a plugin field
222     }
223     elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
224
225         # opening plugin. Just check wether we are on a developper computer on a production one
226         # (the cgidir differs)
227         my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
228         unless ( opendir( DIR, "$cgidir" ) ) {
229             $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
230         }
231         my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
232         do $plugin || die "Plugin Failed: ".$plugin;
233         my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
234         my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
235 #         my ( $function_name, $javascript,$extended_param );
236         
237         $subfield_data{marc_value} =
238     "<input tabindex=\"1\"
239                         type=\"text\"
240                         id=\"".$subfield_data{id}."\"
241       name=\"".$subfield_data{id}."\"
242       value=\"$value\"
243                         class=\"input_marceditor\"
244       onfocus=\"Focus$function_name($index_tag)\"
245       onblur=\"Blur$function_name($index_tag); \" \/>
246     <span class=\"buttonDot\"
247       onclick=\"Clic$function_name('$subfield_data{id}')\">...</a>
248     $javascript";
249         # it's an hidden field
250     }
251     elsif ( $tag eq '' ) {
252         $subfield_data{marc_value} =
253             "<input tabindex=\"1\"
254                     type=\"hidden\"
255                     id=\"".$subfield_data{id}."\"
256                     name=\"".$subfield_data{id}."\"
257                     value=\"$value\" \/>
258             ";
259     }
260     elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
261         $subfield_data{marc_value} =
262             "<input type=\"text\"
263                     id=\"".$subfield_data{id}."\"
264                     name=\"".$subfield_data{id}."\"
265                     class=\"input_marceditor\"
266                     tabindex=\"1\"
267                     value=\"$value\"
268             \/>";
269
270         # it's a standard field
271     }
272     else {
273         if (
274             length($value) > 100
275             or
276             ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
277                 and $tag < 400 && $subfield eq 'a' )
278             or (    $tag >= 500
279                 and $tag < 600
280                 && C4::Context->preference("marcflavour") eq "MARC21" )
281           )
282         {
283             $subfield_data{marc_value} =
284                 "<textarea cols=\"70\"
285                            rows=\"4\"
286                            id=\"".$subfield_data{id}."\"
287                            name=\"".$subfield_data{id}."\"
288                            class=\"input_marceditor\"
289                            tabindex=\"1\"
290                            >$value</textarea>
291                 ";
292         }
293         else {
294             $subfield_data{marc_value} =
295                 "<input type=\"text\"
296                         id=\"".$subfield_data{id}."\"
297                         name=\"".$subfield_data{id}."\"
298                         value=\"$value\"
299                         tabindex=\"1\"
300                         class=\"input_marceditor\"
301                 \/>
302                 ";
303         }
304     }
305     $subfield_data{'index_subfield'} = $index_subfield;
306     return \%subfield_data;
307 }
308
309 =item CreateKey
310
311     Create a random value to set it into the input name
312
313 =cut
314
315 sub CreateKey(){
316     return int(rand(1000000));
317 }
318
319 sub build_tabs ($$$$$) {
320     my ( $template, $record, $dbh, $encoding,$input ) = @_;
321
322     # fill arrays
323     my @loop_data = ();
324     my $tag;
325
326     my $authorised_values_sth = $dbh->prepare(
327         "select authorised_value,lib
328         from authorised_values
329         where category=? order by lib"
330     );
331     
332     # in this array, we will push all the 10 tabs
333     # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
334     my @BIG_LOOP;
335     my %seen;
336     my @tab_data; # all tags to display
337     
338     foreach my $used ( keys %$tagslib ){
339         push @tab_data,$used if not $seen{$used};
340         $seen{$used}++;
341     }
342         
343     my $max_num_tab=9;
344     # loop through each tab 0 through 9
345     for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
346         my @loop_data = (); #innerloop in the template.
347         my $i = 0;
348         foreach my $tag (@tab_data) {
349             $i++;
350             next if ! $tag;
351             my $indicator;
352             my $index_tag = CreateKey;
353
354             # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
355             # if MARC::Record is empty => use tab as master loop.
356             if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
357                 my @fields;
358     if ( $tag ne '000' ) {
359                     @fields = $record->field($tag);
360     }
361     else {
362        push @fields, $record->leader(); # if tag == 000
363     }
364     # loop through each field
365                 foreach my $field (@fields) {
366                     
367                     my @subfields_data;
368                     if ( $tag < 10 ) {
369                         my ( $value, $subfield );
370                         if ( $tag ne '000' ) {
371                             $value    = $field->data();
372                             $subfield = "@";
373                         }
374                         else {
375                             $value    = $field;
376                             $subfield = '@';
377                         }
378                         next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
379                         next
380                           if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
381                             'biblio.biblionumber' );
382                         push(
383                             @subfields_data,
384                             &create_input(
385                                 $tag, $subfield, $value, $index_tag, $tabloop, $record,
386                                 $authorised_values_sth,$input
387                             )
388                         );
389                     }
390                     else {
391                         my @subfields = $field->subfields();
392                         foreach my $subfieldcount ( 0 .. $#subfields ) {
393                             my $subfield = $subfields[$subfieldcount][0];
394                             my $value    = $subfields[$subfieldcount][1];
395                             next if ( length $subfield != 1 );
396                             next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
397                             push(
398                                 @subfields_data,
399                                 &create_input(
400                                     $tag, $subfield, $value, $index_tag, $tabloop,
401                                     $record, $authorised_values_sth,$input
402                                 )
403                             );
404                         }
405                     }
406
407                     # now, loop again to add parameter subfield that are not in the MARC::Record
408                     foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
409                     {
410                         next if ( length $subfield != 1 );
411                         next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
412                         next if ( $tag < 10 );
413                         next
414                           if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
415                             or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
416                           );    #check for visibility flag
417                         next if ( defined( $field->subfield($subfield) ) );
418                         push(
419                             @subfields_data,
420                             &create_input(
421                                 $tag, $subfield, '', $index_tag, $tabloop, $record,
422                                 $authorised_values_sth,$input
423                             )
424                         );
425                     }
426                     if ( $#subfields_data >= 0 ) {
427                         # build the tag entry.
428                         # note that the random() field is mandatory. Otherwise, on repeated fields, you'll 
429                         # have twice the same "name" value, and cgi->param() will return only one, making
430                         # all subfields to be merged in a single field.
431                         my %tag_data = (
432                             tag           => $tag,
433                             index         => $index_tag,
434                             tag_lib       => $tagslib->{$tag}->{lib},
435                             repeatable       => $tagslib->{$tag}->{repeatable},
436                             subfield_loop => \@subfields_data,
437                             fixedfield    => ($tag < 10)?(1):(0),
438                             random        => CreateKey,
439                         );
440                         if ($tag >= 010){ # no indicator for theses tag
441                            $tag_data{indicator} = $field->indicator(1).$field->indicator(2);
442                         }
443                         push( @loop_data, \%tag_data );
444                     }
445                  } # foreach $field end
446
447             # if breeding is empty
448             }
449             else {
450                 my @subfields_data;
451                 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
452                     next if ( length $subfield != 1 );
453                     next
454                       if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
455                         or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
456                       ;    #check for visibility flag
457                     next
458                       if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
459                     push(
460                         @subfields_data,
461                         &create_input(
462                             $tag, $subfield, '', $index_tag, $tabloop, $record,
463                             $authorised_values_sth,$input
464                         )
465                     );
466                 }
467                 if ( $#subfields_data >= 0 ) {
468                     my %tag_data = (
469                         tag              => $tag,
470                         index            => $index_tag,
471                         tag_lib          => $tagslib->{$tag}->{lib},
472                         repeatable       => $tagslib->{$tag}->{repeatable},
473                         indicator        => $indicator,
474                         subfield_loop    => \@subfields_data,
475                         tagfirstsubfield => $subfields_data[0],
476                         fixedfield       => ($tag < 10)?(1):(0)
477                     );
478                     
479                     push @loop_data, \%tag_data ;
480                 }
481             }
482         }
483         if ( $#loop_data >= 0 ) {
484             push @BIG_LOOP, {
485                 number    => $tabloop,
486                 innerloop => \@loop_data,
487             };
488         }
489     }
490     $template->param( singletab => (scalar(@BIG_LOOP)==1), BIG_LOOP => \@BIG_LOOP );
491 }
492
493
494 sub build_hidden_data () {
495     # build hidden data =>
496     # we store everything, even if we show only requested subfields.
497
498     my @loop_data =();
499     my $i=0;
500     foreach my $tag (keys %{$tagslib}) {
501         my $previous_tag = '';
502
503         # loop through each subfield
504         foreach my $subfield (keys %{$tagslib->{$tag}}) {
505             next if ($subfield eq 'lib');
506             next if ($subfield eq 'tab');
507             next if ($subfield eq 'mandatory');
508                 next if ($subfield eq 'repeatable');
509             next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
510             my %subfield_data;
511             $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
512             $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
513             $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
514             $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
515             push(@loop_data, \%subfield_data);
516             $i++
517         }
518     }
519 }
520
521 # ======================== 
522 #          MAIN 
523 #=========================
524 my $input = new CGI;
525 my $z3950 = $input->param('z3950');
526 my $error = $input->param('error');
527 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
528 my $op = $input->param('op');
529 my $nonav = $input->param('nonav');
530 my $myindex = $input->param('index');
531 my $linkid=$input->param('linkid');
532 my $authtypecode = $input->param('authtypecode');
533
534 my $dbh = C4::Context->dbh;
535 $authtypecode = &GetAuthTypeCode($authid) if !$authtypecode;
536
537 my ($template, $loggedinuser, $cookie)
538     = get_template_and_user({template_name => "authorities/authorities.tmpl",
539                              query => $input,
540                              type => "intranet",
541                              authnotrequired => 0,
542                              flagsrequired => {editauthorities => 1},
543                              debug => 1,
544                              });
545 $template->param(nonav   => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
546 $tagslib = GetTagsLabels(1,$authtypecode);
547 my $record=-1;
548 my $encoding="";
549 $record = GetAuthority($authid) if ($authid);
550 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
551 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
552 $is_a_modif=0;
553 if ($authid) {
554   $is_a_modif=1;
555   ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
556   ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
557 }
558
559 #------------------------------------------------------------------------------------------------------------------------------
560 if ($op eq "add") {
561 #------------------------------------------------------------------------------------------------------------------------------
562         # rebuild
563         my @tags = $input->param('tag');
564         my @subfields = $input->param('subfield');
565         my @values = $input->param('field_value');
566         # build indicator hash.
567         my @ind_tag = $input->param('ind_tag');
568         my @indicator = $input->param('indicator');
569   my @params = $input->param();
570   my $record = TransformHtmlToMarc(\@params,$input);
571   if  (C4::Context->preference("marcflavour") eq "UNIMARC"){
572     unless ($record->field('100')){
573       use POSIX qw(strftime);
574       my $string = strftime( "%Y%m%d", localtime(time) );
575       # set 50 to position 26 is biblios, 13 if authorities
576       my $pos=13;
577       $string = sprintf( "%-*s", 35, $string );
578       substr( $string, $pos , 2, "50" );
579       $record->append_fields(MARC::Field->new('100','','',"a"=>$string));
580     }    
581   }
582     
583 #   warn $record->as_formatted;  
584 #       my $record=MARC::Record->new_from_xml($xml,'UTF-8',(C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")));
585 #       $record->encoding('UTF-8');
586         #warn $record->as_formatted;
587         # check for a duplicate
588         my ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
589         my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
590 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
591         if (!$duplicateauthid or $confirm_not_duplicate) {
592 # warn "noduplicate";
593           if ($is_a_modif ) {   
594             ModAuthority($authid,$record,$authtypecode,1);
595           } else {
596             ($authid) = AddAuthority($record,$authid,$authtypecode);
597           }
598           print $input->redirect("detail.pl?authid=$authid");
599           exit;
600         } else {
601         # it may be a duplicate, warn the user and do nothing
602             build_tabs($template, $record, $dbh, $encoding,$input);
603             build_hidden_data;
604             $template->param(authid =>$authid,
605                             duplicateauthid     => $duplicateauthid,
606                             duplicateauthvalue  => $duplicateauthvalue,
607                             );
608         }
609 # #------------------------------------------------------------------------------------------------------------------------------
610 # } elsif ($op eq "addfield") {
611 # #------------------------------------------------------------------------------------------------------------------------------
612 #       my $addedfield = $input->param('addfield_field');
613 #       my $tagaddfield_subfield = $input->param('addfield_subfield');
614 #       my @tags = $input->param('tag');
615 #       my @subfields = $input->param('subfield');
616 #       my @values = $input->param('field_value');
617 #       # build indicator hash.
618 #       my @ind_tag = $input->param('ind_tag');
619 #       my @indicator = $input->param('indicator');
620 #       my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
621 #       my $record=MARC::Record->new_from_xml($xml,'UTF-8');
622 #       $record->encoding('UTF-8');
623 #       # adding an empty field
624 #       my $field = MARC::Field->new("$addedfield",'','','$tagaddfield_subfield' => "");
625 #       $record->append_fields($field);
626 #       build_tabs ($template, $record, $dbh,$encoding,$input);
627 #       build_hidden_data;
628 #       $template->param(
629 #               authid => $authid,);
630
631 } elsif ($op eq "delete") {
632 #------------------------------------------------------------------------------------------------------------------------------
633         &AUTHdelauthority($authid);
634         if ($nonav){
635         print $input->redirect("auth_finder.pl");
636         }else{
637         print $input->redirect("authorities-home.pl?authid=0");
638         }
639                 exit;
640 } else {
641 if ($op eq "duplicate")
642         {
643                 $authid = "";
644         }
645         build_tabs ($template, $record, $dbh,$encoding,$input);
646         build_hidden_data;
647         $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
648                 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
649                 authid                      => $authid , authtypecode=>$authtypecode,   );
650 }
651
652 $template->param(
653         authid                       => $authid,
654         authtypecode => $authtypecode,
655         linkid=>$linkid,
656         );
657
658 my $authtypes = getauthtypes;
659 my @authtypesloop;
660 foreach my $thisauthtype (keys %$authtypes) {
661         my $selected = 1 if $thisauthtype eq $authtypecode;
662         my %row =(value => $thisauthtype,
663                                 selected => $selected,
664                                 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
665                         );
666         push @authtypesloop, \%row;
667 }
668
669 $template->param(authtypesloop => \@authtypesloop,
670                                 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
671                                 hide_marc => C4::Context->preference('hide_marc'),
672                                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
673                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
674                 IntranetNav => C4::Context->preference("IntranetNav"),
675                                 );
676 output_html_with_http_headers $input, $cookie, $template->output;