Displaying system preferences by category
[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 my %tabsysprefs;
53 $tabsysprefs{acquisitions}="Acquisitions";
54 $tabsysprefs{gist}="Acquisitions";
55 $tabsysprefs{authoritysep}="Authorities";
56 $tabsysprefs{ISBD}="Catalogue";
57 $tabsysprefs{marc}="Catalogue";
58 $tabsysprefs{marcflavour}="Catalogue";
59 $tabsysprefs{SubscriptionHistory}="Catalogue";
60 $tabsysprefs{maxoutstanding}="Circulation";
61 $tabsysprefs{printcirculationslips}="Circulation";
62 $tabsysprefs{suggestion}="Circulation";
63 $tabsysprefs{automembernum}="Members";
64 $tabsysprefs{noissuescharge}="Circulation";
65 $tabsysprefs{opacthemes}="OPAC";
66 $tabsysprefs{opaclanguages}="OPAC";
67 $tabsysprefs{LibraryName}="OPAC";
68 $tabsysprefs{opacstylesheet}="OPAC";
69 $tabsysprefs{BiblioDefaultView}="OPAC";
70 $tabsysprefs{hidelostitem}="OPAC";
71 $tabsysprefs{KohaAdmin}="Admin";
72 $tabsysprefs{checkdigit}="Admin";
73 $tabsysprefs{dateformat}="Admin";
74 $tabsysprefs{insecure}="Admin";
75 $tabsysprefs{ldapinfos}="Admin";
76 $tabsysprefs{ldapserver}="Admin";
77
78 sub StringSearch  {
79         my ($env,$searchstring,$type)=@_;
80         my $dbh = C4::Context->dbh;
81         $searchstring=~ s/\'/\\\'/g;
82         my @data=split(' ',$searchstring);
83         my $count=@data;
84         my @results;
85         my $cnt=0;
86         if ($type){
87                 foreach my $syspref (sort keys %tabsysprefs){
88                         if ($tabsysprefs{$syspref} eq $type){
89                                 my $sth=$dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
90                                 $sth->execute($syspref);
91                                 while (my $data=$sth->fetchrow_hashref){
92                                         push(@results,$data);
93                                         $cnt ++;
94                                 }
95                                 $sth->finish;
96                         }
97                 }
98         }else {
99                 my $strsth ="Select variable,value,explanation,type,options from systempreferences where variable not in (";  
100                 foreach my $syspref (keys %tabsysprefs){
101                         $strsth .= $dbh->quote($syspref).",";
102                 }
103                 $strsth =~ s/,$/) /;
104                 $strsth .= " order by variable";
105                 warn $strsth;
106                 my $sth=$dbh->prepare($strsth);
107                 $sth->execute();
108                 while (my $data=$sth->fetchrow_hashref){
109                         push(@results,$data);
110                         $cnt ++;
111                 }
112                 $sth->finish;
113         }
114         return ($cnt,\@results);
115 }
116
117
118 my $input = new CGI;
119 my $searchfield=$input->param('searchfield');
120 my $offset=$input->param('offset');
121 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
122
123 my ($template, $borrowernumber, $cookie)
124     = get_template_and_user({template_name => "parameters/systempreferences.tmpl",
125                              query => $input,
126                              type => "intranet",
127                              authnotrequired => 0,
128                              flagsrequired => {parameters => 1},
129                              debug => 1,
130                              });
131 my $pagesize=100;
132 my $op = $input->param('op');
133 $searchfield=~ s/\,//g;
134
135 if ($op) {
136 $template->param(script_name => $script_name,
137                                                 $op              => 1); # we show only the TMPL_VAR names $op
138 } else {
139 $template->param(script_name => $script_name,
140                                                 else              => 1); # we show only the TMPL_VAR names $op
141 }
142
143 if ($op eq 'update_and_reedit') {
144         foreach ($input->param) {
145         }
146         my $value='';
147         if (my $currentorder=$input->param('currentorder')) {
148                 my @currentorder=split /\|/, $currentorder;
149                 my $orderchanged=0;
150                 foreach my $param ($input->param) {
151                         if ($param=~m#up-(\d+).x#) {
152                                 my $temp=$currentorder[$1];
153                                 $currentorder[$1]=$currentorder[$1-1];
154                                 $currentorder[$1-1]=$temp;
155                                 $orderchanged=1;
156                                 last;
157                         } elsif ($param=~m#down-(\d+).x#) {
158                                 my $temp=$currentorder[$1];
159                                 $currentorder[$1]=$currentorder[$1+1];
160                                 $currentorder[$1+1]=$temp;
161                                 $orderchanged=1;
162                                 last;
163                         }
164                 }
165                 $value=join ' ', @currentorder;
166                 if ($orderchanged) {
167                         $op='add_form';
168                         $template->param(script_name => $script_name,
169                                                         $op              => 1); # we show only the TMPL_VAR names $op
170                 } else {
171                         $op='';
172                         $searchfield='';
173                         $template->param(script_name => $script_name,
174                                                         else              => 1); # we show only the TMPL_VAR names $op
175                 }
176         }
177         my $dbh = C4::Context->dbh;
178         my $query="select * from systempreferences where variable=?";
179         my $sth=$dbh->prepare($query);
180         $sth->execute($input->param('variable'));
181         if ($sth->rows) {
182                 unless (C4::Context->config('demo') eq 1) {
183                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
184                         $sth->execute($value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions'));
185                         $sth->finish;
186                 }
187     } else {
188                 unless (C4::Context->config('demo') eq 1) {
189                         my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
190                         $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
191                         $sth->finish;
192                 }
193         }
194         $sth->finish;
195
196 }
197
198 ################## ADD_FORM ##################################
199 # called by default. Used to create form to add or  modify a record
200
201 if ($op eq 'add_form') {
202         #---- if primkey exists, it's a modify action, so read values to modify...
203         my $data;
204         if ($searchfield) {
205                 my $dbh = C4::Context->dbh;
206                 my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
207                 $sth->execute($searchfield);
208                 $data=$sth->fetchrow_hashref;
209                 $sth->finish;
210                 $template->param(modify => 1);
211         }
212
213         my @options;
214         foreach my $option (split(/\|/, $data->{'options'})) {
215                 my $selected='0';
216                 $option eq $data->{'value'} and $selected=1;
217                 push @options, { option => $option, selected => $selected };
218         }
219         if ($data->{'type'} eq 'Choice') {
220                 $template->param('type-choice' => 1);
221         } elsif ($data->{'type'} eq 'YesNo') {
222                 $template->param('type-yesno' => 1);
223                 $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
224                 ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
225         } elsif ($data->{'type'} eq 'Integer') {
226                 $template->param('type-free' => 1);
227                 $template->param('fieldlength' => $data->{'options'});
228         } elsif ($data->{'type'} eq 'Textarea') {
229                 $template->param('type-textarea' => 1);
230                 $data->{options} =~ /(.*)\|(.*)/;
231                 $template->param('cols' => $1, 'rows' => $2);;
232         } elsif ($data->{'type'} eq 'Float') {
233                 $template->param('type-free' => 1);
234                 $template->param('fieldlength' => $data->{'options'});
235         } elsif ($data->{'type'} eq 'Themes') {
236                 $template->param('type-choice' => 1);
237                 my $type='';
238                 ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
239                 @options=();
240                 my $currently_selected_themes;
241                 my $counter=0;
242                 foreach my $theme (split /\s+/, $data->{'value'}) {
243                     push @options, { option => $theme, counter => $counter };
244                     $currently_selected_themes->{$theme}=1;
245                     $counter++;
246                 }
247                 foreach my $theme (getallthemes($type)) {
248                         my $selected='0';
249                         next if $currently_selected_themes->{$theme};
250                         push @options, { option => $theme, counter => $counter };
251                         $counter++;
252                 }
253         } elsif ($data->{'type'} eq 'Languages') {
254                 $template->param('type-choice' => 1);
255                 my $type='';
256                 @options=();
257                 my $currently_selected_languages;
258                 my $counter=0;
259                 foreach my $language (split /\s+/, $data->{'value'}) {
260                     next if $language eq 'images';
261                     push @options, { option => $language, counter => $counter };
262                     $currently_selected_languages->{$language}=1;
263                     $counter++;
264                 }
265                 foreach my $language (getalllanguages()) {
266                         next if $language eq 'images';
267                         my $selected='0';
268                         next if $currently_selected_languages->{$language};
269                         push @options, { option => $language, counter => $counter };
270                         $counter++;
271                 }
272         } else {
273                 $template->param('type-free' => 1);
274                 $template->param('fieldlength' => $data->{'options'}>0?$data->{'options'}:60);
275         }
276         $template->param(explanation => $data->{'explanation'},
277                          value => $data->{'value'},
278                          type => $data->{'type'},
279                          options => \@options,
280                          preftype => $data->{'type'},
281                          prefoptions => $data->{'options'},
282                          searchfield => $searchfield);
283
284 ################## ADD_VALIDATE ##################################
285 # called by add_form, used to insert/modify data in DB
286 } elsif ($op eq 'add_validate') {
287         my $dbh = C4::Context->dbh;
288         my $sth=$dbh->prepare("select * from systempreferences where variable=?");
289         $sth->execute($input->param('variable'));
290         if ($sth->rows) {
291                 unless (C4::Context->config('demo') eq 1) {
292                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
293                         $sth->execute($input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable'));
294                         $sth->finish;
295                 }
296         } else {
297                 unless (C4::Context->config('demo') eq 1) {
298                         my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
299                         $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
300                         $sth->finish;
301                 }
302         }
303         $sth->finish;
304         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl\"></html>";
305         exit;
306 ################## DELETE_CONFIRM ##################################
307 # called by default form, used to confirm deletion of data in DB
308 } elsif ($op eq 'delete_confirm') {
309         my $dbh = C4::Context->dbh;
310         my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
311         $sth->execute($searchfield);
312         my $data=$sth->fetchrow_hashref;
313         $sth->finish;
314         $template->param(searchfield => $searchfield,
315                                                         Tvalue => $data->{'value'},
316                                                         );
317
318                                                                                                         # END $OP eq DELETE_CONFIRM
319 ################## DELETE_CONFIRMED ##################################
320 # called by delete_confirm, used to effectively confirm deletion of data in DB
321 } elsif ($op eq 'delete_confirmed') {
322         my $dbh = C4::Context->dbh;
323         my $sth=$dbh->prepare("delete from systempreferences where variable=?");
324         $sth->execute($searchfield);
325         $sth->finish;
326                                                                                                         # END $OP eq DELETE_CONFIRMED
327 ################## DEFAULT ##################################
328 } else { # DEFAULT
329         #Adding tab management for system preferences
330         my $tab=$input->param('tab');
331         
332         if  ($searchfield ne '') {
333                  $template->param(searchfield => "<p>You Searched for <strong>$searchfield</strong></p>");
334         }
335         my $env;
336         my ($count,$results)=StringSearch($env,$searchfield,$tab);
337         my $toggle=0;
338         my @loop_data = ();
339         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
340                 if ($toggle eq 0){
341                         $toggle=1;
342                 } else {
343                         $toggle=0;
344                 }
345                 my %row_data;  # get a fresh hash for the row data
346                 $row_data{variable} = $results->[$i]{'variable'};
347                 $row_data{value} = $results->[$i]{'value'};
348                 $row_data{explanation} = $results->[$i]{'explanation'};
349                 $row_data{toggle} = $toggle;
350                 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'variable'};
351                 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'variable'};
352                 push(@loop_data, \%row_data);
353         }
354         $template->param(loop => \@loop_data);
355         if ($offset>0) {
356                 my $prevpage = $offset-$pagesize;
357                 $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
358         }
359         if ($offset+$pagesize<$count) {
360                 my $nextpage =$offset+$pagesize;
361                 $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
362         }
363 } #---- END $OP eq DEFAULT
364
365 output_html_with_http_headers $input, $cookie, $template->output;