Using "boolean_preference()" call to get current status of YesNo type system
[koha.git] / admin / systempreferences.pl
1 #!/usr/bin/perl
2
3 #script to administer the systempref table
4 #written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # ALGO :
8 # this script use an $op to know what to do.
9 # if $op is empty or none of the above values,
10 #       - the default screen is build (with all records, or filtered datas).
11 #       - the   user can clic on add, modify or delete record.
12 # if $op=add_form
13 #       - if primkey exists, this is a modification,so we read the $primkey record
14 #       - builds the add/modify form
15 # if $op=add_validate
16 #       - the user has just send datas, so we create/modify the record
17 # if $op=delete_form
18 #       - we show the record having primkey=$primkey and ask for deletion validation form
19 # if $op=delete_confirm
20 #       - we delete the record having primkey=$primkey
21
22
23 # Copyright 2000-2002 Katipo Communications
24 #
25 # This file is part of Koha.
26 #
27 # Koha is free software; you can redistribute it and/or modify it under the
28 # terms of the GNU General Public License as published by the Free Software
29 # Foundation; either version 2 of the License, or (at your option) any later
30 # version.
31 #
32 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
33 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
34 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
35 #
36 # You should have received a copy of the GNU General Public License along with
37 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
38 # Suite 330, Boston, MA  02111-1307 USA
39
40 use strict;
41 use CGI;
42 use C4::Auth;
43 use C4::Context;
44 use C4::Output;
45 use C4::Interface::CGI::Output;
46 use C4::Search;
47 use HTML::Template;
48 use C4::Context;
49
50
51 sub StringSearch  {
52         my ($env,$searchstring,$type)=@_;
53         my $dbh = C4::Context->dbh;
54         $searchstring=~ s/\'/\\\'/g;
55         my @data=split(' ',$searchstring);
56         my $count=@data;
57         my $query="Select variable,value,explanation,type,options from systempreferences where (variable like \"$data[0]%\") order by variable";
58         my $sth=$dbh->prepare($query);
59         $sth->execute;
60         my @results;
61         my $cnt=0;
62         while (my $data=$sth->fetchrow_hashref){
63                 push(@results,$data);
64                 $cnt ++;
65         }
66         $sth->finish;
67         return ($cnt,\@results);
68 }
69
70 my $input = new CGI;
71 my $searchfield=$input->param('searchfield');
72 my $pkfield="variable";
73 my $reqsel="select variable,value,explanation,type,options from systempreferences where $pkfield='$searchfield'";
74 my $reqdel="delete from systempreferences where $pkfield='$searchfield'";
75 my $offset=$input->param('offset');
76 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
77
78 my ($template, $borrowernumber, $cookie)
79     = get_template_and_user({template_name => "parameters/systempreferences.tmpl",
80                              query => $input,
81                              type => "intranet",
82                              authnotrequired => 0,
83                              flagsrequired => {parameters => 1},
84                              debug => 1,
85                              });
86 my $pagesize=20;
87 my $op = $input->param('op');
88 $searchfield=~ s/\,//g;
89
90 if ($op) {
91 $template->param(script_name => $script_name,
92                                                 $op              => 1); # we show only the TMPL_VAR names $op
93 } else {
94 $template->param(script_name => $script_name,
95                                                 else              => 1); # we show only the TMPL_VAR names $op
96 }
97
98 if ($op eq 'update_and_reedit') {
99     foreach ($input->param) {
100         warn "$_: ".$input->param($_)."\n";
101     }
102     my $value='';
103     if (my $currentorder=$input->param('currentorder')) {
104         my @currentorder=split /\|/, $currentorder;
105         my $orderchanged=0;
106         foreach my $param ($input->param) {
107             if ($param=~m#up-(\d+).x#) {
108                 my $temp=$currentorder[$1];
109                 $currentorder[$1]=$currentorder[$1-1];
110                 $currentorder[$1-1]=$temp;
111                 $orderchanged=1;
112                 last;
113             } elsif ($param=~m#down-(\d+).x#) {
114                 my $temp=$currentorder[$1];
115                 $currentorder[$1]=$currentorder[$1+1];
116                 $currentorder[$1+1]=$temp;
117                 $orderchanged=1;
118                 last;
119             }
120         }
121         $value=join ' ', @currentorder;
122         if ($orderchanged) {
123             $op='add_form';
124             $template->param(script_name => $script_name,
125                                                     $op              => 1); # we show only the TMPL_VAR names $op
126         } else {
127             $op='';
128             $searchfield='';
129             $template->param(script_name => $script_name,
130                                                 else              => 1); # we show only the TMPL_VAR names $op
131         }
132     }
133     my $dbh = C4::Context->dbh;
134     my $query="select * from systempreferences where variable=?";
135     my $sth=$dbh->prepare($query);
136     $sth->execute($input->param('variable'));
137     if ($sth->rows) {
138             my $query = "update systempreferences set value=?,explanation=? where variable=?";
139             my $sth=$dbh->prepare($query);
140             $sth->execute($value, $input->param('explanation'), $input->param('variable'));
141             $sth->finish;
142     } else {
143             my $query = "insert into systempreferences (variable,value,explanation) values (?,?,?)";
144             my $sth=$dbh->prepare($query);
145             $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'));
146             $sth->finish;
147     }
148     $sth->finish;
149
150 }
151
152 ################## ADD_FORM ##################################
153 # called by default. Used to create form to add or  modify a record
154
155 if ($op eq 'add_form') {
156         #---- if primkey exists, it's a modify action, so read values to modify...
157         my $data;
158         if ($searchfield) {
159                 my $dbh = C4::Context->dbh;
160                 my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable='$searchfield'");
161                 $sth->execute;
162                 $data=$sth->fetchrow_hashref;
163                 $sth->finish;
164                 $template->param(modify => 1);
165         }
166
167         my @options;
168         foreach my $option (split(/\|/, $data->{'options'})) {
169                 my $selected='0';
170                 $option eq $data->{'value'} and $selected=1;
171                 push @options, { option => $option, selected => $selected };
172         }
173         if ($data->{'type'} eq 'Choice') {
174                 $template->param('type-choice' => 1);
175         } elsif ($data->{'type'} eq 'YesNo') {
176                 $template->param('type-yesno' => 1);
177                 $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
178                 ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
179         } elsif ($data->{'type'} eq 'free') {
180                 $template->param('type-free' => 1);
181                 $template->param('fieldlength' => $data->{'options'});
182         } elsif ($data->{'type'} eq 'Integer') {
183                 $template->param('type-free' => 1);
184                 $template->param('fieldlength' => $data->{'options'});
185         } elsif ($data->{'type'} eq 'Float') {
186                 $template->param('type-free' => 1);
187                 $template->param('fieldlength' => $data->{'options'});
188         } elsif ($data->{'type'} eq 'Themes') {
189                 $template->param('type-reorderlist' => 1);
190                 my $type='';
191                 ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
192                 @options=();
193                 my $currently_selected_themes;
194                 my $counter=0;
195                 foreach my $theme (split /\s+/, $data->{'value'}) {
196                     push @options, { option => $theme, counter => $counter };
197                     $currently_selected_themes->{$theme}=1;
198                     $counter++;
199                 }
200                 foreach my $theme (getallthemes($type)) {
201                         my $selected='0';
202                         next if $currently_selected_themes->{$theme};
203                         push @options, { option => $theme, counter => $counter };
204                         $counter++;
205                 }
206         } elsif ($data->{'type'} eq 'Languages') {
207                 $template->param('type-reorderlist' => 1);
208                 my $type='';
209                 @options=();
210                 my $currently_selected_languages;
211                 my $counter=0;
212                 foreach my $language (split /\s+/, $data->{'value'}) {
213                     next if $language eq 'images';
214                     push @options, { option => $language, counter => $counter };
215                     $currently_selected_languages->{$language}=1;
216                     $counter++;
217                 }
218                 foreach my $language (getalllanguages()) {
219                         next if $language eq 'images';
220                         my $selected='0';
221                         next if $currently_selected_languages->{$language};
222                         push @options, { option => $language, counter => $counter };
223                         $counter++;
224                 }
225         }
226         $template->param(explanation => $data->{'explanation'},
227                          value => $data->{'value'},
228                          type => $data->{'type'},
229                          options => \@options,
230                          searchfield => $searchfield);
231
232 ################## ADD_VALIDATE ##################################
233 # called by add_form, used to insert/modify data in DB
234 } elsif ($op eq 'add_validate') {
235         my $dbh = C4::Context->dbh;
236         my $query="select * from systempreferences where variable=?";
237         my $sth=$dbh->prepare($query);
238         $sth->execute($input->param('variable'));
239         if ($sth->rows) {
240                 my $query = "update systempreferences set value=?,explanation=? where variable=?";
241                 my $sth=$dbh->prepare($query);
242                 $sth->execute($input->param('value'), $input->param('explanation'), $input->param('variable'));
243                 $sth->finish;
244         } else {
245                 my $query = "insert into systempreferences (variable,value,explanation) values (?,?,?)";
246                 my $sth=$dbh->prepare($query);
247                 $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'));
248                 $sth->finish;
249         }
250         $sth->finish;
251 ################## DELETE_CONFIRM ##################################
252 # called by default form, used to confirm deletion of data in DB
253 } elsif ($op eq 'delete_confirm') {
254         my $dbh = C4::Context->dbh;
255         my $sth=$dbh->prepare($reqsel);
256         $sth->execute;
257         my $data=$sth->fetchrow_hashref;
258         $sth->finish;
259         $template->param(searchfield => $searchfield,
260                                                         Tvalue => $data->{'value'},
261                                                         );
262
263                                                                                                         # END $OP eq DELETE_CONFIRM
264 ################## DELETE_CONFIRMED ##################################
265 # called by delete_confirm, used to effectively confirm deletion of data in DB
266 } elsif ($op eq 'delete_confirmed') {
267         my $dbh = C4::Context->dbh;
268         my $sth=$dbh->prepare($reqdel);
269         $sth->execute;
270         $sth->finish;
271                                                                                                         # END $OP eq DELETE_CONFIRMED
272 ################## DEFAULT ##################################
273 } else { # DEFAULT
274         if  ($searchfield ne '') {
275                  $template->param(searchfield => "You Searched for <b>$searchfield<b><p>");
276         }
277         my $env;
278         my ($count,$results)=StringSearch($env,$searchfield,'web');
279         my $toggle="white";
280         my @loop_data = ();
281         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
282                 if ($toggle eq 'white'){
283                         $toggle="#ffffcc";
284                 } else {
285                         $toggle="white";
286                 }
287                 my %row_data;  # get a fresh hash for the row data
288                 $row_data{variable} = $results->[$i]{'variable'};
289                 $row_data{value} = $results->[$i]{'value'};
290                 $row_data{explanation} = $results->[$i]{'explanation'};
291                 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results->[$i]{'variable'};
292                 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'variable'};
293                 push(@loop_data, \%row_data);
294         }
295         $template->param(loop => \@loop_data);
296         if ($offset>0) {
297                 my $prevpage = $offset-$pagesize;
298                 $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
299         }
300         if ($offset+$pagesize<$count) {
301                 my $nextpage =$offset+$pagesize;
302                 $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
303         }
304 } #---- END $OP eq DEFAULT
305
306 output_html_with_http_headers $input, $cookie, $template->output;