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