Bug 17600: Standardize our EXPORT_OK
[srvgit] / authorities / authorities.pl
index e45f5dd..33c37aa 100755 (executable)
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
+use Modern::Perl;
+
 use CGI qw ( -utf8 );
-use C4::Auth;
-use C4::Output;
-use C4::AuthoritiesMarc;
-use C4::ImportBatch; #GetImportRecordMarc
+use C4::Auth qw( get_template_and_user );
+use C4::Output qw( output_html_with_http_headers );
+use C4::AuthoritiesMarc qw( GetAuthority );
+use C4::ImportBatch qw( GetImportRecordMarc );
 use C4::Context;
-use C4::Koha;
-use Date::Calc qw(Today);
+use Date::Calc qw( Today );
 use MARC::File::USMARC;
 use MARC::File::XML;
-use C4::Biblio;
+use C4::Biblio qw( TransformHtmlToMarc );
 use Koha::Authority::Types;
 use Koha::ItemTypes;
 use vars qw( $tagslib);
@@ -73,7 +72,8 @@ sub build_authorized_values_list {
 
 
     #---- branch
-    if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
+    my $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'};
+    if ( $category eq "branches" ) {
         my $sth =
         $dbh->prepare(
             "select branchcode,branchname from branches order by branchname");
@@ -86,7 +86,7 @@ sub build_authorized_values_list {
             $authorised_lib{$branchcode} = $branchname;
         }
     }
-    elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
+    elsif ( $category eq "itemtypes" ) {
         push @authorised_values, ""
           unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
             && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
@@ -121,6 +121,7 @@ sub build_authorized_values_list {
         values   => \@authorised_values,
         labels   => \%authorised_lib,
         default  => $value,
+        ( ( grep { $_ eq $category } ( qw(branches itemtypes cn_source) ) ) ? () : ( category => $category ) ),
     };
 }
 
@@ -136,8 +137,6 @@ sub create_input {
     
     my $index_subfield = CreateKey(); # create a specifique key for each subfield
 
-    $value =~ s/"/&quot;/g;
-
     # determine maximum length; 9999 bytes per ISO 2709 except for leader and MARC21 008
     my $max_length = 9999;
     if ($tag eq '000') {
@@ -187,7 +186,7 @@ sub create_input {
     }
     
     $subfield_data{visibility} = "display:none;"
-        if (    ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
+        if( $tagslib->{$tag}->{$subfield}->{hidden} and $value ne ''
             or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
         );
     
@@ -289,6 +288,9 @@ sub create_input {
 
         }
     }
+    if ($cgi->param('tagreport') && $subfield_data{tag} == $cgi->param('tagreport')) {
+        $subfield_data{marc_value}{value} = $cgi->param('tag'. $cgi->param('tagbiblio') . 'subfield' . $subfield_data{subfield});
+    }
     $subfield_data{'index_subfield'} = $index_subfield;
     return \%subfield_data;
 }
@@ -320,7 +322,7 @@ sub CreateKey {
 
 =item GetMandatoryFieldZ3950
 
-    This function return an hashref which containts all mandatory field
+    This function returns a hashref which contains all mandatory field
     to search with z3950 server.
 
 =cut
@@ -333,7 +335,7 @@ sub GetMandatoryFieldZ3950 {
             '110a' => 'authorcorp',
             '111a' => 'authormeetingcon',
             '130a' => 'uniformtitle',
-            '150a' => 'topic',
+            '150a' => 'subject',
         };
     }else{
         return {
@@ -404,6 +406,7 @@ sub build_tabs {
                             $subfield = '@';
                         }
                         next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
+                        next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
                         push(
                             @subfields_data,
                             &create_input(
@@ -419,6 +422,7 @@ sub build_tabs {
                             my $value    = $subfields[$subfieldcount][1];
                             next if ( length $subfield != 1 );
                             next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
+                            next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
                             push(
                                 @subfields_data,
                                 &create_input(
@@ -435,10 +439,7 @@ sub build_tabs {
                         next if ( length $subfield != 1 );
                         next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
                         next if ( $tag < 10 );
-                        next
-                        if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
-                            or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
-                        );    #check for visibility flag
+                        next if $tagslib->{$tag}->{$subfield}->{hidden} && $subfield ne '9';
                         next if ( defined( $field->subfield($subfield) ) );
                         push(
                             @subfields_data,
@@ -475,16 +476,17 @@ sub build_tabs {
             }
             else {
                 my @subfields_data;
-                foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
-                    next if ( length $subfield != 1 );
-                    next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
-                                or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
-                            ;    #check for visibility flag
-                    next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
+                foreach my $subfield (
+                    sort { $a->{display_order} <=> $b->{display_order} || $a->{subfield} cmp $b->{subfield} }
+                    grep { ref($_) && %$_ } # Not a subfield (values for "important", "lib", "mandatory", etc.) or empty
+                    values %{ $tagslib->{$tag} } )
+                {
+                    next if $subfield->{hidden} && $subfield->{subfield} ne '9';
+                    next if ( $subfield->{tab} ne $tabloop );
                     push(
                         @subfields_data,
                         &create_input(
-                            $tag, $subfield, '', $index_tag, $tabloop, $record,
+                            $tag, $subfield->{subfield}, '', $index_tag, $tabloop, $record,
                             $authorised_values_sth,$input
                         )
                     );
@@ -556,7 +558,7 @@ sub build_hidden_data {
 # ======================== 
 #          MAIN 
 #=========================
-my $input = new CGI;
+my $input = CGI->new;
 my $z3950 = $input->param('z3950');
 my $error = $input->param('error');
 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
@@ -567,6 +569,7 @@ my $linkid=$input->param('linkid');
 my $authtypecode = $input->param('authtypecode');
 my $breedingid    = $input->param('breedingid');
 
+
 my $dbh = C4::Context->dbh;
 if(!$authtypecode) {
     $authtypecode = $authid ? Koha::Authorities->find($authid)->authtypecode : '';
@@ -576,11 +579,9 @@ my ($template, $loggedinuser, $cookie)
     = get_template_and_user({template_name => "authorities/authorities.tt",
                             query => $input,
                             type => "intranet",
-                            authnotrequired => 0,
                             flagsrequired => {editauthorities => 1},
-                            debug => 1,
                             });
-$template->param(nonav   => $nonav,index=>$myindex,authtypecode=>$authtypecode,breedingid=>$breedingid,);
+$template->param(nonav   => $nonav,index=>$myindex,authtypecode=>$authtypecode,breedingid=>$breedingid);
 
 $tagslib = GetTagsLabels(1,$authtypecode);
 $mandatory_z3950 = GetMandatoryFieldZ3950($authtypecode);
@@ -663,12 +664,13 @@ if ($op eq "duplicate")
 
 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
 
+my $type = $authority_types->find($authtypecode);
 $template->param(
     authority_types => $authority_types,
     authtypecode    => $authtypecode,
     authid          => $authid,
     linkid          => $linkid,
-    authtypetext    => $authority_types->find($authtypecode)->authtypetext,
+    authtypetext    => $type ? $type->authtypetext : "",
     hide_marc       => C4::Context->preference('hide_marc'),
 );
 output_html_with_http_headers $input, $cookie, $template->output;