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