bug 3481 followup - add new sysprefs to circulation section
[koha.git] / admin / systempreferences.pl
1 #!/usr/bin/perl
2
3 # script to administer the systempref table
4 # written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 =head1 systempreferences.pl
25
26 ALGO :
27  this script use an $op to know what to do.
28  if $op is empty or none of the above values,
29     - the default screen is build (with all records, or filtered datas).
30     - the   user can clic on add, modify or delete record.
31  if $op=add_form
32     - if primkey exists, this is a modification,so we read the $primkey record
33     - builds the add/modify form
34  if $op=add_validate
35     - the user has just send datas, so we create/modify the record
36  if $op=delete_form
37     - we show the record having primkey=$primkey and ask for deletion validation form
38  if $op=delete_confirm
39     - we delete the record having primkey=$primkey
40
41 =cut
42
43 use strict;
44 use warnings;
45
46 use CGI;
47 use C4::Auth;
48 use C4::Context;
49 use C4::Koha;
50 use C4::Languages qw(getTranslatedLanguages);
51 use C4::ClassSource;
52 use C4::Log;
53 use C4::Output;
54
55 # use Smart::Comments;
56
57 # FIXME, shouldnt we store this stuff in the systempreferences table?
58
59 # FIXME: This uses hash in a backwards way.  What we really want is:
60 #       $tabsysprefs{key} = $array_ref;
61 #               like
62 #       $tabsysprefs{Cataloguing} = [qw(autoBarcode ISBD marc ...)];
63 #
64 #   Because some things *should* be on more than one tab.
65 #   And the tabname is the unique part (the key).
66
67 my %tabsysprefs;
68
69 # Acquisitions
70 $tabsysprefs{acquisitions}              = "Acquisitions";
71 $tabsysprefs{gist}                      = "Acquisitions";
72 $tabsysprefs{emailPurchaseSuggestions}  = "Acquisitions";
73
74 # Admin
75 $tabsysprefs{singleBranchMode}      = "Admin";
76 $tabsysprefs{staffClientBaseURL}    = "Admin";
77 $tabsysprefs{Version}               = "Admin";
78 $tabsysprefs{OpacMaintenance}       = "Admin";
79 $tabsysprefs{FrameworksLoaded}      = "Admin";
80 $tabsysprefs{libraryAddress}        = "Admin";
81 $tabsysprefs{delimiter}             = "Admin";
82 $tabsysprefs{IndependantBranches}   = "Admin";
83 $tabsysprefs{insecure}              = "Admin";
84 $tabsysprefs{KohaAdmin}             = "Admin";
85 $tabsysprefs{KohaAdminEmailAddress} = "Admin";
86 $tabsysprefs{MIME}                  = "Admin";
87 $tabsysprefs{timeout}               = "Admin";
88 $tabsysprefs{Intranet_includes}     = "Admin";
89 $tabsysprefs{AutoLocation}          = "Admin";
90 $tabsysprefs{DebugLevel}            = "Admin";
91 $tabsysprefs{SessionStorage}        = "Admin";
92 $tabsysprefs{noItemTypeImages}      = "Admin";
93 $tabsysprefs{OPACBaseURL}           = "Admin";
94 $tabsysprefs{GranularPermissions}   = "Admin";
95
96 # Authorities
97 $tabsysprefs{authoritysep}          = "Authorities";
98 $tabsysprefs{AuthDisplayHierarchy}  = "Authorities";
99 $tabsysprefs{dontmerge}             = "Authorities";
100 $tabsysprefs{BiblioAddsAuthorities} = "Authorities";
101
102 # Cataloguing
103 $tabsysprefs{advancedMARCeditor}          = "Cataloging";
104 $tabsysprefs{autoBarcode}                 = "Cataloging";
105 $tabsysprefs{hide_marc}                   = "Cataloging";
106 $tabsysprefs{IntranetBiblioDefaultView}   = "Cataloging";
107 $tabsysprefs{ISBD}                        = "Cataloging";
108 $tabsysprefs{itemcallnumber}              = "Cataloging";
109 $tabsysprefs{LabelMARCView}               = "Cataloging";
110 $tabsysprefs{marc}                        = "Cataloging";
111 $tabsysprefs{marcflavour}                 = "Cataloging";
112 $tabsysprefs{MARCOrgCode}                 = "Cataloging";
113 $tabsysprefs{z3950AuthorAuthFields}       = "Cataloging";
114 $tabsysprefs{z3950NormalizeAuthor}        = "Cataloging";
115 $tabsysprefs{Stemming}                    = "Cataloging";
116 $tabsysprefs{WeightFields}                = "Cataloging";
117 $tabsysprefs{NoZebra}                     = "Cataloging";
118 $tabsysprefs{NoZebraIndexes}              = "Cataloging";
119 $tabsysprefs{ReceiveBackIssues}           = "Cataloging";
120 $tabsysprefs{DefaultClassificationSource} = "Cataloging";
121 $tabsysprefs{RoutingSerials}              = "Cataloging";
122 $tabsysprefs{'item-level_itypes'}         = "Cataloging";
123 $tabsysprefs{OpacSuppression}             = "Cataloging";
124 $tabsysprefs{SpineLabelFormat}            = "Cataloging";
125 $tabsysprefs{SpineLabelAutoPrint}         = "Cataloging";
126
127 # Circulation
128 $tabsysprefs{maxoutstanding}                 = "Circulation";
129 $tabsysprefs{maxreserves}                    = "Circulation";
130 $tabsysprefs{noissuescharge}                 = "Circulation";
131 $tabsysprefs{IssuingInProcess}               = "Circulation";
132 $tabsysprefs{patronimages}                   = "Circulation";
133 $tabsysprefs{printcirculationslips}          = "Circulation";
134 $tabsysprefs{ReturnBeforeExpiry}             = "Circulation";
135 $tabsysprefs{ceilingDueDate}                 = "Circulation";
136 $tabsysprefs{SpecifyDueDate}                 = "Circulation";
137 $tabsysprefs{AutomaticItemReturn}            = "Circulation";
138 $tabsysprefs{ReservesMaxPickUpDelay}         = "Circulation";
139 $tabsysprefs{TransfersMaxDaysWarning}        = "Circulation";
140 $tabsysprefs{useDaysMode}                    = "Circulation";
141 $tabsysprefs{ReservesNeedReturns}            = "Circulation";
142 $tabsysprefs{CircAutocompl}                  = "Circulation";
143 $tabsysprefs{AllowRenewalLimitOverride}      = "Circulation";
144 $tabsysprefs{canreservefromotherbranches}    = "Circulation";
145 $tabsysprefs{finesMode}                      = "Circulation";
146 $tabsysprefs{numReturnedItemsToShow}         = "Circulation";
147 $tabsysprefs{emailLibrarianWhenHoldIsPlaced} = "Circulation";
148 $tabsysprefs{globalDueDate}                  = "Circulation";
149 $tabsysprefs{holdCancelLength}               = "Circulation";
150 $tabsysprefs{itemBarcodeInputFilter}         = "Circulation";
151 $tabsysprefs{WebBasedSelfCheck}              = "Circulation";
152 $tabsysprefs{CircControl}                    = "Circulation";
153 $tabsysprefs{finesCalendar}                  = "Circulation";
154 $tabsysprefs{previousIssuesDefaultSortOrder} = "Circulation";
155 $tabsysprefs{todaysIssuesDefaultSortOrder}   = "Circulation";
156 $tabsysprefs{HomeOrHoldingBranch}            = "Circulation";
157 $tabsysprefs{RandomizeHoldsQueueWeight}      = "Circulation";
158 $tabsysprefs{StaticHoldsQueueWeight}         = "Circulation";
159 $tabsysprefs{AllowOnShelfHolds}              = "Circulation";
160 $tabsysprefs{AllowHoldsOnDamagedItems}       = "Circulation";
161 $tabsysprefs{UseBranchTransferLimits}        = "Circulation";
162 $tabsysprefs{AllowHoldPolicyOverride}        = "Circulation";
163 $tabsysprefs{BranchTransferLimitsType}       = "Circulation";
164 $tabsysprefs{AllowNotForLoanOverride}        = "Circulation";
165 $tabsysprefs{RenewalPeriodBase}              = "Circulation";
166 $tabsysprefs{FilterBeforeOverdueReport}      = "Circulation";
167 $tabsysprefs{AllowHoldDateInFuture}          = "Circulation";
168 $tabsysprefs{OPACFineNoRenewals}             = "Circulation";
169 $tabsysprefs{InProcessingToShelvingCart}     = "Circulation";
170 $tabsysprefs{NewItemsDefaultLocation}        = "Circulation";
171 $tabsysprefs{ReturnToShelvingCart}           = "Circulation";
172
173 # Staff Client
174 $tabsysprefs{TemplateEncoding}        = "StaffClient";
175 $tabsysprefs{template}                = "StaffClient";
176 $tabsysprefs{intranetstylesheet}      = "StaffClient";
177 $tabsysprefs{IntranetNav}             = "StaffClient";
178 $tabsysprefs{intranetcolorstylesheet} = "StaffClient";
179 $tabsysprefs{intranetuserjs}          = "StaffClient";
180 $tabsysprefs{yuipath}                 = "StaffClient";
181 $tabsysprefs{IntranetmainUserblock}   = "StaffClient";
182 $tabsysprefs{viewMARC}                = "StaffClient";
183 $tabsysprefs{viewLabeledMARC}         = "StaffClient";
184 $tabsysprefs{viewISBD}                = "StaffClient";
185
186 # Patrons
187 $tabsysprefs{autoMemberNum}                = "Patrons";
188 $tabsysprefs{checkdigit}                   = "Patrons";
189 $tabsysprefs{intranetreadinghistory}       = "Patrons";
190 $tabsysprefs{NotifyBorrowerDeparture}      = "Patrons";
191 $tabsysprefs{memberofinstitution}          = "Patrons";
192 $tabsysprefs{ReadingHistory}               = "Patrons";
193 $tabsysprefs{BorrowerMandatoryField}       = "Patrons";
194 $tabsysprefs{borrowerRelationship}         = "Patrons";
195 $tabsysprefs{BorrowersTitles}              = "Patrons";
196 $tabsysprefs{patronimages}                 = "Patrons";
197 $tabsysprefs{minPasswordLength}            = "Patrons";
198 $tabsysprefs{uppercasesurnames}            = "Patrons";
199 $tabsysprefs{MaxFine}                      = "Patrons";
200 $tabsysprefs{NotifyBorrowerDeparture}      = "Patrons";
201 $tabsysprefs{AddPatronLists}               = "Patrons";
202 $tabsysprefs{PatronsPerPage}               = "Patrons";
203 $tabsysprefs{ExtendedPatronAttributes}     = "Patrons";
204 $tabsysprefs{AutoEmailOpacUser}            = "Patrons";
205 $tabsysprefs{AutoEmailPrimaryAddress}      = "Patrons";
206 $tabsysprefs{EnhancedMessagingPreferences} = "Patrons";
207 $tabsysprefs{'SMSSendDriver'}              = 'Patrons';
208
209 # I18N/L10N
210 $tabsysprefs{dateformat}    = "I18N/L10N";
211 $tabsysprefs{opaclanguages} = "I18N/L10N";
212 $tabsysprefs{opaclanguagesdisplay} = "I18N/L10N";
213 $tabsysprefs{language}      = "I18N/L10N";
214
215 # Searching
216 $tabsysprefs{defaultSortField}        = "Searching";
217 $tabsysprefs{defaultSortOrder}        = "Searching";
218 $tabsysprefs{numSearchResults}        = "Searching";
219 $tabsysprefs{OPACdefaultSortField}    = "Searching";
220 $tabsysprefs{OPACdefaultSortOrder}    = "Searching";
221 $tabsysprefs{OPACItemsResultsDisplay} = "Searching";
222 $tabsysprefs{OPACnumSearchResults}    = "Searching";
223 $tabsysprefs{QueryFuzzy}              = "Searching";
224 $tabsysprefs{QueryStemming}           = "Searching";
225 $tabsysprefs{QueryWeightFields}       = "Searching";
226 $tabsysprefs{expandedSearchOption}    = "Searching";
227 $tabsysprefs{sortbynonfiling}         = "Searching";
228 $tabsysprefs{QueryAutoTruncate}       = "Searching";
229 $tabsysprefs{QueryRemoveStopwords}    = "Searching";
230 $tabsysprefs{AdvancedSearchTypes}     = "Searching";
231
232 # EnhancedContent
233 $tabsysprefs{AmazonEnabled}          = "EnhancedContent";
234 $tabsysprefs{OPACAmazonEnabled}      = "EnhancedContent";
235 $tabsysprefs{AmazonCoverImages}      = "EnhancedContent";
236 $tabsysprefs{OPACAmazonCoverImages}  = "EnhancedContent";
237 $tabsysprefs{AWSAccessKeyID}         = "EnhancedContent";
238 $tabsysprefs{AWSPrivateKey}          = "EnhancedContent";
239 $tabsysprefs{AmazonLocale}           = "EnhancedContent";
240 $tabsysprefs{AmazonAssocTag}         = "EnhancedContent";
241 $tabsysprefs{AmazonSimilarItems}     = "EnhancedContent";
242 $tabsysprefs{OPACAmazonSimilarItems} = "EnhancedContent";
243 $tabsysprefs{AmazonReviews}          = "EnhancedContent";
244 $tabsysprefs{OPACAmazonReviews}      = "EnhancedContent";
245
246 # Babelthèque
247 $tabsysprefs{Babeltheque}            = "EnhancedContent";
248
249 # Baker & Taylor
250 $tabsysprefs{BakerTaylorBookstoreURL} = 'EnhancedContent';
251 $tabsysprefs{BakerTaylorEnabled}      = 'EnhancedContent';
252 $tabsysprefs{BakerTaylorPassword}     = 'EnhancedContent';
253 $tabsysprefs{BakerTaylorUsername}     = 'EnhancedContent';
254
255 # Library Thing for Libraries
256 $tabsysprefs{LibraryThingForLibrariesID} = "EnhancedContent"; 
257 $tabsysprefs{LibraryThingForLibrariesEnabled} = "EnhancedContent"; 
258 $tabsysprefs{LibraryThingForLibrariesTabbedView} = "EnhancedContent";
259
260 # Syndetics
261 $tabsysprefs{SyndeticsClientCode}     = 'EnhancedContent';
262 $tabsysprefs{SyndeticsEnabled}        = 'EnhancedContent';
263 $tabsysprefs{SyndeticsCoverImages}    = 'EnhancedContent';
264 $tabsysprefs{SyndeticsTOC}            = 'EnhancedContent';
265 $tabsysprefs{SyndeticsSummary}        = 'EnhancedContent';
266 $tabsysprefs{SyndeticsEditions}       = 'EnhancedContent';
267 $tabsysprefs{SyndeticsExcerpt}        = 'EnhancedContent';
268 $tabsysprefs{SyndeticsReviews}        = 'EnhancedContent';
269 $tabsysprefs{SyndeticsAuthorNotes}    = 'EnhancedContent';
270 $tabsysprefs{SyndeticsAwards}         = 'EnhancedContent';
271 $tabsysprefs{SyndeticsSeries}         = 'EnhancedContent';
272 $tabsysprefs{SyndeticsCoverImageSize} = 'EnhancedContent';
273
274
275 # FRBR
276 $tabsysprefs{FRBRizeEditions}     = "EnhancedContent";
277 $tabsysprefs{XISBN}               = "EnhancedContent";
278 $tabsysprefs{OCLCAffiliateID}     = "EnhancedContent";
279 $tabsysprefs{XISBNDailyLimit}     = "EnhancedContent";
280 $tabsysprefs{PINESISBN}           = "EnhancedContent";
281 $tabsysprefs{ThingISBN}           = "EnhancedContent";
282 $tabsysprefs{OPACFRBRizeEditions} = "EnhancedContent";
283
284 # Tags
285 $tabsysprefs{TagsEnabled}            = 'EnhancedContent';
286 $tabsysprefs{TagsExternalDictionary} = 'EnhancedContent';
287 $tabsysprefs{TagsInputOnDetail}      = 'EnhancedContent';
288 $tabsysprefs{TagsInputOnList}        = 'EnhancedContent';
289 $tabsysprefs{TagsShowOnDetail}       = 'EnhancedContent';
290 $tabsysprefs{TagsShowOnList}         = 'EnhancedContent';
291 $tabsysprefs{TagsModeration}         = 'EnhancedContent';
292 $tabsysprefs{GoogleJackets}          = 'EnhancedContent';
293 $tabsysprefs{AuthorisedValueImages}  = "EnhancedContent";
294
295 # OPAC
296 $tabsysprefs{BiblioDefaultView}          = "OPAC";
297 $tabsysprefs{LibraryName}                = "OPAC";
298 $tabsysprefs{opaccolorstylesheet}        = "OPAC";
299 $tabsysprefs{opaccredits}                = "OPAC";
300 $tabsysprefs{opaclayoutstylesheet}       = "OPAC";
301 $tabsysprefs{OpacNav}                    = "OPAC";
302 $tabsysprefs{opacsmallimage}             = "OPAC";
303 $tabsysprefs{opacstylesheet}             = "OPAC";
304 $tabsysprefs{opacthemes}                 = "OPAC";
305 $tabsysprefs{opacuserjs}                 = "OPAC";
306 $tabsysprefs{opacheader}                 = "OPAC";
307 $tabsysprefs{hideBiblioNumber}           = "OPAC";
308 $tabsysprefs{OpacMainUserBlock}          = "OPAC";
309 $tabsysprefs{OPACURLOpenInNewWindow}     = "OPAC";
310 $tabsysprefs{OPACUserCSS}                = "OPAC";
311 $tabsysprefs{OPACHighlightedWords}       = "OPAC";
312 $tabsysprefs{OPACViewOthersSuggestions}  = "OPAC";
313 $tabsysprefs{URLLinkText}                = "OPAC";
314 $tabsysprefs{OPACShelfBrowser}           = "OPAC";
315 $tabsysprefs{OPACDisplayRequestPriority} = "OPAC";
316 $tabsysprefs{OPACAllowHoldDateInFuture}  = "OPAC";
317
318 # OPAC
319 $tabsysprefs{SearchMyLibraryFirst} = "OPAC";
320 $tabsysprefs{hidelostitems}        = "OPAC";
321 $tabsysprefs{opacbookbag}          = "OPAC";
322 $tabsysprefs{OpacPasswordChange}   = "OPAC";
323 $tabsysprefs{opacreadinghistory}   = "OPAC";
324 $tabsysprefs{virtualshelves}       = "OPAC";
325 $tabsysprefs{RequestOnOpac}        = "OPAC";
326 $tabsysprefs{reviewson}            = "OPAC";
327 $tabsysprefs{OpacTopissues}        = "OPAC";
328 $tabsysprefs{OpacAuthorities}      = "OPAC";
329 $tabsysprefs{OpacCloud}            = "OPAC";
330 $tabsysprefs{opacuserlogin}        = "OPAC";
331 $tabsysprefs{AnonSuggestions}      = "OPAC";
332 $tabsysprefs{suggestion}           = "OPAC";
333 $tabsysprefs{OpacTopissue}         = "OPAC";
334 $tabsysprefs{OpacBrowser}          = "OPAC";
335 $tabsysprefs{kohaspsuggest}        = "OPAC";
336 $tabsysprefs{OpacRenewalAllowed}   = "OPAC";
337 $tabsysprefs{OPACItemHolds}        = "OPAC";
338 $tabsysprefs{OPACGroupResults}     = "OPAC";
339 $tabsysprefs{XSLTDetailsDisplay}   = "OPAC";
340 $tabsysprefs{XSLTResultsDisplay}   = "OPAC";
341 $tabsysprefs{OPACShowCheckoutName}   = "OPAC";
342
343 # Serials
344 $tabsysprefs{OPACSerialIssueDisplayCount}  = "Serials";
345 $tabsysprefs{StaffSerialIssueDisplayCount} = "Serials";
346 $tabsysprefs{OPACDisplayExtendedSubInfo}   = "Serials";
347 $tabsysprefs{OPACSubscriptionDisplay}      = "Serials";
348 $tabsysprefs{RenewSerialAddsSuggestion}    = "Serials";
349 $tabsysprefs{SubscriptionHistory}          = "Serials";
350
351 # LOGFeatures
352 $tabsysprefs{CataloguingLog}  = "Logs";
353 $tabsysprefs{BorrowersLog}    = "Logs";
354 $tabsysprefs{SubscriptionLog} = "Logs";
355 $tabsysprefs{IssueLog}        = "Logs";
356 $tabsysprefs{ReturnLog}       = "Logs";
357 $tabsysprefs{LetterLog}       = "Logs";
358 $tabsysprefs{FinesLog}        = "Logs";
359
360 # OAI-PMH variables
361 $tabsysprefs{'OAI-PMH'}           = "OAI-PMH";
362 $tabsysprefs{'OAI-PMH:archiveID'} = "OAI-PMH";
363 $tabsysprefs{'OAI-PMH:MaxCount'}  = "OAI-PMH";
364 $tabsysprefs{'OAI-PMH:Set'}       = "OAI-PMH";
365 $tabsysprefs{'OAI-PMH:Subset'}    = "OAI-PMH";
366
367 sub StringSearch {
368     my ( $searchstring, $type ) = @_;
369     my $dbh = C4::Context->dbh;
370     $searchstring =~ s/\'/\\\'/g;
371     my @data = split( ' ', $searchstring );
372     my $count = @data;
373     my @results;
374     my $cnt = 0;
375     my $sth;
376
377     # used for doing a plain-old sys-pref search
378     if ( $type && $type ne 'all' ) {
379         foreach my $syspref ( sort { lc $a cmp lc $b } keys %tabsysprefs ) {
380             if ( $tabsysprefs{$syspref} eq $type ) {
381                 my $sth = $dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
382                 $sth->execute($syspref);
383                 while ( my $data = $sth->fetchrow_hashref ) {
384                     $data->{shortvalue} = $data->{value};
385                     $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if defined( $data->{value} ) and length( $data->{value} ) > 60;
386                     push( @results, $data );
387                     $cnt++;
388                 }
389                 $sth->finish;
390             }
391         }
392     } else {
393         my $sth;
394
395         if ( $type and $type eq 'all' ) {
396             $sth = $dbh->prepare( "
397             SELECT *
398               FROM systempreferences 
399               WHERE variable LIKE ? OR explanation LIKE ? 
400               ORDER BY VARIABLE" );
401             $sth->execute( "%$searchstring%", "%$searchstring%" );
402         } else {
403             my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable not in (";
404             foreach my $syspref ( keys %tabsysprefs ) {
405                 $strsth .= $dbh->quote($syspref) . ",";
406             }
407             $strsth =~ s/,$/) /;
408             $strsth .= " order by variable";
409             $sth = $dbh->prepare($strsth);
410             $sth->execute();
411         }
412
413         while ( my $data = $sth->fetchrow_hashref ) {
414             $data->{shortvalue} = $data->{value};
415             $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if length( $data->{value} ) > 60;
416             push( @results, $data );
417             $cnt++;
418         }
419
420         $sth->finish;
421     }
422     return ( $cnt, \@results );
423 }
424
425 sub GetPrefParams {
426     my $data   = shift;
427     my $params = $data;
428     my @options;
429
430     if ( defined $data->{'options'} ) {
431         foreach my $option ( split( /\|/, $data->{'options'} ) ) {
432             my $selected = '0';
433             defined( $data->{'value'} ) and $option eq $data->{'value'} and $selected = 1;
434             push @options, { option => $option, selected => $selected };
435         }
436     }
437
438     $params->{'prefoptions'} = $data->{'options'};
439
440     if ( not defined( $data->{'type'} ) ) {
441         $params->{'type-free'} = 1;
442         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 );
443     } elsif ( $data->{'type'} eq 'Choice' ) {
444         $params->{'type-choice'} = 1;
445     } elsif ( $data->{'type'} eq 'YesNo' ) {
446         $params->{'type-yesno'} = 1;
447         $data->{'value'}        = C4::Context->boolean_preference( $data->{'variable'} );
448         if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
449             $params->{'value-yes'} = 1;
450         } else {
451             $params->{'value-no'} = 1;
452         }
453     } elsif ( $data->{'type'} eq 'Integer' || $data->{'type'} eq 'Float' ) {
454         $params->{'type-free'} = 1;
455         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 10;
456     } elsif ( $data->{'type'} eq 'Textarea' ) {
457         $params->{'type-textarea'} = 1;
458         $data->{options} =~ /(.*)\|(.*)/;
459         $params->{'cols'} = $1;
460         $params->{'rows'} = $2;
461     } elsif ( $data->{'type'} eq 'Themes' ) {
462         $params->{'type-choice'} = 1;
463         my $type = '';
464         ( $data->{'variable'} =~ m#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
465         @options = ();
466         my $currently_selected_themes;
467         my $counter = 0;
468         foreach my $theme ( split /\s+/, $data->{'value'} ) {
469             push @options, { option => $theme, counter => $counter };
470             $currently_selected_themes->{$theme} = 1;
471             $counter++;
472         }
473         foreach my $theme ( getallthemes($type) ) {
474             my $selected = '0';
475             next if $currently_selected_themes->{$theme};
476             push @options, { option => $theme, counter => $counter };
477             $counter++;
478         }
479     } elsif ( $data->{'type'} eq 'ClassSources' ) {
480         $params->{'type-choice'} = 1;
481         my $type = '';
482         @options = ();
483         my $sources = GetClassSources();
484         my $counter = 0;
485         foreach my $cn_source ( sort keys %$sources ) {
486             if ( $cn_source eq $data->{'value'} ) {
487                 push @options, { option => $cn_source, counter => $counter, selected => 1 };
488             } else {
489                 push @options, { option => $cn_source, counter => $counter };
490             }
491             $counter++;
492         }
493     } elsif ( $data->{'type'} eq 'Languages' ) {
494         my $currently_selected_languages;
495         foreach my $language ( split /\s+/, $data->{'value'} ) {
496             $currently_selected_languages->{$language} = 1;
497         }
498
499         # current language
500         my $lang = $params->{'lang'};
501         my $theme;
502         my $interface;
503         if ( $data->{'variable'} =~ /opac/ ) {
504
505             # this is the OPAC
506             $interface = 'opac';
507             $theme     = C4::Context->preference('opacthemes');
508         } else {
509
510             # this is the staff client
511             $interface = 'intranet';
512             $theme     = C4::Context->preference('template');
513         }
514         my $languages_loop = getTranslatedLanguages( $interface, $theme, $lang, $currently_selected_languages );
515
516         $params->{'languages_loop'}    = $languages_loop;
517         $params->{'type-langselector'} = 1;
518     } else {
519         $params->{'type-free'} = 1;
520         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 30;
521     }
522
523     if ( $params->{'type-choice'} || $params->{'type-free'} || $params->{'type-yesno'} ) {
524         $params->{'oneline'} = 1;
525     }
526
527     $params->{'preftype'} = $data->{'type'};
528     $params->{'options'}  = \@options;
529
530     return $params;
531 }
532
533 my $input       = new CGI;
534 my $searchfield = $input->param('searchfield') || '';
535 my $Tvalue      = $input->param('Tvalue');
536 my $offset      = $input->param('offset') || 0;
537 my $script_name = "/cgi-bin/koha/admin/systempreferences.pl";
538
539 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
540     {   template_name   => "admin/systempreferences.tmpl",
541         query           => $input,
542         type            => "intranet",
543         authnotrequired => 0,
544         flagsrequired   => { parameters => 1 },
545         debug           => 1,
546     }
547 );
548 my $pagesize = 100;
549 my $op = $input->param('op') || '';
550 $searchfield =~ s/\,//g;
551
552 if ($op) {
553     $template->param(
554         script_name => $script_name,
555         $op         => 1
556     );    # we show only the TMPL_VAR names $op
557 } else {
558     $template->param(
559         script_name => $script_name,
560         else        => 1
561     );    # we show only the TMPL_VAR names $op
562 }
563
564 if ( $op eq 'update_and_reedit' ) {
565     foreach ( $input->param ) {
566     }
567     my $value = '';
568     if ( my $currentorder = $input->param('currentorder') ) {
569         my @currentorder = split /\|/, $currentorder;
570         my $orderchanged = 0;
571         foreach my $param ( $input->param ) {
572             if ( $param =~ m#up-(\d+).x# ) {
573                 my $temp = $currentorder[$1];
574                 $currentorder[$1]       = $currentorder[ $1 - 1 ];
575                 $currentorder[ $1 - 1 ] = $temp;
576                 $orderchanged           = 1;
577                 last;
578             } elsif ( $param =~ m#down-(\d+).x# ) {
579                 my $temp = $currentorder[$1];
580                 $currentorder[$1]       = $currentorder[ $1 + 1 ];
581                 $currentorder[ $1 + 1 ] = $temp;
582                 $orderchanged           = 1;
583                 last;
584             }
585         }
586         $value = join ' ', @currentorder;
587         if ($orderchanged) {
588             $op = 'add_form';
589             $template->param(
590                 script_name => $script_name,
591                 $op         => 1
592             );    # we show only the TMPL_VAR names $op
593         } else {
594             $op          = '';
595             $searchfield = '';
596             $template->param(
597                 script_name => $script_name,
598                 else        => 1
599             );    # we show only the TMPL_VAR names $op
600         }
601     }
602     my $dbh   = C4::Context->dbh;
603     my $query = "select * from systempreferences where variable=?";
604     my $sth   = $dbh->prepare($query);
605     $sth->execute( $input->param('variable') );
606     if ( $sth->rows ) {
607         unless ( C4::Context->config('demo') ) {
608             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
609             $sth->execute( $value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions') );
610             $sth->finish;
611             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
612         }
613     } else {
614         unless ( C4::Context->config('demo') ) {
615             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
616             $sth->execute( $input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
617             $sth->finish;
618             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $input->param('value') );
619         }
620     }
621     $sth->finish;
622
623 }
624
625 ################## ADD_FORM ##################################
626 # called by default. Used to create form to add or  modify a record
627
628 if ( $op eq 'add_form' ) {
629
630     #---- if primkey exists, it's a modify action, so read values to modify...
631     my $data;
632     if ($searchfield) {
633         my $dbh = C4::Context->dbh;
634         my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
635         $sth->execute($searchfield);
636         $data = $sth->fetchrow_hashref;
637         $sth->finish;
638         $template->param( modify => 1 );
639
640         # save tab to return to if user cancels edit
641         $template->param( return_tab => $tabsysprefs{$searchfield} );
642     }
643
644     $data->{'lang'} = $template->param('lang');
645
646     $template->param( GetPrefParams($data) );
647
648     $template->param( searchfield => $searchfield );
649
650 ################## ADD_VALIDATE ##################################
651     # called by add_form, used to insert/modify data in DB
652 } elsif ( $op eq 'add_validate' ) {
653     my $dbh = C4::Context->dbh;
654     my $sth = $dbh->prepare("select * from systempreferences where variable=?");
655     $sth->execute( $input->param('variable') );
656
657     # to handle multiple values
658     my $value;
659
660     # handle multiple value strings (separated by ',')
661     my $params = $input->Vars;
662     if ( defined $params->{'value'} ) {
663         my @values = ();
664         @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
665         if (@values) {
666             $value = "";
667             for my $vl (@values) {
668                 $value .= "$vl,";
669             }
670             $value =~ s/,$//;
671         } else {
672             $value = $params->{'value'};
673         }
674     }
675     if ( $sth->rows ) {
676         unless ( C4::Context->config('demo') ) {
677             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
678             $sth->execute( $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable') );
679             $sth->finish;
680             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
681         }
682     } else {
683         unless ( C4::Context->config('demo') ) {
684             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
685             $sth->execute( $input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
686             $sth->finish;
687             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $value );
688         }
689     }
690     $sth->finish;
691     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=" . $tabsysprefs{ $input->param('variable') } . "\"></html>";
692     exit;
693 ################## DELETE_CONFIRM ##################################
694     # called by default form, used to confirm deletion of data in DB
695 } elsif ( $op eq 'delete_confirm' ) {
696     my $dbh = C4::Context->dbh;
697     my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
698     $sth->execute($searchfield);
699     my $data = $sth->fetchrow_hashref;
700     $sth->finish;
701     $template->param(
702         searchfield => $searchfield,
703         Tvalue      => $data->{'value'},
704     );
705
706     # END $OP eq DELETE_CONFIRM
707 ################## DELETE_CONFIRMED ##################################
708     # called by delete_confirm, used to effectively confirm deletion of data in DB
709 } elsif ( $op eq 'delete_confirmed' ) {
710     my $dbh = C4::Context->dbh;
711     my $sth = $dbh->prepare("delete from systempreferences where variable=?");
712     $sth->execute($searchfield);
713     my $logstring = $searchfield . " | " . $Tvalue;
714     logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring );
715     $sth->finish;
716
717     # END $OP eq DELETE_CONFIRMED
718 ################## DEFAULT ##################################
719 } else {    # DEFAULT
720             #Adding tab management for system preferences
721     my $tab = $input->param('tab');
722     $template->param( $tab => 1 );
723     my ( $count, $results ) = StringSearch( $searchfield, $tab );
724     my @loop_data = ();
725     for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
726         my $row_data = $results->[$i];
727         $row_data->{'lang'} = $template->param('lang');
728         $row_data           = GetPrefParams($row_data);                                                         # get a fresh hash for the row data
729         $row_data->{edit}   = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'variable'};
730         $row_data->{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'variable'};
731         push( @loop_data, $row_data );
732     }
733     $tab = ( $tab ? $tab : "Local Use" );
734     $template->param( loop => \@loop_data, $tab => 1 );
735     if ( $offset > 0 ) {
736         my $prevpage = $offset - $pagesize;
737         $template->param( "<a href=$script_name?offset=" . $prevpage . '&lt;&lt; Prev</a>' );
738     }
739     if ( $offset + $pagesize < $count ) {
740         my $nextpage = $offset + $pagesize;
741         $template->param( "a href=$script_name?offset=" . $nextpage . 'Next &gt;&gt;</a>' );
742     }
743     $template->param( tab => $tab, );
744 }    #---- END $OP eq DEFAULT
745 output_html_with_http_headers $input, $cookie, $template->output;