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