Bug 3098, Bug 3108, Bug 3545 -- Make MARC21 XSLT obey sysprefs in OPAC
[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{OPACSearchForTitleIn}       = "OPAC";
318 $tabsysprefs{OPACShelfBrowser}           = "OPAC";
319 $tabsysprefs{OPACDisplayRequestPriority} = "OPAC";
320 $tabsysprefs{OPACAllowHoldDateInFuture}  = "OPAC";
321 $tabsysprefs{OPACPatronDetails}  = "OPAC";
322 $tabsysprefs{OPACFinesTab}  = "OPAC";
323 $tabsysprefs{DisplayOPACiconsXSLT}       = "OPAC";
324
325 # OPAC
326 $tabsysprefs{SearchMyLibraryFirst} = "OPAC";
327 $tabsysprefs{hidelostitems}        = "OPAC";
328 $tabsysprefs{opacbookbag}          = "OPAC";
329 $tabsysprefs{OpacPasswordChange}   = "OPAC";
330 $tabsysprefs{opacreadinghistory}   = "OPAC";
331 $tabsysprefs{virtualshelves}       = "OPAC";
332 $tabsysprefs{RequestOnOpac}        = "OPAC";
333 $tabsysprefs{reviewson}            = "OPAC";
334 $tabsysprefs{OpacTopissues}        = "OPAC";
335 $tabsysprefs{OpacAuthorities}      = "OPAC";
336 $tabsysprefs{OpacCloud}            = "OPAC";
337 $tabsysprefs{opacuserlogin}        = "OPAC";
338 $tabsysprefs{AnonSuggestions}      = "OPAC";
339 $tabsysprefs{suggestion}           = "OPAC";
340 $tabsysprefs{OpacTopissue}         = "OPAC";
341 $tabsysprefs{OpacBrowser}          = "OPAC";
342 $tabsysprefs{kohaspsuggest}        = "OPAC";
343 $tabsysprefs{OpacRenewalAllowed}   = "OPAC";
344 $tabsysprefs{OPACItemHolds}        = "OPAC";
345 $tabsysprefs{OPACGroupResults}     = "OPAC";
346 $tabsysprefs{XSLTDetailsDisplay}   = "OPAC";
347 $tabsysprefs{XSLTResultsDisplay}   = "OPAC";
348 $tabsysprefs{OPACShowCheckoutName}   = "OPAC";
349
350 # Serials
351 $tabsysprefs{OPACSerialIssueDisplayCount}  = "Serials";
352 $tabsysprefs{StaffSerialIssueDisplayCount} = "Serials";
353 $tabsysprefs{OPACDisplayExtendedSubInfo}   = "Serials";
354 $tabsysprefs{OPACSubscriptionDisplay}      = "Serials";
355 $tabsysprefs{RenewSerialAddsSuggestion}    = "Serials";
356 $tabsysprefs{SubscriptionHistory}          = "Serials";
357
358 # LOGFeatures
359 $tabsysprefs{CataloguingLog}  = "Logs";
360 $tabsysprefs{BorrowersLog}    = "Logs";
361 $tabsysprefs{SubscriptionLog} = "Logs";
362 $tabsysprefs{IssueLog}        = "Logs";
363 $tabsysprefs{ReturnLog}       = "Logs";
364 $tabsysprefs{LetterLog}       = "Logs";
365 $tabsysprefs{FinesLog}        = "Logs";
366
367 # OAI-PMH variables
368 $tabsysprefs{'OAI-PMH'}           = "OAI-PMH";
369 $tabsysprefs{'OAI-PMH:archiveID'} = "OAI-PMH";
370 $tabsysprefs{'OAI-PMH:MaxCount'}  = "OAI-PMH";
371 $tabsysprefs{'OAI-PMH:Set'}       = "OAI-PMH";
372 $tabsysprefs{'OAI-PMH:Subset'}    = "OAI-PMH";
373
374 sub StringSearch {
375     my ( $searchstring, $type ) = @_;
376     my $dbh = C4::Context->dbh;
377     $searchstring =~ s/\'/\\\'/g;
378     my @data = split( ' ', $searchstring );
379     my $count = @data;
380     my @results;
381     my $cnt = 0;
382     my $sth;
383
384     # used for doing a plain-old sys-pref search
385     if ( $type && $type ne 'all' ) {
386         foreach my $syspref ( sort { lc $a cmp lc $b } keys %tabsysprefs ) {
387             if ( $tabsysprefs{$syspref} eq $type ) {
388                 my $sth = $dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
389                 $sth->execute($syspref);
390                 while ( my $data = $sth->fetchrow_hashref ) {
391                     $data->{shortvalue} = $data->{value};
392                     $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if defined( $data->{value} ) and length( $data->{value} ) > 60;
393                     push( @results, $data );
394                     $cnt++;
395                 }
396                 $sth->finish;
397             }
398         }
399     } else {
400         my $sth;
401
402         if ( $type and $type eq 'all' ) {
403             $sth = $dbh->prepare( "
404             SELECT *
405               FROM systempreferences 
406               WHERE variable LIKE ? OR explanation LIKE ? 
407               ORDER BY VARIABLE" );
408             $sth->execute( "%$searchstring%", "%$searchstring%" );
409         } else {
410             my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable not in (";
411             foreach my $syspref ( keys %tabsysprefs ) {
412                 $strsth .= $dbh->quote($syspref) . ",";
413             }
414             $strsth =~ s/,$/) /;
415             $strsth .= " order by variable";
416             $sth = $dbh->prepare($strsth);
417             $sth->execute();
418         }
419
420         while ( my $data = $sth->fetchrow_hashref ) {
421             $data->{shortvalue} = $data->{value};
422             $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if length( $data->{value} ) > 60;
423             push( @results, $data );
424             $cnt++;
425         }
426
427         $sth->finish;
428     }
429     return ( $cnt, \@results );
430 }
431
432 sub GetPrefParams {
433     my $data   = shift;
434     my $params = $data;
435     my @options;
436
437     if ( defined $data->{'options'} ) {
438         foreach my $option ( split( /\|/, $data->{'options'} ) ) {
439             my $selected = '0';
440             defined( $data->{'value'} ) and $option eq $data->{'value'} and $selected = 1;
441             push @options, { option => $option, selected => $selected };
442         }
443     }
444
445     $params->{'prefoptions'} = $data->{'options'};
446
447     if ( not defined( $data->{'type'} ) ) {
448         $params->{'type-free'} = 1;
449         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 );
450     } elsif ( $data->{'type'} eq 'Choice' ) {
451         $params->{'type-choice'} = 1;
452     } elsif ( $data->{'type'} eq 'YesNo' ) {
453         $params->{'type-yesno'} = 1;
454         $data->{'value'}        = C4::Context->boolean_preference( $data->{'variable'} );
455         if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
456             $params->{'value-yes'} = 1;
457         } else {
458             $params->{'value-no'} = 1;
459         }
460     } elsif ( $data->{'type'} eq 'Integer' || $data->{'type'} eq 'Float' ) {
461         $params->{'type-free'} = 1;
462         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 10;
463     } elsif ( $data->{'type'} eq 'Textarea' ) {
464         $params->{'type-textarea'} = 1;
465         $data->{options} =~ /(.*)\|(.*)/;
466         $params->{'cols'} = $1;
467         $params->{'rows'} = $2;
468     } elsif ( $data->{'type'} eq 'Themes' ) {
469         $params->{'type-choice'} = 1;
470         my $type = '';
471         ( $data->{'variable'} =~ m#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
472         @options = ();
473         my $currently_selected_themes;
474         my $counter = 0;
475         foreach my $theme ( split /\s+/, $data->{'value'} ) {
476             push @options, { option => $theme, counter => $counter };
477             $currently_selected_themes->{$theme} = 1;
478             $counter++;
479         }
480         foreach my $theme ( getallthemes($type) ) {
481             my $selected = '0';
482             next if $currently_selected_themes->{$theme};
483             push @options, { option => $theme, counter => $counter };
484             $counter++;
485         }
486     } elsif ( $data->{'type'} eq 'ClassSources' ) {
487         $params->{'type-choice'} = 1;
488         my $type = '';
489         @options = ();
490         my $sources = GetClassSources();
491         my $counter = 0;
492         foreach my $cn_source ( sort keys %$sources ) {
493             if ( $cn_source eq $data->{'value'} ) {
494                 push @options, { option => $cn_source, counter => $counter, selected => 1 };
495             } else {
496                 push @options, { option => $cn_source, counter => $counter };
497             }
498             $counter++;
499         }
500     } elsif ( $data->{'type'} eq 'Languages' ) {
501         my $currently_selected_languages;
502         foreach my $language ( split /\s+/, $data->{'value'} ) {
503             $currently_selected_languages->{$language} = 1;
504         }
505
506         # current language
507         my $lang = $params->{'lang'};
508         my $theme;
509         my $interface;
510         if ( $data->{'variable'} =~ /opac/ ) {
511
512             # this is the OPAC
513             $interface = 'opac';
514             $theme     = C4::Context->preference('opacthemes');
515         } else {
516
517             # this is the staff client
518             $interface = 'intranet';
519             $theme     = C4::Context->preference('template');
520         }
521         my $languages_loop = getTranslatedLanguages( $interface, $theme, $lang, $currently_selected_languages );
522
523         $params->{'languages_loop'}    = $languages_loop;
524         $params->{'type-langselector'} = 1;
525     } else {
526         $params->{'type-free'} = 1;
527         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 30;
528     }
529
530     if ( $params->{'type-choice'} || $params->{'type-free'} || $params->{'type-yesno'} ) {
531         $params->{'oneline'} = 1;
532     }
533
534     $params->{'preftype'} = $data->{'type'};
535     $params->{'options'}  = \@options;
536
537     return $params;
538 }
539
540 my $input       = new CGI;
541 my $searchfield = $input->param('searchfield') || '';
542 my $Tvalue      = $input->param('Tvalue');
543 my $offset      = $input->param('offset') || 0;
544 my $script_name = "/cgi-bin/koha/admin/systempreferences.pl";
545
546 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
547     {   template_name   => "admin/systempreferences.tmpl",
548         query           => $input,
549         type            => "intranet",
550         authnotrequired => 0,
551         flagsrequired   => { parameters => 1 },
552         debug           => 1,
553     }
554 );
555 my $pagesize = 100;
556 my $op = $input->param('op') || '';
557 $searchfield =~ s/\,//g;
558
559 if ($op) {
560     $template->param(
561         script_name => $script_name,
562         $op         => 1
563     );    # we show only the TMPL_VAR names $op
564 } else {
565     $template->param(
566         script_name => $script_name,
567         else        => 1
568     );    # we show only the TMPL_VAR names $op
569 }
570
571 if ( $op eq 'update_and_reedit' ) {
572     foreach ( $input->param ) {
573     }
574     my $value = '';
575     if ( my $currentorder = $input->param('currentorder') ) {
576         my @currentorder = split /\|/, $currentorder;
577         my $orderchanged = 0;
578         foreach my $param ( $input->param ) {
579             if ( $param =~ m#up-(\d+).x# ) {
580                 my $temp = $currentorder[$1];
581                 $currentorder[$1]       = $currentorder[ $1 - 1 ];
582                 $currentorder[ $1 - 1 ] = $temp;
583                 $orderchanged           = 1;
584                 last;
585             } elsif ( $param =~ m#down-(\d+).x# ) {
586                 my $temp = $currentorder[$1];
587                 $currentorder[$1]       = $currentorder[ $1 + 1 ];
588                 $currentorder[ $1 + 1 ] = $temp;
589                 $orderchanged           = 1;
590                 last;
591             }
592         }
593         $value = join ' ', @currentorder;
594         if ($orderchanged) {
595             $op = 'add_form';
596             $template->param(
597                 script_name => $script_name,
598                 $op         => 1
599             );    # we show only the TMPL_VAR names $op
600         } else {
601             $op          = '';
602             $searchfield = '';
603             $template->param(
604                 script_name => $script_name,
605                 else        => 1
606             );    # we show only the TMPL_VAR names $op
607         }
608     }
609     my $dbh   = C4::Context->dbh;
610     my $query = "select * from systempreferences where variable=?";
611     my $sth   = $dbh->prepare($query);
612     $sth->execute( $input->param('variable') );
613     if ( $sth->rows ) {
614         unless ( C4::Context->config('demo') ) {
615             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
616             $sth->execute( $value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions') );
617             $sth->finish;
618             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
619         }
620     } else {
621         unless ( C4::Context->config('demo') ) {
622             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
623             $sth->execute( $input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
624             $sth->finish;
625             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $input->param('value') );
626         }
627     }
628     $sth->finish;
629
630 }
631
632 ################## ADD_FORM ##################################
633 # called by default. Used to create form to add or  modify a record
634
635 if ( $op eq 'add_form' ) {
636
637     #---- if primkey exists, it's a modify action, so read values to modify...
638     my $data;
639     if ($searchfield) {
640         my $dbh = C4::Context->dbh;
641         my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
642         $sth->execute($searchfield);
643         $data = $sth->fetchrow_hashref;
644         $sth->finish;
645         $template->param( modify => 1 );
646
647         # save tab to return to if user cancels edit
648         $template->param( return_tab => $tabsysprefs{$searchfield} );
649     }
650
651     $data->{'lang'} = $template->param('lang');
652
653     $template->param( GetPrefParams($data) );
654
655     $template->param( searchfield => $searchfield );
656
657 ################## ADD_VALIDATE ##################################
658     # called by add_form, used to insert/modify data in DB
659 } elsif ( $op eq 'add_validate' ) {
660     my $dbh = C4::Context->dbh;
661     my $sth = $dbh->prepare("select * from systempreferences where variable=?");
662     $sth->execute( $input->param('variable') );
663
664     # to handle multiple values
665     my $value;
666
667     # handle multiple value strings (separated by ',')
668     my $params = $input->Vars;
669     if ( defined $params->{'value'} ) {
670         my @values = ();
671         @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
672         if (@values) {
673             $value = "";
674             for my $vl (@values) {
675                 $value .= "$vl,";
676             }
677             $value =~ s/,$//;
678         } else {
679             $value = $params->{'value'};
680         }
681     }
682     if ( $sth->rows ) {
683         unless ( C4::Context->config('demo') ) {
684             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
685             $sth->execute( $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable') );
686             $sth->finish;
687             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
688         }
689     } else {
690         unless ( C4::Context->config('demo') ) {
691             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
692             $sth->execute( $input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
693             $sth->finish;
694             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $value );
695         }
696     }
697     $sth->finish;
698     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=" . $tabsysprefs{ $input->param('variable') } . "\"></html>";
699     exit;
700 ################## DELETE_CONFIRM ##################################
701     # called by default form, used to confirm deletion of data in DB
702 } elsif ( $op eq 'delete_confirm' ) {
703     my $dbh = C4::Context->dbh;
704     my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
705     $sth->execute($searchfield);
706     my $data = $sth->fetchrow_hashref;
707     $sth->finish;
708     $template->param(
709         searchfield => $searchfield,
710         Tvalue      => $data->{'value'},
711     );
712
713     # END $OP eq DELETE_CONFIRM
714 ################## DELETE_CONFIRMED ##################################
715     # called by delete_confirm, used to effectively confirm deletion of data in DB
716 } elsif ( $op eq 'delete_confirmed' ) {
717     my $dbh = C4::Context->dbh;
718     my $sth = $dbh->prepare("delete from systempreferences where variable=?");
719     $sth->execute($searchfield);
720     my $logstring = $searchfield . " | " . $Tvalue;
721     logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring );
722     $sth->finish;
723
724     # END $OP eq DELETE_CONFIRMED
725 ################## DEFAULT ##################################
726 } else {    # DEFAULT
727             #Adding tab management for system preferences
728     my $tab = $input->param('tab');
729     $template->param( $tab => 1 );
730     my ( $count, $results ) = StringSearch( $searchfield, $tab );
731     my @loop_data = ();
732     for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
733         my $row_data = $results->[$i];
734         $row_data->{'lang'} = $template->param('lang');
735         $row_data           = GetPrefParams($row_data);                                                         # get a fresh hash for the row data
736         $row_data->{edit}   = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'variable'};
737         $row_data->{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'variable'};
738         push( @loop_data, $row_data );
739     }
740     $tab = ( $tab ? $tab : "Local Use" );
741     $template->param( loop => \@loop_data, $tab => 1 );
742     if ( $offset > 0 ) {
743         my $prevpage = $offset - $pagesize;
744         $template->param( "<a href=$script_name?offset=" . $prevpage . '&lt;&lt; Prev</a>' );
745     }
746     if ( $offset + $pagesize < $count ) {
747         my $nextpage = $offset + $pagesize;
748         $template->param( "a href=$script_name?offset=" . $nextpage . 'Next &gt;&gt;</a>' );
749     }
750     $template->param( tab => $tab, );
751 }    #---- END $OP eq DEFAULT
752 output_html_with_http_headers $input, $cookie, $template->output;