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