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