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