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