kohabug 2219 Corrects inconsistent use of subscription.serialsadditems column
[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{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{hidelostitems}="OPAC";
272     $tabsysprefs{opacbookbag}="OPAC";
273     $tabsysprefs{opaclanguagesdisplay}="OPAC";
274     $tabsysprefs{OpacPasswordChange}="OPAC";
275     $tabsysprefs{opacreadinghistory}="OPAC";
276     $tabsysprefs{virtualshelves}="OPAC";
277     $tabsysprefs{RequestOnOpac}="OPAC";
278     $tabsysprefs{reviewson}="OPAC";
279     $tabsysprefs{OpacTopissues}="OPAC";
280     $tabsysprefs{OpacAuthorities}="OPAC";
281     $tabsysprefs{OpacCloud}="OPAC";
282     $tabsysprefs{opacuserlogin}="OPAC";
283     $tabsysprefs{AnonSuggestions}="OPAC";
284     $tabsysprefs{suggestion}="OPAC";
285     $tabsysprefs{OpacTopissue}="OPAC";
286     $tabsysprefs{OpacBrowser}="OPAC";
287     $tabsysprefs{kohaspsuggest} = "OPAC";
288     $tabsysprefs{OpacRenewalAllowed} = "OPAC";
289     $tabsysprefs{OPACItemHolds} = "OPAC";
290     $tabsysprefs{OPACGroupResults}="OPAC";
291     $tabsysprefs{XSLTDetailsDisplay}="OPAC";
292     $tabsysprefs{XSLTResultsDisplay}="OPAC";
293
294 # LOGFeatures
295     $tabsysprefs{CataloguingLog}  = "Logs";
296     $tabsysprefs{BorrowersLog}    = "Logs";
297     $tabsysprefs{SubscriptionLog} = "Logs";
298     $tabsysprefs{IssueLog}        = "Logs";
299     $tabsysprefs{ReturnLog}       = "Logs";
300     $tabsysprefs{LetterLog}       = "Logs";
301     $tabsysprefs{FinesLog}        = "Logs";
302     
303 # OAI-PMH variables
304    $tabsysprefs{'OAI-PMH'}           = "OAI-PMH";
305    $tabsysprefs{'OAI-PMH:archiveID'} = "OAI-PMH";
306    $tabsysprefs{'OAI-PMH:MaxCount'}  = "OAI-PMH";
307    $tabsysprefs{'OAI-PMH:Set'}       = "OAI-PMH";
308    $tabsysprefs{'OAI-PMH:Subset'}    = "OAI-PMH";
309
310 sub StringSearch  {
311     my ($searchstring,$type)=@_;
312     my $dbh = C4::Context->dbh;
313     $searchstring=~ s/\'/\\\'/g;
314     my @data=split(' ',$searchstring);
315     my $count=@data;
316     my @results;
317     my $cnt=0;
318
319     # used for doing a plain-old sys-pref search
320     if ($type eq 'all' ){
321         my $sth=$dbh->prepare("SELECT * FROM systempreferences 
322                 WHERE variable LIKE ? OR explanation LIKE ? 
323                 ORDER BY VARIABLE");
324         $sth->execute("%$searchstring%", "%$searchstring%");
325         while (my $data=$sth->fetchrow_hashref){
326             $data->{value} =~ s/</&lt;/g;
327             $data->{value} =~ s/>/&gt;/g;
328             $data->{value}=substr($data->{value},0,60)."..." if length($data->{value}) >60;
329             push(@results,$data);
330             $cnt++;
331         }
332         $sth->finish;
333
334     }  elsif ($type){
335         foreach my $syspref (sort { lc $a cmp lc $b } keys %tabsysprefs){
336             if ($tabsysprefs{$syspref} eq $type){
337                 my $sth=$dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
338                 $sth->execute($syspref);
339                 while (my $data=$sth->fetchrow_hashref){
340                     $data->{value} =~ s/</&lt;/g;
341                     $data->{value} =~ s/>/&gt;/g;
342                     $data->{value}=substr($data->{value},0,60)."..." if length($data->{value}) >60;
343                     push(@results,$data);
344                     $cnt++;
345                 }
346                 $sth->finish;
347             }
348         }
349     } else {
350         my $strsth ="Select variable,value,explanation,type,options from systempreferences where variable not in (";
351         foreach my $syspref (keys %tabsysprefs){
352             $strsth .= $dbh->quote($syspref).",";
353         }
354         $strsth =~ s/,$/) /;
355         $strsth .= " order by variable";
356         my $sth=$dbh->prepare($strsth);
357         $sth->execute();
358         while (my $data=$sth->fetchrow_hashref){
359             $data->{value}=substr($data->{value},0,60);
360             push(@results,$data);
361             $cnt++;
362         }
363         $sth->finish;
364     }
365     return ($cnt,\@results);
366 }
367
368 my $input = new CGI;
369 my $searchfield = $input->param('searchfield');
370 my $Tvalue = $input->param('Tvalue');
371 my $offset = $input->param('offset');
372 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
373
374 my ($template, $borrowernumber, $cookie)
375     = get_template_and_user({template_name => "admin/systempreferences.tmpl",
376                  query => $input,
377                  type => "intranet",
378                  authnotrequired => 0,
379                  flagsrequired => {parameters => 1},
380                  debug => 1,
381                  });
382 my $pagesize=100;
383 my $op = $input->param('op');
384 $searchfield=~ s/\,//g;
385
386 if ($op) {
387 $template->param(script_name => $script_name,
388                         $op              => 1); # we show only the TMPL_VAR names $op
389 } else {
390 $template->param(script_name => $script_name,
391                         else              => 1); # we show only the TMPL_VAR names $op
392 }
393
394 if ($op eq 'update_and_reedit') {
395     foreach ($input->param) {
396     }
397     my $value='';
398     if (my $currentorder=$input->param('currentorder')) {
399         my @currentorder=split /\|/, $currentorder;
400         my $orderchanged=0;
401         foreach my $param ($input->param) {
402             if ($param=~m#up-(\d+).x#) {
403                 my $temp=$currentorder[$1];
404                 $currentorder[$1]=$currentorder[$1-1];
405                 $currentorder[$1-1]=$temp;
406                 $orderchanged=1;
407                 last;
408             } elsif ($param=~m#down-(\d+).x#) {
409                 my $temp=$currentorder[$1];
410                 $currentorder[$1]=$currentorder[$1+1];
411                 $currentorder[$1+1]=$temp;
412                 $orderchanged=1;
413                 last;
414             }
415         }
416         $value=join ' ', @currentorder;
417         if ($orderchanged) {
418             $op='add_form';
419             $template->param(script_name => $script_name,
420                             $op              => 1); # we show only the TMPL_VAR names $op
421         } else {
422             $op='';
423             $searchfield='';
424             $template->param(script_name => $script_name,
425                             else              => 1); # we show only the TMPL_VAR names $op
426         }
427     }
428     my $dbh = C4::Context->dbh;
429     my $query="select * from systempreferences where variable=?";
430     my $sth=$dbh->prepare($query);
431     $sth->execute($input->param('variable'));
432     if ($sth->rows) {
433         unless (C4::Context->config('demo') eq 1) {
434             my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
435             $sth->execute($value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions'));
436             $sth->finish;
437             warn "logaction !! mod ";
438             logaction('SYSTEMPREFERENCE','MODIFY',undef, $input->param('variable') . " | " . $value );
439         }
440     } else {
441         unless (C4::Context->config('demo') eq 1) {
442             my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
443             $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
444             $sth->finish;
445             warn "logaction !! add ";
446             logaction('SYSTEMPREFERENCE','ADD',undef, $input->param('variable') . " | " . $input->param('value') );
447         }
448     }
449     $sth->finish;
450
451 }
452
453 ################## ADD_FORM ##################################
454 # called by default. Used to create form to add or  modify a record
455
456 if ($op eq 'add_form') {
457     #---- if primkey exists, it's a modify action, so read values to modify...
458     my $data;
459     if ($searchfield) {
460         my $dbh = C4::Context->dbh;
461         my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
462         $sth->execute($searchfield);
463         $data=$sth->fetchrow_hashref;
464         $sth->finish;
465         $template->param(modify => 1);
466         # save tab to return to if user cancels edit
467         $template->param(return_tab => $tabsysprefs{$searchfield});
468     }
469
470     my @options;
471     foreach my $option (split(/\|/, $data->{'options'})) {
472         my $selected='0';
473         $option eq $data->{'value'} and $selected=1;
474         push @options, { option => $option, selected => $selected };
475     }
476     if ($data->{'type'} eq 'Choice') {
477         $template->param('type-choice' => 1);
478     } elsif ($data->{'type'} eq 'YesNo') {
479         $template->param('type-yesno' => 1);
480         $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
481         ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
482     } elsif ($data->{'type'} eq 'Integer') {
483         $template->param('type-free' => 1);
484         $template->param('fieldlength' => $data->{'options'});
485     } elsif ($data->{'type'} eq 'Textarea') {
486         $template->param('type-textarea' => 1);
487         $data->{options} =~ /(.*)\|(.*)/;
488         $template->param('cols' => $1, 'rows' => $2);;
489     } elsif ($data->{'type'} eq 'Float') {
490         $template->param('type-free' => 1);
491         $template->param('fieldlength' => $data->{'options'});
492     } elsif ($data->{'type'} eq 'Themes') {
493         $template->param('type-choice' => 1);
494         my $type='';
495         ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
496         @options=();
497         my $currently_selected_themes;
498         my $counter=0;
499         foreach my $theme (split /\s+/, $data->{'value'}) {
500             push @options, { option => $theme, counter => $counter };
501             $currently_selected_themes->{$theme}=1;
502             $counter++;
503         }
504         foreach my $theme (getallthemes($type)) {
505             my $selected='0';
506             next if $currently_selected_themes->{$theme};
507             push @options, { option => $theme, counter => $counter };
508             $counter++;
509         }
510     } elsif ($data->{'type'} eq 'ClassSources') {
511         $template->param('type-choice' => 1);
512         my $type='';
513         @options=();
514         my $sources = GetClassSources();
515         my $counter=0;
516         foreach my $cn_source (sort keys %$sources) {
517             if ($cn_source eq $data->{'value'}) {
518                 push @options, { option => $cn_source, counter => $counter, selected => 1 };
519             } else {
520                 push @options, { option => $cn_source, counter => $counter };
521             }
522             $counter++; 
523         }
524     } elsif ($data->{'type'} eq 'Languages') {
525     my $currently_selected_languages;
526         foreach my $language (split /\s+/, $data->{'value'}) {
527             $currently_selected_languages->{$language}=1;
528         }
529     # current language
530         my $lang = $template->param('lang');
531         my $theme;
532     my $interface;
533     if ($data->{'variable'} =~ /opac/) {
534         # this is the OPAC
535         $interface = 'opac';
536             $theme = C4::Context->preference('opacthemes');
537     }
538     else {
539         # this is the staff client  
540         $interface = 'intranet';
541         $theme = C4::Context->preference('template');
542     }
543     my $languages_loop = getTranslatedLanguages($interface,$theme,$lang,$currently_selected_languages);
544
545         $template->param('languages_loop' => $languages_loop);
546         $template->param('type-langselector' => 1);
547     } else {
548         $template->param('type-free' => 1);
549         $template->param('fieldlength' => $data->{'options'}>0?$data->{'options'}:60);
550     }
551     $template->param(explanation => $data->{'explanation'},
552              value => $data->{'value'},
553              type => $data->{'type'},
554              options => \@options,
555              preftype => $data->{'type'},
556              prefoptions => $data->{'options'},
557              searchfield => $searchfield);
558
559 ################## ADD_VALIDATE ##################################
560 # called by add_form, used to insert/modify data in DB
561 } elsif ($op eq 'add_validate') {
562     my $dbh = C4::Context->dbh;
563     my $sth=$dbh->prepare("select * from systempreferences where variable=?");
564     $sth->execute($input->param('variable'));
565     # to handle multiple values
566     my $value;
567     # handle multiple value strings (separated by ',')
568     my $params = $input->Vars;
569     my @values = split("\0",$params->{'value'}) if $params->{'value'};
570     for my $vl (@values) {
571         $value .= "$vl,";
572     }
573     $value =~ s/,$//;
574     if ($sth->rows) {
575         unless (C4::Context->config('demo') eq 1) {
576             my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
577             $sth->execute($value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable'));
578             $sth->finish;
579             logaction('SYSTEMPREFERENCE','MODIFY',undef, $input->param('variable') . " | " . $value );
580         }
581     } else {
582         unless (C4::Context->config('demo') eq 1) {
583             my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
584             $sth->execute($input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
585             $sth->finish;
586             logaction('SYSTEMPREFERENCE','ADD',undef, $input->param('variable') . " | " . $value );
587         }
588     }
589     $sth->finish;
590     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=".$tabsysprefs{$input->param('variable')}."\"></html>";
591     exit;
592 ################## DELETE_CONFIRM ##################################
593 # called by default form, used to confirm deletion of data in DB
594 } elsif ($op eq 'delete_confirm') {
595     my $dbh = C4::Context->dbh;
596     my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
597     $sth->execute($searchfield);
598     my $data=$sth->fetchrow_hashref;
599     $sth->finish;
600     $template->param(searchfield => $searchfield,
601                             Tvalue => $data->{'value'},
602                             );
603
604                                                     # END $OP eq DELETE_CONFIRM
605 ################## DELETE_CONFIRMED ##################################
606 # called by delete_confirm, used to effectively confirm deletion of data in DB
607 } elsif ($op eq 'delete_confirmed') {
608     my $dbh = C4::Context->dbh;
609     my $sth=$dbh->prepare("delete from systempreferences where variable=?");
610     $sth->execute($searchfield);
611     my $logstring =  $searchfield . " | " . $Tvalue ;
612     logaction('SYSTEMPREFERENCE','DELETE',undef,$logstring);
613     $sth->finish;
614
615                                                     # END $OP eq DELETE_CONFIRMED
616 ################## DEFAULT ##################################
617 } else { # DEFAULT
618     #Adding tab management for system preferences
619     my $tab=$input->param('tab');
620     $template->param($tab => 1);
621     my ($count,$results)=StringSearch($searchfield,$tab);
622     my $toggle=0;
623     my @loop_data = ();
624     for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
625           if ($toggle eq 0){
626             $toggle=1;
627           } else {
628             $toggle=0;
629           }
630         my %row_data;  # get a fresh hash for the row data
631         $row_data{variable} = $results->[$i]{'variable'};
632         $row_data{value} = $results->[$i]{'value'};
633         $row_data{yes} = 1 if ($results->[$i]{'value'} == 1);
634         $row_data{yesno} = 1 if ($results->[$i]{'type'} eq 'YesNo');
635         $row_data{explanation} = $results->[$i]{'explanation'};
636         $row_data{toggle} = $toggle;
637         $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'variable'};
638         $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'variable'};
639         push(@loop_data, \%row_data);
640     }
641     $tab=($tab?$tab:"Local Use");
642     $template->param(loop => \@loop_data, $tab => 1);
643     if ($offset>0) {
644         my $prevpage = $offset-$pagesize;
645         $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
646     }
647     if ($offset+$pagesize<$count) {
648         my $nextpage =$offset+$pagesize;
649         $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
650     }
651     $template->param(        tab => $tab,
652             );
653 } #---- END $OP eq DEFAULT
654 output_html_with_http_headers $input, $cookie, $template->output;