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