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