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