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