whitespace cleanup - admin/systempreferences.pl
[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 $tabsysprefs{RenewSerialAddsSuggestion} = "Acquisitions";
74
75 # Admin
76 $tabsysprefs{singleBranchMode}      = "Admin";
77 $tabsysprefs{staffClientBaseURL}    = "Admin";
78 $tabsysprefs{Version}               = "Admin";
79 $tabsysprefs{OpacMaintenance}       = "Admin";
80 $tabsysprefs{FrameworksLoaded}      = "Admin";
81 $tabsysprefs{libraryAddress}        = "Admin";
82 $tabsysprefs{delimiter}             = "Admin";
83 $tabsysprefs{IndependantBranches}   = "Admin";
84 $tabsysprefs{insecure}              = "Admin";
85 $tabsysprefs{KohaAdmin}             = "Admin";
86 $tabsysprefs{KohaAdminEmailAddress} = "Admin";
87 $tabsysprefs{MIME}                  = "Admin";
88 $tabsysprefs{timeout}               = "Admin";
89 $tabsysprefs{Intranet_includes}     = "Admin";
90 $tabsysprefs{AutoLocation}          = "Admin";
91 $tabsysprefs{DebugLevel}            = "Admin";
92 $tabsysprefs{SessionStorage}        = "Admin";
93 $tabsysprefs{noItemTypeImages}      = "Admin";
94 $tabsysprefs{OPACBaseURL}           = "Admin";
95 $tabsysprefs{GranularPermissions}   = "Admin";
96
97 # Authorities
98 $tabsysprefs{authoritysep}          = "Authorities";
99 $tabsysprefs{AuthDisplayHierarchy}  = "Authorities";
100 $tabsysprefs{dontmerge}             = "Authorities";
101 $tabsysprefs{BiblioAddsAuthorities} = "Authorities";
102
103 # Cataloguing
104 $tabsysprefs{advancedMARCEditor}          = "Cataloguing";
105 $tabsysprefs{autoBarcode}                 = "Cataloguing";
106 $tabsysprefs{hide_marc}                   = "Cataloguing";
107 $tabsysprefs{IntranetBiblioDefaultView}   = "Cataloguing";
108 $tabsysprefs{ISBD}                        = "Cataloguing";
109 $tabsysprefs{itemcallnumber}              = "Cataloguing";
110 $tabsysprefs{LabelMARCView}               = "Cataloguing";
111 $tabsysprefs{marc}                        = "Cataloguing";
112 $tabsysprefs{marcflavour}                 = "Cataloguing";
113 $tabsysprefs{MARCOrgCode}                 = "Cataloguing";
114 $tabsysprefs{z3950AuthorAuthFields}       = "Cataloguing";
115 $tabsysprefs{z3950NormalizeAuthor}        = "Cataloguing";
116 $tabsysprefs{Stemming}                    = "Cataloguing";
117 $tabsysprefs{WeightFields}                = "Cataloguing";
118 $tabsysprefs{NoZebra}                     = "Cataloguing";
119 $tabsysprefs{NoZebraIndexes}              = "Cataloguing";
120 $tabsysprefs{ReceiveBackIssues}           = "Cataloguing";
121 $tabsysprefs{DefaultClassificationSource} = "Cataloguing";
122 $tabsysprefs{RoutingSerials}              = "Cataloguing";
123 $tabsysprefs{'item-level_itypes'}         = "Cataloguing";
124 $tabsysprefs{OpacSuppression}             = "Cataloguing";
125
126 # Circulation
127 $tabsysprefs{maxoutstanding}                 = "Circulation";
128 $tabsysprefs{maxreserves}                    = "Circulation";
129 $tabsysprefs{noissuescharge}                 = "Circulation";
130 $tabsysprefs{IssuingInProcess}               = "Circulation";
131 $tabsysprefs{patronimages}                   = "Circulation";
132 $tabsysprefs{printcirculationslips}          = "Circulation";
133 $tabsysprefs{ReturnBeforeExpiry}             = "Circulation";
134 $tabsysprefs{SpecifyDueDate}                 = "Circulation";
135 $tabsysprefs{AutomaticItemReturn}            = "Circulation";
136 $tabsysprefs{ReservesMaxPickUpDelay}         = "Circulation";
137 $tabsysprefs{TransfersMaxDaysWarning}        = "Circulation";
138 $tabsysprefs{useDaysMode}                    = "Circulation";
139 $tabsysprefs{ReservesNeedReturns}            = "Circulation";
140 $tabsysprefs{CircAutocompl}                  = "Circulation";
141 $tabsysprefs{AllowRenewalLimitOverride}      = "Circulation";
142 $tabsysprefs{canreservefromotherbranches}    = "Circulation";
143 $tabsysprefs{finesMode}                      = "Circulation";
144 $tabsysprefs{emailLibrarianWhenHoldIsPlaced} = "Circulation";
145 $tabsysprefs{globalDueDate}                  = "Circulation";
146 $tabsysprefs{holdCancelLength}               = "Circulation";
147 $tabsysprefs{itemBarcodeInputFilter}         = "Circulation";
148 $tabsysprefs{WebBasedSelfCheck}              = "Circulation";
149 $tabsysprefs{CircControl}                    = "Circulation";
150 $tabsysprefs{finesCalendar}                  = "Circulation";
151 $tabsysprefs{previousIssuesDefaultSortOrder} = "Circulation";
152 $tabsysprefs{todaysIssuesDefaultSortOrder}   = "Circulation";
153 $tabsysprefs{HomeOrHoldingBranch}            = "Circulation";
154 $tabsysprefs{RandomizeHoldsQueueWeight}      = "Circulation";
155 $tabsysprefs{StaticHoldsQueueWeight}         = "Circulation";
156 $tabsysprefs{AllowOnShelfHolds}              = "Circulation";
157 $tabsysprefs{AllowHoldsOnDamagedItems}       = "Circulation";
158
159 # Staff Client
160 $tabsysprefs{TemplateEncoding}        = "StaffClient";
161 $tabsysprefs{template}                = "StaffClient";
162 $tabsysprefs{intranetstylesheet}      = "StaffClient";
163 $tabsysprefs{IntranetNav}             = "StaffClient";
164 $tabsysprefs{intranetcolorstylesheet} = "StaffClient";
165 $tabsysprefs{intranetuserjs}          = "StaffClient";
166 $tabsysprefs{yuipath}                 = "StaffClient";
167 $tabsysprefs{IntranetmainUserblock}   = "StaffClient";
168
169 # Patrons
170 $tabsysprefs{autoMemberNum}                = "Patrons";
171 $tabsysprefs{checkdigit}                   = "Patrons";
172 $tabsysprefs{intranetreadinghistory}       = "Patrons";
173 $tabsysprefs{NotifyBorrowerDeparture}      = "Patrons";
174 $tabsysprefs{memberofinstitution}          = "Patrons";
175 $tabsysprefs{ReadingHistory}               = "Patrons";
176 $tabsysprefs{BorrowerMandatoryField}       = "Patrons";
177 $tabsysprefs{borrowerRelationship}         = "Patrons";
178 $tabsysprefs{BorrowersTitles}              = "Patrons";
179 $tabsysprefs{patronimages}                 = "Patrons";
180 $tabsysprefs{MinPasswordLength}            = "Patrons";
181 $tabsysprefs{uppercasesurnames}            = "Patrons";
182 $tabsysprefs{NoReturnSetLost}              = "Patrons";
183 $tabsysprefs{MaxFine}                      = "Patrons";
184 $tabsysprefs{NotifyBorrowerDeparture}      = "Patrons";
185 $tabsysprefs{AddPatronLists}               = "Patrons";
186 $tabsysprefs{PatronsPerPage}               = "Patrons";
187 $tabsysprefs{ExtendedPatronAttributes}     = "Patrons";
188 $tabsysprefs{AutoEmailOpacUser}            = "Patrons";
189 $tabsysprefs{AutoEmailPrimaryAddress}      = "Patrons";
190 $tabsysprefs{EnhancedMessagingPreferences} = "Patrons";
191 $tabsysprefs{'SMSSendDriver'}              = 'Patrons';
192
193 # I18N/L10N
194 $tabsysprefs{dateformat}    = "I18N/L10N";
195 $tabsysprefs{opaclanguages} = "I18N/L10N";
196 $tabsysprefs{opacthemes}    = "I18N/L10N";
197 $tabsysprefs{language}      = "I18N/L10N";
198
199 # Searching
200 $tabsysprefs{defaultSortField}        = "Searching";
201 $tabsysprefs{defaultSortOrder}        = "Searching";
202 $tabsysprefs{numSearchResults}        = "Searching";
203 $tabsysprefs{OPACdefaultSortField}    = "Searching";
204 $tabsysprefs{OPACdefaultSortOrder}    = "Searching";
205 $tabsysprefs{OPACItemsResultsDisplay} = "Searching";
206 $tabsysprefs{OPACnumSearchResults}    = "Searching";
207 $tabsysprefs{QueryFuzzy}              = "Searching";
208 $tabsysprefs{QueryStemming}           = "Searching";
209 $tabsysprefs{QueryWeightFields}       = "Searching";
210 $tabsysprefs{expandedSearchOption}    = "Searching";
211 $tabsysprefs{sortbynonfiling}         = "Searching";
212 $tabsysprefs{QueryAutoTruncate}       = "Searching";
213 $tabsysprefs{QueryRemoveStopwords}    = "Searching";
214 $tabsysprefs{AdvancedSearchTypes}     = "Searching";
215
216 # EnhancedContent
217 $tabsysprefs{AmazonContent}          = "EnhancedContent";
218 $tabsysprefs{AWSAccessKeyID}         = "EnhancedContent";
219 $tabsysprefs{AmazonLocale}           = "EnhancedContent";
220 $tabsysprefs{AmazonAssocTag}         = "EnhancedContent";
221 $tabsysprefs{AmazonSimilarItems}     = "EnhancedContent";
222 $tabsysprefs{OPACAmazonContent}      = "EnhancedContent";
223 $tabsysprefs{OPACAmazonSimilarItems} = "EnhancedContent";
224
225 # Baker & Taylor
226 $tabsysprefs{BakerTaylorBookstoreURL} = 'EnhancedContent';
227 $tabsysprefs{BakerTaylorEnabled}      = 'EnhancedContent';
228 $tabsysprefs{BakerTaylorPassword}     = 'EnhancedContent';
229 $tabsysprefs{BakerTaylorUsername}     = 'EnhancedContent';
230
231 # FRBR
232 $tabsysprefs{FRBRizeEditions}     = "EnhancedContent";
233 $tabsysprefs{XISBN}               = "EnhancedContent";
234 $tabsysprefs{OCLCAffiliateID}     = "EnhancedContent";
235 $tabsysprefs{XISBNDailyLimit}     = "EnhancedContent";
236 $tabsysprefs{PINESISBN}           = "EnhancedContent";
237 $tabsysprefs{ThingISBN}           = "EnhancedContent";
238 $tabsysprefs{OPACFRBRizeEditions} = "EnhancedContent";
239
240 # Tags
241 $tabsysprefs{TagsEnabled}            = 'EnhancedContent';
242 $tabsysprefs{TagsExternalDictionary} = 'EnhancedContent';
243 $tabsysprefs{TagsInputOnDetail}      = 'EnhancedContent';
244 $tabsysprefs{TagsInputOnList}        = 'EnhancedContent';
245 $tabsysprefs{TagsShowOnDetail}       = 'EnhancedContent';
246 $tabsysprefs{TagsShowOnList}         = 'EnhancedContent';
247 $tabsysprefs{TagsModeration}         = 'EnhancedContent';
248 $tabsysprefs{GoogleJackets}          = 'EnhancedContent';
249 $tabsysprefs{AuthorisedValueImages}  = "EnhancedContent";
250
251 # OPAC
252 $tabsysprefs{BiblioDefaultView}          = "OPAC";
253 $tabsysprefs{LibraryName}                = "OPAC";
254 $tabsysprefs{opaccolorstylesheet}        = "OPAC";
255 $tabsysprefs{opaccredits}                = "OPAC";
256 $tabsysprefs{opaclayoutstylesheet}       = "OPAC";
257 $tabsysprefs{OpacNav}                    = "OPAC";
258 $tabsysprefs{opacsmallimage}             = "OPAC";
259 $tabsysprefs{opacstylesheet}             = "OPAC";
260 $tabsysprefs{opacthemes}                 = "OPAC";
261 $tabsysprefs{opacuserjs}                 = "OPAC";
262 $tabsysprefs{SubscriptionHistory}        = "OPAC";
263 $tabsysprefs{opacheader}                 = "OPAC";
264 $tabsysprefs{noOPACUserLogin}            = "OPAC";
265 $tabsysprefs{hideBiblioNumber}           = "OPAC";
266 $tabsysprefs{OPACDisplayExtendedSubInfo} = "OPAC";
267 $tabsysprefs{OpacMainUserBlock}          = "OPAC";
268 $tabsysprefs{OPACSubscriptionDisplay}    = "OPAC";
269 $tabsysprefs{OPACURLOpenInNewWindow}     = "OPAC";
270 $tabsysprefs{OPACUserCSS}                = "OPAC";
271 $tabsysprefs{OPACHighlightedWords}       = "OPAC";
272 $tabsysprefs{OPACViewOthersSuggestions}  = "OPAC";
273 $tabsysprefs{URLLinkText}                = "OPAC";
274 $tabsysprefs{OPACShelfBrowser}           = "OPAC";
275 $tabsysprefs{OPACDisplayRequestPriority} = "OPAC";
276
277 # OPAC
278 $tabsysprefs{SearchMyLibraryFirst} = "OPAC";
279 $tabsysprefs{hidelostitems}        = "OPAC";
280 $tabsysprefs{opacbookbag}          = "OPAC";
281 $tabsysprefs{opaclanguagesdisplay} = "OPAC";
282 $tabsysprefs{OpacPasswordChange}   = "OPAC";
283 $tabsysprefs{opacreadinghistory}   = "OPAC";
284 $tabsysprefs{virtualshelves}       = "OPAC";
285 $tabsysprefs{RequestOnOpac}        = "OPAC";
286 $tabsysprefs{reviewson}            = "OPAC";
287 $tabsysprefs{OpacTopissues}        = "OPAC";
288 $tabsysprefs{OpacAuthorities}      = "OPAC";
289 $tabsysprefs{OpacCloud}            = "OPAC";
290 $tabsysprefs{opacuserlogin}        = "OPAC";
291 $tabsysprefs{AnonSuggestions}      = "OPAC";
292 $tabsysprefs{suggestion}           = "OPAC";
293 $tabsysprefs{OpacTopissue}         = "OPAC";
294 $tabsysprefs{OpacBrowser}          = "OPAC";
295 $tabsysprefs{kohaspsuggest}        = "OPAC";
296 $tabsysprefs{OpacRenewalAllowed}   = "OPAC";
297 $tabsysprefs{OPACItemHolds}        = "OPAC";
298 $tabsysprefs{OPACGroupResults}     = "OPAC";
299 $tabsysprefs{XSLTDetailsDisplay}   = "OPAC";
300 $tabsysprefs{XSLTResultsDisplay}   = "OPAC";
301
302 # LOGFeatures
303 $tabsysprefs{CataloguingLog}  = "Logs";
304 $tabsysprefs{BorrowersLog}    = "Logs";
305 $tabsysprefs{SubscriptionLog} = "Logs";
306 $tabsysprefs{IssueLog}        = "Logs";
307 $tabsysprefs{ReturnLog}       = "Logs";
308 $tabsysprefs{LetterLog}       = "Logs";
309 $tabsysprefs{FinesLog}        = "Logs";
310
311 # OAI-PMH variables
312 $tabsysprefs{'OAI-PMH'}           = "OAI-PMH";
313 $tabsysprefs{'OAI-PMH:archiveID'} = "OAI-PMH";
314 $tabsysprefs{'OAI-PMH:MaxCount'}  = "OAI-PMH";
315 $tabsysprefs{'OAI-PMH:Set'}       = "OAI-PMH";
316 $tabsysprefs{'OAI-PMH:Subset'}    = "OAI-PMH";
317
318 sub StringSearch {
319     my ( $searchstring, $type ) = @_;
320     my $dbh = C4::Context->dbh;
321     $searchstring =~ s/\'/\\\'/g;
322     my @data = split( ' ', $searchstring );
323     my $count = @data;
324     my @results;
325     my $cnt = 0;
326     my $sth;
327
328     # used for doing a plain-old sys-pref search
329     if ( $type && $type ne 'all' ) {
330         foreach my $syspref ( sort { lc $a cmp lc $b } keys %tabsysprefs ) {
331             if ( $tabsysprefs{$syspref} eq $type ) {
332                 my $sth = $dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
333                 $sth->execute($syspref);
334                 while ( my $data = $sth->fetchrow_hashref ) {
335                     $data->{shortvalue} = $data->{value};
336                     $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if defined( $data->{value} ) and length( $data->{value} ) > 60;
337                     push( @results, $data );
338                     $cnt++;
339                 }
340                 $sth->finish;
341             }
342         }
343     } else {
344         my $sth;
345
346         if ( $type and $type eq 'all' ) {
347             $sth = $dbh->prepare( "
348             SELECT *
349               FROM systempreferences 
350               WHERE variable LIKE ? OR explanation LIKE ? 
351               ORDER BY VARIABLE" );
352             $sth->execute( "%$searchstring%", "%$searchstring%" );
353         } else {
354             my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable not in (";
355             foreach my $syspref ( keys %tabsysprefs ) {
356                 $strsth .= $dbh->quote($syspref) . ",";
357             }
358             $strsth =~ s/,$/) /;
359             $strsth .= " order by variable";
360             $sth = $dbh->prepare($strsth);
361             $sth->execute();
362         }
363
364         while ( my $data = $sth->fetchrow_hashref ) {
365             $data->{shortvalue} = $data->{value};
366             $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if length( $data->{value} ) > 60;
367             push( @results, $data );
368             $cnt++;
369         }
370
371         $sth->finish;
372     }
373     return ( $cnt, \@results );
374 }
375
376 sub GetPrefParams {
377     my $data   = shift;
378     my $params = $data;
379     my @options;
380
381     if ( defined $data->{'options'} ) {
382         foreach my $option ( split( /\|/, $data->{'options'} ) ) {
383             my $selected = '0';
384             defined( $data->{'value'} ) and $option eq $data->{'value'} and $selected = 1;
385             push @options, { option => $option, selected => $selected };
386         }
387     }
388
389     $params->{'prefoptions'} = $data->{'options'};
390
391     if ( not defined( $data->{'type'} ) ) {
392         $params->{'type-free'} = 1;
393         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 );
394     } elsif ( $data->{'type'} eq 'Choice' ) {
395         $params->{'type-choice'} = 1;
396     } elsif ( $data->{'type'} eq 'YesNo' ) {
397         $params->{'type-yesno'} = 1;
398         $data->{'value'}        = C4::Context->boolean_preference( $data->{'variable'} );
399         if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
400             $params->{'value-yes'} = 1;
401         } else {
402             $params->{'value-no'} = 1;
403         }
404     } elsif ( $data->{'type'} eq 'Integer' || $data->{'type'} eq 'Float' ) {
405         $params->{'type-free'} = 1;
406         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 10;
407     } elsif ( $data->{'type'} eq 'Textarea' ) {
408         $params->{'type-textarea'} = 1;
409         $data->{options} =~ /(.*)\|(.*)/;
410         $params->{'cols'} = $1;
411         $params->{'rows'} = $2;
412     } elsif ( $data->{'type'} eq 'Themes' ) {
413         $params->{'type-choice'} = 1;
414         my $type = '';
415         ( $data->{'variable'} =~ m#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
416         @options = ();
417         my $currently_selected_themes;
418         my $counter = 0;
419         foreach my $theme ( split /\s+/, $data->{'value'} ) {
420             push @options, { option => $theme, counter => $counter };
421             $currently_selected_themes->{$theme} = 1;
422             $counter++;
423         }
424         foreach my $theme ( getallthemes($type) ) {
425             my $selected = '0';
426             next if $currently_selected_themes->{$theme};
427             push @options, { option => $theme, counter => $counter };
428             $counter++;
429         }
430     } elsif ( $data->{'type'} eq 'ClassSources' ) {
431         $params->{'type-choice'} = 1;
432         my $type = '';
433         @options = ();
434         my $sources = GetClassSources();
435         my $counter = 0;
436         foreach my $cn_source ( sort keys %$sources ) {
437             if ( $cn_source eq $data->{'value'} ) {
438                 push @options, { option => $cn_source, counter => $counter, selected => 1 };
439             } else {
440                 push @options, { option => $cn_source, counter => $counter };
441             }
442             $counter++;
443         }
444     } elsif ( $data->{'type'} eq 'Languages' ) {
445         my $currently_selected_languages;
446         foreach my $language ( split /\s+/, $data->{'value'} ) {
447             $currently_selected_languages->{$language} = 1;
448         }
449
450         # current language
451         my $lang = $params->{'lang'};
452         my $theme;
453         my $interface;
454         if ( $data->{'variable'} =~ /opac/ ) {
455
456             # this is the OPAC
457             $interface = 'opac';
458             $theme     = C4::Context->preference('opacthemes');
459         } else {
460
461             # this is the staff client
462             $interface = 'intranet';
463             $theme     = C4::Context->preference('template');
464         }
465         my $languages_loop = getTranslatedLanguages( $interface, $theme, $lang, $currently_selected_languages );
466
467         $params->{'languages_loop'}    = $languages_loop;
468         $params->{'type-langselector'} = 1;
469     } else {
470         $params->{'type-free'} = 1;
471         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 30;
472     }
473
474     if ( $params->{'type-choice'} || $params->{'type-free'} || $params->{'type-yesno'} ) {
475         $params->{'oneline'} = 1;
476     }
477
478     $params->{'preftype'} = $data->{'type'};
479     $params->{'options'}  = \@options;
480
481     return $params;
482 }
483
484 my $input       = new CGI;
485 my $searchfield = $input->param('searchfield') || '';
486 my $Tvalue      = $input->param('Tvalue');
487 my $offset      = $input->param('offset') || 0;
488 my $script_name = "/cgi-bin/koha/admin/systempreferences.pl";
489
490 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
491     {   template_name   => "admin/systempreferences.tmpl",
492         query           => $input,
493         type            => "intranet",
494         authnotrequired => 0,
495         flagsrequired   => { parameters => 1 },
496         debug           => 1,
497     }
498 );
499 my $pagesize = 100;
500 my $op = $input->param('op') || '';
501 $searchfield =~ s/\,//g;
502
503 if ($op) {
504     $template->param(
505         script_name => $script_name,
506         $op         => 1
507     );    # we show only the TMPL_VAR names $op
508 } else {
509     $template->param(
510         script_name => $script_name,
511         else        => 1
512     );    # we show only the TMPL_VAR names $op
513 }
514
515 if ( $op eq 'update_and_reedit' ) {
516     foreach ( $input->param ) {
517     }
518     my $value = '';
519     if ( my $currentorder = $input->param('currentorder') ) {
520         my @currentorder = split /\|/, $currentorder;
521         my $orderchanged = 0;
522         foreach my $param ( $input->param ) {
523             if ( $param =~ m#up-(\d+).x# ) {
524                 my $temp = $currentorder[$1];
525                 $currentorder[$1]       = $currentorder[ $1 - 1 ];
526                 $currentorder[ $1 - 1 ] = $temp;
527                 $orderchanged           = 1;
528                 last;
529             } elsif ( $param =~ m#down-(\d+).x# ) {
530                 my $temp = $currentorder[$1];
531                 $currentorder[$1]       = $currentorder[ $1 + 1 ];
532                 $currentorder[ $1 + 1 ] = $temp;
533                 $orderchanged           = 1;
534                 last;
535             }
536         }
537         $value = join ' ', @currentorder;
538         if ($orderchanged) {
539             $op = 'add_form';
540             $template->param(
541                 script_name => $script_name,
542                 $op         => 1
543             );    # we show only the TMPL_VAR names $op
544         } else {
545             $op          = '';
546             $searchfield = '';
547             $template->param(
548                 script_name => $script_name,
549                 else        => 1
550             );    # we show only the TMPL_VAR names $op
551         }
552     }
553     my $dbh   = C4::Context->dbh;
554     my $query = "select * from systempreferences where variable=?";
555     my $sth   = $dbh->prepare($query);
556     $sth->execute( $input->param('variable') );
557     if ( $sth->rows ) {
558         unless ( C4::Context->config('demo') ) {
559             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
560             $sth->execute( $value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions') );
561             $sth->finish;
562             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
563         }
564     } else {
565         unless ( C4::Context->config('demo') ) {
566             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
567             $sth->execute( $input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
568             $sth->finish;
569             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $input->param('value') );
570         }
571     }
572     $sth->finish;
573
574 }
575
576 ################## ADD_FORM ##################################
577 # called by default. Used to create form to add or  modify a record
578
579 if ( $op eq 'add_form' ) {
580
581     #---- if primkey exists, it's a modify action, so read values to modify...
582     my $data;
583     if ($searchfield) {
584         my $dbh = C4::Context->dbh;
585         my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
586         $sth->execute($searchfield);
587         $data = $sth->fetchrow_hashref;
588         $sth->finish;
589         $template->param( modify => 1 );
590
591         # save tab to return to if user cancels edit
592         $template->param( return_tab => $tabsysprefs{$searchfield} );
593     }
594
595     $data->{'lang'} = $template->param('lang');
596
597     $template->param( GetPrefParams($data) );
598
599     $template->param( searchfield => $searchfield );
600
601 ################## ADD_VALIDATE ##################################
602     # called by add_form, used to insert/modify data in DB
603 } elsif ( $op eq 'add_validate' ) {
604     my $dbh = C4::Context->dbh;
605     my $sth = $dbh->prepare("select * from systempreferences where variable=?");
606     $sth->execute( $input->param('variable') );
607
608     # to handle multiple values
609     my $value;
610
611     # handle multiple value strings (separated by ',')
612     my $params = $input->Vars;
613     if ( defined $params->{'value'} ) {
614         my @values = ();
615         @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
616         if (@values) {
617             $value = "";
618             for my $vl (@values) {
619                 $value .= "$vl,";
620             }
621             $value =~ s/,$//;
622         } else {
623             $value = $params->{'value'};
624         }
625     }
626     if ( $sth->rows ) {
627         unless ( C4::Context->config('demo') ) {
628             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
629             $sth->execute( $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable') );
630             $sth->finish;
631             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
632         }
633     } else {
634         unless ( C4::Context->config('demo') ) {
635             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
636             $sth->execute( $input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
637             $sth->finish;
638             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $value );
639         }
640     }
641     $sth->finish;
642     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=" . $tabsysprefs{ $input->param('variable') } . "\"></html>";
643     exit;
644 ################## DELETE_CONFIRM ##################################
645     # called by default form, used to confirm deletion of data in DB
646 } elsif ( $op eq 'delete_confirm' ) {
647     my $dbh = C4::Context->dbh;
648     my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
649     $sth->execute($searchfield);
650     my $data = $sth->fetchrow_hashref;
651     $sth->finish;
652     $template->param(
653         searchfield => $searchfield,
654         Tvalue      => $data->{'value'},
655     );
656
657     # END $OP eq DELETE_CONFIRM
658 ################## DELETE_CONFIRMED ##################################
659     # called by delete_confirm, used to effectively confirm deletion of data in DB
660 } elsif ( $op eq 'delete_confirmed' ) {
661     my $dbh = C4::Context->dbh;
662     my $sth = $dbh->prepare("delete from systempreferences where variable=?");
663     $sth->execute($searchfield);
664     my $logstring = $searchfield . " | " . $Tvalue;
665     logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring );
666     $sth->finish;
667
668     # END $OP eq DELETE_CONFIRMED
669 ################## DEFAULT ##################################
670 } else {    # DEFAULT
671             #Adding tab management for system preferences
672     my $tab = $input->param('tab');
673     $template->param( $tab => 1 );
674     my ( $count, $results ) = StringSearch( $searchfield, $tab );
675     my $toggle    = 0;
676     my @loop_data = ();
677     for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
678         if ( $toggle == 0 ) {
679             $toggle = 1;
680         } else {
681             $toggle = 0;
682         }
683         my $row_data = $results->[$i];
684         $row_data->{'lang'} = $template->param('lang');
685         $row_data           = GetPrefParams($row_data);                                                         # get a fresh hash for the row data
686         $row_data->{toggle} = $toggle;
687         $row_data->{edit}   = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'variable'};
688         $row_data->{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'variable'};
689         push( @loop_data, $row_data );
690     }
691     $tab = ( $tab ? $tab : "Local Use" );
692     $template->param( loop => \@loop_data, $tab => 1 );
693     if ( $offset > 0 ) {
694         my $prevpage = $offset - $pagesize;
695         $template->param( "<a href=$script_name?offset=" . $prevpage . '&lt;&lt; Prev</a>' );
696     }
697     if ( $offset + $pagesize < $count ) {
698         my $nextpage = $offset + $pagesize;
699         $template->param( "a href=$script_name?offset=" . $nextpage . 'Next &gt;&gt;</a>' );
700     }
701     $template->param( tab => $tab, );
702 }    #---- END $OP eq DEFAULT
703 output_html_with_http_headers $input, $cookie, $template->output;