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