adding debuglevel to admin tab
[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 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 =head1 systempreferences.pl
25
26 ALGO :
27  this script use an $op to know what to do.
28  if $op is empty or none of the above values,
29     - the default screen is build (with all records, or filtered datas).
30     - the   user can clic on add, modify or delete record.
31  if $op=add_form
32     - if primkey exists, this is a modification,so we read the $primkey record
33     - builds the add/modify form
34  if $op=add_validate
35     - the user has just send datas, so we create/modify the record
36  if $op=delete_form
37     - we show the record having primkey=$primkey and ask for deletion validation form
38  if $op=delete_confirm
39     - we delete the record having primkey=$primkey
40
41 =cut
42
43 use strict;
44 use CGI;
45 use C4::Auth;
46 use C4::Context;
47 use C4::Koha;
48 use C4::Languages;
49 use C4::Output;
50 use C4::Context;
51
52
53 # FIXME, shouldnt we store this stuff in the systempreferences table? 
54
55 my %tabsysprefs;
56 # Acquisitions
57     $tabsysprefs{acquisitions}="Acquisitions";
58     $tabsysprefs{gist}="Acquisitions";
59 # Admin
60     $tabsysprefs{dateformat}="Admin";
61     $tabsysprefs{delimiter}="Admin";
62     $tabsysprefs{IndependantBranches}="Admin";
63     $tabsysprefs{insecure}="Admin";
64     $tabsysprefs{KohaAdmin}="Admin";
65     $tabsysprefs{KohaAdminEmailAddress}="Admin";
66     $tabsysprefs{MIME}="Admin";
67     $tabsysprefs{timeout}="Admin";
68     $tabsysprefs{Intranet_includes}="Admin";
69     $tabsysprefs{AutoLocation}="Admin";
70     $tabsysprefs{DebugLevel}="Admin";
71
72 # Authorities
73     $tabsysprefs{authoritysep}="Authorities";
74     $tabsysprefs{AuthDisplayHierarchy}="Authorities";
75 # Catalogue
76     $tabsysprefs{advancedMARCEditor}="Catalogue";
77     $tabsysprefs{autoBarcode}="Catalogue";
78     $tabsysprefs{hide_marc}="Catalogue";
79     $tabsysprefs{IntranetBiblioDefaultView} = "Catalogue";
80     $tabsysprefs{ISBD}="Catalogue";
81     $tabsysprefs{itemcallnumber}="Catalogue";
82     $tabsysprefs{kohaspsuggest} = "Catalogue";
83     $tabsysprefs{LabelMARCView}="Catalogue";
84     $tabsysprefs{marc}="Catalogue";
85     $tabsysprefs{marcflavour}="Catalogue";
86     $tabsysprefs{serialsadditems}="Catalogue";
87     $tabsysprefs{sortbynonfiling}="Catalogue";
88     $tabsysprefs{MARCOrgCode}="Catalogue";
89     $tabsysprefs{z3950AuthorAuthFields}="Catalogue";
90     $tabsysprefs{z3950NormalizeAuthor}="Catalogue";
91     $tabsysprefs{Stemming}="Catalogue";
92     $tabsysprefs{WeightFields}="Catalogue";
93     $tabsysprefs{expandedSearchOption}="Catalogue";
94     $tabsysprefs{NoZebra}="Catalogue";
95     $tabsysprefs{NoZebraIndexes}="Catalogue";
96     $tabsysprefs{ReceiveBackIssues}="Catalogue";
97     
98 # Circulation
99     $tabsysprefs{maxoutstanding}="Circulation";
100     $tabsysprefs{maxreserves}="Circulation";
101     $tabsysprefs{noissuescharge}="Circulation";
102     $tabsysprefs{IssuingInProcess}="Circulation";
103     $tabsysprefs{patronimages}="Circulation";
104     $tabsysprefs{printcirculationslips}="Circulation";
105     $tabsysprefs{ReturnBeforeExpiry}="Circulation";
106     $tabsysprefs{SpecifyDueDate}="Circulation";
107     $tabsysprefs{AutomaticItemReturn}="Circulation";
108     $tabsysprefs{ReservesMaxPickUpDelay}="Circulation";
109     $tabsysprefs{TransfersMaxDaysWarning}="Circulation";
110     $tabsysprefs{useDaysMode}="Circulation";
111     $tabsysprefs{ReservesNeedReturns}="Circulation";
112
113 # Intranet
114     $tabsysprefs{TemplateEncoding}="Intranet";
115     $tabsysprefs{template}="Intranet";
116     $tabsysprefs{intranetstylesheet}="Intranet";
117     $tabsysprefs{IntranetNav}="Intranet";
118     $tabsysprefs{intranetcolorstylesheet}="Intranet";
119     $tabsysprefs{intranetuserjs}="Intranet";
120 # Members
121     $tabsysprefs{automembernum}="Members";
122     $tabsysprefs{checkdigit}="Members";
123     $tabsysprefs{intranetreadinghistory}="Members";
124     $tabsysprefs{NotifyBorrowerDeparture}="Members";
125     $tabsysprefs{memberofinstitution}="Members";
126     $tabsysprefs{ReadingHistory}="Members";
127     $tabsysprefs{BorrowerMandatoryField}="Members";
128     $tabsysprefs{borrowerRelationship}="Members";
129     $tabsysprefs{BorrowersTitles}="Members";    
130     $tabsysprefs{patronimages}="Members";
131     $tabsysprefs{MinPasswordLength}="Members";
132     $tabsysprefs{uppercasesurnames}="Members";
133
134 # OPAC
135     $tabsysprefs{AmazonAssocTag}="OPAC";
136     $tabsysprefs{AmazonContent}="OPAC";
137     $tabsysprefs{AmazonDevKey}="OPAC";
138     $tabsysprefs{BiblioDefaultView}="OPAC";
139     $tabsysprefs{LibraryName}="OPAC";
140     $tabsysprefs{opaccolorstylesheet}="OPAC";
141     $tabsysprefs{opaccredits}="OPAC";
142     $tabsysprefs{opaclanguages}="OPAC";
143     $tabsysprefs{opaclargeimage}="OPAC";
144     $tabsysprefs{opaclayoutstylesheet}="OPAC";
145     $tabsysprefs{OpacNav}="OPAC";
146     $tabsysprefs{opacsmallimage}="OPAC";
147     $tabsysprefs{opacstylesheet}="OPAC";
148     $tabsysprefs{opacthemes}="OPAC";
149     $tabsysprefs{opacuserjs}="OPAC";
150     $tabsysprefs{SubscriptionHistory}="OPAC";
151     $tabsysprefs{opacheader}="OPAC";
152     
153 # OPACFeatures
154     $tabsysprefs{Disable_Dictionary}="OPACFeatures";
155     $tabsysprefs{hidelostitems}="OPACFeatures";
156     $tabsysprefs{opacbookbag}="OPACFeatures";
157     $tabsysprefs{opaclanguagesdisplay}="OPACFeatures";
158     $tabsysprefs{OpacPasswordChange}="OPACFeatures";
159     $tabsysprefs{opacreadinghistory}="OPACFeatures";
160     $tabsysprefs{virtualshelves}="OPACFeatures";
161     $tabsysprefs{RequestOnOpac}="OPACFeatures";
162     $tabsysprefs{reviewson}="OPACFeatures";
163     $tabsysprefs{OpacTopissues}="OPACFeatures";
164     $tabsysprefs{OpacAuthorities}="OPACFeatures";
165     $tabsysprefs{OpacCloud}="OPACFeatures";
166     $tabsysprefs{opacuserlogin}="OPACFeatures";
167     $tabsysprefs{AnonSuggestions}="OPACFeatures";
168     $tabsysprefs{suggestion}="OPACFeatures";
169     $tabsysprefs{OpacTopissue}="OPACFeatures";
170     $tabsysprefs{OpacBrowser}="OPACFeatures";
171
172 # LOGFeatures
173     $tabsysprefs{CataloguingLog}  = "LOGFeatures";
174     $tabsysprefs{BorrowersLog}    = "LOGFeatures";
175     $tabsysprefs{SubscriptionLog} = "LOGFeatures";
176     $tabsysprefs{IssueLog}        = "LOGFeatures";
177     $tabsysprefs{ReturnLog}       = "LOGFeatures";
178     $tabsysprefs{LetterLog}       = "LOGFeatures";
179     $tabsysprefs{FinesLog}        = "LOGFeatures";
180     
181 sub StringSearch  {
182     my ($searchstring,$type)=@_;
183     my $dbh = C4::Context->dbh;
184     $searchstring=~ s/\'/\\\'/g;
185     my @data=split(' ',$searchstring);
186     my $count=@data;
187     my @results;
188     my $cnt=0;
189     if ($type){
190         foreach my $syspref (sort keys %tabsysprefs){
191             if ($tabsysprefs{$syspref} eq $type){
192                 my $sth=$dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
193                 $sth->execute($syspref);
194                 while (my $data=$sth->fetchrow_hashref){
195                     $data->{value} =~ s/</&lt;/g;
196                     $data->{value} =~ s/>/&gt;/g;
197                     $data->{value}=substr($data->{value},0,100)."..." if length($data->{value}) >100;
198                     push(@results,$data);
199                     $cnt++;
200                 }
201                 $sth->finish;
202             }
203         }
204     } else {
205         my $strsth ="Select variable,value,explanation,type,options from systempreferences where variable not in (";
206         foreach my $syspref (keys %tabsysprefs){
207             $strsth .= $dbh->quote($syspref).",";
208         }
209         $strsth =~ s/,$/) /;
210         $strsth .= " order by variable";
211         my $sth=$dbh->prepare($strsth);
212         $sth->execute();
213         while (my $data=$sth->fetchrow_hashref){
214             $data->{value}=substr($data->{value},0,100);
215             push(@results,$data);
216             $cnt++;
217         }
218         $sth->finish;
219     }
220     return ($cnt,\@results);
221 }
222
223 my $input = new CGI;
224 my $searchfield=$input->param('searchfield');
225 my $offset=$input->param('offset');
226 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
227
228 my ($template, $borrowernumber, $cookie)
229     = get_template_and_user({template_name => "admin/systempreferences.tmpl",
230                  query => $input,
231                  type => "intranet",
232                  authnotrequired => 0,
233                  flagsrequired => {parameters => 1},
234                  debug => 1,
235                  });
236 my $pagesize=100;
237 my $op = $input->param('op');
238 $searchfield=~ s/\,//g;
239
240 if ($op) {
241 $template->param(script_name => $script_name,
242                         $op              => 1); # we show only the TMPL_VAR names $op
243 } else {
244 $template->param(script_name => $script_name,
245                         else              => 1); # we show only the TMPL_VAR names $op
246 }
247
248 if ($op eq 'update_and_reedit') {
249     foreach ($input->param) {
250     }
251     my $value='';
252     if (my $currentorder=$input->param('currentorder')) {
253         my @currentorder=split /\|/, $currentorder;
254         my $orderchanged=0;
255         foreach my $param ($input->param) {
256             if ($param=~m#up-(\d+).x#) {
257                 my $temp=$currentorder[$1];
258                 $currentorder[$1]=$currentorder[$1-1];
259                 $currentorder[$1-1]=$temp;
260                 $orderchanged=1;
261                 last;
262             } elsif ($param=~m#down-(\d+).x#) {
263                 my $temp=$currentorder[$1];
264                 $currentorder[$1]=$currentorder[$1+1];
265                 $currentorder[$1+1]=$temp;
266                 $orderchanged=1;
267                 last;
268             }
269         }
270         $value=join ' ', @currentorder;
271         if ($orderchanged) {
272             $op='add_form';
273             $template->param(script_name => $script_name,
274                             $op              => 1); # we show only the TMPL_VAR names $op
275         } else {
276             $op='';
277             $searchfield='';
278             $template->param(script_name => $script_name,
279                             else              => 1); # we show only the TMPL_VAR names $op
280         }
281     }
282     my $dbh = C4::Context->dbh;
283     my $query="select * from systempreferences where variable=?";
284     my $sth=$dbh->prepare($query);
285     $sth->execute($input->param('variable'));
286     if ($sth->rows) {
287         unless (C4::Context->config('demo') eq 1) {
288             my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
289             $sth->execute($value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions'));
290             $sth->finish;
291         }
292     } else {
293         unless (C4::Context->config('demo') eq 1) {
294             my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
295             $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
296             $sth->finish;
297         }
298     }
299     $sth->finish;
300
301 }
302
303 ################## ADD_FORM ##################################
304 # called by default. Used to create form to add or  modify a record
305
306 if ($op eq 'add_form') {
307     #---- if primkey exists, it's a modify action, so read values to modify...
308     my $data;
309     if ($searchfield) {
310         my $dbh = C4::Context->dbh;
311         my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
312         $sth->execute($searchfield);
313         $data=$sth->fetchrow_hashref;
314         $sth->finish;
315         $template->param(modify => 1);
316     }
317
318     my @options;
319     foreach my $option (split(/\|/, $data->{'options'})) {
320         my $selected='0';
321         $option eq $data->{'value'} and $selected=1;
322         push @options, { option => $option, selected => $selected };
323     }
324     if ($data->{'type'} eq 'Choice') {
325         $template->param('type-choice' => 1);
326     } elsif ($data->{'type'} eq 'YesNo') {
327         $template->param('type-yesno' => 1);
328         $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
329         ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
330     } elsif ($data->{'type'} eq 'Integer') {
331         $template->param('type-free' => 1);
332         $template->param('fieldlength' => $data->{'options'});
333     } elsif ($data->{'type'} eq 'Textarea') {
334         $template->param('type-textarea' => 1);
335         $data->{options} =~ /(.*)\|(.*)/;
336         $template->param('cols' => $1, 'rows' => $2);;
337     } elsif ($data->{'type'} eq 'Float') {
338         $template->param('type-free' => 1);
339         $template->param('fieldlength' => $data->{'options'});
340     } elsif ($data->{'type'} eq 'Themes') {
341         $template->param('type-choice' => 1);
342         my $type='';
343         ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
344         @options=();
345         my $currently_selected_themes;
346         my $counter=0;
347         foreach my $theme (split /\s+/, $data->{'value'}) {
348             push @options, { option => $theme, counter => $counter };
349             $currently_selected_themes->{$theme}=1;
350             $counter++;
351         }
352         foreach my $theme (getallthemes($type)) {
353             my $selected='0';
354             next if $currently_selected_themes->{$theme};
355             push @options, { option => $theme, counter => $counter };
356             $counter++;
357         }
358     } elsif ($data->{'type'} eq 'Languages') {
359         $template->param('type-choice' => 1);
360         my $type='';
361         @options=();
362         my $currently_selected_languages;
363         my $counter=0;
364         foreach my $language (split /\s+/, $data->{'value'}) {
365             next if $language eq 'images';
366             push @options, { option => $language, counter => $counter };
367             $currently_selected_languages->{$language}=1;
368             $counter++;
369         }
370                 my $langavail = getTranslatedLanguages();
371         foreach my $language (@$langavail) {
372             my $selected='0';
373             next if $currently_selected_languages->{$language->{'language_code'}};
374                         #FIXME: could add language_name and language_locale_name for better display
375             push @options, { option => $language->{'language_code'}, counter => $counter };
376             $counter++;
377         }
378     } else {
379         $template->param('type-free' => 1);
380         $template->param('fieldlength' => $data->{'options'}>0?$data->{'options'}:60);
381     }
382     $template->param(explanation => $data->{'explanation'},
383              value => $data->{'value'},
384              type => $data->{'type'},
385              options => \@options,
386              preftype => $data->{'type'},
387              prefoptions => $data->{'options'},
388              searchfield => $searchfield);
389
390 ################## ADD_VALIDATE ##################################
391 # called by add_form, used to insert/modify data in DB
392 } elsif ($op eq 'add_validate') {
393     my $dbh = C4::Context->dbh;
394     my $sth=$dbh->prepare("select * from systempreferences where variable=?");
395     $sth->execute($input->param('variable'));
396     if ($sth->rows) {
397         unless (C4::Context->config('demo') eq 1) {
398             my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
399             $sth->execute($input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable'));
400             $sth->finish;
401         }
402     } else {
403         unless (C4::Context->config('demo') eq 1) {
404             my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
405             $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
406             $sth->finish;
407         }
408     }
409     $sth->finish;
410     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=".$tabsysprefs{$input->param('variable')}."\"></html>";
411     exit;
412 ################## DELETE_CONFIRM ##################################
413 # called by default form, used to confirm deletion of data in DB
414 } elsif ($op eq 'delete_confirm') {
415     my $dbh = C4::Context->dbh;
416     my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
417     $sth->execute($searchfield);
418     my $data=$sth->fetchrow_hashref;
419     $sth->finish;
420     $template->param(searchfield => $searchfield,
421                             Tvalue => $data->{'value'},
422                             );
423
424                                                     # END $OP eq DELETE_CONFIRM
425 ################## DELETE_CONFIRMED ##################################
426 # called by delete_confirm, used to effectively confirm deletion of data in DB
427 } elsif ($op eq 'delete_confirmed') {
428     my $dbh = C4::Context->dbh;
429     my $sth=$dbh->prepare("delete from systempreferences where variable=?");
430     $sth->execute($searchfield);
431     $sth->finish;
432                                                     # END $OP eq DELETE_CONFIRMED
433 ################## DEFAULT ##################################
434 } else { # DEFAULT
435     #Adding tab management for system preferences
436     my $tab=$input->param('tab');
437     
438     my ($count,$results)=StringSearch($searchfield,$tab);
439     my $toggle=0;
440     my @loop_data = ();
441     for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
442           if ($toggle eq 0){
443             $toggle=1;
444           } else {
445             $toggle=0;
446           }
447         my %row_data;  # get a fresh hash for the row data
448         $row_data{variable} = $results->[$i]{'variable'};
449         $row_data{value} = $results->[$i]{'value'};
450         $row_data{explanation} = $results->[$i]{'explanation'};
451         $row_data{toggle} = $toggle;
452         $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'variable'};
453         $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'variable'};
454         push(@loop_data, \%row_data);
455     }
456     $tab=($tab?$tab:"Others");
457     $template->param(loop => \@loop_data, $tab => 1);
458     if ($offset>0) {
459         my $prevpage = $offset-$pagesize;
460         $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
461     }
462     if ($offset+$pagesize<$count) {
463         my $nextpage =$offset+$pagesize;
464         $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
465     }
466     $template->param(        tab => $tab,
467             );
468 } #---- END $OP eq DEFAULT
469 output_html_with_http_headers $input, $cookie, $template->output;