Make the page headings translatable while trying not to break old templates
[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 $sth=$dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
58         $sth->execute("$data[0]%");
59         my @results;
60         my $cnt=0;
61         while (my $data=$sth->fetchrow_hashref){
62                 push(@results,$data);
63                 $cnt ++;
64         }
65         $sth->finish;
66         return ($cnt,\@results);
67 }
68
69 my $input = new CGI;
70 my $searchfield=$input->param('searchfield');
71 my $offset=$input->param('offset');
72 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
73
74 my ($template, $borrowernumber, $cookie)
75     = get_template_and_user({template_name => "parameters/systempreferences.tmpl",
76                              query => $input,
77                              type => "intranet",
78                              authnotrequired => 0,
79                              flagsrequired => {parameters => 1},
80                              debug => 1,
81                              });
82 my $pagesize=20;
83 my $op = $input->param('op');
84 $searchfield=~ s/\,//g;
85
86 if ($op) {
87 $template->param(script_name => $script_name,
88                                                 $op              => 1); # we show only the TMPL_VAR names $op
89 } else {
90 $template->param(script_name => $script_name,
91                                                 else              => 1); # we show only the TMPL_VAR names $op
92 }
93
94 if ($op eq 'update_and_reedit') {
95         foreach ($input->param) {
96         }
97         my $value='';
98         if (my $currentorder=$input->param('currentorder')) {
99                 my @currentorder=split /\|/, $currentorder;
100                 my $orderchanged=0;
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;
106                                 $orderchanged=1;
107                                 last;
108                         } elsif ($param=~m#down-(\d+).x#) {
109                                 my $temp=$currentorder[$1];
110                                 $currentorder[$1]=$currentorder[$1+1];
111                                 $currentorder[$1+1]=$temp;
112                                 $orderchanged=1;
113                                 last;
114                         }
115                 }
116                 $value=join ' ', @currentorder;
117                 if ($orderchanged) {
118                         $op='add_form';
119                         $template->param(script_name => $script_name,
120                                                         $op              => 1); # we show only the TMPL_VAR names $op
121                 } else {
122                         $op='';
123                         $searchfield='';
124                         $template->param(script_name => $script_name,
125                                                         else              => 1); # we show only the TMPL_VAR names $op
126                 }
127         }
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'));
132         if ($sth->rows) {
133                 unless (C4::Context->config('demo') eq 1) {
134                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=? where variable=?");
135                         $sth->execute($value, $input->param('explanation'), $input->param('variable'));
136                         $sth->finish;
137                 }
138     } else {
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'));
142                         $sth->finish;
143                 }
144         }
145         $sth->finish;
146
147 }
148
149 ################## ADD_FORM ##################################
150 # called by default. Used to create form to add or  modify a record
151
152 if ($op eq 'add_form') {
153         #---- if primkey exists, it's a modify action, so read values to modify...
154         my $data;
155         if ($searchfield) {
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;
160                 $sth->finish;
161                 $template->param(modify => 1);
162         }
163
164         my @options;
165         foreach my $option (split(/\|/, $data->{'options'})) {
166                 my $selected='0';
167                 $option eq $data->{'value'} and $selected=1;
168                 push @options, { option => $option, selected => $selected };
169         }
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 'free') {
177                 $template->param('type-free' => 1);
178                 $template->param('fieldlength' => $data->{'options'});
179         } elsif ($data->{'type'} eq 'Integer') {
180                 $template->param('type-free' => 1);
181                 $template->param('fieldlength' => $data->{'options'});
182         } elsif ($data->{'type'} eq 'Float') {
183                 $template->param('type-free' => 1);
184                 $template->param('fieldlength' => $data->{'options'});
185         } elsif ($data->{'type'} eq 'Themes') {
186                 $template->param('type-reorderlist' => 1);
187                 my $type='';
188                 ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
189                 @options=();
190                 my $currently_selected_themes;
191                 my $counter=0;
192                 foreach my $theme (split /\s+/, $data->{'value'}) {
193                     push @options, { option => $theme, counter => $counter };
194                     $currently_selected_themes->{$theme}=1;
195                     $counter++;
196                 }
197                 foreach my $theme (getallthemes($type)) {
198                         my $selected='0';
199                         next if $currently_selected_themes->{$theme};
200                         push @options, { option => $theme, counter => $counter };
201                         $counter++;
202                 }
203         } elsif ($data->{'type'} eq 'Languages') {
204                 $template->param('type-reorderlist' => 1);
205                 my $type='';
206                 @options=();
207                 my $currently_selected_languages;
208                 my $counter=0;
209                 foreach my $language (split /\s+/, $data->{'value'}) {
210                     next if $language eq 'images';
211                     push @options, { option => $language, counter => $counter };
212                     $currently_selected_languages->{$language}=1;
213                     $counter++;
214                 }
215                 foreach my $language (getalllanguages()) {
216                         next if $language eq 'images';
217                         my $selected='0';
218                         next if $currently_selected_languages->{$language};
219                         push @options, { option => $language, counter => $counter };
220                         $counter++;
221                 }
222         }
223         $template->param(explanation => $data->{'explanation'},
224                          value => $data->{'value'},
225                          type => $data->{'type'},
226                          options => \@options,
227                          searchfield => $searchfield);
228
229 ################## ADD_VALIDATE ##################################
230 # called by add_form, used to insert/modify data in DB
231 } elsif ($op eq 'add_validate') {
232         my $dbh = C4::Context->dbh;
233         my $sth=$dbh->prepare("select * from systempreferences where variable=?");
234         $sth->execute($input->param('variable'));
235         if ($sth->rows) {
236                 unless (C4::Context->config('demo') eq 1) {
237                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=? where variable=?");
238                         $sth->execute($input->param('value'), $input->param('explanation'), $input->param('variable'));
239                         $sth->finish;
240                 }
241         } else {
242                 unless (C4::Context->config('demo') eq 1) {
243                         my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?)");
244                         $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'));
245                         $sth->finish;
246                 }
247         }
248         $sth->finish;
249 ################## DELETE_CONFIRM ##################################
250 # called by default form, used to confirm deletion of data in DB
251 } elsif ($op eq 'delete_confirm') {
252         my $dbh = C4::Context->dbh;
253         my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
254         $sth->execute($searchfield);
255         my $data=$sth->fetchrow_hashref;
256         $sth->finish;
257         $template->param(searchfield => $searchfield,
258                                                         Tvalue => $data->{'value'},
259                                                         );
260
261                                                                                                         # END $OP eq DELETE_CONFIRM
262 ################## DELETE_CONFIRMED ##################################
263 # called by delete_confirm, used to effectively confirm deletion of data in DB
264 } elsif ($op eq 'delete_confirmed') {
265         my $dbh = C4::Context->dbh;
266         my $sth=$dbh->prepare("delete from systempreferences where variable=?");
267         $sth->execute($searchfield);
268         $sth->finish;
269                                                                                                         # END $OP eq DELETE_CONFIRMED
270 ################## DEFAULT ##################################
271 } else { # DEFAULT
272         if  ($searchfield ne '') {
273                  $template->param(searchfield => "<p>You Searched for <strong>$searchfield</strong></p>");
274         }
275         my $env;
276         my ($count,$results)=StringSearch($env,$searchfield,'web');
277         my $toggle="white";
278         my @loop_data = ();
279         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
280                 if ($toggle eq 'white'){
281                         $toggle="#ffffcc";
282                 } else {
283                         $toggle="white";
284                 }
285                 my %row_data;  # get a fresh hash for the row data
286                 $row_data{variable} = $results->[$i]{'variable'};
287                 $row_data{value} = $results->[$i]{'value'};
288                 $row_data{explanation} = $results->[$i]{'explanation'};
289                 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'variable'};
290                 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'variable'};
291                 push(@loop_data, \%row_data);
292         }
293         $template->param(loop => \@loop_data);
294         if ($offset>0) {
295                 my $prevpage = $offset-$pagesize;
296                 $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
297         }
298         if ($offset+$pagesize<$count) {
299                 my $nextpage =$offset+$pagesize;
300                 $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
301         }
302 } #---- END $OP eq DEFAULT
303
304 output_html_with_http_headers $input, $cookie, $template->output;