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