Allow renewal limits to be overridden
[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
73 # Admin
74     $tabsysprefs{singleBranchMode}="Admin";
75     $tabsysprefs{staffClientBaseURL}="Admin";
76     $tabsysprefs{Version}="Admin";
77     $tabsysprefs{OpacMaintenance}="Admin";
78     $tabsysprefs{FrameworksLoaded}="Admin";
79     $tabsysprefs{libraryAddress}="Admin";
80     $tabsysprefs{delimiter}="Admin";
81     $tabsysprefs{IndependantBranches}="Admin";
82     $tabsysprefs{insecure}="Admin";
83     $tabsysprefs{KohaAdmin}="Admin";
84     $tabsysprefs{KohaAdminEmailAddress}="Admin";
85     $tabsysprefs{MIME}="Admin";
86     $tabsysprefs{timeout}="Admin";
87     $tabsysprefs{Intranet_includes}="Admin";
88     $tabsysprefs{AutoLocation}="Admin";
89     $tabsysprefs{DebugLevel}="Admin";
90     $tabsysprefs{SessionStorage}="Admin";
91     $tabsysprefs{noItemTypeImages}="Admin";
92     $tabsysprefs{OPACBaseURL}="Admin";
93     $tabsysprefs{GranularPermissions}="Admin";
94
95 # Authorities
96     $tabsysprefs{authoritysep}="Authorities";
97     $tabsysprefs{AuthDisplayHierarchy}="Authorities";
98     $tabsysprefs{dontmerge}="Authorities";
99     $tabsysprefs{BiblioAddsAuthorities}="Authorities";
100 # Cataloguing
101     $tabsysprefs{advancedMARCEditor}="Cataloguing";
102     $tabsysprefs{autoBarcode}="Cataloguing";
103     $tabsysprefs{hide_marc}="Cataloguing";
104     $tabsysprefs{IntranetBiblioDefaultView} = "Cataloguing";
105     $tabsysprefs{ISBD}="Cataloguing";
106     $tabsysprefs{itemcallnumber}="Cataloguing";
107     $tabsysprefs{LabelMARCView}="Cataloguing";
108     $tabsysprefs{marc}="Cataloguing";
109     $tabsysprefs{marcflavour}="Cataloguing";
110     $tabsysprefs{MARCOrgCode}="Cataloguing";
111     $tabsysprefs{z3950AuthorAuthFields}="Cataloguing";
112     $tabsysprefs{z3950NormalizeAuthor}="Cataloguing";
113     $tabsysprefs{Stemming}="Cataloguing";
114     $tabsysprefs{WeightFields}="Cataloguing";
115     $tabsysprefs{NoZebra}="Cataloguing";
116     $tabsysprefs{NoZebraIndexes}="Cataloguing";
117     $tabsysprefs{ReceiveBackIssues}="Cataloguing";
118     $tabsysprefs{DefaultClassificationSource}="Cataloguing";
119     $tabsysprefs{RoutingSerials}="Cataloguing";
120     $tabsysprefs{'item-level_itypes'}="Cataloguing";
121     $tabsysprefs{OpacSuppression}="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{AllowRenewalLimitOverride}="Circulation";
139     $tabsysprefs{canreservefromotherbranches}="Circulation";
140     $tabsysprefs{finesMode}="Circulation";
141     $tabsysprefs{emailLibrarianWhenHoldIsPlaced}="Circulation";
142     $tabsysprefs{globalDueDate}="Circulation";
143     $tabsysprefs{holdCancelLength}="Circulation";
144     $tabsysprefs{itemBarcodeInputFilter}="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     $tabsysprefs{'SMSSendDriver'}             = 'Patrons';
189
190 # I18N/L10N
191     $tabsysprefs{dateformat}="I18N/L10N";
192     $tabsysprefs{opaclanguages}="I18N/L10N";
193     $tabsysprefs{opacthemes}="I18N/L10N";
194     $tabsysprefs{language}="I18N/L10N";
195
196 # Searching
197     $tabsysprefs{defaultSortField}="Searching";
198     $tabsysprefs{defaultSortOrder}="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{opaclayoutstylesheet}="OPAC";
252     $tabsysprefs{OpacNav}="OPAC";
253     $tabsysprefs{opacsmallimage}="OPAC";
254     $tabsysprefs{opacstylesheet}="OPAC";
255     $tabsysprefs{opacthemes}="OPAC";
256     $tabsysprefs{opacuserjs}="OPAC";
257     $tabsysprefs{SubscriptionHistory}="OPAC";
258     $tabsysprefs{opacheader}="OPAC";
259     $tabsysprefs{noOPACUserLogin}="OPAC";
260     $tabsysprefs{hideBiblioNumber}="OPAC";
261     $tabsysprefs{OPACDisplayExtendedSubInfo}="OPAC";
262     $tabsysprefs{OpacMainUserBlock}="OPAC";
263     $tabsysprefs{OPACSubscriptionDisplay}="OPAC";
264     $tabsysprefs{OPACURLOpenInNewWindow}="OPAC";
265     $tabsysprefs{OPACUserCSS}="OPAC";
266     $tabsysprefs{OPACHighlightedWords}="OPAC";
267     $tabsysprefs{OPACViewOthersSuggestions}="OPAC";
268     $tabsysprefs{URLLinkText}="OPAC";
269     $tabsysprefs{OPACShelfBrowser}="OPAC";
270
271 # OPAC
272     $tabsysprefs{SearchMyLibraryFirst}="OPAC";
273     $tabsysprefs{hidelostitems}="OPAC";
274     $tabsysprefs{opacbookbag}="OPAC";
275     $tabsysprefs{opaclanguagesdisplay}="OPAC";
276     $tabsysprefs{OpacPasswordChange}="OPAC";
277     $tabsysprefs{opacreadinghistory}="OPAC";
278     $tabsysprefs{virtualshelves}="OPAC";
279     $tabsysprefs{RequestOnOpac}="OPAC";
280     $tabsysprefs{reviewson}="OPAC";
281     $tabsysprefs{OpacTopissues}="OPAC";
282     $tabsysprefs{OpacAuthorities}="OPAC";
283     $tabsysprefs{OpacCloud}="OPAC";
284     $tabsysprefs{opacuserlogin}="OPAC";
285     $tabsysprefs{AnonSuggestions}="OPAC";
286     $tabsysprefs{suggestion}="OPAC";
287     $tabsysprefs{OpacTopissue}="OPAC";
288     $tabsysprefs{OpacBrowser}="OPAC";
289     $tabsysprefs{kohaspsuggest} = "OPAC";
290     $tabsysprefs{OpacRenewalAllowed} = "OPAC";
291     $tabsysprefs{OPACItemHolds} = "OPAC";
292     $tabsysprefs{OPACGroupResults}="OPAC";
293     $tabsysprefs{XSLTDetailsDisplay}="OPAC";
294     $tabsysprefs{XSLTResultsDisplay}="OPAC";
295
296 # LOGFeatures
297     $tabsysprefs{CataloguingLog}  = "Logs";
298     $tabsysprefs{BorrowersLog}    = "Logs";
299     $tabsysprefs{SubscriptionLog} = "Logs";
300     $tabsysprefs{IssueLog}        = "Logs";
301     $tabsysprefs{ReturnLog}       = "Logs";
302     $tabsysprefs{LetterLog}       = "Logs";
303     $tabsysprefs{FinesLog}        = "Logs";
304     
305 # OAI-PMH variables
306    $tabsysprefs{'OAI-PMH'}           = "OAI-PMH";
307    $tabsysprefs{'OAI-PMH:archiveID'} = "OAI-PMH";
308    $tabsysprefs{'OAI-PMH:MaxCount'}  = "OAI-PMH";
309    $tabsysprefs{'OAI-PMH:Set'}       = "OAI-PMH";
310    $tabsysprefs{'OAI-PMH:Subset'}    = "OAI-PMH";
311
312 sub StringSearch  {
313     my ($searchstring,$type)=@_;
314     my $dbh = C4::Context->dbh;
315     $searchstring=~ s/\'/\\\'/g;
316     my @data=split(' ',$searchstring);
317     my $count=@data;
318     my @results;
319     my $cnt=0;
320
321     # used for doing a plain-old sys-pref search
322     if ($type eq 'all' ){
323         my $sth=$dbh->prepare("SELECT * FROM systempreferences 
324                 WHERE variable LIKE ? OR explanation LIKE ? 
325                 ORDER BY VARIABLE");
326         $sth->execute("%$searchstring%", "%$searchstring%");
327         while (my $data=$sth->fetchrow_hashref){
328             $data->{value} =~ s/</&lt;/g;
329             $data->{value} =~ s/>/&gt;/g;
330             $data->{value}=substr($data->{value},0,60)."..." if length($data->{value}) >60;
331             push(@results,$data);
332             $cnt++;
333         }
334         $sth->finish;
335
336     }  elsif ($type){
337         foreach my $syspref (sort { lc $a cmp lc $b } keys %tabsysprefs){
338             if ($tabsysprefs{$syspref} eq $type){
339                 my $sth=$dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
340                 $sth->execute($syspref);
341                 while (my $data=$sth->fetchrow_hashref){
342                     $data->{value} =~ s/</&lt;/g;
343                     $data->{value} =~ s/>/&gt;/g;
344                     $data->{value}=substr($data->{value},0,60)."..." if length($data->{value}) >60;
345                     push(@results,$data);
346                     $cnt++;
347                 }
348                 $sth->finish;
349             }
350         }
351     } else {
352         my $strsth ="Select variable,value,explanation,type,options from systempreferences where variable not in (";
353         foreach my $syspref (keys %tabsysprefs){
354             $strsth .= $dbh->quote($syspref).",";
355         }
356         $strsth =~ s/,$/) /;
357         $strsth .= " order by variable";
358         my $sth=$dbh->prepare($strsth);
359         $sth->execute();
360         while (my $data=$sth->fetchrow_hashref){
361             $data->{value}=substr($data->{value},0,60);
362             push(@results,$data);
363             $cnt++;
364         }
365         $sth->finish;
366     }
367     return ($cnt,\@results);
368 }
369
370 my $input = new CGI;
371 my $searchfield = $input->param('searchfield');
372 my $Tvalue = $input->param('Tvalue');
373 my $offset = $input->param('offset');
374 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
375
376 my ($template, $borrowernumber, $cookie)
377     = get_template_and_user({template_name => "admin/systempreferences.tmpl",
378                  query => $input,
379                  type => "intranet",
380                  authnotrequired => 0,
381                  flagsrequired => {parameters => 1},
382                  debug => 1,
383                  });
384 my $pagesize=100;
385 my $op = $input->param('op');
386 $searchfield=~ s/\,//g;
387
388 if ($op) {
389 $template->param(script_name => $script_name,
390                         $op              => 1); # we show only the TMPL_VAR names $op
391 } else {
392 $template->param(script_name => $script_name,
393                         else              => 1); # we show only the TMPL_VAR names $op
394 }
395
396 if ($op eq 'update_and_reedit') {
397     foreach ($input->param) {
398     }
399     my $value='';
400     if (my $currentorder=$input->param('currentorder')) {
401         my @currentorder=split /\|/, $currentorder;
402         my $orderchanged=0;
403         foreach my $param ($input->param) {
404             if ($param=~m#up-(\d+).x#) {
405                 my $temp=$currentorder[$1];
406                 $currentorder[$1]=$currentorder[$1-1];
407                 $currentorder[$1-1]=$temp;
408                 $orderchanged=1;
409                 last;
410             } elsif ($param=~m#down-(\d+).x#) {
411                 my $temp=$currentorder[$1];
412                 $currentorder[$1]=$currentorder[$1+1];
413                 $currentorder[$1+1]=$temp;
414                 $orderchanged=1;
415                 last;
416             }
417         }
418         $value=join ' ', @currentorder;
419         if ($orderchanged) {
420             $op='add_form';
421             $template->param(script_name => $script_name,
422                             $op              => 1); # we show only the TMPL_VAR names $op
423         } else {
424             $op='';
425             $searchfield='';
426             $template->param(script_name => $script_name,
427                             else              => 1); # we show only the TMPL_VAR names $op
428         }
429     }
430     my $dbh = C4::Context->dbh;
431     my $query="select * from systempreferences where variable=?";
432     my $sth=$dbh->prepare($query);
433     $sth->execute($input->param('variable'));
434     if ($sth->rows) {
435         unless (C4::Context->config('demo') eq 1) {
436             my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
437             $sth->execute($value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions'));
438             $sth->finish;
439             warn "logaction !! mod ";
440             logaction('SYSTEMPREFERENCE','MODIFY',undef, $input->param('variable') . " | " . $value );
441         }
442     } else {
443         unless (C4::Context->config('demo') eq 1) {
444             my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
445             $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
446             $sth->finish;
447             warn "logaction !! add ";
448             logaction('SYSTEMPREFERENCE','ADD',undef, $input->param('variable') . " | " . $input->param('value') );
449         }
450     }
451     $sth->finish;
452
453 }
454
455 ################## ADD_FORM ##################################
456 # called by default. Used to create form to add or  modify a record
457
458 if ($op eq 'add_form') {
459     #---- if primkey exists, it's a modify action, so read values to modify...
460     my $data;
461     if ($searchfield) {
462         my $dbh = C4::Context->dbh;
463         my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
464         $sth->execute($searchfield);
465         $data=$sth->fetchrow_hashref;
466         $sth->finish;
467         $template->param(modify => 1);
468         # save tab to return to if user cancels edit
469         $template->param(return_tab => $tabsysprefs{$searchfield});
470     }
471
472     my @options;
473     foreach my $option (split(/\|/, $data->{'options'})) {
474         my $selected='0';
475         $option eq $data->{'value'} and $selected=1;
476         push @options, { option => $option, selected => $selected };
477     }
478     if ($data->{'type'} eq 'Choice') {
479         $template->param('type-choice' => 1);
480     } elsif ($data->{'type'} eq 'YesNo') {
481         $template->param('type-yesno' => 1);
482         $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
483         ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
484     } elsif ($data->{'type'} eq 'Integer') {
485         $template->param('type-free' => 1);
486         $template->param('fieldlength' => $data->{'options'});
487     } elsif ($data->{'type'} eq 'Textarea') {
488         $template->param('type-textarea' => 1);
489         $data->{options} =~ /(.*)\|(.*)/;
490         $template->param('cols' => $1, 'rows' => $2);;
491     } elsif ($data->{'type'} eq 'Float') {
492         $template->param('type-free' => 1);
493         $template->param('fieldlength' => $data->{'options'});
494     } elsif ($data->{'type'} eq 'Themes') {
495         $template->param('type-choice' => 1);
496         my $type='';
497         ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
498         @options=();
499         my $currently_selected_themes;
500         my $counter=0;
501         foreach my $theme (split /\s+/, $data->{'value'}) {
502             push @options, { option => $theme, counter => $counter };
503             $currently_selected_themes->{$theme}=1;
504             $counter++;
505         }
506         foreach my $theme (getallthemes($type)) {
507             my $selected='0';
508             next if $currently_selected_themes->{$theme};
509             push @options, { option => $theme, counter => $counter };
510             $counter++;
511         }
512     } elsif ($data->{'type'} eq 'ClassSources') {
513         $template->param('type-choice' => 1);
514         my $type='';
515         @options=();
516         my $sources = GetClassSources();
517         my $counter=0;
518         foreach my $cn_source (sort keys %$sources) {
519             if ($cn_source eq $data->{'value'}) {
520                 push @options, { option => $cn_source, counter => $counter, selected => 1 };
521             } else {
522                 push @options, { option => $cn_source, counter => $counter };
523             }
524             $counter++; 
525         }
526     } elsif ($data->{'type'} eq 'Languages') {
527     my $currently_selected_languages;
528         foreach my $language (split /\s+/, $data->{'value'}) {
529             $currently_selected_languages->{$language}=1;
530         }
531     # current language
532         my $lang = $template->param('lang');
533         my $theme;
534     my $interface;
535     if ($data->{'variable'} =~ /opac/) {
536         # this is the OPAC
537         $interface = 'opac';
538             $theme = C4::Context->preference('opacthemes');
539     }
540     else {
541         # this is the staff client  
542         $interface = 'intranet';
543         $theme = C4::Context->preference('template');
544     }
545     my $languages_loop = getTranslatedLanguages($interface,$theme,$lang,$currently_selected_languages);
546
547         $template->param('languages_loop' => $languages_loop);
548         $template->param('type-langselector' => 1);
549     } else {
550         $template->param('type-free' => 1);
551         $template->param('fieldlength' => $data->{'options'}>0?$data->{'options'}:60);
552     }
553     $template->param(explanation => $data->{'explanation'},
554              value => $data->{'value'},
555              type => $data->{'type'},
556              options => \@options,
557              preftype => $data->{'type'},
558              prefoptions => $data->{'options'},
559              searchfield => $searchfield);
560
561 ################## ADD_VALIDATE ##################################
562 # called by add_form, used to insert/modify data in DB
563 } elsif ($op eq 'add_validate') {
564     my $dbh = C4::Context->dbh;
565     my $sth=$dbh->prepare("select * from systempreferences where variable=?");
566     $sth->execute($input->param('variable'));
567     # to handle multiple values
568     my $value;
569     # handle multiple value strings (separated by ',')
570     my $params = $input->Vars;
571     my @values = split("\0",$params->{'value'}) if $params->{'value'};
572     for my $vl (@values) {
573         $value .= "$vl,";
574     }
575     $value =~ s/,$//;
576     if ($sth->rows) {
577         unless (C4::Context->config('demo') eq 1) {
578             my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
579             $sth->execute($value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable'));
580             $sth->finish;
581             logaction('SYSTEMPREFERENCE','MODIFY',undef, $input->param('variable') . " | " . $value );
582         }
583     } else {
584         unless (C4::Context->config('demo') eq 1) {
585             my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
586             $sth->execute($input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
587             $sth->finish;
588             logaction('SYSTEMPREFERENCE','ADD',undef, $input->param('variable') . " | " . $value );
589         }
590     }
591     $sth->finish;
592     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=".$tabsysprefs{$input->param('variable')}."\"></html>";
593     exit;
594 ################## DELETE_CONFIRM ##################################
595 # called by default form, used to confirm deletion of data in DB
596 } elsif ($op eq 'delete_confirm') {
597     my $dbh = C4::Context->dbh;
598     my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
599     $sth->execute($searchfield);
600     my $data=$sth->fetchrow_hashref;
601     $sth->finish;
602     $template->param(searchfield => $searchfield,
603                             Tvalue => $data->{'value'},
604                             );
605
606                                                     # END $OP eq DELETE_CONFIRM
607 ################## DELETE_CONFIRMED ##################################
608 # called by delete_confirm, used to effectively confirm deletion of data in DB
609 } elsif ($op eq 'delete_confirmed') {
610     my $dbh = C4::Context->dbh;
611     my $sth=$dbh->prepare("delete from systempreferences where variable=?");
612     $sth->execute($searchfield);
613     my $logstring =  $searchfield . " | " . $Tvalue ;
614     logaction('SYSTEMPREFERENCE','DELETE',undef,$logstring);
615     $sth->finish;
616
617                                                     # END $OP eq DELETE_CONFIRMED
618 ################## DEFAULT ##################################
619 } else { # DEFAULT
620     #Adding tab management for system preferences
621     my $tab=$input->param('tab');
622     $template->param($tab => 1);
623     my ($count,$results)=StringSearch($searchfield,$tab);
624     my $toggle=0;
625     my @loop_data = ();
626     for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
627           if ($toggle eq 0){
628             $toggle=1;
629           } else {
630             $toggle=0;
631           }
632         my %row_data;  # get a fresh hash for the row data
633         $row_data{variable} = $results->[$i]{'variable'};
634         $row_data{value} = $results->[$i]{'value'};
635         $row_data{yes} = 1 if ($results->[$i]{'value'} == 1);
636         $row_data{yesno} = 1 if ($results->[$i]{'type'} eq 'YesNo');
637         $row_data{explanation} = $results->[$i]{'explanation'};
638         $row_data{toggle} = $toggle;
639         $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'variable'};
640         $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'variable'};
641         push(@loop_data, \%row_data);
642     }
643     $tab=($tab?$tab:"Local Use");
644     $template->param(loop => \@loop_data, $tab => 1);
645     if ($offset>0) {
646         my $prevpage = $offset-$pagesize;
647         $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
648     }
649     if ($offset+$pagesize<$count) {
650         my $nextpage =$offset+$pagesize;
651         $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
652     }
653     $template->param(        tab => $tab,
654             );
655 } #---- END $OP eq DEFAULT
656 output_html_with_http_headers $input, $cookie, $template->output;