[3.2.x](bug #3623) PDF printing enhancements
[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 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
182 # Staff Client
183 $tabsysprefs{TemplateEncoding}        = "StaffClient";
184 $tabsysprefs{template}                = "StaffClient";
185 $tabsysprefs{intranetstylesheet}      = "StaffClient";
186 $tabsysprefs{IntranetNav}             = "StaffClient";
187 $tabsysprefs{intranetcolorstylesheet} = "StaffClient";
188 $tabsysprefs{intranetuserjs}          = "StaffClient";
189 $tabsysprefs{yuipath}                 = "StaffClient";
190 $tabsysprefs{IntranetmainUserblock}   = "StaffClient";
191 $tabsysprefs{viewMARC}                = "StaffClient";
192 $tabsysprefs{viewLabeledMARC}         = "StaffClient";
193 $tabsysprefs{viewISBD}                = "StaffClient";
194
195 # Patrons
196 $tabsysprefs{autoMemberNum}                = "Patrons";
197 $tabsysprefs{checkdigit}                   = "Patrons";
198 $tabsysprefs{intranetreadinghistory}       = "Patrons";
199 $tabsysprefs{NotifyBorrowerDeparture}      = "Patrons";
200 $tabsysprefs{memberofinstitution}          = "Patrons";
201 $tabsysprefs{ReadingHistory}               = "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
333 # OPAC
334 $tabsysprefs{SearchMyLibraryFirst} = "OPAC";
335 $tabsysprefs{hidelostitems}        = "OPAC";
336 $tabsysprefs{opacbookbag}          = "OPAC";
337 $tabsysprefs{OpacPasswordChange}   = "OPAC";
338 $tabsysprefs{opacreadinghistory}   = "OPAC";
339 $tabsysprefs{virtualshelves}       = "OPAC";
340 $tabsysprefs{RequestOnOpac}        = "OPAC";
341 $tabsysprefs{reviewson}            = "OPAC";
342 $tabsysprefs{OpacTopissues}        = "OPAC";
343 $tabsysprefs{OpacAuthorities}      = "OPAC";
344 $tabsysprefs{OpacCloud}            = "OPAC";
345 $tabsysprefs{opacuserlogin}        = "OPAC";
346 $tabsysprefs{AnonSuggestions}      = "OPAC";
347 $tabsysprefs{suggestion}           = "OPAC";
348 $tabsysprefs{OpacTopissue}         = "OPAC";
349 $tabsysprefs{OpacBrowser}          = "OPAC";
350 $tabsysprefs{kohaspsuggest}        = "OPAC";
351 $tabsysprefs{OpacRenewalAllowed}   = "OPAC";
352 $tabsysprefs{OPACItemHolds}        = "OPAC";
353 $tabsysprefs{OPACGroupResults}     = "OPAC";
354 $tabsysprefs{XSLTDetailsDisplay}   = "OPAC";
355 $tabsysprefs{XSLTResultsDisplay}   = "OPAC";
356 $tabsysprefs{OPACShowCheckoutName}   = "OPAC";
357
358 # Serials
359 $tabsysprefs{RoutingListAddReserves}       = "Serials";
360 $tabsysprefs{OPACSerialIssueDisplayCount}  = "Serials";
361 $tabsysprefs{StaffSerialIssueDisplayCount} = "Serials";
362 $tabsysprefs{OPACDisplayExtendedSubInfo}   = "Serials";
363 $tabsysprefs{OPACSubscriptionDisplay}      = "Serials";
364 $tabsysprefs{RenewSerialAddsSuggestion}    = "Serials";
365 $tabsysprefs{SubscriptionHistory}          = "Serials";
366
367 # LOGFeatures
368 $tabsysprefs{CataloguingLog}  = "Logs";
369 $tabsysprefs{BorrowersLog}    = "Logs";
370 $tabsysprefs{SubscriptionLog} = "Logs";
371 $tabsysprefs{IssueLog}        = "Logs";
372 $tabsysprefs{ReturnLog}       = "Logs";
373 $tabsysprefs{LetterLog}       = "Logs";
374 $tabsysprefs{FinesLog}        = "Logs";
375
376 # OAI-PMH variables
377 $tabsysprefs{'OAI-PMH'}           = "OAI-PMH";
378 $tabsysprefs{'OAI-PMH:archiveID'} = "OAI-PMH";
379 $tabsysprefs{'OAI-PMH:MaxCount'}  = "OAI-PMH";
380 $tabsysprefs{'OAI-PMH:Set'}       = "OAI-PMH";
381 $tabsysprefs{'OAI-PMH:Subset'}    = "OAI-PMH";
382
383 # ILS-DI variables
384 $tabsysprefs{'ILS-DI'} = "ILS-DI";
385 sub StringSearch {
386     my ( $searchstring, $type ) = @_;
387     my $dbh = C4::Context->dbh;
388     $searchstring =~ s/\'/\\\'/g;
389     my @data = split( ' ', $searchstring );
390     my $count = @data;
391     my @results;
392     my $cnt = 0;
393     my $sth;
394
395     # used for doing a plain-old sys-pref search
396     if ( $type && $type ne 'all' ) {
397         foreach my $syspref ( sort { lc $a cmp lc $b } keys %tabsysprefs ) {
398             if ( $tabsysprefs{$syspref} eq $type ) {
399                 my $sth = $dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
400                 $sth->execute($syspref);
401                 while ( my $data = $sth->fetchrow_hashref ) {
402                     $data->{shortvalue} = $data->{value};
403                     $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if defined( $data->{value} ) and length( $data->{value} ) > 60;
404                     push( @results, $data );
405                     $cnt++;
406                 }
407                 $sth->finish;
408             }
409         }
410     } else {
411         my $sth;
412
413         if ( $type and $type eq 'all' ) {
414             $sth = $dbh->prepare( "
415             SELECT *
416               FROM systempreferences 
417               WHERE variable LIKE ? OR explanation LIKE ? 
418               ORDER BY VARIABLE" );
419             $sth->execute( "%$searchstring%", "%$searchstring%" );
420         } else {
421             my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable not in (";
422             foreach my $syspref ( keys %tabsysprefs ) {
423                 $strsth .= $dbh->quote($syspref) . ",";
424             }
425             $strsth =~ s/,$/) /;
426             $strsth .= " order by variable";
427             $sth = $dbh->prepare($strsth);
428             $sth->execute();
429         }
430
431         while ( my $data = $sth->fetchrow_hashref ) {
432             $data->{shortvalue} = $data->{value};
433             $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if length( $data->{value} ) > 60;
434             push( @results, $data );
435             $cnt++;
436         }
437
438         $sth->finish;
439     }
440     return ( $cnt, \@results );
441 }
442
443 sub GetPrefParams {
444     my $data   = shift;
445     my $params = $data;
446     my @options;
447
448     if ( defined $data->{'options'} ) {
449         foreach my $option ( split( /\|/, $data->{'options'} ) ) {
450             my $selected = '0';
451             defined( $data->{'value'} ) and $option eq $data->{'value'} and $selected = 1;
452             push @options, { option => $option, selected => $selected };
453         }
454     }
455
456     $params->{'prefoptions'} = $data->{'options'};
457
458     if ( not defined( $data->{'type'} ) ) {
459         $params->{'type-free'} = 1;
460         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 );
461     } elsif ( $data->{'type'} eq 'Upload' ) {
462         $params->{'type-upload'} = 1;
463     } elsif ( $data->{'type'} eq 'Choice' ) {
464         $params->{'type-choice'} = 1;
465     } elsif ( $data->{'type'} eq 'YesNo' ) {
466         $params->{'type-yesno'} = 1;
467         $data->{'value'}        = C4::Context->boolean_preference( $data->{'variable'} );
468         if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
469             $params->{'value-yes'} = 1;
470         } else {
471             $params->{'value-no'} = 1;
472         }
473     } elsif ( $data->{'type'} eq 'Integer' || $data->{'type'} eq 'Float' ) {
474         $params->{'type-free'} = 1;
475         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 10;
476     } elsif ( $data->{'type'} eq 'Textarea' ) {
477         $params->{'type-textarea'} = 1;
478         $data->{options} =~ /(.*)\|(.*)/;
479         $params->{'cols'} = $1;
480         $params->{'rows'} = $2;
481     } elsif ( $data->{'type'} eq 'Themes' ) {
482         $params->{'type-choice'} = 1;
483         my $type = '';
484         ( $data->{'variable'} =~ m#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
485         @options = ();
486         my $currently_selected_themes;
487         my $counter = 0;
488         foreach my $theme ( split /\s+/, $data->{'value'} ) {
489             push @options, { option => $theme, counter => $counter };
490             $currently_selected_themes->{$theme} = 1;
491             $counter++;
492         }
493         foreach my $theme ( getallthemes($type) ) {
494             my $selected = '0';
495             next if $currently_selected_themes->{$theme};
496             push @options, { option => $theme, counter => $counter };
497             $counter++;
498         }
499     } elsif ( $data->{'type'} eq 'ClassSources' ) {
500         $params->{'type-choice'} = 1;
501         my $type = '';
502         @options = ();
503         my $sources = GetClassSources();
504         my $counter = 0;
505         foreach my $cn_source ( sort keys %$sources ) {
506             if ( $cn_source eq $data->{'value'} ) {
507                 push @options, { option => $cn_source, counter => $counter, selected => 1 };
508             } else {
509                 push @options, { option => $cn_source, counter => $counter };
510             }
511             $counter++;
512         }
513     } elsif ( $data->{'type'} eq 'Languages' ) {
514         my $currently_selected_languages;
515         foreach my $language ( split /\s+/, $data->{'value'} ) {
516             $currently_selected_languages->{$language} = 1;
517         }
518
519         # current language
520         my $lang = $params->{'lang'};
521         my $theme;
522         my $interface;
523         if ( $data->{'variable'} =~ /opac/ ) {
524
525             # this is the OPAC
526             $interface = 'opac';
527             $theme     = C4::Context->preference('opacthemes');
528         } else {
529
530             # this is the staff client
531             $interface = 'intranet';
532             $theme     = C4::Context->preference('template');
533         }
534         my $languages_loop = getTranslatedLanguages( $interface, $theme, $lang, $currently_selected_languages );
535
536         $params->{'languages_loop'}    = $languages_loop;
537         $params->{'type-langselector'} = 1;
538     } else {
539         $params->{'type-free'} = 1;
540         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 30;
541     }
542
543     if ( $params->{'type-choice'} || $params->{'type-free'} || $params->{'type-yesno'} ) {
544         $params->{'oneline'} = 1;
545     }
546
547     $params->{'preftype'} = $data->{'type'};
548     $params->{'options'}  = \@options;
549
550     return $params;
551 }
552
553 my $input       = new CGI;
554 my $searchfield = $input->param('searchfield') || '';
555 my $Tvalue      = $input->param('Tvalue');
556 my $offset      = $input->param('offset') || 0;
557 my $script_name = "/cgi-bin/koha/admin/systempreferences.pl";
558
559 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
560     {   template_name   => "admin/systempreferences.tmpl",
561         query           => $input,
562         type            => "intranet",
563         authnotrequired => 0,
564         flagsrequired   => { parameters => 1 },
565         debug           => 1,
566     }
567 );
568 my $pagesize = 100;
569 my $op = $input->param('op') || '';
570 $searchfield =~ s/\,//g;
571
572 if ($op) {
573     $template->param(
574         script_name => $script_name,
575         $op         => 1
576     );    # we show only the TMPL_VAR names $op
577 } else {
578     $template->param(
579         script_name => $script_name,
580         else        => 1
581     );    # we show only the TMPL_VAR names $op
582 }
583
584 if ( $op eq 'update_and_reedit' ) {
585     foreach ( $input->param ) {
586     }
587     my $value = '';
588     if ( my $currentorder = $input->param('currentorder') ) {
589         my @currentorder = split /\|/, $currentorder;
590         my $orderchanged = 0;
591         foreach my $param ( $input->param ) {
592             if ( $param =~ m#up-(\d+).x# ) {
593                 my $temp = $currentorder[$1];
594                 $currentorder[$1]       = $currentorder[ $1 - 1 ];
595                 $currentorder[ $1 - 1 ] = $temp;
596                 $orderchanged           = 1;
597                 last;
598             } elsif ( $param =~ m#down-(\d+).x# ) {
599                 my $temp = $currentorder[$1];
600                 $currentorder[$1]       = $currentorder[ $1 + 1 ];
601                 $currentorder[ $1 + 1 ] = $temp;
602                 $orderchanged           = 1;
603                 last;
604             }
605         }
606         $value = join ' ', @currentorder;
607         if ($orderchanged) {
608             $op = 'add_form';
609             $template->param(
610                 script_name => $script_name,
611                 $op         => 1
612             );    # we show only the TMPL_VAR names $op
613         } else {
614             $op          = '';
615             $searchfield = '';
616             $template->param(
617                 script_name => $script_name,
618                 else        => 1
619             );    # we show only the TMPL_VAR names $op
620         }
621     }
622     my $dbh   = C4::Context->dbh;
623     my $query = "select * from systempreferences where variable=?";
624     my $sth   = $dbh->prepare($query);
625     $sth->execute( $input->param('variable') );
626     if ( $sth->rows ) {
627         unless ( C4::Context->config('demo') ) {
628             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
629             $sth->execute( $value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions') );
630             $sth->finish;
631             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
632         }
633     } else {
634         unless ( C4::Context->config('demo') ) {
635             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
636             $sth->execute( $input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
637             $sth->finish;
638             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $input->param('value') );
639         }
640     }
641     $sth->finish;
642
643 }
644
645 ################## ADD_FORM ##################################
646 # called by default. Used to create form to add or  modify a record
647
648 if ( $op eq 'add_form' ) {
649
650     #---- if primkey exists, it's a modify action, so read values to modify...
651     my $data;
652     if ($searchfield) {
653         my $dbh = C4::Context->dbh;
654         my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
655         $sth->execute($searchfield);
656         $data = $sth->fetchrow_hashref;
657         $sth->finish;
658         $template->param( modify => 1 );
659
660         # save tab to return to if user cancels edit
661         $template->param( return_tab => $tabsysprefs{$searchfield} );
662     }
663
664     $data->{'lang'} = $template->param('lang');
665
666     $template->param( GetPrefParams($data) );
667
668     $template->param( searchfield => $searchfield );
669
670 ################## ADD_VALIDATE ##################################
671     # called by add_form, used to insert/modify data in DB
672 } elsif ( $op eq 'add_validate' ) {
673     my $dbh = C4::Context->dbh;
674     my $sth = $dbh->prepare("select * from systempreferences where variable=?");
675     $sth->execute( $input->param('variable') );
676
677     # to handle multiple values
678     my $value;
679
680     # handle multiple value strings (separated by ',')
681     my $params = $input->Vars;
682     if ( defined $params->{'value'} ) {
683         my @values = ();
684         @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
685         if (@values) {
686             $value = "";
687             for my $vl (@values) {
688                 $value .= "$vl,";
689             }
690             $value =~ s/,$//;
691         } else {
692             $value = $params->{'value'};
693         }
694     }
695
696     if ( $input->param('preftype') eq 'Upload' ) {
697         my $lgtfh = $input->upload('value');
698         $value = join '', <$lgtfh>;
699         $value = encode_base64($value);
700     }
701
702     if ( $sth->rows ) {
703         unless ( C4::Context->config('demo') ) {
704             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
705             $sth->execute( $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable') );
706             $sth->finish;
707             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
708         }
709     } else {
710         unless ( C4::Context->config('demo') ) {
711             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
712             $sth->execute( $input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
713             $sth->finish;
714             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $value );
715         }
716     }
717     $sth->finish;
718     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=" . $tabsysprefs{ $input->param('variable') } . "\"></html>";
719     exit;
720 ################## DELETE_CONFIRM ##################################
721     # called by default form, used to confirm deletion of data in DB
722 } elsif ( $op eq 'delete_confirm' ) {
723     my $dbh = C4::Context->dbh;
724     my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
725     $sth->execute($searchfield);
726     my $data = $sth->fetchrow_hashref;
727     $sth->finish;
728     $template->param(
729         searchfield => $searchfield,
730         Tvalue      => $data->{'value'},
731     );
732
733     # END $OP eq DELETE_CONFIRM
734 ################## DELETE_CONFIRMED ##################################
735     # called by delete_confirm, used to effectively confirm deletion of data in DB
736 } elsif ( $op eq 'delete_confirmed' ) {
737     my $dbh = C4::Context->dbh;
738     my $sth = $dbh->prepare("delete from systempreferences where variable=?");
739     $sth->execute($searchfield);
740     my $logstring = $searchfield . " | " . $Tvalue;
741     logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring );
742     $sth->finish;
743
744     # END $OP eq DELETE_CONFIRMED
745 ################## DEFAULT ##################################
746 } else {    # DEFAULT
747             #Adding tab management for system preferences
748     my $tab = $input->param('tab');
749     $template->param( $tab => 1 );
750     my ( $count, $results ) = StringSearch( $searchfield, $tab );
751     my @loop_data = ();
752     for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
753         my $row_data = $results->[$i];
754         $row_data->{'lang'} = $template->param('lang');
755         $row_data           = GetPrefParams($row_data);                                                         # get a fresh hash for the row data
756         $row_data->{edit}   = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'variable'};
757         $row_data->{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'variable'};
758         push( @loop_data, $row_data );
759     }
760     $tab = ( $tab ? $tab : "Local Use" );
761     $template->param( loop => \@loop_data, $tab => 1 );
762     if ( $offset > 0 ) {
763         my $prevpage = $offset - $pagesize;
764         $template->param( "<a href=$script_name?offset=" . $prevpage . '&lt;&lt; Prev</a>' );
765     }
766     if ( $offset + $pagesize < $count ) {
767         my $nextpage = $offset + $pagesize;
768         $template->param( "a href=$script_name?offset=" . $nextpage . 'Next &gt;&gt;</a>' );
769     }
770     $template->param( tab => $tab, );
771 }    #---- END $OP eq DEFAULT
772 output_html_with_http_headers $input, $cookie, $template->output;