Adding id attributes to script-generated form fields to allow more flexible interface...
[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::Koha;
45 use C4::Output;
46 use C4::Interface::CGI::Output;
47 use C4::Search;
48 use HTML::Template;
49 use C4::Context;
50
51
52 sub StringSearch  {
53         my ($env,$searchstring,$type)=@_;
54         my $dbh = C4::Context->dbh;
55         $searchstring=~ s/\'/\\\'/g;
56         my @data=split(' ',$searchstring);
57         my $count=@data;
58         my $sth=$dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
59         $sth->execute("$data[0]%");
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 $offset=$input->param('offset');
73 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
74
75 my ($template, $borrowernumber, $cookie)
76     = get_template_and_user({template_name => "parameters/systempreferences.tmpl",
77                              query => $input,
78                              type => "intranet",
79                              authnotrequired => 0,
80                              flagsrequired => {parameters => 1},
81                              debug => 1,
82                              });
83 my $pagesize=100;
84 my $op = $input->param('op');
85 $searchfield=~ s/\,//g;
86
87 if ($op) {
88 $template->param(script_name => $script_name,
89                                                 $op              => 1); # we show only the TMPL_VAR names $op
90 } else {
91 $template->param(script_name => $script_name,
92                                                 else              => 1); # we show only the TMPL_VAR names $op
93 }
94
95 if ($op eq 'update_and_reedit') {
96         foreach ($input->param) {
97         }
98         my $value='';
99         if (my $currentorder=$input->param('currentorder')) {
100                 my @currentorder=split /\|/, $currentorder;
101                 my $orderchanged=0;
102                 foreach my $param ($input->param) {
103                         if ($param=~m#up-(\d+).x#) {
104                                 my $temp=$currentorder[$1];
105                                 $currentorder[$1]=$currentorder[$1-1];
106                                 $currentorder[$1-1]=$temp;
107                                 $orderchanged=1;
108                                 last;
109                         } elsif ($param=~m#down-(\d+).x#) {
110                                 my $temp=$currentorder[$1];
111                                 $currentorder[$1]=$currentorder[$1+1];
112                                 $currentorder[$1+1]=$temp;
113                                 $orderchanged=1;
114                                 last;
115                         }
116                 }
117                 $value=join ' ', @currentorder;
118                 if ($orderchanged) {
119                         $op='add_form';
120                         $template->param(script_name => $script_name,
121                                                         $op              => 1); # we show only the TMPL_VAR names $op
122                 } else {
123                         $op='';
124                         $searchfield='';
125                         $template->param(script_name => $script_name,
126                                                         else              => 1); # we show only the TMPL_VAR names $op
127                 }
128         }
129         my $dbh = C4::Context->dbh;
130         my $query="select * from systempreferences where variable=?";
131         my $sth=$dbh->prepare($query);
132         $sth->execute($input->param('variable'));
133         if ($sth->rows) {
134                 unless (C4::Context->config('demo') eq 1) {
135                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
136                         $sth->execute($value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions'));
137                         $sth->finish;
138                 }
139     } else {
140                 unless (C4::Context->config('demo') eq 1) {
141                         my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
142                         $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
143                         $sth->finish;
144                 }
145         }
146         $sth->finish;
147
148 }
149
150 ################## ADD_FORM ##################################
151 # called by default. Used to create form to add or  modify a record
152
153 if ($op eq 'add_form') {
154         #---- if primkey exists, it's a modify action, so read values to modify...
155         my $data;
156         if ($searchfield) {
157                 my $dbh = C4::Context->dbh;
158                 my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
159                 $sth->execute($searchfield);
160                 $data=$sth->fetchrow_hashref;
161                 $sth->finish;
162                 $template->param(modify => 1);
163         }
164
165         my @options;
166         foreach my $option (split(/\|/, $data->{'options'})) {
167                 my $selected='0';
168                 $option eq $data->{'value'} and $selected=1;
169                 push @options, { option => $option, selected => $selected };
170         }
171         if ($data->{'type'} eq 'Choice') {
172                 $template->param('type-choice' => 1);
173         } elsif ($data->{'type'} eq 'YesNo') {
174                 $template->param('type-yesno' => 1);
175                 $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
176                 ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
177         } elsif ($data->{'type'} eq 'Integer') {
178                 $template->param('type-free' => 1);
179                 $template->param('fieldlength' => $data->{'options'});
180         } elsif ($data->{'type'} eq 'Textarea') {
181                 $template->param('type-textarea' => 1);
182                 $data->{options} =~ /(.*)\|(.*)/;
183                 $template->param('cols' => $1, 'rows' => $2);;
184         } elsif ($data->{'type'} eq 'Float') {
185                 $template->param('type-free' => 1);
186                 $template->param('fieldlength' => $data->{'options'});
187         } elsif ($data->{'type'} eq 'Themes') {
188                 $template->param('type-choice' => 1);
189                 my $type='';
190                 ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
191                 @options=();
192                 my $currently_selected_themes;
193                 my $counter=0;
194                 foreach my $theme (split /\s+/, $data->{'value'}) {
195                     push @options, { option => $theme, counter => $counter };
196                     $currently_selected_themes->{$theme}=1;
197                     $counter++;
198                 }
199                 foreach my $theme (getallthemes($type)) {
200                         my $selected='0';
201                         next if $currently_selected_themes->{$theme};
202                         push @options, { option => $theme, counter => $counter };
203                         $counter++;
204                 }
205         } elsif ($data->{'type'} eq 'Languages') {
206                 $template->param('type-choice' => 1);
207                 my $type='';
208                 @options=();
209                 my $currently_selected_languages;
210                 my $counter=0;
211                 foreach my $language (split /\s+/, $data->{'value'}) {
212                     next if $language eq 'images';
213                     push @options, { option => $language, counter => $counter };
214                     $currently_selected_languages->{$language}=1;
215                     $counter++;
216                 }
217                 foreach my $language (getalllanguages()) {
218                         next if $language eq 'images';
219                         my $selected='0';
220                         next if $currently_selected_languages->{$language};
221                         push @options, { option => $language, counter => $counter };
222                         $counter++;
223                 }
224         } else {
225                 $template->param('type-free' => 1);
226                 $template->param('fieldlength' => $data->{'options'}>0?$data->{'options'}:60);
227         }
228         $template->param(explanation => $data->{'explanation'},
229                          value => $data->{'value'},
230                          type => $data->{'type'},
231                          options => \@options,
232                          preftype => $data->{'type'},
233                          prefoptions => $data->{'options'},
234                          searchfield => $searchfield);
235
236 ################## ADD_VALIDATE ##################################
237 # called by add_form, used to insert/modify data in DB
238 } elsif ($op eq 'add_validate') {
239         my $dbh = C4::Context->dbh;
240         my $sth=$dbh->prepare("select * from systempreferences where variable=?");
241         $sth->execute($input->param('variable'));
242         if ($sth->rows) {
243                 unless (C4::Context->config('demo') eq 1) {
244                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
245                         $sth->execute($input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable'));
246                         $sth->finish;
247                 }
248         } else {
249                 unless (C4::Context->config('demo') eq 1) {
250                         my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
251                         $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
252                         $sth->finish;
253                 }
254         }
255         $sth->finish;
256         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl\"></html>";
257         exit;
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("select variable,value,explanation,type,options from systempreferences where variable=?");
263         $sth->execute($searchfield);
264         my $data=$sth->fetchrow_hashref;
265         $sth->finish;
266         $template->param(searchfield => $searchfield,
267                                                         Tvalue => $data->{'value'},
268                                                         );
269
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("delete from systempreferences where variable=?");
276         $sth->execute($searchfield);
277         $sth->finish;
278                                                                                                         # END $OP eq DELETE_CONFIRMED
279 ################## DEFAULT ##################################
280 } else { # DEFAULT
281         if  ($searchfield ne '') {
282                  $template->param(searchfield => "<p>You Searched for <strong>$searchfield</strong></p>");
283         }
284         my $env;
285         my ($count,$results)=StringSearch($env,$searchfield,'web');
286         my $toggle=0;
287         my @loop_data = ();
288         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
289                 if ($toggle eq 0){
290                         $toggle=1;
291                 } else {
292                         $toggle=0;
293                 }
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{toggle} = $toggle;
299                 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'variable'};
300                 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'variable'};
301                 push(@loop_data, \%row_data);
302         }
303         $template->param(loop => \@loop_data);
304         if ($offset>0) {
305                 my $prevpage = $offset-$pagesize;
306                 $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
307         }
308         if ($offset+$pagesize<$count) {
309                 my $nextpage =$offset+$pagesize;
310                 $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
311         }
312 } #---- END $OP eq DEFAULT
313
314 output_html_with_http_headers $input, $cookie, $template->output;