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