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