* frameworks and itemtypes are independant
[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=100;
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=?,type=?,options=? where variable=?");
135                         $sth->execute($value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions'));
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'), $input->param('preftype'), $input->param('prefoptions'));
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 '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);
188                 my $type='';
189                 ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
190                 @options=();
191                 my $currently_selected_themes;
192                 my $counter=0;
193                 foreach my $theme (split /\s+/, $data->{'value'}) {
194                     push @options, { option => $theme, counter => $counter };
195                     $currently_selected_themes->{$theme}=1;
196                     $counter++;
197                 }
198                 foreach my $theme (getallthemes($type)) {
199                         my $selected='0';
200                         next if $currently_selected_themes->{$theme};
201                         push @options, { option => $theme, counter => $counter };
202                         $counter++;
203                 }
204         } elsif ($data->{'type'} eq 'Languages') {
205                 $template->param('type-choice' => 1);
206                 my $type='';
207                 @options=();
208                 my $currently_selected_languages;
209                 my $counter=0;
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;
214                     $counter++;
215                 }
216                 foreach my $language (getalllanguages()) {
217                         next if $language eq 'images';
218                         my $selected='0';
219                         next if $currently_selected_languages->{$language};
220                         push @options, { option => $language, counter => $counter };
221                         $counter++;
222                 }
223         } else {
224                 $template->param('type-free' => 1);
225                 $template->param('fieldlength' => $data->{'options'}>0?$data->{'options'}:60);
226         }
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);
234
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'));
241         if ($sth->rows) {
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'));
245                         $sth->finish;
246                 }
247         } else {
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'));
251                         $sth->finish;
252                 }
253         }
254         $sth->finish;
255         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl\"></html>";
256         exit;
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;
264         $sth->finish;
265         $template->param(searchfield => $searchfield,
266                                                         Tvalue => $data->{'value'},
267                                                         );
268
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);
276         $sth->finish;
277                                                                                                         # END $OP eq DELETE_CONFIRMED
278 ################## DEFAULT ##################################
279 } else { # DEFAULT
280         if  ($searchfield ne '') {
281                  $template->param(searchfield => "<p>You Searched for <strong>$searchfield</strong></p>");
282         }
283         my $env;
284         my ($count,$results)=StringSearch($env,$searchfield,'web');
285         my $toggle="white";
286         my @loop_data = ();
287         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
288                 if ($toggle eq 'white'){
289                         $toggle="#ffffcc";
290                 } else {
291                         $toggle="white";
292                 }
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&amp;searchfield=".$results->[$i]{'variable'};
298                 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'variable'};
299                 push(@loop_data, \%row_data);
300         }
301         $template->param(loop => \@loop_data);
302         if ($offset>0) {
303                 my $prevpage = $offset-$pagesize;
304                 $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
305         }
306         if ($offset+$pagesize<$count) {
307                 my $nextpage =$offset+$pagesize;
308                 $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
309         }
310 } #---- END $OP eq DEFAULT
311
312 output_html_with_http_headers $input, $cookie, $template->output;