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