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