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