Merge branch 'bug_2832' into 3.12-master
[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                     unless (defined $data->{value}) { $data->{value} = "";}
424                     $data->{shortvalue} = $data->{value};
425                     $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if length( $data->{value} ) > 60;
426                     push( @results, $data );
427                     $cnt++;
428                 }
429                 $sth->finish;
430             }
431         }
432     } else {
433         my $sth;
434
435         if ( $type and $type eq 'all' ) {
436             $sth = $dbh->prepare( "
437             SELECT *
438               FROM systempreferences
439               WHERE variable LIKE ? OR explanation LIKE ?
440               ORDER BY VARIABLE" );
441             $sth->execute( "%$searchstring%", "%$searchstring%" );
442         } else {
443             my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable in (";
444             my $first = 1;
445             for my $name ( get_local_prefs() ) {
446                 $strsth .= ',' unless $first;
447                 $strsth .= "'$name'";
448                 $first = 0;
449             }
450             $strsth .= ") order by variable";
451             $sth = $dbh->prepare($strsth);
452             $sth->execute();
453         }
454
455         while ( my $data = $sth->fetchrow_hashref ) {
456             unless (defined $data->{value}) { $data->{value} = "";}
457             $data->{shortvalue} = $data->{value};
458             $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if length( $data->{value} ) > 60;
459             push( @results, $data );
460             $cnt++;
461         }
462
463         $sth->finish;
464     }
465     return ( $cnt, \@results );
466 }
467
468 sub GetPrefParams {
469     my $data   = shift;
470     my $params = $data;
471     my @options;
472
473     if ( defined $data->{'options'} ) {
474         foreach my $option ( split( /\|/, $data->{'options'} ) ) {
475             my $selected = '0';
476             defined( $data->{'value'} ) and $option eq $data->{'value'} and $selected = 1;
477             push @options, { option => $option, selected => $selected };
478         }
479     }
480
481     $params->{'prefoptions'} = $data->{'options'};
482
483     if ( not defined( $data->{'type'} ) ) {
484         $params->{'type_free'} = 1;
485         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 );
486     } elsif ( $data->{'type'} eq 'Upload' ) {
487         $params->{'type_upload'} = 1;
488     } elsif ( $data->{'type'} eq 'Choice' ) {
489         $params->{'type_choice'} = 1;
490     } elsif ( $data->{'type'} eq 'YesNo' ) {
491         $params->{'type_yesno'} = 1;
492         $data->{'value'}        = C4::Context->boolean_preference( $data->{'variable'} );
493         if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
494             $params->{'value_yes'} = 1;
495         } else {
496             $params->{'value_no'} = 1;
497         }
498     } elsif ( $data->{'type'} eq 'Integer' || $data->{'type'} eq 'Float' ) {
499         $params->{'type_free'} = 1;
500         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 10;
501     } elsif ( $data->{'type'} eq 'Textarea' ) {
502         $params->{'type_textarea'} = 1;
503         $data->{options} =~ /(.*)\|(.*)/;
504         $params->{'cols'} = $1;
505         $params->{'rows'} = $2;
506     } elsif ( $data->{'type'} eq 'Themes' ) {
507         $params->{'type_choice'} = 1;
508         my $type = '';
509         ( $data->{'variable'} =~ m#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
510         @options = ();
511         my $currently_selected_themes;
512         my $counter = 0;
513         foreach my $theme ( split /\s+/, $data->{'value'} ) {
514             push @options, { option => $theme, counter => $counter };
515             $currently_selected_themes->{$theme} = 1;
516             $counter++;
517         }
518         foreach my $theme ( getallthemes($type) ) {
519             my $selected = '0';
520             next if $currently_selected_themes->{$theme};
521             push @options, { option => $theme, counter => $counter };
522             $counter++;
523         }
524     } elsif ( $data->{'type'} eq 'ClassSources' ) {
525         $params->{'type_choice'} = 1;
526         my $type = '';
527         @options = ();
528         my $sources = GetClassSources();
529         my $counter = 0;
530         foreach my $cn_source ( sort keys %$sources ) {
531             if ( $cn_source eq $data->{'value'} ) {
532                 push @options, { option => $cn_source, counter => $counter, selected => 1 };
533             } else {
534                 push @options, { option => $cn_source, counter => $counter };
535             }
536             $counter++;
537         }
538     } elsif ( $data->{'type'} eq 'Languages' ) {
539         my $currently_selected_languages;
540         foreach my $language ( split /\s+/, $data->{'value'} ) {
541             $currently_selected_languages->{$language} = 1;
542         }
543
544         # current language
545         my $lang = $params->{'lang'};
546         my $theme;
547         my $interface;
548         if ( $data->{'variable'} =~ /opac/ ) {
549
550             # this is the OPAC
551             $interface = 'opac';
552             $theme     = C4::Context->preference('opacthemes');
553         } else {
554
555             # this is the staff client
556             $interface = 'intranet';
557             $theme     = C4::Context->preference('template');
558         }
559         my $languages_loop = getTranslatedLanguages( $interface, $theme, $lang, $currently_selected_languages );
560
561         $params->{'languages_loop'}    = $languages_loop;
562         $params->{'type_langselector'} = 1;
563     } else {
564         $params->{'type_free'} = 1;
565         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 30;
566     }
567
568     if ( $params->{'type_choice'} || $params->{'type_free'} || $params->{'type_yesno'} ) {
569         $params->{'oneline'} = 1;
570     }
571
572     $params->{'preftype'} = $data->{'type'};
573     $params->{'options'}  = \@options;
574
575     return $params;
576 }
577
578 my $input       = new CGI;
579 my $searchfield = $input->param('searchfield') || '';
580 my $Tvalue      = $input->param('Tvalue');
581 my $offset      = $input->param('offset') || 0;
582 my $script_name = "/cgi-bin/koha/admin/systempreferences.pl";
583
584 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
585     {   template_name   => "admin/systempreferences.tmpl",
586         query           => $input,
587         type            => "intranet",
588         authnotrequired => 0,
589         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
590         debug           => 1,
591     }
592 );
593 my $pagesize = 100;
594 my $op = $input->param('op') || '';
595 $searchfield =~ s/\,//g;
596
597 if ($op) {
598     $template->param(
599         script_name => $script_name,
600         $op         => 1
601     );    # we show only the TMPL_VAR names $op
602 } else {
603     $template->param(
604         script_name => $script_name,
605         else        => 1
606     );    # we show only the TMPL_VAR names $op
607 }
608
609 if ( $op eq 'update_and_reedit' ) {
610     foreach ( $input->param ) {
611     }
612     my $value = '';
613     if ( my $currentorder = $input->param('currentorder') ) {
614         my @currentorder = split /\|/, $currentorder;
615         my $orderchanged = 0;
616         foreach my $param ( $input->param ) {
617             if ( $param =~ m#up-(\d+).x# ) {
618                 my $temp = $currentorder[$1];
619                 $currentorder[$1]       = $currentorder[ $1 - 1 ];
620                 $currentorder[ $1 - 1 ] = $temp;
621                 $orderchanged           = 1;
622                 last;
623             } elsif ( $param =~ m#down-(\d+).x# ) {
624                 my $temp = $currentorder[$1];
625                 $currentorder[$1]       = $currentorder[ $1 + 1 ];
626                 $currentorder[ $1 + 1 ] = $temp;
627                 $orderchanged           = 1;
628                 last;
629             }
630         }
631         $value = join ' ', @currentorder;
632         if ($orderchanged) {
633             $op = 'add_form';
634             $template->param(
635                 script_name => $script_name,
636                 $op         => 1
637             );    # we show only the TMPL_VAR names $op
638         } else {
639             $op          = '';
640             $searchfield = '';
641             $template->param(
642                 script_name => $script_name,
643                 else        => 1
644             );    # we show only the TMPL_VAR names $op
645         }
646     }
647     my $dbh   = C4::Context->dbh;
648     my $query = "select * from systempreferences where variable=?";
649     my $sth   = $dbh->prepare($query);
650     $sth->execute( $input->param('variable') );
651     if ( $sth->rows ) {
652         unless ( C4::Context->config('demo') ) {
653             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
654             $sth->execute( $value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions') );
655             $sth->finish;
656             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
657         }
658     } else {
659         unless ( C4::Context->config('demo') ) {
660             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
661             $sth->execute( $input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
662             $sth->finish;
663             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $input->param('value') );
664         }
665     }
666     $sth->finish;
667
668 }
669
670 ################## ADD_FORM ##################################
671 # called by default. Used to create form to add or  modify a record
672
673 if ( $op eq 'add_form' ) {
674
675     #---- if primkey exists, it's a modify action, so read values to modify...
676     my $data;
677     if ($searchfield) {
678         my $dbh = C4::Context->dbh;
679         my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
680         $sth->execute($searchfield);
681         $data = $sth->fetchrow_hashref;
682         $sth->finish;
683         $template->param( modify => 1 );
684
685         # save tab to return to if user cancels edit
686         $template->param( return_tab => $tabsysprefs{$searchfield} );
687     }
688
689     $data->{'lang'} = $template->param('lang');
690     my $prefparams = GetPrefParams($data);
691     $template->param( %$prefparams );
692     $template->param( searchfield => $searchfield );
693
694 ################## ADD_VALIDATE ##################################
695     # called by add_form, used to insert/modify data in DB
696 } elsif ( $op eq 'add_validate' ) {
697     my $dbh = C4::Context->dbh;
698     my $sth = $dbh->prepare("select * from systempreferences where variable=?");
699     $sth->execute( $input->param('variable') );
700
701     # to handle multiple values
702     my $value;
703
704     # handle multiple value strings (separated by ',')
705     my $params = $input->Vars;
706     if ( defined $params->{'value'} ) {
707         my @values = ();
708         @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
709         if (@values) {
710             $value = "";
711             for my $vl (@values) {
712                 $value .= "$vl,";
713             }
714             $value =~ s/,$//;
715         } else {
716             $value = $params->{'value'};
717         }
718     }
719
720     if ( $input->param('preftype') eq 'Upload' ) {
721         my $lgtfh = $input->upload('value');
722         $value = join '', <$lgtfh>;
723         $value = encode_base64($value);
724     }
725
726     if ( $sth->rows ) {
727         unless ( C4::Context->config('demo') ) {
728             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
729             $sth->execute( $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable') );
730             $sth->finish;
731             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
732         }
733     } else {
734         unless ( C4::Context->config('demo') ) {
735             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
736             $sth->execute( $input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
737             $sth->finish;
738             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $value );
739         }
740     }
741     $sth->finish;
742     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=\"></html>";
743     exit;
744 ################## DELETE_CONFIRM ##################################
745     # called by default form, used to confirm deletion of data in DB
746 } elsif ( $op eq 'delete_confirm' ) {
747     my $dbh = C4::Context->dbh;
748     my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
749     $sth->execute($searchfield);
750     my $data = $sth->fetchrow_hashref;
751     $sth->finish;
752     $template->param(
753         searchfield => $searchfield,
754         Tvalue      => $data->{'value'},
755     );
756
757     # END $OP eq DELETE_CONFIRM
758 ################## DELETE_CONFIRMED ##################################
759     # called by delete_confirm, used to effectively confirm deletion of data in DB
760 } elsif ( $op eq 'delete_confirmed' ) {
761     my $dbh = C4::Context->dbh;
762     my $sth = $dbh->prepare("delete from systempreferences where variable=?");
763     $sth->execute($searchfield);
764     my $logstring = $searchfield . " | " . $Tvalue;
765     logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring );
766     $sth->finish;
767
768     # END $OP eq DELETE_CONFIRMED
769 ################## DEFAULT ##################################
770 } else {    # DEFAULT
771             #Adding tab management for system preferences
772     my $tab = $input->param('tab');
773     $template->param( $tab => 1 );
774     my ( $count, $results ) = StringSearch( $searchfield, $tab );
775     my @loop_data = ();
776     for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
777         my $row_data = $results->[$i];
778         $row_data->{'lang'} = $template->param('lang');
779         $row_data           = GetPrefParams($row_data);                                                         # get a fresh hash for the row data
780         $row_data->{edit}   = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'variable'};
781         $row_data->{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'variable'};
782         push( @loop_data, $row_data );
783     }
784     $tab = ( $tab ? $tab : "local_use" );
785     $template->param( loop => \@loop_data, $tab => 1 );
786     if ( $offset > 0 ) {
787         my $prevpage = $offset - $pagesize;
788         $template->param( "<a href=$script_name?offset=" . $prevpage . '&lt;&lt; Prev</a>' );
789     }
790     if ( $offset + $pagesize < $count ) {
791         my $nextpage = $offset + $pagesize;
792         $template->param( "a href=$script_name?offset=" . $nextpage . 'Next &gt;&gt;</a>' );
793     }
794     $template->param( tab => $tab, );
795 }    #---- END $OP eq DEFAULT
796 output_html_with_http_headers $input, $cookie, $template->output;
797
798
799 # Return an array containing all preferences defined in current Koha instance
800 # .pref files.
801
802 sub get_prefs_from_files {
803     my $context       = C4::Context->new();
804     my $path_pref_en  = $context->config('intrahtdocs') .
805                         '/prog/en/modules/admin/preferences';
806     # Get all .pref file names
807     opendir ( my $fh, $path_pref_en );
808     my @pref_files = grep { /.pref/ } readdir($fh);
809     close $fh;
810
811     my @names = ();
812     my $append = sub {
813         my $prefs = shift;
814         for my $pref ( @$prefs ) {
815             for my $element ( @$pref ) {
816                 if ( ref( $element) eq 'HASH' ) {
817                     my $name = $element->{pref};
818                     next unless $name;
819                     push @names, $name;
820                     next;
821                 }
822             }
823         }
824     };
825     for my $file (@pref_files) {
826         my $pref = LoadFile( "$path_pref_en/$file" );
827         for my $tab ( keys %$pref ) {
828             my $content = $pref->{$tab};
829             if ( ref($content) eq 'ARRAY' ) {
830                 $append->($content);
831                 next;
832             }
833             for my $section ( keys %$content ) {
834                 my $syspref = $content->{$section};
835                 $append->($syspref);
836             }
837         }
838     }
839     return @names;
840 }
841
842
843 # Return an array containg all preferences defined in DB
844
845 sub get_prefs_from_db {
846     my $dbh = C4::Context->dbh;
847     my $sth = $dbh->prepare("SELECT variable FROM systempreferences");
848     $sth->execute;
849     my @names = ();
850     while ( (my $name) = $sth->fetchrow_array ) {
851         push @names, $name if $name;
852     }
853     return @names;
854 }
855
856
857 # Return an array containing all local preferences: those which are defined in
858 # DB and not defined in Koha .pref files.
859
860 sub get_local_prefs {
861     my @prefs_file = get_prefs_from_files();
862     my @prefs_db = get_prefs_from_db();
863
864     my %prefs_file = map { $_ => 1 } @prefs_file;
865     my @names = ();
866     foreach my $name (@prefs_db) {
867         push @names, $name  unless $prefs_file{$name};
868     }
869
870     return @names;
871 }
872