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 $sth=$dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
58 $sth->execute("$data[0]%");
61 while (my $data=$sth->fetchrow_hashref){
66 return ($cnt,\@results);
70 my $searchfield=$input->param('searchfield');
71 my $offset=$input->param('offset');
72 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
74 my ($template, $borrowernumber, $cookie)
75 = get_template_and_user({template_name => "parameters/systempreferences.tmpl",
79 flagsrequired => {parameters => 1},
83 my $op = $input->param('op');
84 $searchfield=~ s/\,//g;
87 $template->param(script_name => $script_name,
88 $op => 1); # we show only the TMPL_VAR names $op
90 $template->param(script_name => $script_name,
91 else => 1); # we show only the TMPL_VAR names $op
94 if ($op eq 'update_and_reedit') {
95 foreach ($input->param) {
98 if (my $currentorder=$input->param('currentorder')) {
99 my @currentorder=split /\|/, $currentorder;
101 foreach my $param ($input->param) {
102 if ($param=~m#up-(\d+).x#) {
103 my $temp=$currentorder[$1];
104 $currentorder[$1]=$currentorder[$1-1];
105 $currentorder[$1-1]=$temp;
108 } elsif ($param=~m#down-(\d+).x#) {
109 my $temp=$currentorder[$1];
110 $currentorder[$1]=$currentorder[$1+1];
111 $currentorder[$1+1]=$temp;
116 $value=join ' ', @currentorder;
119 $template->param(script_name => $script_name,
120 $op => 1); # we show only the TMPL_VAR names $op
124 $template->param(script_name => $script_name,
125 else => 1); # we show only the TMPL_VAR names $op
128 my $dbh = C4::Context->dbh;
129 my $query="select * from systempreferences where variable=?";
130 my $sth=$dbh->prepare($query);
131 $sth->execute($input->param('variable'));
133 unless (C4::Context->config('demo') eq 1) {
134 my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
135 $sth->execute($value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions'));
139 unless (C4::Context->config('demo') eq 1) {
140 my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
141 $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
149 ################## ADD_FORM ##################################
150 # called by default. Used to create form to add or modify a record
152 if ($op eq 'add_form') {
153 #---- if primkey exists, it's a modify action, so read values to modify...
156 my $dbh = C4::Context->dbh;
157 my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
158 $sth->execute($searchfield);
159 $data=$sth->fetchrow_hashref;
161 $template->param(modify => 1);
165 foreach my $option (split(/\|/, $data->{'options'})) {
167 $option eq $data->{'value'} and $selected=1;
168 push @options, { option => $option, selected => $selected };
170 if ($data->{'type'} eq 'Choice') {
171 $template->param('type-choice' => 1);
172 } elsif ($data->{'type'} eq 'YesNo') {
173 $template->param('type-yesno' => 1);
174 $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
175 ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
176 } elsif ($data->{'type'} eq 'Integer') {
177 $template->param('type-free' => 1);
178 $template->param('fieldlength' => $data->{'options'});
179 } elsif ($data->{'type'} eq 'Textarea') {
180 $template->param('type-textarea' => 1);
181 $data->{options} =~ /(.*)\|(.*)/;
182 $template->param('cols' => $1, 'rows' => $2);;
183 } elsif ($data->{'type'} eq 'Float') {
184 $template->param('type-free' => 1);
185 $template->param('fieldlength' => $data->{'options'});
186 } elsif ($data->{'type'} eq 'Themes') {
187 $template->param('type-choice' => 1);
189 ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
191 my $currently_selected_themes;
193 foreach my $theme (split /\s+/, $data->{'value'}) {
194 push @options, { option => $theme, counter => $counter };
195 $currently_selected_themes->{$theme}=1;
198 foreach my $theme (getallthemes($type)) {
200 next if $currently_selected_themes->{$theme};
201 push @options, { option => $theme, counter => $counter };
204 } elsif ($data->{'type'} eq 'Languages') {
205 $template->param('type-choice' => 1);
208 my $currently_selected_languages;
210 foreach my $language (split /\s+/, $data->{'value'}) {
211 next if $language eq 'images';
212 push @options, { option => $language, counter => $counter };
213 $currently_selected_languages->{$language}=1;
216 foreach my $language (getalllanguages()) {
217 next if $language eq 'images';
219 next if $currently_selected_languages->{$language};
220 push @options, { option => $language, counter => $counter };
224 $template->param('type-free' => 1);
225 $template->param('fieldlength' => $data->{'options'}>0?$data->{'options'}:60);
227 $template->param(explanation => $data->{'explanation'},
228 value => $data->{'value'},
229 type => $data->{'type'},
230 options => \@options,
231 preftype => $data->{'type'},
232 prefoptions => $data->{'options'},
233 searchfield => $searchfield);
235 ################## ADD_VALIDATE ##################################
236 # called by add_form, used to insert/modify data in DB
237 } elsif ($op eq 'add_validate') {
238 my $dbh = C4::Context->dbh;
239 my $sth=$dbh->prepare("select * from systempreferences where variable=?");
240 $sth->execute($input->param('variable'));
242 unless (C4::Context->config('demo') eq 1) {
243 my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
244 $sth->execute($input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable'));
248 unless (C4::Context->config('demo') eq 1) {
249 my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?)");
250 $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
255 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl\"></html>";
257 ################## DELETE_CONFIRM ##################################
258 # called by default form, used to confirm deletion of data in DB
259 } elsif ($op eq 'delete_confirm') {
260 my $dbh = C4::Context->dbh;
261 my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
262 $sth->execute($searchfield);
263 my $data=$sth->fetchrow_hashref;
265 $template->param(searchfield => $searchfield,
266 Tvalue => $data->{'value'},
269 # END $OP eq DELETE_CONFIRM
270 ################## DELETE_CONFIRMED ##################################
271 # called by delete_confirm, used to effectively confirm deletion of data in DB
272 } elsif ($op eq 'delete_confirmed') {
273 my $dbh = C4::Context->dbh;
274 my $sth=$dbh->prepare("delete from systempreferences where variable=?");
275 $sth->execute($searchfield);
277 # END $OP eq DELETE_CONFIRMED
278 ################## DEFAULT ##################################
280 if ($searchfield ne '') {
281 $template->param(searchfield => "<p>You Searched for <strong>$searchfield</strong></p>");
284 my ($count,$results)=StringSearch($env,$searchfield,'web');
287 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
288 if ($toggle eq 'white'){
293 my %row_data; # get a fresh hash for the row data
294 $row_data{variable} = $results->[$i]{'variable'};
295 $row_data{value} = $results->[$i]{'value'};
296 $row_data{explanation} = $results->[$i]{'explanation'};
297 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results->[$i]{'variable'};
298 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'variable'};
299 push(@loop_data, \%row_data);
301 $template->param(loop => \@loop_data);
303 my $prevpage = $offset-$pagesize;
304 $template->param("<a href=$script_name?offset=".$prevpage.'<< Prev</a>');
306 if ($offset+$pagesize<$count) {
307 my $nextpage =$offset+$pagesize;
308 $template->param("a href=$script_name?offset=".$nextpage.'Next >></a>');
310 } #---- END $OP eq DEFAULT
312 output_html_with_http_headers $input, $cookie, $template->output;