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