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