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