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