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