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