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