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