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