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