Fixed bug #35. System preference settings now give selectable options where
authortonnesen <tonnesen>
Wed, 11 Jun 2003 18:45:36 +0000 (18:45 +0000)
committertonnesen <tonnesen>
Wed, 11 Jun 2003 18:45:36 +0000 (18:45 +0000)
appropriate.

admin/systempreferences.pl

index 9838371..ccccdea 100755 (executable)
@@ -54,14 +54,14 @@ sub StringSearch  {
        $searchstring=~ s/\'/\\\'/g;
        my @data=split(' ',$searchstring);
        my $count=@data;
-       my $query="Select variable,value,explanation from systempreferences where (variable like \"$data[0]%\") order by variable";
+       my $query="Select variable,value,explanation,type,options from systempreferences where (variable like \"$data[0]%\") order by variable";
        my $sth=$dbh->prepare($query);
        $sth->execute;
        my @results;
        my $cnt=0;
        while (my $data=$sth->fetchrow_hashref){
-       push(@results,$data);
-       $cnt ++;
+               push(@results,$data);
+               $cnt ++;
        }
        $sth->finish;
        return ($cnt,\@results);
@@ -70,7 +70,7 @@ sub StringSearch  {
 my $input = new CGI;
 my $searchfield=$input->param('searchfield');
 my $pkfield="variable";
-my $reqsel="select variable,value,explanation from systempreferences where $pkfield='$searchfield'";
+my $reqsel="select variable,value,explanation,type,options from systempreferences where $pkfield='$searchfield'";
 my $reqdel="delete from systempreferences where $pkfield='$searchfield'";
 my $offset=$input->param('offset');
 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
@@ -94,36 +94,158 @@ $template->param(script_name => $script_name,
 $template->param(script_name => $script_name,
                                                else              => 1); # we show only the TMPL_VAR names $op
 }
+
+if ($op eq 'update_and_reedit') {
+    foreach ($input->param) {
+       warn "$_: ".$input->param($_)."\n";
+    }
+    my $value='';
+    if (my $currentorder=$input->param('currentorder')) {
+       my @currentorder=split /\|/, $currentorder;
+       my $orderchanged=0;
+       foreach my $param ($input->param) {
+           if ($param=~m#up-(\d+).x#) {
+               my $temp=$currentorder[$1];
+               $currentorder[$1]=$currentorder[$1-1];
+               $currentorder[$1-1]=$temp;
+               $orderchanged=1;
+               last;
+           } elsif ($param=~m#down-(\d+).x#) {
+               my $temp=$currentorder[$1];
+               $currentorder[$1]=$currentorder[$1+1];
+               $currentorder[$1+1]=$temp;
+               $orderchanged=1;
+               last;
+           }
+       }
+       $value=join ' ', @currentorder;
+       if ($orderchanged) {
+           $op='add_form';
+           $template->param(script_name => $script_name,
+                                                   $op              => 1); # we show only the TMPL_VAR names $op
+       } else {
+           $op='';
+           $searchfield='';
+           $template->param(script_name => $script_name,
+                                               else              => 1); # 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) {
+           my $query = "update systempreferences set value=?,explanation=? where variable=?";
+           my $sth=$dbh->prepare($query);
+           $sth->execute($value, $input->param('explanation'), $input->param('variable'));
+           $sth->finish;
+    } else {
+           my $query = "insert into systempreferences (variable,value,explanation) values (?,?,?)";
+           my $sth=$dbh->prepare($query);
+           $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'));
+           $sth->finish;
+    }
+    $sth->finish;
+
+}
+
 ################## ADD_FORM ##################################
 # called by default. Used to create form to add or  modify a record
+
 if ($op eq 'add_form') {
        #---- if primkey exists, it's a modify action, so read values to modify...
        my $data;
        if ($searchfield) {
                my $dbh = C4::Context->dbh;
-               my $sth=$dbh->prepare("select variable,value,explanation from systempreferences where variable='$searchfield'");
+               my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable='$searchfield'");
                $sth->execute;
                $data=$sth->fetchrow_hashref;
                $sth->finish;
-       }
-       if ($searchfield) {
                $template->param(modify => 1);
        }
 
+       my @options;
+       foreach my $option (split(/\|/, $data->{'options'})) {
+               my $selected='0';
+               $option eq $data->{'value'} and $selected=1;
+               push @options, { option => $option, selected => $selected };
+       }
+       if ($data->{'type'} eq 'Choice') {
+               $template->param('type-choice' => 1);
+       } elsif ($data->{'type'} eq 'YesNo') {
+               $template->param('type-yesno' => 1);
+               ($data->{'value'} eq 'yes') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
+       } elsif ($data->{'type'} eq 'free') {
+               $template->param('type-free' => 1);
+               $template->param('fieldlength' => $data->{'options'});
+       } elsif ($data->{'type'} eq 'Integer') {
+               $template->param('type-free' => 1);
+               $template->param('fieldlength' => $data->{'options'});
+       } elsif ($data->{'type'} eq 'Float') {
+               $template->param('type-free' => 1);
+               $template->param('fieldlength' => $data->{'options'});
+       } elsif ($data->{'type'} eq 'Themes') {
+               $template->param('type-reorderlist' => 1);
+               my $type='';
+               ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
+               @options=();
+               my $currently_selected_themes;
+               my $counter=0;
+               foreach my $theme (split /\s+/, $data->{'value'}) {
+                   push @options, { option => $theme, counter => $counter };
+                   $currently_selected_themes->{$theme}=1;
+                   $counter++;
+               }
+               foreach my $theme (getallthemes($type)) {
+                       my $selected='0';
+                       next if $currently_selected_themes->{$theme};
+                       push @options, { option => $theme, counter => $counter };
+                       $counter++;
+               }
+       } elsif ($data->{'type'} eq 'Languages') {
+               $template->param('type-reorderlist' => 1);
+               my $type='';
+               @options=();
+               my $currently_selected_languages;
+               my $counter=0;
+               foreach my $language (split /\s+/, $data->{'value'}) {
+                   next if $language eq 'images';
+                   push @options, { option => $language, counter => $counter };
+                   $currently_selected_languages->{$language}=1;
+                   $counter++;
+               }
+               foreach my $language (getalllanguages()) {
+                       next if $language eq 'images';
+                       my $selected='0';
+                       next if $currently_selected_languages->{$language};
+                       push @options, { option => $language, counter => $counter };
+                       $counter++;
+               }
+       }
        $template->param(explanation => $data->{'explanation'},
                         value => $data->{'value'},
+                        type => $data->{'type'},
+                        options => \@options,
                         searchfield => $searchfield);
 
 ################## ADD_VALIDATE ##################################
 # called by add_form, used to insert/modify data in DB
 } elsif ($op eq 'add_validate') {
        my $dbh = C4::Context->dbh;
-       my $query = "replace systempreferences (variable,value,explanation) values (";
-       $query.= $dbh->quote($input->param('variable')).",";
-       $query.= $dbh->quote($input->param('value')).",";
-       $query.= $dbh->quote($input->param('explanation')).")";
+       my $query="select * from systempreferences where variable=?";
        my $sth=$dbh->prepare($query);
-       $sth->execute;
+       $sth->execute($input->param('variable'));
+       if ($sth->rows) {
+               my $query = "update systempreferences set value=?,explanation=? where variable=?";
+               my $sth=$dbh->prepare($query);
+               $sth->execute($input->param('value'), $input->param('explanation'), $input->param('variable'));
+               $sth->finish;
+       } else {
+               my $query = "insert into systempreferences (variable,value,explanation) values (?,?,?)";
+               my $sth=$dbh->prepare($query);
+               $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'));
+               $sth->finish;
+       }
        $sth->finish;
 ################## DELETE_CONFIRM ##################################
 # called by default form, used to confirm deletion of data in DB