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)
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.
13 # - if primkey exists, this is a modification,so we read the $primkey record
14 # - builds the add/modify form
16 # - the user has just send datas, so we create/modify the record
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
23 # Copyright 2000-2002 Katipo Communications
25 # This file is part of Koha.
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
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.
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
45 use C4::Interface::CGI::Output;
52 my ($env,$searchstring,$type)=@_;
53 my $dbh = C4::Context->dbh;
54 $searchstring=~ s/\'/\\\'/g;
55 my @data=split(' ',$searchstring);
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);
62 while (my $data=$sth->fetchrow_hashref){
67 return ($cnt,\@results);
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";
78 my ($template, $borrowernumber, $cookie)
79 = get_template_and_user({template_name => "parameters/systempreferences.tmpl",
83 flagsrequired => {parameters => 1},
87 my $op = $input->param('op');
88 $searchfield=~ s/\,//g;
91 $template->param(script_name => $script_name,
92 $op => 1); # we show only the TMPL_VAR names $op
94 $template->param(script_name => $script_name,
95 else => 1); # we show only the TMPL_VAR names $op
98 if ($op eq 'update_and_reedit') {
99 foreach ($input->param) {
100 warn "$_: ".$input->param($_)."\n";
103 if (my $currentorder=$input->param('currentorder')) {
104 my @currentorder=split /\|/, $currentorder;
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;
113 } elsif ($param=~m#down-(\d+).x#) {
114 my $temp=$currentorder[$1];
115 $currentorder[$1]=$currentorder[$1+1];
116 $currentorder[$1+1]=$temp;
121 $value=join ' ', @currentorder;
124 $template->param(script_name => $script_name,
125 $op => 1); # we show only the TMPL_VAR names $op
129 $template->param(script_name => $script_name,
130 else => 1); # we show only the TMPL_VAR names $op
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'));
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'));
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'));
152 ################## ADD_FORM ##################################
153 # called by default. Used to create form to add or modify a record
155 if ($op eq 'add_form') {
156 #---- if primkey exists, it's a modify action, so read values to modify...
159 my $dbh = C4::Context->dbh;
160 my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable='$searchfield'");
162 $data=$sth->fetchrow_hashref;
164 $template->param(modify => 1);
168 foreach my $option (split(/\|/, $data->{'options'})) {
170 $option eq $data->{'value'} and $selected=1;
171 push @options, { option => $option, selected => $selected };
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);
191 ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
193 my $currently_selected_themes;
195 foreach my $theme (split /\s+/, $data->{'value'}) {
196 push @options, { option => $theme, counter => $counter };
197 $currently_selected_themes->{$theme}=1;
200 foreach my $theme (getallthemes($type)) {
202 next if $currently_selected_themes->{$theme};
203 push @options, { option => $theme, counter => $counter };
206 } elsif ($data->{'type'} eq 'Languages') {
207 $template->param('type-reorderlist' => 1);
210 my $currently_selected_languages;
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;
218 foreach my $language (getalllanguages()) {
219 next if $language eq 'images';
221 next if $currently_selected_languages->{$language};
222 push @options, { option => $language, counter => $counter };
226 $template->param(explanation => $data->{'explanation'},
227 value => $data->{'value'},
228 type => $data->{'type'},
229 options => \@options,
230 searchfield => $searchfield);
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'));
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'));
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'));
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);
257 my $data=$sth->fetchrow_hashref;
259 $template->param(searchfield => $searchfield,
260 Tvalue => $data->{'value'},
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);
271 # END $OP eq DELETE_CONFIRMED
272 ################## DEFAULT ##################################
274 if ($searchfield ne '') {
275 $template->param(searchfield => "You Searched for <b>$searchfield<b><p>");
278 my ($count,$results)=StringSearch($env,$searchfield,'web');
281 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
282 if ($toggle eq 'white'){
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);
295 $template->param(loop => \@loop_data);
297 my $prevpage = $offset-$pagesize;
298 $template->param("<a href=$script_name?offset=".$prevpage.'<< Prev</a>');
300 if ($offset+$pagesize<$count) {
301 my $nextpage =$offset+$pagesize;
302 $template->param("a href=$script_name?offset=".$nextpage.'Next >></a>');
304 } #---- END $OP eq DEFAULT
306 output_html_with_http_headers $input, $cookie, $template->output;