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