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