bug 4358: remove disused ReadingHistory syspref and related code
[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
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{BorrowerMandatoryField}       = "Patrons";
203 $tabsysprefs{borrowerRelationship}         = "Patrons";
204 $tabsysprefs{BorrowersTitles}              = "Patrons";
205 $tabsysprefs{patronimages}                 = "Patrons";
206 $tabsysprefs{minPasswordLength}            = "Patrons";
207 $tabsysprefs{uppercasesurnames}            = "Patrons";
208 $tabsysprefs{MaxFine}                      = "Patrons";
209 $tabsysprefs{NotifyBorrowerDeparture}      = "Patrons";
210 $tabsysprefs{AddPatronLists}               = "Patrons";
211 $tabsysprefs{PatronsPerPage}               = "Patrons";
212 $tabsysprefs{ExtendedPatronAttributes}     = "Patrons";
213 $tabsysprefs{AutoEmailOpacUser}            = "Patrons";
214 $tabsysprefs{AutoEmailPrimaryAddress}      = "Patrons";
215 $tabsysprefs{EnhancedMessagingPreferences} = "Patrons";
216 $tabsysprefs{'SMSSendDriver'}              = 'Patrons';
217 $tabsysprefs{HidePatronName}               = "Patrons";
218
219
220 # I18N/L10N
221 $tabsysprefs{dateformat}    = "I18N/L10N";
222 $tabsysprefs{opaclanguages} = "I18N/L10N";
223 $tabsysprefs{opaclanguagesdisplay} = "I18N/L10N";
224 $tabsysprefs{language}      = "I18N/L10N";
225
226 # Searching
227 $tabsysprefs{defaultSortField}        = "Searching";
228 $tabsysprefs{defaultSortOrder}        = "Searching";
229 $tabsysprefs{numSearchResults}        = "Searching";
230 $tabsysprefs{OPACdefaultSortField}    = "Searching";
231 $tabsysprefs{OPACdefaultSortOrder}    = "Searching";
232 $tabsysprefs{OPACItemsResultsDisplay} = "Searching";
233 $tabsysprefs{OPACnumSearchResults}    = "Searching";
234 $tabsysprefs{QueryFuzzy}              = "Searching";
235 $tabsysprefs{QueryStemming}           = "Searching";
236 $tabsysprefs{QueryWeightFields}       = "Searching";
237 $tabsysprefs{expandedSearchOption}    = "Searching";
238 $tabsysprefs{sortbynonfiling}         = "Searching";
239 $tabsysprefs{QueryAutoTruncate}       = "Searching";
240 $tabsysprefs{QueryRemoveStopwords}    = "Searching";
241 $tabsysprefs{AdvancedSearchTypes}     = "Searching";
242
243 # EnhancedContent
244 $tabsysprefs{AmazonEnabled}          = "EnhancedContent";
245 $tabsysprefs{OPACAmazonEnabled}      = "EnhancedContent";
246 $tabsysprefs{AmazonCoverImages}      = "EnhancedContent";
247 $tabsysprefs{OPACAmazonCoverImages}  = "EnhancedContent";
248 $tabsysprefs{AWSAccessKeyID}         = "EnhancedContent";
249 $tabsysprefs{AWSPrivateKey}          = "EnhancedContent";
250 $tabsysprefs{AmazonLocale}           = "EnhancedContent";
251 $tabsysprefs{AmazonAssocTag}         = "EnhancedContent";
252 $tabsysprefs{AmazonSimilarItems}     = "EnhancedContent";
253 $tabsysprefs{OPACAmazonSimilarItems} = "EnhancedContent";
254 $tabsysprefs{AmazonReviews}          = "EnhancedContent";
255 $tabsysprefs{OPACAmazonReviews}      = "EnhancedContent";
256
257 # Babelthèque
258 $tabsysprefs{Babeltheque}            = "EnhancedContent";
259
260 # Baker & Taylor
261 $tabsysprefs{BakerTaylorBookstoreURL} = 'EnhancedContent';
262 $tabsysprefs{BakerTaylorEnabled}      = 'EnhancedContent';
263 $tabsysprefs{BakerTaylorPassword}     = 'EnhancedContent';
264 $tabsysprefs{BakerTaylorUsername}     = 'EnhancedContent';
265
266 # Library Thing for Libraries
267 $tabsysprefs{LibraryThingForLibrariesID} = "EnhancedContent";
268 $tabsysprefs{LibraryThingForLibrariesEnabled} = "EnhancedContent";
269 $tabsysprefs{LibraryThingForLibrariesTabbedView} = "EnhancedContent";
270
271 # Syndetics
272 $tabsysprefs{SyndeticsClientCode}     = 'EnhancedContent';
273 $tabsysprefs{SyndeticsEnabled}        = 'EnhancedContent';
274 $tabsysprefs{SyndeticsCoverImages}    = 'EnhancedContent';
275 $tabsysprefs{SyndeticsTOC}            = 'EnhancedContent';
276 $tabsysprefs{SyndeticsSummary}        = 'EnhancedContent';
277 $tabsysprefs{SyndeticsEditions}       = 'EnhancedContent';
278 $tabsysprefs{SyndeticsExcerpt}        = 'EnhancedContent';
279 $tabsysprefs{SyndeticsReviews}        = 'EnhancedContent';
280 $tabsysprefs{SyndeticsAuthorNotes}    = 'EnhancedContent';
281 $tabsysprefs{SyndeticsAwards}         = 'EnhancedContent';
282 $tabsysprefs{SyndeticsSeries}         = 'EnhancedContent';
283 $tabsysprefs{SyndeticsCoverImageSize} = 'EnhancedContent';
284
285
286 # FRBR
287 $tabsysprefs{FRBRizeEditions}     = "EnhancedContent";
288 $tabsysprefs{XISBN}               = "EnhancedContent";
289 $tabsysprefs{OCLCAffiliateID}     = "EnhancedContent";
290 $tabsysprefs{XISBNDailyLimit}     = "EnhancedContent";
291 $tabsysprefs{PINESISBN}           = "EnhancedContent";
292 $tabsysprefs{ThingISBN}           = "EnhancedContent";
293 $tabsysprefs{OPACFRBRizeEditions} = "EnhancedContent";
294
295 # Tags
296 $tabsysprefs{TagsEnabled}            = 'EnhancedContent';
297 $tabsysprefs{TagsExternalDictionary} = 'EnhancedContent';
298 $tabsysprefs{TagsInputOnDetail}      = 'EnhancedContent';
299 $tabsysprefs{TagsInputOnList}        = 'EnhancedContent';
300 $tabsysprefs{TagsShowOnDetail}       = 'EnhancedContent';
301 $tabsysprefs{TagsShowOnList}         = 'EnhancedContent';
302 $tabsysprefs{TagsModeration}         = 'EnhancedContent';
303 $tabsysprefs{GoogleJackets}          = 'EnhancedContent';
304 $tabsysprefs{AuthorisedValueImages}  = "EnhancedContent";
305
306 # OPAC
307 $tabsysprefs{BiblioDefaultView}          = "OPAC";
308 $tabsysprefs{LibraryName}                = "OPAC";
309 $tabsysprefs{opaccolorstylesheet}        = "OPAC";
310 $tabsysprefs{opaccredits}                = "OPAC";
311 $tabsysprefs{opaclayoutstylesheet}       = "OPAC";
312 $tabsysprefs{OpacNav}                    = "OPAC";
313 $tabsysprefs{opacsmallimage}             = "OPAC";
314 $tabsysprefs{opacstylesheet}             = "OPAC";
315 $tabsysprefs{opacthemes}                 = "OPAC";
316 $tabsysprefs{opacuserjs}                 = "OPAC";
317 $tabsysprefs{opacheader}                 = "OPAC";
318 $tabsysprefs{hideBiblioNumber}           = "OPAC";
319 $tabsysprefs{OpacMainUserBlock}          = "OPAC";
320 $tabsysprefs{OPACURLOpenInNewWindow}     = "OPAC";
321 $tabsysprefs{OPACUserCSS}                = "OPAC";
322 $tabsysprefs{OPACHighlightedWords}       = "OPAC";
323 $tabsysprefs{OPACViewOthersSuggestions}  = "OPAC";
324 $tabsysprefs{URLLinkText}                = "OPAC";
325 $tabsysprefs{OPACSearchForTitleIn}       = "OPAC";
326 $tabsysprefs{OPACShelfBrowser}           = "OPAC";
327 $tabsysprefs{OPACDisplayRequestPriority} = "OPAC";
328 $tabsysprefs{OPACAllowHoldDateInFuture}  = "OPAC";
329 $tabsysprefs{OPACPatronDetails}  = "OPAC";
330 $tabsysprefs{OPACFinesTab}  = "OPAC";
331 $tabsysprefs{DisplayOPACiconsXSLT}       = "OPAC";
332 $tabsysprefs{AutoSelfCheckAllowed}       = "OPAC";
333 $tabsysprefs{AutoSelfCheckID}            = "OPAC";
334 $tabsysprefs{AutoSelfCheckPass}          = "OPAC";
335
336 # OPAC
337 $tabsysprefs{SearchMyLibraryFirst} = "OPAC";
338 $tabsysprefs{hidelostitems}        = "OPAC";
339 $tabsysprefs{opacbookbag}          = "OPAC";
340 $tabsysprefs{OpacPasswordChange}   = "OPAC";
341 $tabsysprefs{opacreadinghistory}   = "OPAC";
342 $tabsysprefs{virtualshelves}       = "OPAC";
343 $tabsysprefs{RequestOnOpac}        = "OPAC";
344 $tabsysprefs{reviewson}            = "OPAC";
345 $tabsysprefs{OpacTopissues}        = "OPAC";
346 $tabsysprefs{OpacAuthorities}      = "OPAC";
347 $tabsysprefs{OpacCloud}            = "OPAC";
348 $tabsysprefs{opacuserlogin}        = "OPAC";
349 $tabsysprefs{AnonSuggestions}      = "OPAC";
350 $tabsysprefs{suggestion}           = "OPAC";
351 $tabsysprefs{OpacTopissue}         = "OPAC";
352 $tabsysprefs{OpacBrowser}          = "OPAC";
353 $tabsysprefs{kohaspsuggest}        = "OPAC";
354 $tabsysprefs{OpacRenewalAllowed}   = "OPAC";
355 $tabsysprefs{OPACItemHolds}        = "OPAC";
356 $tabsysprefs{OPACGroupResults}     = "OPAC";
357 $tabsysprefs{XSLTDetailsDisplay}   = "OPAC";
358 $tabsysprefs{XSLTResultsDisplay}   = "OPAC";
359 $tabsysprefs{OPACShowCheckoutName}   = "OPAC";
360
361 # Serials
362 $tabsysprefs{RoutingListAddReserves}       = "Serials";
363 $tabsysprefs{OPACSerialIssueDisplayCount}  = "Serials";
364 $tabsysprefs{StaffSerialIssueDisplayCount} = "Serials";
365 $tabsysprefs{OPACDisplayExtendedSubInfo}   = "Serials";
366 $tabsysprefs{OPACSubscriptionDisplay}      = "Serials";
367 $tabsysprefs{RenewSerialAddsSuggestion}    = "Serials";
368 $tabsysprefs{SubscriptionHistory}          = "Serials";
369
370 # LOGFeatures
371 $tabsysprefs{CataloguingLog}  = "Logs";
372 $tabsysprefs{BorrowersLog}    = "Logs";
373 $tabsysprefs{SubscriptionLog} = "Logs";
374 $tabsysprefs{IssueLog}        = "Logs";
375 $tabsysprefs{ReturnLog}       = "Logs";
376 $tabsysprefs{LetterLog}       = "Logs";
377 $tabsysprefs{FinesLog}        = "Logs";
378
379 # OAI-PMH variables
380 $tabsysprefs{'OAI-PMH'}           = "OAI-PMH";
381 $tabsysprefs{'OAI-PMH:archiveID'} = "OAI-PMH";
382 $tabsysprefs{'OAI-PMH:MaxCount'}  = "OAI-PMH";
383 $tabsysprefs{'OAI-PMH:Set'}       = "OAI-PMH";
384 $tabsysprefs{'OAI-PMH:Subset'}    = "OAI-PMH";
385
386 # ILS-DI variables
387 $tabsysprefs{'ILS-DI'} = "ILS-DI";
388
389 # Creator variables
390
391 $tabsysprefs{'ImageLimit'} = "Creators";
392
393 sub StringSearch {
394     my ( $searchstring, $type ) = @_;
395     my $dbh = C4::Context->dbh;
396     $searchstring =~ s/\'/\\\'/g;
397     my @data = split( ' ', $searchstring );
398     my $count = @data;
399     my @results;
400     my $cnt = 0;
401     my $sth;
402
403     # used for doing a plain-old sys-pref search
404     if ( $type && $type ne 'all' ) {
405         foreach my $syspref ( sort { lc $a cmp lc $b } keys %tabsysprefs ) {
406             if ( $tabsysprefs{$syspref} eq $type ) {
407                 my $sth = $dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
408                 $sth->execute($syspref);
409                 while ( my $data = $sth->fetchrow_hashref ) {
410                     $data->{shortvalue} = $data->{value};
411                     $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if defined( $data->{value} ) and length( $data->{value} ) > 60;
412                     push( @results, $data );
413                     $cnt++;
414                 }
415                 $sth->finish;
416             }
417         }
418     } else {
419         my $sth;
420
421         if ( $type and $type eq 'all' ) {
422             $sth = $dbh->prepare( "
423             SELECT *
424               FROM systempreferences
425               WHERE variable LIKE ? OR explanation LIKE ?
426               ORDER BY VARIABLE" );
427             $sth->execute( "%$searchstring%", "%$searchstring%" );
428         } else {
429             my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable not in (";
430             foreach my $syspref ( keys %tabsysprefs ) {
431                 $strsth .= $dbh->quote($syspref) . ",";
432             }
433             $strsth =~ s/,$/) /;
434             $strsth .= " order by variable";
435             $sth = $dbh->prepare($strsth);
436             $sth->execute();
437         }
438
439         while ( my $data = $sth->fetchrow_hashref ) {
440             $data->{shortvalue} = $data->{value};
441             $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if length( $data->{value} ) > 60;
442             push( @results, $data );
443             $cnt++;
444         }
445
446         $sth->finish;
447     }
448     return ( $cnt, \@results );
449 }
450
451 sub GetPrefParams {
452     my $data   = shift;
453     my $params = $data;
454     my @options;
455
456     if ( defined $data->{'options'} ) {
457         foreach my $option ( split( /\|/, $data->{'options'} ) ) {
458             my $selected = '0';
459             defined( $data->{'value'} ) and $option eq $data->{'value'} and $selected = 1;
460             push @options, { option => $option, selected => $selected };
461         }
462     }
463
464     $params->{'prefoptions'} = $data->{'options'};
465
466     if ( not defined( $data->{'type'} ) ) {
467         $params->{'type-free'} = 1;
468         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 );
469     } elsif ( $data->{'type'} eq 'Upload' ) {
470         $params->{'type-upload'} = 1;
471     } elsif ( $data->{'type'} eq 'Choice' ) {
472         $params->{'type-choice'} = 1;
473     } elsif ( $data->{'type'} eq 'YesNo' ) {
474         $params->{'type-yesno'} = 1;
475         $data->{'value'}        = C4::Context->boolean_preference( $data->{'variable'} );
476         if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
477             $params->{'value-yes'} = 1;
478         } else {
479             $params->{'value-no'} = 1;
480         }
481     } elsif ( $data->{'type'} eq 'Integer' || $data->{'type'} eq 'Float' ) {
482         $params->{'type-free'} = 1;
483         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 10;
484     } elsif ( $data->{'type'} eq 'Textarea' ) {
485         $params->{'type-textarea'} = 1;
486         $data->{options} =~ /(.*)\|(.*)/;
487         $params->{'cols'} = $1;
488         $params->{'rows'} = $2;
489     } elsif ( $data->{'type'} eq 'Themes' ) {
490         $params->{'type-choice'} = 1;
491         my $type = '';
492         ( $data->{'variable'} =~ m#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
493         @options = ();
494         my $currently_selected_themes;
495         my $counter = 0;
496         foreach my $theme ( split /\s+/, $data->{'value'} ) {
497             push @options, { option => $theme, counter => $counter };
498             $currently_selected_themes->{$theme} = 1;
499             $counter++;
500         }
501         foreach my $theme ( getallthemes($type) ) {
502             my $selected = '0';
503             next if $currently_selected_themes->{$theme};
504             push @options, { option => $theme, counter => $counter };
505             $counter++;
506         }
507     } elsif ( $data->{'type'} eq 'ClassSources' ) {
508         $params->{'type-choice'} = 1;
509         my $type = '';
510         @options = ();
511         my $sources = GetClassSources();
512         my $counter = 0;
513         foreach my $cn_source ( sort keys %$sources ) {
514             if ( $cn_source eq $data->{'value'} ) {
515                 push @options, { option => $cn_source, counter => $counter, selected => 1 };
516             } else {
517                 push @options, { option => $cn_source, counter => $counter };
518             }
519             $counter++;
520         }
521     } elsif ( $data->{'type'} eq 'Languages' ) {
522         my $currently_selected_languages;
523         foreach my $language ( split /\s+/, $data->{'value'} ) {
524             $currently_selected_languages->{$language} = 1;
525         }
526
527         # current language
528         my $lang = $params->{'lang'};
529         my $theme;
530         my $interface;
531         if ( $data->{'variable'} =~ /opac/ ) {
532
533             # this is the OPAC
534             $interface = 'opac';
535             $theme     = C4::Context->preference('opacthemes');
536         } else {
537
538             # this is the staff client
539             $interface = 'intranet';
540             $theme     = C4::Context->preference('template');
541         }
542         my $languages_loop = getTranslatedLanguages( $interface, $theme, $lang, $currently_selected_languages );
543
544         $params->{'languages_loop'}    = $languages_loop;
545         $params->{'type-langselector'} = 1;
546     } else {
547         $params->{'type-free'} = 1;
548         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 30;
549     }
550
551     if ( $params->{'type-choice'} || $params->{'type-free'} || $params->{'type-yesno'} ) {
552         $params->{'oneline'} = 1;
553     }
554
555     $params->{'preftype'} = $data->{'type'};
556     $params->{'options'}  = \@options;
557
558     return $params;
559 }
560
561 my $input       = new CGI;
562 my $searchfield = $input->param('searchfield') || '';
563 my $Tvalue      = $input->param('Tvalue');
564 my $offset      = $input->param('offset') || 0;
565 my $script_name = "/cgi-bin/koha/admin/systempreferences.pl";
566
567 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
568     {   template_name   => "admin/systempreferences.tmpl",
569         query           => $input,
570         type            => "intranet",
571         authnotrequired => 0,
572         flagsrequired   => { parameters => 1 },
573         debug           => 1,
574     }
575 );
576 my $pagesize = 100;
577 my $op = $input->param('op') || '';
578 $searchfield =~ s/\,//g;
579
580 if ($op) {
581     $template->param(
582         script_name => $script_name,
583         $op         => 1
584     );    # we show only the TMPL_VAR names $op
585 } else {
586     $template->param(
587         script_name => $script_name,
588         else        => 1
589     );    # we show only the TMPL_VAR names $op
590 }
591
592 if ( $op eq 'update_and_reedit' ) {
593     foreach ( $input->param ) {
594     }
595     my $value = '';
596     if ( my $currentorder = $input->param('currentorder') ) {
597         my @currentorder = split /\|/, $currentorder;
598         my $orderchanged = 0;
599         foreach my $param ( $input->param ) {
600             if ( $param =~ m#up-(\d+).x# ) {
601                 my $temp = $currentorder[$1];
602                 $currentorder[$1]       = $currentorder[ $1 - 1 ];
603                 $currentorder[ $1 - 1 ] = $temp;
604                 $orderchanged           = 1;
605                 last;
606             } elsif ( $param =~ m#down-(\d+).x# ) {
607                 my $temp = $currentorder[$1];
608                 $currentorder[$1]       = $currentorder[ $1 + 1 ];
609                 $currentorder[ $1 + 1 ] = $temp;
610                 $orderchanged           = 1;
611                 last;
612             }
613         }
614         $value = join ' ', @currentorder;
615         if ($orderchanged) {
616             $op = 'add_form';
617             $template->param(
618                 script_name => $script_name,
619                 $op         => 1
620             );    # we show only the TMPL_VAR names $op
621         } else {
622             $op          = '';
623             $searchfield = '';
624             $template->param(
625                 script_name => $script_name,
626                 else        => 1
627             );    # we show only the TMPL_VAR names $op
628         }
629     }
630     my $dbh   = C4::Context->dbh;
631     my $query = "select * from systempreferences where variable=?";
632     my $sth   = $dbh->prepare($query);
633     $sth->execute( $input->param('variable') );
634     if ( $sth->rows ) {
635         unless ( C4::Context->config('demo') ) {
636             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
637             $sth->execute( $value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions') );
638             $sth->finish;
639             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
640         }
641     } else {
642         unless ( C4::Context->config('demo') ) {
643             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
644             $sth->execute( $input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
645             $sth->finish;
646             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $input->param('value') );
647         }
648     }
649     $sth->finish;
650
651 }
652
653 ################## ADD_FORM ##################################
654 # called by default. Used to create form to add or  modify a record
655
656 if ( $op eq 'add_form' ) {
657
658     #---- if primkey exists, it's a modify action, so read values to modify...
659     my $data;
660     if ($searchfield) {
661         my $dbh = C4::Context->dbh;
662         my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
663         $sth->execute($searchfield);
664         $data = $sth->fetchrow_hashref;
665         $sth->finish;
666         $template->param( modify => 1 );
667
668         # save tab to return to if user cancels edit
669         $template->param( return_tab => $tabsysprefs{$searchfield} );
670     }
671
672     $data->{'lang'} = $template->param('lang');
673
674     $template->param( GetPrefParams($data) );
675
676     $template->param( searchfield => $searchfield );
677
678 ################## ADD_VALIDATE ##################################
679     # called by add_form, used to insert/modify data in DB
680 } elsif ( $op eq 'add_validate' ) {
681     my $dbh = C4::Context->dbh;
682     my $sth = $dbh->prepare("select * from systempreferences where variable=?");
683     $sth->execute( $input->param('variable') );
684
685     # to handle multiple values
686     my $value;
687
688     # handle multiple value strings (separated by ',')
689     my $params = $input->Vars;
690     if ( defined $params->{'value'} ) {
691         my @values = ();
692         @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
693         if (@values) {
694             $value = "";
695             for my $vl (@values) {
696                 $value .= "$vl,";
697             }
698             $value =~ s/,$//;
699         } else {
700             $value = $params->{'value'};
701         }
702     }
703
704     if ( $input->param('preftype') eq 'Upload' ) {
705         my $lgtfh = $input->upload('value');
706         $value = join '', <$lgtfh>;
707         $value = encode_base64($value);
708     }
709
710     if ( $sth->rows ) {
711         unless ( C4::Context->config('demo') ) {
712             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
713             $sth->execute( $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable') );
714             $sth->finish;
715             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
716         }
717     } else {
718         unless ( C4::Context->config('demo') ) {
719             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
720             $sth->execute( $input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
721             $sth->finish;
722             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $value );
723         }
724     }
725     $sth->finish;
726     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=" . $tabsysprefs{ $input->param('variable') } . "\"></html>";
727     exit;
728 ################## DELETE_CONFIRM ##################################
729     # called by default form, used to confirm deletion of data in DB
730 } elsif ( $op eq 'delete_confirm' ) {
731     my $dbh = C4::Context->dbh;
732     my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
733     $sth->execute($searchfield);
734     my $data = $sth->fetchrow_hashref;
735     $sth->finish;
736     $template->param(
737         searchfield => $searchfield,
738         Tvalue      => $data->{'value'},
739     );
740
741     # END $OP eq DELETE_CONFIRM
742 ################## DELETE_CONFIRMED ##################################
743     # called by delete_confirm, used to effectively confirm deletion of data in DB
744 } elsif ( $op eq 'delete_confirmed' ) {
745     my $dbh = C4::Context->dbh;
746     my $sth = $dbh->prepare("delete from systempreferences where variable=?");
747     $sth->execute($searchfield);
748     my $logstring = $searchfield . " | " . $Tvalue;
749     logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring );
750     $sth->finish;
751
752     # END $OP eq DELETE_CONFIRMED
753 ################## DEFAULT ##################################
754 } else {    # DEFAULT
755             #Adding tab management for system preferences
756     my $tab = $input->param('tab');
757     $template->param( $tab => 1 );
758     my ( $count, $results ) = StringSearch( $searchfield, $tab );
759     my @loop_data = ();
760     for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
761         my $row_data = $results->[$i];
762         $row_data->{'lang'} = $template->param('lang');
763         $row_data           = GetPrefParams($row_data);                                                         # get a fresh hash for the row data
764         $row_data->{edit}   = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'variable'};
765         $row_data->{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'variable'};
766         push( @loop_data, $row_data );
767     }
768     $tab = ( $tab ? $tab : "Local Use" );
769     $template->param( loop => \@loop_data, $tab => 1 );
770     if ( $offset > 0 ) {
771         my $prevpage = $offset - $pagesize;
772         $template->param( "<a href=$script_name?offset=" . $prevpage . '&lt;&lt; Prev</a>' );
773     }
774     if ( $offset + $pagesize < $count ) {
775         my $nextpage = $offset + $pagesize;
776         $template->param( "a href=$script_name?offset=" . $nextpage . 'Next &gt;&gt;</a>' );
777     }
778     $template->param( tab => $tab, );
779 }    #---- END $OP eq DEFAULT
780 output_html_with_http_headers $input, $cookie, $template->output;