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