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