fix for bug 2255: Sys Prefs need to be moved from Local Use
[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 qw(getTranslatedLanguages);
49 use C4::ClassSource;
50 use C4::Log;
51 use C4::Output;
52
53 # use Smart::Comments;
54
55
56 # FIXME, shouldnt we store this stuff in the systempreferences table? 
57
58 # FIXME: This uses hash in a backwards way.  What we really want is:
59 #               $tabsysprefs{key} = $array_ref;
60 #                               like
61 #               $tabsysprefs{Cataloguing} = [qw(autoBarcode ISBD marc ...)];
62 #
63 #       Because some things *should* be on more than one tab.
64 #       And the tabname is the unique part (the key).
65
66 my %tabsysprefs;
67 # Acquisitions
68     $tabsysprefs{acquisitions}="Acquisitions";
69     $tabsysprefs{gist}="Acquisitions";
70     $tabsysprefs{emailPurchaseSuggestions}="Acquisitions";
71         $tabsysprefs{RenewSerialAddsSuggestion}="Acquisitions";
72     $tabsysprefs{serialsadditems}="Acquisitions";
73
74 # Admin
75     $tabsysprefs{singleBranchMode}="Admin";
76     $tabsysprefs{staffClientBaseURL}="Admin";
77     $tabsysprefs{Version}="Admin";
78     $tabsysprefs{OpacMaintenance}="Admin";
79     $tabsysprefs{FrameworksLoaded}="Admin";
80     $tabsysprefs{libraryAddress}="Admin";
81     $tabsysprefs{delimiter}="Admin";
82     $tabsysprefs{IndependantBranches}="Admin";
83     $tabsysprefs{insecure}="Admin";
84     $tabsysprefs{KohaAdmin}="Admin";
85     $tabsysprefs{KohaAdminEmailAddress}="Admin";
86     $tabsysprefs{MIME}="Admin";
87     $tabsysprefs{timeout}="Admin";
88     $tabsysprefs{Intranet_includes}="Admin";
89     $tabsysprefs{AutoLocation}="Admin";
90     $tabsysprefs{DebugLevel}="Admin";
91     $tabsysprefs{SessionStorage}="Admin";
92     $tabsysprefs{noItemTypeImages}="Admin";
93     $tabsysprefs{OPACBaseURL}="Admin";
94     $tabsysprefs{GranularPermissions}="Admin";
95
96 # Authorities
97     $tabsysprefs{authoritysep}="Authorities";
98     $tabsysprefs{AuthDisplayHierarchy}="Authorities";
99     $tabsysprefs{dontmerge}="Authorities";
100     $tabsysprefs{BiblioAddsAuthorities}="Authorities";
101 # Cataloguing
102     $tabsysprefs{advancedMARCEditor}="Cataloguing";
103     $tabsysprefs{autoBarcode}="Cataloguing";
104     $tabsysprefs{hide_marc}="Cataloguing";
105     $tabsysprefs{IntranetBiblioDefaultView} = "Cataloguing";
106     $tabsysprefs{ISBD}="Cataloguing";
107     $tabsysprefs{itemcallnumber}="Cataloguing";
108     $tabsysprefs{LabelMARCView}="Cataloguing";
109     $tabsysprefs{marc}="Cataloguing";
110     $tabsysprefs{marcflavour}="Cataloguing";
111     $tabsysprefs{MARCOrgCode}="Cataloguing";
112     $tabsysprefs{z3950AuthorAuthFields}="Cataloguing";
113     $tabsysprefs{z3950NormalizeAuthor}="Cataloguing";
114     $tabsysprefs{Stemming}="Cataloguing";
115     $tabsysprefs{WeightFields}="Cataloguing";
116     $tabsysprefs{NoZebra}="Cataloguing";
117     $tabsysprefs{NoZebraIndexes}="Cataloguing";
118     $tabsysprefs{ReceiveBackIssues}="Cataloguing";
119     $tabsysprefs{DefaultClassificationSource}="Cataloguing";
120     $tabsysprefs{RoutingSerials}="Cataloguing";
121     $tabsysprefs{'item-level_itypes'}="Cataloguing";
122
123 # Circulation
124     $tabsysprefs{maxoutstanding}="Circulation";
125     $tabsysprefs{maxreserves}="Circulation";
126     $tabsysprefs{noissuescharge}="Circulation";
127     $tabsysprefs{IssuingInProcess}="Circulation";
128     $tabsysprefs{patronimages}="Circulation";
129     $tabsysprefs{printcirculationslips}="Circulation";
130     $tabsysprefs{ReturnBeforeExpiry}="Circulation";
131     $tabsysprefs{SpecifyDueDate}="Circulation";
132     $tabsysprefs{AutomaticItemReturn}="Circulation";
133     $tabsysprefs{ReservesMaxPickUpDelay}="Circulation";
134     $tabsysprefs{TransfersMaxDaysWarning}="Circulation";
135     $tabsysprefs{useDaysMode}="Circulation";
136     $tabsysprefs{ReservesNeedReturns}="Circulation";
137     $tabsysprefs{CircAutocompl}="Circulation";
138     $tabsysprefs{canreservefromotherbranches}="Circulation";
139     $tabsysprefs{finesMode}="Circulation";
140     $tabsysprefs{emailLibrarianWhenHoldIsPlaced}="Circulation";
141     $tabsysprefs{globalDueDate}="Circulation";
142     $tabsysprefs{holdCancelLength}="Circulation";
143     $tabsysprefs{itemBarcodeInputFilter}="Circulation";
144     $tabsysprefs{noOPACHolds}="Circulation";
145     $tabsysprefs{WebBasedSelfCheck}="Circulation";
146     $tabsysprefs{CircControl}="Circulation";
147     $tabsysprefs{finesCalendar}="Circulation";
148     $tabsysprefs{previousIssuesDefaultSortOrder}="Circulation";
149     $tabsysprefs{todaysIssuesDefaultSortOrder}="Circulation";
150     $tabsysprefs{HomeOrHoldingBranch}="Circulation";
151         $tabsysprefs{RandomizeHoldsQueueWeight}="Circulation";
152         $tabsysprefs{StaticHoldsQueueWeight}="Circulation";
153     $tabsysprefs{AllowOnShelfHolds}="Circulation";
154     $tabsysprefs{AllowHoldsOnDamagedItems}="Circulation";
155
156 # Staff Client
157     $tabsysprefs{TemplateEncoding}="StaffClient";
158     $tabsysprefs{template}="StaffClient";
159     $tabsysprefs{intranetstylesheet}="StaffClient";
160     $tabsysprefs{IntranetNav}="StaffClient";
161     $tabsysprefs{intranetcolorstylesheet}="StaffClient";
162     $tabsysprefs{intranetuserjs}="StaffClient";
163     $tabsysprefs{yuipath}="StaffClient";
164     $tabsysprefs{IntranetmainUserblock}="StaffClient";
165     
166 # Patrons
167     $tabsysprefs{autoMemberNum}="Patrons";
168     $tabsysprefs{checkdigit}="Patrons";
169     $tabsysprefs{intranetreadinghistory}="Patrons";
170     $tabsysprefs{NotifyBorrowerDeparture}="Patrons";
171     $tabsysprefs{memberofinstitution}="Patrons";
172     $tabsysprefs{ReadingHistory}="Patrons";
173     $tabsysprefs{BorrowerMandatoryField}="Patrons";
174     $tabsysprefs{borrowerRelationship}="Patrons";
175     $tabsysprefs{BorrowersTitles}="Patrons";    
176     $tabsysprefs{patronimages}="Patrons";
177     $tabsysprefs{MinPasswordLength}="Patrons";
178     $tabsysprefs{uppercasesurnames}="Patrons";
179     $tabsysprefs{NoReturnSetLost}="Patrons";
180     $tabsysprefs{MaxFine}="Patrons";
181     $tabsysprefs{NotifyBorrowerDeparture}="Patrons";
182     $tabsysprefs{AddPatronLists}="Patrons";
183     $tabsysprefs{PatronsPerPage}="Patrons";
184     $tabsysprefs{ExtendedPatronAttributes}="Patrons";
185     $tabsysprefs{AutoEmailOpacUser}="Patrons";
186     $tabsysprefs{AutoEmailPrimaryAddress}="Patrons";
187     $tabsysprefs{EnhancedMessagingPreferences}="Patrons";
188
189 # I18N/L10N
190     $tabsysprefs{dateformat}="I18N/L10N";
191     $tabsysprefs{opaclanguages}="I18N/L10N";
192     $tabsysprefs{opacthemes}="I18N/L10N";
193     $tabsysprefs{language}="I18N/L10N";
194
195 # Searching
196     $tabsysprefs{defaultSortField}="Searching";
197     $tabsysprefs{defaultSortOrder}="Searching";
198     $tabsysprefs{maxItemsInSearchResults}="Searching";
199     $tabsysprefs{numSearchResults}="Searching";
200     $tabsysprefs{OPACdefaultSortField}="Searching";
201     $tabsysprefs{OPACdefaultSortOrder}="Searching";
202     $tabsysprefs{OPACItemsResultsDisplay}="Searching";
203     $tabsysprefs{OPACnumSearchResults}="Searching";
204     $tabsysprefs{QueryFuzzy}="Searching";
205     $tabsysprefs{QueryStemming}="Searching";
206     $tabsysprefs{QueryWeightFields}="Searching";
207     $tabsysprefs{expandedSearchOption}="Searching";
208     $tabsysprefs{sortbynonfiling}="Searching";
209     $tabsysprefs{QueryAutoTruncate}="Searching";
210     $tabsysprefs{QueryRemoveStopwords}="Searching";
211     $tabsysprefs{AdvancedSearchTypes}="Searching";
212
213 # EnhancedContent
214    $tabsysprefs{AmazonContent}="EnhancedContent";
215    $tabsysprefs{AWSAccessKeyID}="EnhancedContent";
216    $tabsysprefs{AmazonLocale}="EnhancedContent";
217    $tabsysprefs{AmazonAssocTag}="EnhancedContent";
218    $tabsysprefs{AmazonSimilarItems}="EnhancedContent";
219    $tabsysprefs{OPACAmazonContent}="EnhancedContent";
220    $tabsysprefs{OPACAmazonSimilarItems}="EnhancedContent";
221
222 # Baker & Taylor 
223    $tabsysprefs{BakerTaylorBookstoreURL} = 'EnhancedContent';
224    $tabsysprefs{BakerTaylorEnabled}      = 'EnhancedContent';
225    $tabsysprefs{BakerTaylorPassword}     = 'EnhancedContent';
226    $tabsysprefs{BakerTaylorUsername}     = 'EnhancedContent';
227 # FRBR    
228    $tabsysprefs{FRBRizeEditions}="EnhancedContent";
229    $tabsysprefs{XISBN}="EnhancedContent";
230    $tabsysprefs{OCLCAffiliateID}="EnhancedContent";
231    $tabsysprefs{XISBNDailyLimit}="EnhancedContent";
232    $tabsysprefs{PINESISBN}="EnhancedContent";
233    $tabsysprefs{ThingISBN}="EnhancedContent";
234    $tabsysprefs{OPACFRBRizeEditions}="EnhancedContent";
235 # Tags
236    $tabsysprefs{TagsEnabled}            = 'EnhancedContent';
237    $tabsysprefs{TagsExternalDictionary} = 'EnhancedContent';
238    $tabsysprefs{TagsInputOnDetail}      = 'EnhancedContent';
239    $tabsysprefs{TagsInputOnList}        = 'EnhancedContent';
240    $tabsysprefs{TagsShowOnDetail}       = 'EnhancedContent';
241    $tabsysprefs{TagsShowOnList}         = 'EnhancedContent';
242    $tabsysprefs{TagsModeration}         = 'EnhancedContent';
243    $tabsysprefs{GoogleJackets}                  = 'EnhancedContent';
244    $tabsysprefs{AuthorisedValueImages}  = "EnhancedContent";
245
246 # OPAC
247     $tabsysprefs{BiblioDefaultView}="OPAC";
248     $tabsysprefs{LibraryName}="OPAC";
249     $tabsysprefs{opaccolorstylesheet}="OPAC";
250     $tabsysprefs{opaccredits}="OPAC";
251     $tabsysprefs{opaclargeimage}="OPAC";
252     $tabsysprefs{opaclayoutstylesheet}="OPAC";
253     $tabsysprefs{OpacNav}="OPAC";
254     $tabsysprefs{opacsmallimage}="OPAC";
255     $tabsysprefs{opacstylesheet}="OPAC";
256     $tabsysprefs{opacthemes}="OPAC";
257     $tabsysprefs{opacuserjs}="OPAC";
258     $tabsysprefs{SubscriptionHistory}="OPAC";
259     $tabsysprefs{opacheader}="OPAC";
260     $tabsysprefs{noOPACUserLogin}="OPAC";
261     $tabsysprefs{hideBiblioNumber}="OPAC";
262     $tabsysprefs{OPACDisplayExtendedSubInfo}="OPAC";
263     $tabsysprefs{OpacMainUserBlock}="OPAC";
264     $tabsysprefs{OPACSubscriptionDisplay}="OPAC";
265     $tabsysprefs{OPACURLOpenInNewWindow}="OPAC";
266     $tabsysprefs{OPACUserCSS}="OPAC";
267     $tabsysprefs{OPACHighlightedWords}="OPAC";
268     $tabsysprefs{OPACViewOthersSuggestions}="OPAC";
269     $tabsysprefs{URLLinkText}="OPAC";
270     $tabsysprefs{OPACShelfBrowser}="OPAC";
271
272 # OPAC
273     $tabsysprefs{SearchMyLibraryFirst}="OPAC";
274     $tabsysprefs{Disable_Dictionary}="OPAC";
275     $tabsysprefs{hidelostitems}="OPAC";
276     $tabsysprefs{opacbookbag}="OPAC";
277     $tabsysprefs{opaclanguagesdisplay}="OPAC";
278     $tabsysprefs{OpacPasswordChange}="OPAC";
279     $tabsysprefs{opacreadinghistory}="OPAC";
280     $tabsysprefs{virtualshelves}="OPAC";
281     $tabsysprefs{RequestOnOpac}="OPAC";
282     $tabsysprefs{reviewson}="OPAC";
283     $tabsysprefs{OpacTopissues}="OPAC";
284     $tabsysprefs{OpacAuthorities}="OPAC";
285     $tabsysprefs{OpacCloud}="OPAC";
286     $tabsysprefs{opacuserlogin}="OPAC";
287     $tabsysprefs{AnonSuggestions}="OPAC";
288     $tabsysprefs{suggestion}="OPAC";
289     $tabsysprefs{OpacTopissue}="OPAC";
290     $tabsysprefs{OpacBrowser}="OPAC";
291     $tabsysprefs{kohaspsuggest} = "OPAC";
292     $tabsysprefs{OpacRenewalAllowed} = "OPAC";
293     $tabsysprefs{OPACItemHolds} = "OPAC";
294     $tabsysprefs{OPACGroupResults}="OPAC";
295     $tabsysprefs{XSLTDetailsDisplay}="OPAC";
296     $tabsysprefs{XSLTResultsDisplay}="OPAC";
297
298 # LOGFeatures
299     $tabsysprefs{CataloguingLog}  = "Logs";
300     $tabsysprefs{BorrowersLog}    = "Logs";
301     $tabsysprefs{SubscriptionLog} = "Logs";
302     $tabsysprefs{IssueLog}        = "Logs";
303     $tabsysprefs{ReturnLog}       = "Logs";
304     $tabsysprefs{LetterLog}       = "Logs";
305     $tabsysprefs{FinesLog}        = "Logs";
306     
307 # OAI-PMH variables
308    $tabsysprefs{'OAI-PMH'}           = "OAI-PMH";
309    $tabsysprefs{'OAI-PMH:archiveID'} = "OAI-PMH";
310    $tabsysprefs{'OAI-PMH:MaxCount'}  = "OAI-PMH";
311    $tabsysprefs{'OAI-PMH:Set'}       = "OAI-PMH";
312    $tabsysprefs{'OAI-PMH:Subset'}    = "OAI-PMH";
313
314 sub StringSearch  {
315     my ($searchstring,$type)=@_;
316     my $dbh = C4::Context->dbh;
317     $searchstring=~ s/\'/\\\'/g;
318     my @data=split(' ',$searchstring);
319     my $count=@data;
320     my @results;
321     my $cnt=0;
322
323     # used for doing a plain-old sys-pref search
324     if ($type eq 'all' ){
325         my $sth=$dbh->prepare("SELECT * FROM systempreferences 
326                 WHERE variable LIKE ? OR explanation LIKE ? 
327                 ORDER BY VARIABLE");
328         $sth->execute("%$searchstring%", "%$searchstring%");
329         while (my $data=$sth->fetchrow_hashref){
330             $data->{value} =~ s/</&lt;/g;
331             $data->{value} =~ s/>/&gt;/g;
332             $data->{value}=substr($data->{value},0,60)."..." if length($data->{value}) >60;
333             push(@results,$data);
334             $cnt++;
335         }
336         $sth->finish;
337
338     }  elsif ($type){
339         foreach my $syspref (sort { lc $a cmp lc $b } keys %tabsysprefs){
340             if ($tabsysprefs{$syspref} eq $type){
341                 my $sth=$dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
342                 $sth->execute($syspref);
343                 while (my $data=$sth->fetchrow_hashref){
344                     $data->{value} =~ s/</&lt;/g;
345                     $data->{value} =~ s/>/&gt;/g;
346                     $data->{value}=substr($data->{value},0,60)."..." if length($data->{value}) >60;
347                     push(@results,$data);
348                     $cnt++;
349                 }
350                 $sth->finish;
351             }
352         }
353     } else {
354         my $strsth ="Select variable,value,explanation,type,options from systempreferences where variable not in (";
355         foreach my $syspref (keys %tabsysprefs){
356             $strsth .= $dbh->quote($syspref).",";
357         }
358         $strsth =~ s/,$/) /;
359         $strsth .= " order by variable";
360         my $sth=$dbh->prepare($strsth);
361         $sth->execute();
362         while (my $data=$sth->fetchrow_hashref){
363             $data->{value}=substr($data->{value},0,60);
364             push(@results,$data);
365             $cnt++;
366         }
367         $sth->finish;
368     }
369     return ($cnt,\@results);
370 }
371
372 my $input = new CGI;
373 my $searchfield = $input->param('searchfield');
374 my $Tvalue = $input->param('Tvalue');
375 my $offset = $input->param('offset');
376 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
377
378 my ($template, $borrowernumber, $cookie)
379     = get_template_and_user({template_name => "admin/systempreferences.tmpl",
380                  query => $input,
381                  type => "intranet",
382                  authnotrequired => 0,
383                  flagsrequired => {parameters => 1},
384                  debug => 1,
385                  });
386 my $pagesize=100;
387 my $op = $input->param('op');
388 $searchfield=~ s/\,//g;
389
390 if ($op) {
391 $template->param(script_name => $script_name,
392                         $op              => 1); # we show only the TMPL_VAR names $op
393 } else {
394 $template->param(script_name => $script_name,
395                         else              => 1); # we show only the TMPL_VAR names $op
396 }
397
398 if ($op eq 'update_and_reedit') {
399     foreach ($input->param) {
400     }
401     my $value='';
402     if (my $currentorder=$input->param('currentorder')) {
403         my @currentorder=split /\|/, $currentorder;
404         my $orderchanged=0;
405         foreach my $param ($input->param) {
406             if ($param=~m#up-(\d+).x#) {
407                 my $temp=$currentorder[$1];
408                 $currentorder[$1]=$currentorder[$1-1];
409                 $currentorder[$1-1]=$temp;
410                 $orderchanged=1;
411                 last;
412             } elsif ($param=~m#down-(\d+).x#) {
413                 my $temp=$currentorder[$1];
414                 $currentorder[$1]=$currentorder[$1+1];
415                 $currentorder[$1+1]=$temp;
416                 $orderchanged=1;
417                 last;
418             }
419         }
420         $value=join ' ', @currentorder;
421         if ($orderchanged) {
422             $op='add_form';
423             $template->param(script_name => $script_name,
424                             $op              => 1); # we show only the TMPL_VAR names $op
425         } else {
426             $op='';
427             $searchfield='';
428             $template->param(script_name => $script_name,
429                             else              => 1); # we show only the TMPL_VAR names $op
430         }
431     }
432     my $dbh = C4::Context->dbh;
433     my $query="select * from systempreferences where variable=?";
434     my $sth=$dbh->prepare($query);
435     $sth->execute($input->param('variable'));
436     if ($sth->rows) {
437         unless (C4::Context->config('demo') eq 1) {
438             my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
439             $sth->execute($value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions'));
440             $sth->finish;
441                         warn "logaction !! mod ";
442                         logaction('SYSTEMPREFERENCE','MODIFY',undef, $input->param('variable') . " | " . $value );
443         }
444     } else {
445         unless (C4::Context->config('demo') eq 1) {
446             my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
447             $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
448             $sth->finish;
449                         warn "logaction !! add ";
450                         logaction('SYSTEMPREFERENCE','ADD',undef, $input->param('variable') . " | " . $input->param('value') );
451         }
452     }
453     $sth->finish;
454
455 }
456
457 ################## ADD_FORM ##################################
458 # called by default. Used to create form to add or  modify a record
459
460 if ($op eq 'add_form') {
461     #---- if primkey exists, it's a modify action, so read values to modify...
462     my $data;
463     if ($searchfield) {
464         my $dbh = C4::Context->dbh;
465         my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
466         $sth->execute($searchfield);
467         $data=$sth->fetchrow_hashref;
468         $sth->finish;
469         $template->param(modify => 1);
470         # save tab to return to if user cancels edit
471         $template->param(return_tab => $tabsysprefs{$searchfield});
472     }
473
474     my @options;
475     foreach my $option (split(/\|/, $data->{'options'})) {
476         my $selected='0';
477         $option eq $data->{'value'} and $selected=1;
478         push @options, { option => $option, selected => $selected };
479     }
480     if ($data->{'type'} eq 'Choice') {
481         $template->param('type-choice' => 1);
482     } elsif ($data->{'type'} eq 'YesNo') {
483         $template->param('type-yesno' => 1);
484         $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
485         ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
486     } elsif ($data->{'type'} eq 'Integer') {
487         $template->param('type-free' => 1);
488         $template->param('fieldlength' => $data->{'options'});
489     } elsif ($data->{'type'} eq 'Textarea') {
490         $template->param('type-textarea' => 1);
491         $data->{options} =~ /(.*)\|(.*)/;
492         $template->param('cols' => $1, 'rows' => $2);;
493     } elsif ($data->{'type'} eq 'Float') {
494         $template->param('type-free' => 1);
495         $template->param('fieldlength' => $data->{'options'});
496     } elsif ($data->{'type'} eq 'Themes') {
497         $template->param('type-choice' => 1);
498         my $type='';
499         ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
500         @options=();
501         my $currently_selected_themes;
502         my $counter=0;
503         foreach my $theme (split /\s+/, $data->{'value'}) {
504             push @options, { option => $theme, counter => $counter };
505             $currently_selected_themes->{$theme}=1;
506             $counter++;
507         }
508         foreach my $theme (getallthemes($type)) {
509             my $selected='0';
510             next if $currently_selected_themes->{$theme};
511             push @options, { option => $theme, counter => $counter };
512             $counter++;
513         }
514     } elsif ($data->{'type'} eq 'ClassSources') {
515         $template->param('type-choice' => 1);
516         my $type='';
517         @options=();
518         my $sources = GetClassSources();
519         my $counter=0;
520         foreach my $cn_source (sort keys %$sources) {
521             if ($cn_source eq $data->{'value'}) {
522                 push @options, { option => $cn_source, counter => $counter, selected => 1 };
523             } else {
524                 push @options, { option => $cn_source, counter => $counter };
525             }
526             $counter++; 
527         }
528     } elsif ($data->{'type'} eq 'Languages') {
529         my $currently_selected_languages;
530         foreach my $language (split /\s+/, $data->{'value'}) {
531             $currently_selected_languages->{$language}=1;
532         }
533         # current language
534         my $lang = $template->param('lang');
535         my $theme;
536         my $interface;
537         if ($data->{'variable'} =~ /opac/) {
538                 # this is the OPAC
539                 $interface = 'opac';
540                 $theme = C4::Context->preference('opacthemes');
541         }
542         else {
543                 # this is the staff client      
544                 $interface = 'intranet';
545                 $theme = C4::Context->preference('template');
546         }
547         my $languages_loop = getTranslatedLanguages($interface,$theme,$lang,$currently_selected_languages);
548
549         $template->param('languages_loop' => $languages_loop);
550         $template->param('type-langselector' => 1);
551     } else {
552         $template->param('type-free' => 1);
553         $template->param('fieldlength' => $data->{'options'}>0?$data->{'options'}:60);
554     }
555     $template->param(explanation => $data->{'explanation'},
556              value => $data->{'value'},
557              type => $data->{'type'},
558              options => \@options,
559              preftype => $data->{'type'},
560              prefoptions => $data->{'options'},
561              searchfield => $searchfield);
562
563 ################## ADD_VALIDATE ##################################
564 # called by add_form, used to insert/modify data in DB
565 } elsif ($op eq 'add_validate') {
566     my $dbh = C4::Context->dbh;
567     my $sth=$dbh->prepare("select * from systempreferences where variable=?");
568     $sth->execute($input->param('variable'));
569     # to handle multiple values
570     my $value;
571     # handle multiple value strings (separated by ',')
572     my $params = $input->Vars;
573     my @values = split("\0",$params->{'value'}) if $params->{'value'};
574     for my $vl (@values) {
575         $value .= "$vl,";
576     }
577     $value =~ s/,$//;
578     if ($sth->rows) {
579         unless (C4::Context->config('demo') eq 1) {
580             my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
581             $sth->execute($value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable'));
582             $sth->finish;
583                         logaction('SYSTEMPREFERENCE','MODIFY',undef, $input->param('variable') . " | " . $value );
584         }
585     } else {
586         unless (C4::Context->config('demo') eq 1) {
587             my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
588             $sth->execute($input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
589             $sth->finish;
590                         logaction('SYSTEMPREFERENCE','ADD',undef, $input->param('variable') . " | " . $value );
591         }
592     }
593     $sth->finish;
594     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=".$tabsysprefs{$input->param('variable')}."\"></html>";
595     exit;
596 ################## DELETE_CONFIRM ##################################
597 # called by default form, used to confirm deletion of data in DB
598 } elsif ($op eq 'delete_confirm') {
599     my $dbh = C4::Context->dbh;
600     my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
601     $sth->execute($searchfield);
602     my $data=$sth->fetchrow_hashref;
603     $sth->finish;
604     $template->param(searchfield => $searchfield,
605                             Tvalue => $data->{'value'},
606                             );
607
608                                                     # END $OP eq DELETE_CONFIRM
609 ################## DELETE_CONFIRMED ##################################
610 # called by delete_confirm, used to effectively confirm deletion of data in DB
611 } elsif ($op eq 'delete_confirmed') {
612     my $dbh = C4::Context->dbh;
613     my $sth=$dbh->prepare("delete from systempreferences where variable=?");
614     $sth->execute($searchfield);
615         my $logstring =  $searchfield . " | " . $Tvalue ;
616         logaction('SYSTEMPREFERENCE','DELETE',undef,$logstring);
617     $sth->finish;
618
619                                                     # END $OP eq DELETE_CONFIRMED
620 ################## DEFAULT ##################################
621 } else { # DEFAULT
622     #Adding tab management for system preferences
623     my $tab=$input->param('tab');
624     $template->param($tab => 1);
625     my ($count,$results)=StringSearch($searchfield,$tab);
626     my $toggle=0;
627     my @loop_data = ();
628     for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
629           if ($toggle eq 0){
630             $toggle=1;
631           } else {
632             $toggle=0;
633           }
634         my %row_data;  # get a fresh hash for the row data
635         $row_data{variable} = $results->[$i]{'variable'};
636         $row_data{value} = $results->[$i]{'value'};
637         $row_data{yes} = 1 if ($results->[$i]{'value'} == 1);
638         $row_data{yesno} = 1 if ($results->[$i]{'type'} eq 'YesNo');
639         $row_data{explanation} = $results->[$i]{'explanation'};
640         $row_data{toggle} = $toggle;
641         $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'variable'};
642         $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'variable'};
643         push(@loop_data, \%row_data);
644     }
645     $tab=($tab?$tab:"Local Use");
646     $template->param(loop => \@loop_data, $tab => 1);
647     if ($offset>0) {
648         my $prevpage = $offset-$pagesize;
649         $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
650     }
651     if ($offset+$pagesize<$count) {
652         my $nextpage =$offset+$pagesize;
653         $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
654     }
655     $template->param(        tab => $tab,
656             );
657 } #---- END $OP eq DEFAULT
658 output_html_with_http_headers $input, $cookie, $template->output;