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