Revised for biblibre/3.2_community: Fix for Bug 3780, various OPAC pages have no...
[srvgit] / admin / systempreferences.pl
1 #!/usr/bin/perl
2
3 # script to administer the systempref table
4 # written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 =head1 systempreferences.pl
25
26 ALGO :
27  this script use an $op to know what to do.
28  if $op is empty or none of the above values,
29     - the default screen is build (with all records, or filtered datas).
30     - the   user can clic on add, modify or delete record.
31  if $op=add_form
32     - if primkey exists, this is a modification,so we read the $primkey record
33     - builds the add/modify form
34  if $op=add_validate
35     - the user has just send datas, so we create/modify the record
36  if $op=delete_form
37     - we show the record having primkey=$primkey and ask for deletion validation form
38  if $op=delete_confirm
39     - we delete the record having primkey=$primkey
40
41 =cut
42
43 use strict;
44 use warnings;
45
46 use CGI;
47 use 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
334 # OPAC
335 $tabsysprefs{SearchMyLibraryFirst} = "OPAC";
336 $tabsysprefs{hidelostitems}        = "OPAC";
337 $tabsysprefs{opacbookbag}          = "OPAC";
338 $tabsysprefs{OpacPasswordChange}   = "OPAC";
339 $tabsysprefs{opacreadinghistory}   = "OPAC";
340 $tabsysprefs{virtualshelves}       = "OPAC";
341 $tabsysprefs{RequestOnOpac}        = "OPAC";
342 $tabsysprefs{reviewson}            = "OPAC";
343 $tabsysprefs{OpacTopissues}        = "OPAC";
344 $tabsysprefs{OpacAuthorities}      = "OPAC";
345 $tabsysprefs{OpacCloud}            = "OPAC";
346 $tabsysprefs{opacuserlogin}        = "OPAC";
347 $tabsysprefs{AnonSuggestions}      = "OPAC";
348 $tabsysprefs{suggestion}           = "OPAC";
349 $tabsysprefs{OpacTopissue}         = "OPAC";
350 $tabsysprefs{OpacBrowser}          = "OPAC";
351 $tabsysprefs{kohaspsuggest}        = "OPAC";
352 $tabsysprefs{OpacRenewalAllowed}   = "OPAC";
353 $tabsysprefs{OPACItemHolds}        = "OPAC";
354 $tabsysprefs{OPACGroupResults}     = "OPAC";
355 $tabsysprefs{XSLTDetailsDisplay}   = "OPAC";
356 $tabsysprefs{XSLTResultsDisplay}   = "OPAC";
357 $tabsysprefs{OPACShowCheckoutName}   = "OPAC";
358
359 # Serials
360 $tabsysprefs{RoutingListAddReserves}       = "Serials";
361 $tabsysprefs{OPACSerialIssueDisplayCount}  = "Serials";
362 $tabsysprefs{StaffSerialIssueDisplayCount} = "Serials";
363 $tabsysprefs{OPACDisplayExtendedSubInfo}   = "Serials";
364 $tabsysprefs{OPACSubscriptionDisplay}      = "Serials";
365 $tabsysprefs{RenewSerialAddsSuggestion}    = "Serials";
366 $tabsysprefs{SubscriptionHistory}          = "Serials";
367
368 # LOGFeatures
369 $tabsysprefs{CataloguingLog}  = "Logs";
370 $tabsysprefs{BorrowersLog}    = "Logs";
371 $tabsysprefs{SubscriptionLog} = "Logs";
372 $tabsysprefs{IssueLog}        = "Logs";
373 $tabsysprefs{ReturnLog}       = "Logs";
374 $tabsysprefs{LetterLog}       = "Logs";
375 $tabsysprefs{FinesLog}        = "Logs";
376
377 # OAI-PMH variables
378 $tabsysprefs{'OAI-PMH'}           = "OAI-PMH";
379 $tabsysprefs{'OAI-PMH:archiveID'} = "OAI-PMH";
380 $tabsysprefs{'OAI-PMH:MaxCount'}  = "OAI-PMH";
381 $tabsysprefs{'OAI-PMH:Set'}       = "OAI-PMH";
382 $tabsysprefs{'OAI-PMH:Subset'}    = "OAI-PMH";
383
384 # ILS-DI variables
385 $tabsysprefs{'ILS-DI'} = "ILS-DI";
386 sub StringSearch {
387     my ( $searchstring, $type ) = @_;
388     my $dbh = C4::Context->dbh;
389     $searchstring =~ s/\'/\\\'/g;
390     my @data = split( ' ', $searchstring );
391     my $count = @data;
392     my @results;
393     my $cnt = 0;
394     my $sth;
395
396     # used for doing a plain-old sys-pref search
397     if ( $type && $type ne 'all' ) {
398         foreach my $syspref ( sort { lc $a cmp lc $b } keys %tabsysprefs ) {
399             if ( $tabsysprefs{$syspref} eq $type ) {
400                 my $sth = $dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
401                 $sth->execute($syspref);
402                 while ( my $data = $sth->fetchrow_hashref ) {
403                     $data->{shortvalue} = $data->{value};
404                     $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if defined( $data->{value} ) and length( $data->{value} ) > 60;
405                     push( @results, $data );
406                     $cnt++;
407                 }
408                 $sth->finish;
409             }
410         }
411     } else {
412         my $sth;
413
414         if ( $type and $type eq 'all' ) {
415             $sth = $dbh->prepare( "
416             SELECT *
417               FROM systempreferences 
418               WHERE variable LIKE ? OR explanation LIKE ? 
419               ORDER BY VARIABLE" );
420             $sth->execute( "%$searchstring%", "%$searchstring%" );
421         } else {
422             my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable not in (";
423             foreach my $syspref ( keys %tabsysprefs ) {
424                 $strsth .= $dbh->quote($syspref) . ",";
425             }
426             $strsth =~ s/,$/) /;
427             $strsth .= " order by variable";
428             $sth = $dbh->prepare($strsth);
429             $sth->execute();
430         }
431
432         while ( my $data = $sth->fetchrow_hashref ) {
433             $data->{shortvalue} = $data->{value};
434             $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if length( $data->{value} ) > 60;
435             push( @results, $data );
436             $cnt++;
437         }
438
439         $sth->finish;
440     }
441     return ( $cnt, \@results );
442 }
443
444 sub GetPrefParams {
445     my $data   = shift;
446     my $params = $data;
447     my @options;
448
449     if ( defined $data->{'options'} ) {
450         foreach my $option ( split( /\|/, $data->{'options'} ) ) {
451             my $selected = '0';
452             defined( $data->{'value'} ) and $option eq $data->{'value'} and $selected = 1;
453             push @options, { option => $option, selected => $selected };
454         }
455     }
456
457     $params->{'prefoptions'} = $data->{'options'};
458
459     if ( not defined( $data->{'type'} ) ) {
460         $params->{'type-free'} = 1;
461         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 );
462     } elsif ( $data->{'type'} eq 'Upload' ) {
463         $params->{'type-upload'} = 1;
464     } elsif ( $data->{'type'} eq 'Choice' ) {
465         $params->{'type-choice'} = 1;
466     } elsif ( $data->{'type'} eq 'YesNo' ) {
467         $params->{'type-yesno'} = 1;
468         $data->{'value'}        = C4::Context->boolean_preference( $data->{'variable'} );
469         if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
470             $params->{'value-yes'} = 1;
471         } else {
472             $params->{'value-no'} = 1;
473         }
474     } elsif ( $data->{'type'} eq 'Integer' || $data->{'type'} eq 'Float' ) {
475         $params->{'type-free'} = 1;
476         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 10;
477     } elsif ( $data->{'type'} eq 'Textarea' ) {
478         $params->{'type-textarea'} = 1;
479         $data->{options} =~ /(.*)\|(.*)/;
480         $params->{'cols'} = $1;
481         $params->{'rows'} = $2;
482     } elsif ( $data->{'type'} eq 'Themes' ) {
483         $params->{'type-choice'} = 1;
484         my $type = '';
485         ( $data->{'variable'} =~ m#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
486         @options = ();
487         my $currently_selected_themes;
488         my $counter = 0;
489         foreach my $theme ( split /\s+/, $data->{'value'} ) {
490             push @options, { option => $theme, counter => $counter };
491             $currently_selected_themes->{$theme} = 1;
492             $counter++;
493         }
494         foreach my $theme ( getallthemes($type) ) {
495             my $selected = '0';
496             next if $currently_selected_themes->{$theme};
497             push @options, { option => $theme, counter => $counter };
498             $counter++;
499         }
500     } elsif ( $data->{'type'} eq 'ClassSources' ) {
501         $params->{'type-choice'} = 1;
502         my $type = '';
503         @options = ();
504         my $sources = GetClassSources();
505         my $counter = 0;
506         foreach my $cn_source ( sort keys %$sources ) {
507             if ( $cn_source eq $data->{'value'} ) {
508                 push @options, { option => $cn_source, counter => $counter, selected => 1 };
509             } else {
510                 push @options, { option => $cn_source, counter => $counter };
511             }
512             $counter++;
513         }
514     } elsif ( $data->{'type'} eq 'Languages' ) {
515         my $currently_selected_languages;
516         foreach my $language ( split /\s+/, $data->{'value'} ) {
517             $currently_selected_languages->{$language} = 1;
518         }
519
520         # current language
521         my $lang = $params->{'lang'};
522         my $theme;
523         my $interface;
524         if ( $data->{'variable'} =~ /opac/ ) {
525
526             # this is the OPAC
527             $interface = 'opac';
528             $theme     = C4::Context->preference('opacthemes');
529         } else {
530
531             # this is the staff client
532             $interface = 'intranet';
533             $theme     = C4::Context->preference('template');
534         }
535         my $languages_loop = getTranslatedLanguages( $interface, $theme, $lang, $currently_selected_languages );
536
537         $params->{'languages_loop'}    = $languages_loop;
538         $params->{'type-langselector'} = 1;
539     } else {
540         $params->{'type-free'} = 1;
541         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 30;
542     }
543
544     if ( $params->{'type-choice'} || $params->{'type-free'} || $params->{'type-yesno'} ) {
545         $params->{'oneline'} = 1;
546     }
547
548     $params->{'preftype'} = $data->{'type'};
549     $params->{'options'}  = \@options;
550
551     return $params;
552 }
553
554 my $input       = new CGI;
555 my $searchfield = $input->param('searchfield') || '';
556 my $Tvalue      = $input->param('Tvalue');
557 my $offset      = $input->param('offset') || 0;
558 my $script_name = "/cgi-bin/koha/admin/systempreferences.pl";
559
560 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
561     {   template_name   => "admin/systempreferences.tmpl",
562         query           => $input,
563         type            => "intranet",
564         authnotrequired => 0,
565         flagsrequired   => { parameters => 1 },
566         debug           => 1,
567     }
568 );
569 my $pagesize = 100;
570 my $op = $input->param('op') || '';
571 $searchfield =~ s/\,//g;
572
573 if ($op) {
574     $template->param(
575         script_name => $script_name,
576         $op         => 1
577     );    # we show only the TMPL_VAR names $op
578 } else {
579     $template->param(
580         script_name => $script_name,
581         else        => 1
582     );    # we show only the TMPL_VAR names $op
583 }
584
585 if ( $op eq 'update_and_reedit' ) {
586     foreach ( $input->param ) {
587     }
588     my $value = '';
589     if ( my $currentorder = $input->param('currentorder') ) {
590         my @currentorder = split /\|/, $currentorder;
591         my $orderchanged = 0;
592         foreach my $param ( $input->param ) {
593             if ( $param =~ m#up-(\d+).x# ) {
594                 my $temp = $currentorder[$1];
595                 $currentorder[$1]       = $currentorder[ $1 - 1 ];
596                 $currentorder[ $1 - 1 ] = $temp;
597                 $orderchanged           = 1;
598                 last;
599             } elsif ( $param =~ m#down-(\d+).x# ) {
600                 my $temp = $currentorder[$1];
601                 $currentorder[$1]       = $currentorder[ $1 + 1 ];
602                 $currentorder[ $1 + 1 ] = $temp;
603                 $orderchanged           = 1;
604                 last;
605             }
606         }
607         $value = join ' ', @currentorder;
608         if ($orderchanged) {
609             $op = 'add_form';
610             $template->param(
611                 script_name => $script_name,
612                 $op         => 1
613             );    # we show only the TMPL_VAR names $op
614         } else {
615             $op          = '';
616             $searchfield = '';
617             $template->param(
618                 script_name => $script_name,
619                 else        => 1
620             );    # we show only the TMPL_VAR names $op
621         }
622     }
623     my $dbh   = C4::Context->dbh;
624     my $query = "select * from systempreferences where variable=?";
625     my $sth   = $dbh->prepare($query);
626     $sth->execute( $input->param('variable') );
627     if ( $sth->rows ) {
628         unless ( C4::Context->config('demo') ) {
629             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
630             $sth->execute( $value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions') );
631             $sth->finish;
632             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
633         }
634     } else {
635         unless ( C4::Context->config('demo') ) {
636             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
637             $sth->execute( $input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
638             $sth->finish;
639             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $input->param('value') );
640         }
641     }
642     $sth->finish;
643
644 }
645
646 ################## ADD_FORM ##################################
647 # called by default. Used to create form to add or  modify a record
648
649 if ( $op eq 'add_form' ) {
650
651     #---- if primkey exists, it's a modify action, so read values to modify...
652     my $data;
653     if ($searchfield) {
654         my $dbh = C4::Context->dbh;
655         my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
656         $sth->execute($searchfield);
657         $data = $sth->fetchrow_hashref;
658         $sth->finish;
659         $template->param( modify => 1 );
660
661         # save tab to return to if user cancels edit
662         $template->param( return_tab => $tabsysprefs{$searchfield} );
663     }
664
665     $data->{'lang'} = $template->param('lang');
666
667     $template->param( GetPrefParams($data) );
668
669     $template->param( searchfield => $searchfield );
670
671 ################## ADD_VALIDATE ##################################
672     # called by add_form, used to insert/modify data in DB
673 } elsif ( $op eq 'add_validate' ) {
674     my $dbh = C4::Context->dbh;
675     my $sth = $dbh->prepare("select * from systempreferences where variable=?");
676     $sth->execute( $input->param('variable') );
677
678     # to handle multiple values
679     my $value;
680
681     # handle multiple value strings (separated by ',')
682     my $params = $input->Vars;
683     if ( defined $params->{'value'} ) {
684         my @values = ();
685         @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
686         if (@values) {
687             $value = "";
688             for my $vl (@values) {
689                 $value .= "$vl,";
690             }
691             $value =~ s/,$//;
692         } else {
693             $value = $params->{'value'};
694         }
695     }
696
697     if ( $input->param('preftype') eq 'Upload' ) {
698         my $lgtfh = $input->upload('value');
699         $value = join '', <$lgtfh>;
700         $value = encode_base64($value);
701     }
702
703     if ( $sth->rows ) {
704         unless ( C4::Context->config('demo') ) {
705             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
706             $sth->execute( $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable') );
707             $sth->finish;
708             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
709         }
710     } else {
711         unless ( C4::Context->config('demo') ) {
712             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
713             $sth->execute( $input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
714             $sth->finish;
715             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $value );
716         }
717     }
718     $sth->finish;
719     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=" . $tabsysprefs{ $input->param('variable') } . "\"></html>";
720     exit;
721 ################## DELETE_CONFIRM ##################################
722     # called by default form, used to confirm deletion of data in DB
723 } elsif ( $op eq 'delete_confirm' ) {
724     my $dbh = C4::Context->dbh;
725     my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
726     $sth->execute($searchfield);
727     my $data = $sth->fetchrow_hashref;
728     $sth->finish;
729     $template->param(
730         searchfield => $searchfield,
731         Tvalue      => $data->{'value'},
732     );
733
734     # END $OP eq DELETE_CONFIRM
735 ################## DELETE_CONFIRMED ##################################
736     # called by delete_confirm, used to effectively confirm deletion of data in DB
737 } elsif ( $op eq 'delete_confirmed' ) {
738     my $dbh = C4::Context->dbh;
739     my $sth = $dbh->prepare("delete from systempreferences where variable=?");
740     $sth->execute($searchfield);
741     my $logstring = $searchfield . " | " . $Tvalue;
742     logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring );
743     $sth->finish;
744
745     # END $OP eq DELETE_CONFIRMED
746 ################## DEFAULT ##################################
747 } else {    # DEFAULT
748             #Adding tab management for system preferences
749     my $tab = $input->param('tab');
750     $template->param( $tab => 1 );
751     my ( $count, $results ) = StringSearch( $searchfield, $tab );
752     my @loop_data = ();
753     for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
754         my $row_data = $results->[$i];
755         $row_data->{'lang'} = $template->param('lang');
756         $row_data           = GetPrefParams($row_data);                                                         # get a fresh hash for the row data
757         $row_data->{edit}   = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'variable'};
758         $row_data->{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'variable'};
759         push( @loop_data, $row_data );
760     }
761     $tab = ( $tab ? $tab : "Local Use" );
762     $template->param( loop => \@loop_data, $tab => 1 );
763     if ( $offset > 0 ) {
764         my $prevpage = $offset - $pagesize;
765         $template->param( "<a href=$script_name?offset=" . $prevpage . '&lt;&lt; Prev</a>' );
766     }
767     if ( $offset + $pagesize < $count ) {
768         my $nextpage = $offset + $pagesize;
769         $template->param( "a href=$script_name?offset=" . $nextpage . 'Next &gt;&gt;</a>' );
770     }
771     $template->param( tab => $tab, );
772 }    #---- END $OP eq DEFAULT
773 output_html_with_http_headers $input, $cookie, $template->output;