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) {
102 if (my $currentorder=$input->param('currentorder')) {
103 my @currentorder=split /\|/, $currentorder;
105 foreach my $param ($input->param) {
106 if ($param=~m#up-(\d+).x#) {
107 my $temp=$currentorder[$1];
108 $currentorder[$1]=$currentorder[$1-1];
109 $currentorder[$1-1]=$temp;
112 } elsif ($param=~m#down-(\d+).x#) {
113 my $temp=$currentorder[$1];
114 $currentorder[$1]=$currentorder[$1+1];
115 $currentorder[$1+1]=$temp;
120 $value=join ' ', @currentorder;
123 $template->param(script_name => $script_name,
124 $op => 1); # we show only the TMPL_VAR names $op
128 $template->param(script_name => $script_name,
129 else => 1); # we show only the TMPL_VAR names $op
132 my $dbh = C4::Context->dbh;
133 my $query="select * from systempreferences where variable=?";
134 my $sth=$dbh->prepare($query);
135 $sth->execute($input->param('variable'));
137 unless (C4::Context->config('demo') eq 1) {
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'));
144 unless (C4::Context->config('demo') eq 1) {
145 my $query = "insert into systempreferences (variable,value,explanation) values (?,?,?)";
146 my $sth=$dbh->prepare($query);
147 $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'));
155 ################## ADD_FORM ##################################
156 # called by default. Used to create form to add or modify a record
158 if ($op eq 'add_form') {
159 #---- if primkey exists, it's a modify action, so read values to modify...
162 my $dbh = C4::Context->dbh;
163 my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable='$searchfield'");
165 $data=$sth->fetchrow_hashref;
167 $template->param(modify => 1);
171 foreach my $option (split(/\|/, $data->{'options'})) {
173 $option eq $data->{'value'} and $selected=1;
174 push @options, { option => $option, selected => $selected };
176 if ($data->{'type'} eq 'Choice') {
177 $template->param('type-choice' => 1);
178 } elsif ($data->{'type'} eq 'YesNo') {
179 $template->param('type-yesno' => 1);
180 $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
181 ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
182 } elsif ($data->{'type'} eq 'free') {
183 $template->param('type-free' => 1);
184 $template->param('fieldlength' => $data->{'options'});
185 } elsif ($data->{'type'} eq 'Integer') {
186 $template->param('type-free' => 1);
187 $template->param('fieldlength' => $data->{'options'});
188 } elsif ($data->{'type'} eq 'Float') {
189 $template->param('type-free' => 1);
190 $template->param('fieldlength' => $data->{'options'});
191 } elsif ($data->{'type'} eq 'Themes') {
192 $template->param('type-reorderlist' => 1);
194 ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
196 my $currently_selected_themes;
198 foreach my $theme (split /\s+/, $data->{'value'}) {
199 push @options, { option => $theme, counter => $counter };
200 $currently_selected_themes->{$theme}=1;
203 foreach my $theme (getallthemes($type)) {
205 next if $currently_selected_themes->{$theme};
206 push @options, { option => $theme, counter => $counter };
209 } elsif ($data->{'type'} eq 'Languages') {
210 $template->param('type-reorderlist' => 1);
213 my $currently_selected_languages;
215 foreach my $language (split /\s+/, $data->{'value'}) {
216 next if $language eq 'images';
217 push @options, { option => $language, counter => $counter };
218 $currently_selected_languages->{$language}=1;
221 foreach my $language (getalllanguages()) {
222 next if $language eq 'images';
224 next if $currently_selected_languages->{$language};
225 push @options, { option => $language, counter => $counter };
229 $template->param(explanation => $data->{'explanation'},
230 value => $data->{'value'},
231 type => $data->{'type'},
232 options => \@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 $query="select * from systempreferences where variable=?";
240 my $sth=$dbh->prepare($query);
241 $sth->execute($input->param('variable'));
243 unless (C4::Context->config('demo') eq 1) {
244 my $query = "update systempreferences set value=?,explanation=? where variable=?";
245 my $sth=$dbh->prepare($query);
246 $sth->execute($input->param('value'), $input->param('explanation'), $input->param('variable'));
250 unless (C4::Context->config('demo') eq 1) {
251 my $query = "insert into systempreferences (variable,value,explanation) values (?,?,?)";
252 my $sth=$dbh->prepare($query);
253 $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'));
258 ################## DELETE_CONFIRM ##################################
259 # called by default form, used to confirm deletion of data in DB
260 } elsif ($op eq 'delete_confirm') {
261 my $dbh = C4::Context->dbh;
262 my $sth=$dbh->prepare($reqsel);
264 my $data=$sth->fetchrow_hashref;
266 $template->param(searchfield => $searchfield,
267 Tvalue => $data->{'value'},
270 # END $OP eq DELETE_CONFIRM
271 ################## DELETE_CONFIRMED ##################################
272 # called by delete_confirm, used to effectively confirm deletion of data in DB
273 } elsif ($op eq 'delete_confirmed') {
274 my $dbh = C4::Context->dbh;
275 my $sth=$dbh->prepare($reqdel);
278 # END $OP eq DELETE_CONFIRMED
279 ################## DEFAULT ##################################
281 if ($searchfield ne '') {
282 $template->param(searchfield => "You Searched for <b>$searchfield<b><p>");
285 my ($count,$results)=StringSearch($env,$searchfield,'web');
288 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
289 if ($toggle eq 'white'){
294 my %row_data; # get a fresh hash for the row data
295 $row_data{variable} = $results->[$i]{'variable'};
296 $row_data{value} = $results->[$i]{'value'};
297 $row_data{explanation} = $results->[$i]{'explanation'};
298 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results->[$i]{'variable'};
299 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'variable'};
300 push(@loop_data, \%row_data);
302 $template->param(loop => \@loop_data);
304 my $prevpage = $offset-$pagesize;
305 $template->param("<a href=$script_name?offset=".$prevpage.'<< Prev</a>');
307 if ($offset+$pagesize<$count) {
308 my $nextpage =$offset+$pagesize;
309 $template->param("a href=$script_name?offset=".$nextpage.'Next >></a>');
311 } #---- END $OP eq DEFAULT
313 output_html_with_http_headers $input, $cookie, $template->output;