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