Merge remote-tracking branch 'origin/new/bug_5339'
[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
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
24 =head1 systempreferences.pl
25
26 ALSO :
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 MIME::Base64;
48 use C4::Auth;
49 use C4::Context;
50 use C4::Koha;
51 use C4::Languages qw(getTranslatedLanguages);
52 use C4::ClassSource;
53 use C4::Log;
54 use C4::Output;
55 use YAML::Syck qw( Dump LoadFile );
56
57
58 # use Smart::Comments;
59
60 # FIXME, shouldnt we store this stuff in the systempreferences table?
61
62 # FIXME: This uses hash in a backwards way.  What we really want is:
63 #       $tabsysprefs{key} = $array_ref;
64 #               like
65 #       $tabsysprefs{Cataloguing} = [qw(autoBarcode ISBD marc ...)];
66 #
67 #   Because some things *should* be on more than one tab.
68 #   And the tabname is the unique part (the key).
69
70 my %tabsysprefs;
71
72 # Acquisitions
73     $tabsysprefs{gist}="Acquisitions";
74     $tabsysprefs{emailPurchaseSuggestions}="Acquisitions";
75     $tabsysprefs{RenewSerialAddsSuggestion}="Acquisitions";
76     $tabsysprefs{AcqCreateItem}="Acquisitions";
77     $tabsysprefs{OrderPdfFormat}="Acquisitions";
78     $tabsysprefs{CurrencyFormat}="Acquisitions";
79     $tabsysprefs{AcqViewBaskets}="Acquisitions";
80
81 # Admin
82 $tabsysprefs{singleBranchMode}      = "Admin";
83 $tabsysprefs{staffClientBaseURL}    = "Admin";
84 $tabsysprefs{Version}               = "Admin";
85 $tabsysprefs{OpacMaintenance}       = "Admin";
86 $tabsysprefs{FrameworksLoaded}      = "Admin";
87 $tabsysprefs{delimiter}             = "Admin";
88 $tabsysprefs{IndependantBranches}   = "Admin";
89 $tabsysprefs{insecure}              = "Admin";
90 $tabsysprefs{KohaAdmin}             = "Admin";
91 $tabsysprefs{KohaAdminEmailAddress} = "Admin";
92 $tabsysprefs{timeout}               = "Admin";
93 $tabsysprefs{Intranet_includes}     = "Admin";
94 $tabsysprefs{AutoLocation}          = "Admin";
95 $tabsysprefs{DebugLevel}            = "Admin";
96 $tabsysprefs{SessionStorage}        = "Admin";
97
98 # This script is deprecated so all of these prefs are lumped here to avoid their being displayed in the local use prefs tab
99
100 $tabsysprefs{noItemTypeImages}      = "Admin";
101 $tabsysprefs{OPACBaseURL}           = "Admin";
102 $tabsysprefs{AnonymousPatron}       = "Admin";
103 $tabsysprefs{casAuthentication}     = "Admin";
104 $tabsysprefs{casLogout}             = "Admin";
105 $tabsysprefs{casServerUrl}          = "Admin";
106 $tabsysprefs{Disable_Dictionary}    = "Admin";
107 $tabsysprefs{EnableOpacSearchHistory}   = "Admin";
108 $tabsysprefs{Intranetbookbag}       = "Admin";
109 $tabsysprefs{maxitemsinSearchResults}   = "Admin";
110 $tabsysprefs{noOPACUserLogin}       = "Admin";
111 $tabsysprefs{'OAI-PMH:ConfFile'}    = "Admin";
112 $tabsysprefs{OpacAddMastheadLibraryPulldown}    = "Admin";
113 $tabsysprefs{opaclargeimage}        = "Admin";
114 $tabsysprefs{OPACXSLTDetailsDisplay}    = "Admin";
115 $tabsysprefs{OPACXSLTResultsDisplay}    = "Admin";
116 $tabsysprefs{PDFFontType}           = "Admin";
117 $tabsysprefs{PrintNoticesMaxLines}  = "Admin";
118 $tabsysprefs{ReservesControlBranch} = "Admin";
119 $tabsysprefs{ResultsDisplay}        = "Admin";
120 $tabsysprefs{NoReturnSetLost}       = "Admin";
121 $tabsysprefs{SearchURL}             = "Admin";
122 $tabsysprefs{ShowPictures}          = "Admin";
123 $tabsysprefs{soundon}               = "Admin";
124 $tabsysprefs{SpineLabelShowPrintOnBibDetails}   = "Admin";
125 $tabsysprefs{WebBasedSelfCheckHeader}           = "Admin";
126 $tabsysprefs{WebBasedSelfCheckTimeout}          = "Admin";
127
128 # Authorities
129 $tabsysprefs{authoritysep}          = "Authorities";
130 $tabsysprefs{AuthDisplayHierarchy}  = "Authorities";
131 $tabsysprefs{dontmerge}             = "Authorities";
132 $tabsysprefs{BiblioAddsAuthorities} = "Authorities";
133
134 # Cataloguing
135 $tabsysprefs{advancedMARCeditor}          = "Cataloging";
136 $tabsysprefs{autoBarcode}                 = "Cataloging";
137 $tabsysprefs{hide_marc}                   = "Cataloging";
138 $tabsysprefs{IntranetBiblioDefaultView}   = "Cataloging";
139 $tabsysprefs{ISBD}                        = "Cataloging";
140 $tabsysprefs{itemcallnumber}              = "Cataloging";
141 $tabsysprefs{LabelMARCView}               = "Cataloging";
142 $tabsysprefs{marcflavour}                 = "Cataloging";
143 $tabsysprefs{MARCOrgCode}                 = "Cataloging";
144 $tabsysprefs{z3950AuthorAuthFields}       = "Cataloging";
145 $tabsysprefs{z3950NormalizeAuthor}        = "Cataloging";
146 $tabsysprefs{Stemming}                    = "Cataloging";
147 $tabsysprefs{WeightFields}                = "Cataloging";
148 $tabsysprefs{NoZebra}                     = "Cataloging";
149 $tabsysprefs{NoZebraIndexes}              = "Cataloging";
150 $tabsysprefs{ReceiveBackIssues}           = "Cataloging";
151 $tabsysprefs{DefaultClassificationSource} = "Cataloging";
152 $tabsysprefs{RoutingSerials}              = "Cataloging";
153 $tabsysprefs{'item-level_itypes'}         = "Cataloging";
154 $tabsysprefs{OpacSuppression}             = "Cataloging";
155 $tabsysprefs{SpineLabelFormat}            = "Cataloging";
156 $tabsysprefs{SpineLabelAutoPrint}         = "Cataloging";
157
158 # Circulation
159 $tabsysprefs{maxoutstanding}                 = "Circulation";
160 $tabsysprefs{maxreserves}                    = "Circulation";
161 $tabsysprefs{noissuescharge}                 = "Circulation";
162 $tabsysprefs{IssuingInProcess}               = "Circulation";
163 $tabsysprefs{patronimages}                   = "Circulation";
164 $tabsysprefs{printcirculationslips}          = "Circulation";
165 $tabsysprefs{ReturnBeforeExpiry}             = "Circulation";
166 $tabsysprefs{SpecifyDueDate}                 = "Circulation";
167 $tabsysprefs{AutomaticItemReturn}            = "Circulation";
168 $tabsysprefs{ReservesMaxPickUpDelay}         = "Circulation";
169 $tabsysprefs{TransfersMaxDaysWarning}        = "Circulation";
170 $tabsysprefs{useDaysMode}                    = "Circulation";
171 $tabsysprefs{ReservesNeedReturns}            = "Circulation";
172 $tabsysprefs{CircAutocompl}                  = "Circulation";
173 $tabsysprefs{AllowRenewalLimitOverride}      = "Circulation";
174 $tabsysprefs{canreservefromotherbranches}    = "Circulation";
175 $tabsysprefs{finesMode}                      = "Circulation";
176 $tabsysprefs{numReturnedItemsToShow}         = "Circulation";
177 $tabsysprefs{emailLibrarianWhenHoldIsPlaced} = "Circulation";
178 $tabsysprefs{itemBarcodeInputFilter}         = "Circulation";
179 $tabsysprefs{WebBasedSelfCheck}              = "Circulation";
180 $tabsysprefs{ShowPatronImageInWebBasedSelfCheck} = "Circulation";
181 $tabsysprefs{CircControl}                    = "Circulation";
182 $tabsysprefs{finesCalendar}                  = "Circulation";
183 $tabsysprefs{previousIssuesDefaultSortOrder} = "Circulation";
184 $tabsysprefs{todaysIssuesDefaultSortOrder}   = "Circulation";
185 $tabsysprefs{HomeOrHoldingBranch}            = "Circulation";
186 $tabsysprefs{HomeOrHoldingBranchReturn}      = "Circulation";
187 $tabsysprefs{RandomizeHoldsQueueWeight}      = "Circulation";
188 $tabsysprefs{StaticHoldsQueueWeight}         = "Circulation";
189 $tabsysprefs{AllowOnShelfHolds}              = "Circulation";
190 $tabsysprefs{AllowHoldsOnDamagedItems}       = "Circulation";
191 $tabsysprefs{UseBranchTransferLimits}        = "Circulation";
192 $tabsysprefs{AllowHoldPolicyOverride}        = "Circulation";
193 $tabsysprefs{BranchTransferLimitsType}       = "Circulation";
194 $tabsysprefs{AllowNotForLoanOverride}        = "Circulation";
195 $tabsysprefs{RenewalPeriodBase}              = "Circulation";
196 $tabsysprefs{FilterBeforeOverdueReport}      = "Circulation";
197 $tabsysprefs{AllowHoldDateInFuture}          = "Circulation";
198 $tabsysprefs{OPACFineNoRenewals}             = "Circulation";
199 $tabsysprefs{InProcessingToShelvingCart}     = "Circulation";
200 $tabsysprefs{NewItemsDefaultLocation}        = "Circulation";
201 $tabsysprefs{ReturnToShelvingCart}           = "Circulation";
202 $tabsysprefs{DisplayClearScreenButton}       = "Circulation";
203 $tabsysprefs{AllowAllMessageDeletion}        = "Circulation";
204 $tabsysprefs{OverdueNoticeBcc}               = "Circulation";
205 $tabsysprefs{OverduesBlockCirc}              = "Circulation";
206 $tabsysprefs{UseTransportCostMatrix}         = "Circulation";
207 $tabsysprefs{AllowReturnToBranch}            = "Circulation";
208
209 # Staff Client
210 $tabsysprefs{template}                = "StaffClient";
211 $tabsysprefs{intranetstylesheet}      = "StaffClient";
212 $tabsysprefs{IntranetNav}             = "StaffClient";
213 $tabsysprefs{intranetcolorstylesheet} = "StaffClient";
214 $tabsysprefs{intranetuserjs}          = "StaffClient";
215 $tabsysprefs{yuipath}                 = "StaffClient";
216 $tabsysprefs{IntranetmainUserblock}   = "StaffClient";
217 $tabsysprefs{viewMARC}                = "StaffClient";
218 $tabsysprefs{viewLabeledMARC}         = "StaffClient";
219 $tabsysprefs{viewISBD}                = "StaffClient";
220
221 # Patrons
222 $tabsysprefs{autoMemberNum}                = "Patrons";
223 $tabsysprefs{checkdigit}                   = "Patrons";
224 $tabsysprefs{intranetreadinghistory}       = "Patrons";
225 $tabsysprefs{NotifyBorrowerDeparture}      = "Patrons";
226 $tabsysprefs{memberofinstitution}          = "Patrons";
227 $tabsysprefs{BorrowerMandatoryField}       = "Patrons";
228 $tabsysprefs{BorrowerUnwantedField}        = "Patrons";
229 $tabsysprefs{borrowerRelationship}         = "Patrons";
230 $tabsysprefs{BorrowersTitles}              = "Patrons";
231 $tabsysprefs{patronimages}                 = "Patrons";
232 $tabsysprefs{minPasswordLength}            = "Patrons";
233 $tabsysprefs{uppercasesurnames}            = "Patrons";
234 $tabsysprefs{MaxFine}                      = "Patrons";
235 $tabsysprefs{NotifyBorrowerDeparture}      = "Patrons";
236 $tabsysprefs{AddPatronLists}               = "Patrons";
237 $tabsysprefs{PatronsPerPage}               = "Patrons";
238 $tabsysprefs{ExtendedPatronAttributes}     = "Patrons";
239 $tabsysprefs{AutoEmailOpacUser}            = "Patrons";
240 $tabsysprefs{AutoEmailPrimaryAddress}      = "Patrons";
241 $tabsysprefs{EnhancedMessagingPreferences} = "Patrons";
242 $tabsysprefs{'SMSSendDriver'}              = 'Patrons';
243 $tabsysprefs{HidePatronName}               = "Patrons";
244
245
246 # I18N/L10N
247 $tabsysprefs{dateformat}    = "I18N/L10N";
248 $tabsysprefs{opaclanguages} = "I18N/L10N";
249 $tabsysprefs{opaclanguagesdisplay} = "I18N/L10N";
250 $tabsysprefs{language}      = "I18N/L10N";
251
252 # Searching
253 $tabsysprefs{defaultSortField}        = "Searching";
254 $tabsysprefs{defaultSortOrder}        = "Searching";
255 $tabsysprefs{numSearchResults}        = "Searching";
256 $tabsysprefs{OPACdefaultSortField}    = "Searching";
257 $tabsysprefs{OPACdefaultSortOrder}    = "Searching";
258 $tabsysprefs{OPACItemsResultsDisplay} = "Searching";
259 $tabsysprefs{OPACnumSearchResults}    = "Searching";
260 $tabsysprefs{QueryFuzzy}              = "Searching";
261 $tabsysprefs{QueryStemming}           = "Searching";
262 $tabsysprefs{QueryWeightFields}       = "Searching";
263 $tabsysprefs{expandedSearchOption}    = "Searching";
264 $tabsysprefs{QueryAutoTruncate}       = "Searching";
265 $tabsysprefs{QueryRemoveStopwords}    = "Searching";
266 $tabsysprefs{AdvancedSearchTypes}     = "Searching";
267 $tabsysprefs{DisplayMultiPlaceHold}   = "Searching";
268
269 # EnhancedContent
270 $tabsysprefs{AmazonCoverImages}      = "EnhancedContent";
271 $tabsysprefs{OPACAmazonCoverImages}  = "EnhancedContent";
272 $tabsysprefs{AmazonLocale}           = "EnhancedContent";
273 $tabsysprefs{AmazonAssocTag}         = "EnhancedContent";
274
275 # Babelthèque
276 $tabsysprefs{Babeltheque}            = "EnhancedContent";
277
278 # Baker & Taylor
279 $tabsysprefs{BakerTaylorBookstoreURL} = 'EnhancedContent';
280 $tabsysprefs{BakerTaylorEnabled}      = 'EnhancedContent';
281 $tabsysprefs{BakerTaylorPassword}     = 'EnhancedContent';
282 $tabsysprefs{BakerTaylorUsername}     = 'EnhancedContent';
283
284 # Library Thing for Libraries
285 $tabsysprefs{LibraryThingForLibrariesID} = "EnhancedContent";
286 $tabsysprefs{LibraryThingForLibrariesEnabled} = "EnhancedContent";
287 $tabsysprefs{LibraryThingForLibrariesTabbedView} = "EnhancedContent";
288
289 # Syndetics
290 $tabsysprefs{SyndeticsClientCode}     = 'EnhancedContent';
291 $tabsysprefs{SyndeticsEnabled}        = 'EnhancedContent';
292 $tabsysprefs{SyndeticsCoverImages}    = 'EnhancedContent';
293 $tabsysprefs{SyndeticsTOC}            = 'EnhancedContent';
294 $tabsysprefs{SyndeticsSummary}        = 'EnhancedContent';
295 $tabsysprefs{SyndeticsEditions}       = 'EnhancedContent';
296 $tabsysprefs{SyndeticsExcerpt}        = 'EnhancedContent';
297 $tabsysprefs{SyndeticsReviews}        = 'EnhancedContent';
298 $tabsysprefs{SyndeticsAuthorNotes}    = 'EnhancedContent';
299 $tabsysprefs{SyndeticsAwards}         = 'EnhancedContent';
300 $tabsysprefs{SyndeticsSeries}         = 'EnhancedContent';
301 $tabsysprefs{SyndeticsCoverImageSize} = 'EnhancedContent';
302
303
304 # FRBR
305 $tabsysprefs{FRBRizeEditions}     = "EnhancedContent";
306 $tabsysprefs{XISBN}               = "EnhancedContent";
307 $tabsysprefs{OCLCAffiliateID}     = "EnhancedContent";
308 $tabsysprefs{XISBNDailyLimit}     = "EnhancedContent";
309 $tabsysprefs{ThingISBN}           = "EnhancedContent";
310 $tabsysprefs{OPACFRBRizeEditions} = "EnhancedContent";
311
312 # Tags
313 $tabsysprefs{TagsEnabled}            = 'EnhancedContent';
314 $tabsysprefs{TagsExternalDictionary} = 'EnhancedContent';
315 $tabsysprefs{TagsInputOnDetail}      = 'EnhancedContent';
316 $tabsysprefs{TagsInputOnList}        = 'EnhancedContent';
317 $tabsysprefs{TagsShowOnDetail}       = 'EnhancedContent';
318 $tabsysprefs{TagsShowOnList}         = 'EnhancedContent';
319 $tabsysprefs{TagsModeration}         = 'EnhancedContent';
320 $tabsysprefs{GoogleJackets}          = 'EnhancedContent';
321 $tabsysprefs{AuthorisedValueImages}  = "EnhancedContent";
322
323 # OPAC
324 $tabsysprefs{BiblioDefaultView}          = "OPAC";
325 $tabsysprefs{LibraryName}                = "OPAC";
326 $tabsysprefs{opaccolorstylesheet}        = "OPAC";
327 $tabsysprefs{opaccredits}                = "OPAC";
328 $tabsysprefs{opaclayoutstylesheet}       = "OPAC";
329 $tabsysprefs{OpacNav}                    = "OPAC";
330 $tabsysprefs{opacsmallimage}             = "OPAC";
331 $tabsysprefs{opacthemes}                 = "OPAC";
332 $tabsysprefs{opacuserjs}                 = "OPAC";
333 $tabsysprefs{opacheader}                 = "OPAC";
334 $tabsysprefs{hideBiblioNumber}           = "OPAC";
335 $tabsysprefs{OpacMainUserBlock}          = "OPAC";
336 $tabsysprefs{OPACURLOpenInNewWindow}     = "OPAC";
337 $tabsysprefs{OPACUserCSS}                = "OPAC";
338 $tabsysprefs{OPACHighlightedWords}       = "OPAC";
339 $tabsysprefs{OPACViewOthersSuggestions}  = "OPAC";
340 $tabsysprefs{URLLinkText}                = "OPAC";
341 $tabsysprefs{OPACSearchForTitleIn}       = "OPAC";
342 $tabsysprefs{OPACShelfBrowser}           = "OPAC";
343 $tabsysprefs{OPACShowHoldQueueDetails}   = "OPAC";
344 $tabsysprefs{OPACAllowHoldDateInFuture}  = "OPAC";
345 $tabsysprefs{OPACPatronDetails}  = "OPAC";
346 $tabsysprefs{OPACFinesTab}  = "OPAC";
347 $tabsysprefs{DisplayOPACiconsXSLT}       = "OPAC";
348 $tabsysprefs{AutoSelfCheckAllowed}       = "OPAC";
349 $tabsysprefs{AutoSelfCheckID}            = "OPAC";
350 $tabsysprefs{AutoSelfCheckPass}          = "OPAC";
351
352 # OPAC
353 $tabsysprefs{SearchMyLibraryFirst} = "OPAC";
354 $tabsysprefs{hidelostitems}        = "OPAC";
355 $tabsysprefs{opacbookbag}          = "OPAC";
356 $tabsysprefs{OpacPasswordChange}   = "OPAC";
357 $tabsysprefs{opacreadinghistory}   = "OPAC";
358 $tabsysprefs{virtualshelves}       = "OPAC";
359 $tabsysprefs{RequestOnOpac}        = "OPAC";
360 $tabsysprefs{reviewson}            = "OPAC";
361 $tabsysprefs{OpacTopissues}        = "OPAC";
362 $tabsysprefs{OpacAuthorities}      = "OPAC";
363 $tabsysprefs{OpacCloud}            = "OPAC";
364 $tabsysprefs{opacuserlogin}        = "OPAC";
365 $tabsysprefs{AnonSuggestions}      = "OPAC";
366 $tabsysprefs{suggestion}           = "OPAC";
367 $tabsysprefs{OpacTopissue}         = "OPAC";
368 $tabsysprefs{OpacBrowser}          = "OPAC";
369 $tabsysprefs{OpacRenewalAllowed}   = "OPAC";
370 $tabsysprefs{OPACItemHolds}        = "OPAC";
371 $tabsysprefs{OPACGroupResults}     = "OPAC";
372 $tabsysprefs{XSLTDetailsDisplay}   = "OPAC";
373 $tabsysprefs{XSLTResultsDisplay}   = "OPAC";
374 $tabsysprefs{OPACShowCheckoutName}   = "OPAC";
375 $tabsysprefs{AllowPurchaseSuggestionBranchChoice}   = "OPAC";
376
377 # Serials
378 $tabsysprefs{RoutingListAddReserves}       = "Serials";
379 $tabsysprefs{OPACSerialIssueDisplayCount}  = "Serials";
380 $tabsysprefs{StaffSerialIssueDisplayCount} = "Serials";
381 $tabsysprefs{RenewSerialAddsSuggestion}    = "Serials";
382 $tabsysprefs{SubscriptionHistory}          = "Serials";
383
384 # LOGFeatures
385 $tabsysprefs{CataloguingLog}  = "Logs";
386 $tabsysprefs{BorrowersLog}    = "Logs";
387 $tabsysprefs{SubscriptionLog} = "Logs";
388 $tabsysprefs{IssueLog}        = "Logs";
389 $tabsysprefs{ReturnLog}       = "Logs";
390 $tabsysprefs{LetterLog}       = "Logs";
391 $tabsysprefs{FinesLog}        = "Logs";
392
393 # OAI-PMH variables
394 $tabsysprefs{'OAI-PMH'}           = "OAI-PMH";
395 $tabsysprefs{'OAI-PMH:archiveID'} = "OAI-PMH";
396 $tabsysprefs{'OAI-PMH:MaxCount'}  = "OAI-PMH";
397
398 # ILS-DI variables
399 $tabsysprefs{'ILS-DI'} = "ILS-DI";
400 $tabsysprefs{'ILS-DI:AuthorizedIPs'}    = "Admin";
401
402 # Creator variables
403
404 $tabsysprefs{'ImageLimit'} = "Creators";
405
406 sub StringSearch {
407     my ( $searchstring, $type ) = @_;
408     my $dbh = C4::Context->dbh;
409     $searchstring =~ s/\'/\\\'/g;
410     my @data = split( ' ', $searchstring );
411     my $count = @data;
412     my @results;
413     my $cnt = 0;
414     my $sth;
415
416     # used for doing a plain-old sys-pref search
417     if ( $type && $type ne 'all' ) {
418         foreach my $syspref ( sort { lc $a cmp lc $b } keys %tabsysprefs ) {
419             if ( $tabsysprefs{$syspref} eq $type ) {
420                 my $sth = $dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
421                 $sth->execute($syspref);
422                 while ( my $data = $sth->fetchrow_hashref ) {
423                     $data->{shortvalue} = $data->{value};
424                     $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if defined( $data->{value} ) and length( $data->{value} ) > 60;
425                     push( @results, $data );
426                     $cnt++;
427                 }
428                 $sth->finish;
429             }
430         }
431     } else {
432         my $sth;
433
434         if ( $type and $type eq 'all' ) {
435             $sth = $dbh->prepare( "
436             SELECT *
437               FROM systempreferences
438               WHERE variable LIKE ? OR explanation LIKE ?
439               ORDER BY VARIABLE" );
440             $sth->execute( "%$searchstring%", "%$searchstring%" );
441         } else {
442             my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable in (";
443             my $first = 1;
444             for my $name ( get_local_prefs() ) {
445                 $strsth .= ',' unless $first;
446                 $strsth .= "'$name'";
447                 $first = 0;
448             }
449             $strsth .= ") order by variable";
450             $sth = $dbh->prepare($strsth);
451             $sth->execute();
452         }
453
454         while ( my $data = $sth->fetchrow_hashref ) {
455             $data->{shortvalue} = $data->{value};
456             $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if length( $data->{value} ) > 60;
457             push( @results, $data );
458             $cnt++;
459         }
460
461         $sth->finish;
462     }
463     return ( $cnt, \@results );
464 }
465
466 sub GetPrefParams {
467     my $data   = shift;
468     my $params = $data;
469     my @options;
470
471     if ( defined $data->{'options'} ) {
472         foreach my $option ( split( /\|/, $data->{'options'} ) ) {
473             my $selected = '0';
474             defined( $data->{'value'} ) and $option eq $data->{'value'} and $selected = 1;
475             push @options, { option => $option, selected => $selected };
476         }
477     }
478
479     $params->{'prefoptions'} = $data->{'options'};
480
481     if ( not defined( $data->{'type'} ) ) {
482         $params->{'type_free'} = 1;
483         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 );
484     } elsif ( $data->{'type'} eq 'Upload' ) {
485         $params->{'type_upload'} = 1;
486     } elsif ( $data->{'type'} eq 'Choice' ) {
487         $params->{'type_choice'} = 1;
488     } elsif ( $data->{'type'} eq 'YesNo' ) {
489         $params->{'type_yesno'} = 1;
490         $data->{'value'}        = C4::Context->boolean_preference( $data->{'variable'} );
491         if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
492             $params->{'value_yes'} = 1;
493         } else {
494             $params->{'value_no'} = 1;
495         }
496     } elsif ( $data->{'type'} eq 'Integer' || $data->{'type'} eq 'Float' ) {
497         $params->{'type_free'} = 1;
498         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 10;
499     } elsif ( $data->{'type'} eq 'Textarea' ) {
500         $params->{'type_textarea'} = 1;
501         $data->{options} =~ /(.*)\|(.*)/;
502         $params->{'cols'} = $1;
503         $params->{'rows'} = $2;
504     } elsif ( $data->{'type'} eq 'Themes' ) {
505         $params->{'type_choice'} = 1;
506         my $type = '';
507         ( $data->{'variable'} =~ m#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
508         @options = ();
509         my $currently_selected_themes;
510         my $counter = 0;
511         foreach my $theme ( split /\s+/, $data->{'value'} ) {
512             push @options, { option => $theme, counter => $counter };
513             $currently_selected_themes->{$theme} = 1;
514             $counter++;
515         }
516         foreach my $theme ( getallthemes($type) ) {
517             my $selected = '0';
518             next if $currently_selected_themes->{$theme};
519             push @options, { option => $theme, counter => $counter };
520             $counter++;
521         }
522     } elsif ( $data->{'type'} eq 'ClassSources' ) {
523         $params->{'type_choice'} = 1;
524         my $type = '';
525         @options = ();
526         my $sources = GetClassSources();
527         my $counter = 0;
528         foreach my $cn_source ( sort keys %$sources ) {
529             if ( $cn_source eq $data->{'value'} ) {
530                 push @options, { option => $cn_source, counter => $counter, selected => 1 };
531             } else {
532                 push @options, { option => $cn_source, counter => $counter };
533             }
534             $counter++;
535         }
536     } elsif ( $data->{'type'} eq 'Languages' ) {
537         my $currently_selected_languages;
538         foreach my $language ( split /\s+/, $data->{'value'} ) {
539             $currently_selected_languages->{$language} = 1;
540         }
541
542         # current language
543         my $lang = $params->{'lang'};
544         my $theme;
545         my $interface;
546         if ( $data->{'variable'} =~ /opac/ ) {
547
548             # this is the OPAC
549             $interface = 'opac';
550             $theme     = C4::Context->preference('opacthemes');
551         } else {
552
553             # this is the staff client
554             $interface = 'intranet';
555             $theme     = C4::Context->preference('template');
556         }
557         my $languages_loop = getTranslatedLanguages( $interface, $theme, $lang, $currently_selected_languages );
558
559         $params->{'languages_loop'}    = $languages_loop;
560         $params->{'type_langselector'} = 1;
561     } else {
562         $params->{'type_free'} = 1;
563         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 30;
564     }
565
566     if ( $params->{'type_choice'} || $params->{'type_free'} || $params->{'type_yesno'} ) {
567         $params->{'oneline'} = 1;
568     }
569
570     $params->{'preftype'} = $data->{'type'};
571     $params->{'options'}  = \@options;
572
573     return $params;
574 }
575
576 my $input       = new CGI;
577 my $searchfield = $input->param('searchfield') || '';
578 my $Tvalue      = $input->param('Tvalue');
579 my $offset      = $input->param('offset') || 0;
580 my $script_name = "/cgi-bin/koha/admin/systempreferences.pl";
581
582 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
583     {   template_name   => "admin/systempreferences.tmpl",
584         query           => $input,
585         type            => "intranet",
586         authnotrequired => 0,
587         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
588         debug           => 1,
589     }
590 );
591 my $pagesize = 100;
592 my $op = $input->param('op') || '';
593 $searchfield =~ s/\,//g;
594
595 if ($op) {
596     $template->param(
597         script_name => $script_name,
598         $op         => 1
599     );    # we show only the TMPL_VAR names $op
600 } else {
601     $template->param(
602         script_name => $script_name,
603         else        => 1
604     );    # we show only the TMPL_VAR names $op
605 }
606
607 if ( $op eq 'update_and_reedit' ) {
608     foreach ( $input->param ) {
609     }
610     my $value = '';
611     if ( my $currentorder = $input->param('currentorder') ) {
612         my @currentorder = split /\|/, $currentorder;
613         my $orderchanged = 0;
614         foreach my $param ( $input->param ) {
615             if ( $param =~ m#up-(\d+).x# ) {
616                 my $temp = $currentorder[$1];
617                 $currentorder[$1]       = $currentorder[ $1 - 1 ];
618                 $currentorder[ $1 - 1 ] = $temp;
619                 $orderchanged           = 1;
620                 last;
621             } elsif ( $param =~ m#down-(\d+).x# ) {
622                 my $temp = $currentorder[$1];
623                 $currentorder[$1]       = $currentorder[ $1 + 1 ];
624                 $currentorder[ $1 + 1 ] = $temp;
625                 $orderchanged           = 1;
626                 last;
627             }
628         }
629         $value = join ' ', @currentorder;
630         if ($orderchanged) {
631             $op = 'add_form';
632             $template->param(
633                 script_name => $script_name,
634                 $op         => 1
635             );    # we show only the TMPL_VAR names $op
636         } else {
637             $op          = '';
638             $searchfield = '';
639             $template->param(
640                 script_name => $script_name,
641                 else        => 1
642             );    # we show only the TMPL_VAR names $op
643         }
644     }
645     my $dbh   = C4::Context->dbh;
646     my $query = "select * from systempreferences where variable=?";
647     my $sth   = $dbh->prepare($query);
648     $sth->execute( $input->param('variable') );
649     if ( $sth->rows ) {
650         unless ( C4::Context->config('demo') ) {
651             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
652             $sth->execute( $value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions') );
653             $sth->finish;
654             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
655         }
656     } else {
657         unless ( C4::Context->config('demo') ) {
658             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
659             $sth->execute( $input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
660             $sth->finish;
661             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $input->param('value') );
662         }
663     }
664     $sth->finish;
665
666 }
667
668 ################## ADD_FORM ##################################
669 # called by default. Used to create form to add or  modify a record
670
671 if ( $op eq 'add_form' ) {
672
673     #---- if primkey exists, it's a modify action, so read values to modify...
674     my $data;
675     if ($searchfield) {
676         my $dbh = C4::Context->dbh;
677         my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
678         $sth->execute($searchfield);
679         $data = $sth->fetchrow_hashref;
680         $sth->finish;
681         $template->param( modify => 1 );
682
683         # save tab to return to if user cancels edit
684         $template->param( return_tab => $tabsysprefs{$searchfield} );
685     }
686
687     $data->{'lang'} = $template->param('lang');
688     my $prefparams = GetPrefParams($data);
689     $template->param( %$prefparams );
690     $template->param( searchfield => $searchfield );
691
692 ################## ADD_VALIDATE ##################################
693     # called by add_form, used to insert/modify data in DB
694 } elsif ( $op eq 'add_validate' ) {
695     my $dbh = C4::Context->dbh;
696     my $sth = $dbh->prepare("select * from systempreferences where variable=?");
697     $sth->execute( $input->param('variable') );
698
699     # to handle multiple values
700     my $value;
701
702     # handle multiple value strings (separated by ',')
703     my $params = $input->Vars;
704     if ( defined $params->{'value'} ) {
705         my @values = ();
706         @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
707         if (@values) {
708             $value = "";
709             for my $vl (@values) {
710                 $value .= "$vl,";
711             }
712             $value =~ s/,$//;
713         } else {
714             $value = $params->{'value'};
715         }
716     }
717
718     if ( $input->param('preftype') eq 'Upload' ) {
719         my $lgtfh = $input->upload('value');
720         $value = join '', <$lgtfh>;
721         $value = encode_base64($value);
722     }
723
724     if ( $sth->rows ) {
725         unless ( C4::Context->config('demo') ) {
726             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
727             $sth->execute( $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable') );
728             $sth->finish;
729             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
730         }
731     } else {
732         unless ( C4::Context->config('demo') ) {
733             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
734             $sth->execute( $input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
735             $sth->finish;
736             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $value );
737         }
738     }
739     $sth->finish;
740     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=\"></html>";
741     exit;
742 ################## DELETE_CONFIRM ##################################
743     # called by default form, used to confirm deletion of data in DB
744 } elsif ( $op eq 'delete_confirm' ) {
745     my $dbh = C4::Context->dbh;
746     my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
747     $sth->execute($searchfield);
748     my $data = $sth->fetchrow_hashref;
749     $sth->finish;
750     $template->param(
751         searchfield => $searchfield,
752         Tvalue      => $data->{'value'},
753     );
754
755     # END $OP eq DELETE_CONFIRM
756 ################## DELETE_CONFIRMED ##################################
757     # called by delete_confirm, used to effectively confirm deletion of data in DB
758 } elsif ( $op eq 'delete_confirmed' ) {
759     my $dbh = C4::Context->dbh;
760     my $sth = $dbh->prepare("delete from systempreferences where variable=?");
761     $sth->execute($searchfield);
762     my $logstring = $searchfield . " | " . $Tvalue;
763     logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring );
764     $sth->finish;
765
766     # END $OP eq DELETE_CONFIRMED
767 ################## DEFAULT ##################################
768 } else {    # DEFAULT
769             #Adding tab management for system preferences
770     my $tab = $input->param('tab');
771     $template->param( $tab => 1 );
772     my ( $count, $results ) = StringSearch( $searchfield, $tab );
773     my @loop_data = ();
774     for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
775         my $row_data = $results->[$i];
776         $row_data->{'lang'} = $template->param('lang');
777         $row_data           = GetPrefParams($row_data);                                                         # get a fresh hash for the row data
778         $row_data->{edit}   = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'variable'};
779         $row_data->{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'variable'};
780         push( @loop_data, $row_data );
781     }
782     $tab = ( $tab ? $tab : "local_use" );
783     $template->param( loop => \@loop_data, $tab => 1 );
784     if ( $offset > 0 ) {
785         my $prevpage = $offset - $pagesize;
786         $template->param( "<a href=$script_name?offset=" . $prevpage . '&lt;&lt; Prev</a>' );
787     }
788     if ( $offset + $pagesize < $count ) {
789         my $nextpage = $offset + $pagesize;
790         $template->param( "a href=$script_name?offset=" . $nextpage . 'Next &gt;&gt;</a>' );
791     }
792     $template->param( tab => $tab, );
793 }    #---- END $OP eq DEFAULT
794 output_html_with_http_headers $input, $cookie, $template->output;
795
796
797 # Return an array containing all preferences defined in current Koha instance
798 # .pref files.
799
800 sub get_prefs_from_files {
801     my $context       = C4::Context->new();
802     my $path_pref_en  = $context->config('intrahtdocs') .
803                         '/prog/en/modules/admin/preferences';
804     # Get all .pref file names
805     opendir ( my $fh, $path_pref_en );
806     my @pref_files = grep { /.pref/ } readdir($fh);
807     close $fh;
808
809     my @names = ();
810     my $append = sub {
811         my $prefs = shift;
812         for my $pref ( @$prefs ) {
813             for my $element ( @$pref ) {
814                 if ( ref( $element) eq 'HASH' ) {
815                     my $name = $element->{pref};
816                     next unless $name;
817                     push @names, $name;
818                     next;
819                 }
820             }
821         }
822     };
823     for my $file (@pref_files) {
824         my $pref = LoadFile( "$path_pref_en/$file" );
825         for my $tab ( keys %$pref ) {
826             my $content = $pref->{$tab};
827             if ( ref($content) eq 'ARRAY' ) {
828                 $append->($content);
829                 next;
830             }
831             for my $section ( keys %$content ) {
832                 my $syspref = $content->{$section};
833                 $append->($syspref);
834             }
835         }
836     }
837     return @names;
838 }
839
840
841 # Return an array containg all preferences defined in DB
842
843 sub get_prefs_from_db {
844     my $dbh = C4::Context->dbh;
845     my $sth = $dbh->prepare("SELECT variable FROM systempreferences");
846     $sth->execute;
847     my @names = ();
848     while ( (my $name) = $sth->fetchrow_array ) {
849         push @names, $name if $name;
850     }
851     return @names;
852 }
853
854
855 # Return an array containing all local preferences: those which are defined in
856 # DB and not defined in Koha .pref files.
857
858 sub get_local_prefs {
859     my @prefs_file = get_prefs_from_files();
860     my @prefs_db = get_prefs_from_db();
861
862     my %prefs_file = map { $_ => 1 } @prefs_file;
863     my @names = ();
864     foreach my $name (@prefs_db) {
865         push @names, $name  unless $prefs_file{$name};
866     }
867
868     return @names;
869 }
870