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