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