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