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