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