Bug 17600: Standardize our EXPORT_OK
[srvgit] / admin / systempreferences.pl
index 68bff18..1eb5455 100755 (executable)
@@ -40,19 +40,17 @@ ALSO :
 
 =cut
 
-use strict;
-use warnings;
+use Modern::Perl;
 
 use CGI qw ( -utf8 );
-use MIME::Base64;
-use C4::Auth;
+use MIME::Base64 qw( encode_base64 );
+use C4::Auth qw( get_template_and_user );
 use C4::Context;
-use C4::Koha;
-use C4::Languages qw(getTranslatedLanguages);
-use C4::ClassSource;
-use C4::Log;
-use C4::Output;
-use YAML::Syck qw( Dump LoadFile );
+use C4::Koha qw( getallthemes );
+use C4::Languages qw( getTranslatedLanguages );
+use C4::ClassSource qw( GetClassSources GetClassSource );
+use C4::Output qw( output_html_with_http_headers );
+use YAML::XS;
 
 my %tabsysprefs; #we do no longer need to keep track of a tab per pref (yaml)
 
@@ -70,14 +68,16 @@ sub StringSearch {
 
     my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable in (";
     my $first = 1;
+    my @sql_bind;
     for my $name ( get_local_prefs() ) {
                 $strsth .= ',' unless $first;
-                $strsth .= "'$name'";
+                $strsth .= "?";
+                push(@sql_bind,$name);
                 $first = 0;
     }
     $strsth .= ") order by variable";
     $sth = $dbh->prepare($strsth);
-    $sth->execute();
+    $sth->execute(@sql_bind);
 
     while ( my $data = $sth->fetchrow_hashref ) {
             unless (defined $data->{value}) { $data->{value} = "";}
@@ -114,7 +114,7 @@ sub GetPrefParams {
         $params->{'type_choice'} = 1;
     } elsif ( $data->{'type'} eq 'YesNo' ) {
         $params->{'type_yesno'} = 1;
-        $data->{'value'}        = C4::Context->boolean_preference( $data->{'variable'} );
+        $data->{'value'}        = C4::Context->preference( $data->{'variable'} );
         if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
             $params->{'value_yes'} = 1;
         } else {
@@ -128,6 +128,11 @@ sub GetPrefParams {
         $data->{options} =~ /(.*)\|(.*)/;
         $params->{'cols'} = $1;
         $params->{'rows'} = $2;
+    } elsif ( $data->{'type'} eq 'Htmlarea' ) {
+        $params->{'type_htmlarea'} = 1;
+        $data->{options} =~ /(.*)\|(.*)/;
+        $params->{'cols'} = $1;
+        $params->{'rows'} = $2;
     } elsif ( $data->{'type'} eq 'Themes' ) {
         $params->{'type_choice'} = 1;
         my $type = '';
@@ -177,7 +182,7 @@ sub GetPrefParams {
             $theme     = C4::Context->preference('opacthemes');
         } else {
 
-            # this is the staff client
+            # this is the staff interface
             $interface = 'intranet';
             $theme     = C4::Context->preference('template');
         }
@@ -200,7 +205,7 @@ sub GetPrefParams {
     return $params;
 }
 
-my $input       = new CGI;
+my $input       = CGI->new;
 my $searchfield = $input->param('searchfield') || '';
 my $Tvalue      = $input->param('Tvalue');
 my $offset      = $input->param('offset') || 0;
@@ -210,9 +215,7 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     {   template_name   => "admin/systempreferences.tt",
         query           => $input,
         type            => "intranet",
-        authnotrequired => 0,
-        flagsrequired   => { parameters => 'parameters_remaining_permissions' },
-        debug           => 1,
+        flagsrequired   => { parameters => 'manage_sysprefs' },
     }
 );
 my $pagesize = 100;
@@ -269,24 +272,8 @@ if ( $op eq 'update_and_reedit' ) {
             );    # we show only the TMPL_VAR names $op
         }
     }
-    my $dbh   = C4::Context->dbh;
-    my $query = "select * from systempreferences where variable=?";
-    my $sth   = $dbh->prepare($query);
-    $sth->execute( $input->param('variable') );
-    if ( $sth->rows ) {
-        unless ( C4::Context->config('demo') ) {
-            my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
-            $sth->execute( $value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions') );
-            logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
-        }
-    } else {
-        unless ( C4::Context->config('demo') ) {
-            my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
-            $sth->execute( $input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
-            logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $input->param('value') );
-        }
-    }
-
+    my $variable = $input->param('variable');
+    C4::Context->set_preference($variable, $value);
 }
 
 ################## ADD_FORM ##################################
@@ -315,13 +302,14 @@ if ( $op eq 'add_form' ) {
 ################## ADD_VALIDATE ##################################
     # called by add_form, used to insert/modify data in DB
 } elsif ( $op eq 'add_validate' ) {
-    my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare("select * from systempreferences where variable=?");
-    $sth->execute( $input->param('variable') );
-
     # to handle multiple values
     my $value;
 
+    my $variable = $input->param('variable');
+    my $expl     = $input->param('explanation');
+    my $type     = $input->param('preftype');
+    my $options  = $input->param('prefoptions');
+
     # handle multiple value strings (separated by ',')
     my $params = $input->Vars;
     if ( defined $params->{'value'} ) {
@@ -338,49 +326,29 @@ if ( $op eq 'add_form' ) {
         }
     }
 
-    if ( $input->param('preftype') eq 'Upload' ) {
+    if ( $type eq 'Upload' ) {
         my $lgtfh = $input->upload('value');
         $value = join '', <$lgtfh>;
         $value = encode_base64($value);
     }
 
-    if ( $sth->rows ) {
-        unless ( C4::Context->config('demo') ) {
-            my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
-            $sth->execute( $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable') );
-            logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
-        }
-    } else {
-        unless ( C4::Context->config('demo') ) {
-            my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
-            $sth->execute( $input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
-            logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $value );
-        }
-    }
+    C4::Context->set_preference( $variable, $value, $expl, $type, $options );
     print $input->redirect("/cgi-bin/koha/admin/systempreferences.pl?tab=");
     exit;
 ################## DELETE_CONFIRM ##################################
     # called by default form, used to confirm deletion of data in DB
 } elsif ( $op eq 'delete_confirm' ) {
-    my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
-    $sth->execute($searchfield);
-    my $data = $sth->fetchrow_hashref;
+    my $value = C4::Context->preference($searchfield);
     $template->param(
         searchfield => $searchfield,
-        Tvalue      => $data->{'value'},
+        Tvalue      => $value,
     );
 
     # END $OP eq DELETE_CONFIRM
 ################## DELETE_CONFIRMED ##################################
     # called by delete_confirm, used to effectively confirm deletion of data in DB
 } elsif ( $op eq 'delete_confirmed' ) {
-    my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare("delete from systempreferences where variable=?");
-    $sth->execute($searchfield);
-    my $logstring = $searchfield . " | " . $Tvalue;
-    logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring );
-
+    C4::Context->delete_preference($searchfield);
     # END $OP eq DELETE_CONFIRMED
 ################## DEFAULT ##################################
 } else {    # DEFAULT
@@ -415,12 +383,11 @@ output_html_with_http_headers $input, $cookie, $template->output;
 # .pref files.
 
 sub get_prefs_from_files {
-    my $context       = C4::Context->new();
-    my $path_pref_en  = $context->config('intrahtdocs') .
+    my $path_pref_en  = C4::Context->config('intrahtdocs') .
                         '/prog/en/modules/admin/preferences';
     # Get all .pref file names
     opendir ( my $fh, $path_pref_en );
-    my @pref_files = grep { /.pref/ } readdir($fh);
+    my @pref_files = grep { /.pref$/ } readdir($fh);
     close $fh;
 
     my @names = ();
@@ -438,7 +405,7 @@ sub get_prefs_from_files {
         }
     };
     for my $file (@pref_files) {
-        my $pref = LoadFile( "$path_pref_en/$file" );
+        my $pref = YAML::XS::LoadFile( "$path_pref_en/$file" );
         for my $tab ( keys %$pref ) {
             my $content = $pref->{$tab};
             if ( ref($content) eq 'ARRAY' ) {