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