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