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