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