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