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