fixed typos in LTFL sysprefs
[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
125 # Circulation
126 $tabsysprefs{maxoutstanding}                 = "Circulation";
127 $tabsysprefs{maxreserves}                    = "Circulation";
128 $tabsysprefs{noissuescharge}                 = "Circulation";
129 $tabsysprefs{IssuingInProcess}               = "Circulation";
130 $tabsysprefs{patronimages}                   = "Circulation";
131 $tabsysprefs{printcirculationslips}          = "Circulation";
132 $tabsysprefs{ReturnBeforeExpiry}             = "Circulation";
133 $tabsysprefs{SpecifyDueDate}                 = "Circulation";
134 $tabsysprefs{AutomaticItemReturn}            = "Circulation";
135 $tabsysprefs{ReservesMaxPickUpDelay}         = "Circulation";
136 $tabsysprefs{TransfersMaxDaysWarning}        = "Circulation";
137 $tabsysprefs{useDaysMode}                    = "Circulation";
138 $tabsysprefs{ReservesNeedReturns}            = "Circulation";
139 $tabsysprefs{CircAutocompl}                  = "Circulation";
140 $tabsysprefs{AllowRenewalLimitOverride}      = "Circulation";
141 $tabsysprefs{canreservefromotherbranches}    = "Circulation";
142 $tabsysprefs{finesMode}                      = "Circulation";
143 $tabsysprefs{emailLibrarianWhenHoldIsPlaced} = "Circulation";
144 $tabsysprefs{globalDueDate}                  = "Circulation";
145 $tabsysprefs{holdCancelLength}               = "Circulation";
146 $tabsysprefs{itemBarcodeInputFilter}         = "Circulation";
147 $tabsysprefs{WebBasedSelfCheck}              = "Circulation";
148 $tabsysprefs{CircControl}                    = "Circulation";
149 $tabsysprefs{finesCalendar}                  = "Circulation";
150 $tabsysprefs{previousIssuesDefaultSortOrder} = "Circulation";
151 $tabsysprefs{todaysIssuesDefaultSortOrder}   = "Circulation";
152 $tabsysprefs{HomeOrHoldingBranch}            = "Circulation";
153 $tabsysprefs{RandomizeHoldsQueueWeight}      = "Circulation";
154 $tabsysprefs{StaticHoldsQueueWeight}         = "Circulation";
155 $tabsysprefs{AllowOnShelfHolds}              = "Circulation";
156 $tabsysprefs{AllowHoldsOnDamagedItems}       = "Circulation";
157 $tabsysprefs{UseBranchTransferLimits}        = "Circulation";
158 $tabsysprefs{AllowHoldPolicyOverride}        = "Circulation";
159 $tabsysprefs{BranchTransferLimitsType}       = "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{AmazonEnabled}          = "EnhancedContent";
219 $tabsysprefs{OPACAmazonEnabled}      = "EnhancedContent";
220 $tabsysprefs{AmazonCoverImages}      = "EnhancedContent";
221 $tabsysprefs{OPACAmazonCoverImages}  = "EnhancedContent";
222 $tabsysprefs{AWSAccessKeyID}         = "EnhancedContent";
223 $tabsysprefs{AmazonLocale}           = "EnhancedContent";
224 $tabsysprefs{AmazonAssocTag}         = "EnhancedContent";
225 $tabsysprefs{AmazonSimilarItems}     = "EnhancedContent";
226 $tabsysprefs{OPACAmazonSimilarItems} = "EnhancedContent";
227 $tabsysprefs{OPACAmazonReviews}      = "EnhancedContent";
228
229 # Babelthèque
230 $tabsysprefs{Babeltheque}            = "EnhancedContent";
231
232 # Baker & Taylor
233 $tabsysprefs{BakerTaylorBookstoreURL} = 'EnhancedContent';
234 $tabsysprefs{BakerTaylorEnabled}      = 'EnhancedContent';
235 $tabsysprefs{BakerTaylorPassword}     = 'EnhancedContent';
236 $tabsysprefs{BakerTaylorUsername}     = 'EnhancedContent';
237
238 # Library Thing for Libraries
239 $tabsysprefs{LibraryThingForLibrariesID} = "EnhancedContent"; 
240 $tabsysprefs{LibraryThingForLibrariesEnabled} = "EnhancedContent"; 
241 $tabsysprefs{LibraryThingForLibrariesTabbedView} = "EnhancedContent";
242
243 # Syndetics
244 $tabsysprefs{SyndeticsClientCode}     = 'EnhancedContent';
245 $tabsysprefs{SyndeticsEnabled}        = 'EnhancedContent';
246 $tabsysprefs{SyndeticsCoverImages}    = 'EnhancedContent';
247 $tabsysprefs{SyndeticsTOC}            = 'EnhancedContent';
248 $tabsysprefs{SyndeticsSummary}        = 'EnhancedContent';
249 $tabsysprefs{SyndeticsEditions}       = 'EnhancedContent';
250 $tabsysprefs{SyndeticsExcerpt}        = 'EnhancedContent';
251 $tabsysprefs{SyndeticsReviews}        = 'EnhancedContent';
252 $tabsysprefs{SyndeticsAuthorNotes}    = 'EnhancedContent';
253 $tabsysprefs{SyndeticsAwards}         = 'EnhancedContent';
254 $tabsysprefs{SyndeticsSeries}         = 'EnhancedContent';
255 $tabsysprefs{SyndeticsCoverImageSize} = 'EnhancedContent';
256
257
258 # FRBR
259 $tabsysprefs{FRBRizeEditions}     = "EnhancedContent";
260 $tabsysprefs{XISBN}               = "EnhancedContent";
261 $tabsysprefs{OCLCAffiliateID}     = "EnhancedContent";
262 $tabsysprefs{XISBNDailyLimit}     = "EnhancedContent";
263 $tabsysprefs{PINESISBN}           = "EnhancedContent";
264 $tabsysprefs{ThingISBN}           = "EnhancedContent";
265 $tabsysprefs{OPACFRBRizeEditions} = "EnhancedContent";
266
267 # Tags
268 $tabsysprefs{TagsEnabled}            = 'EnhancedContent';
269 $tabsysprefs{TagsExternalDictionary} = 'EnhancedContent';
270 $tabsysprefs{TagsInputOnDetail}      = 'EnhancedContent';
271 $tabsysprefs{TagsInputOnList}        = 'EnhancedContent';
272 $tabsysprefs{TagsShowOnDetail}       = 'EnhancedContent';
273 $tabsysprefs{TagsShowOnList}         = 'EnhancedContent';
274 $tabsysprefs{TagsModeration}         = 'EnhancedContent';
275 $tabsysprefs{GoogleJackets}          = 'EnhancedContent';
276 $tabsysprefs{AuthorisedValueImages}  = "EnhancedContent";
277
278 # OPAC
279 $tabsysprefs{BiblioDefaultView}          = "OPAC";
280 $tabsysprefs{LibraryName}                = "OPAC";
281 $tabsysprefs{opaccolorstylesheet}        = "OPAC";
282 $tabsysprefs{opaccredits}                = "OPAC";
283 $tabsysprefs{opaclayoutstylesheet}       = "OPAC";
284 $tabsysprefs{OpacNav}                    = "OPAC";
285 $tabsysprefs{opacsmallimage}             = "OPAC";
286 $tabsysprefs{opacstylesheet}             = "OPAC";
287 $tabsysprefs{opacthemes}                 = "OPAC";
288 $tabsysprefs{opacuserjs}                 = "OPAC";
289 $tabsysprefs{opacheader}                 = "OPAC";
290 $tabsysprefs{hideBiblioNumber}           = "OPAC";
291 $tabsysprefs{OpacMainUserBlock}          = "OPAC";
292 $tabsysprefs{OPACURLOpenInNewWindow}     = "OPAC";
293 $tabsysprefs{OPACUserCSS}                = "OPAC";
294 $tabsysprefs{OPACHighlightedWords}       = "OPAC";
295 $tabsysprefs{OPACViewOthersSuggestions}  = "OPAC";
296 $tabsysprefs{URLLinkText}                = "OPAC";
297 $tabsysprefs{OPACShelfBrowser}           = "OPAC";
298 $tabsysprefs{OPACDisplayRequestPriority} = "OPAC";
299
300 # OPAC
301 $tabsysprefs{SearchMyLibraryFirst} = "OPAC";
302 $tabsysprefs{hidelostitems}        = "OPAC";
303 $tabsysprefs{opacbookbag}          = "OPAC";
304 $tabsysprefs{OpacPasswordChange}   = "OPAC";
305 $tabsysprefs{opacreadinghistory}   = "OPAC";
306 $tabsysprefs{virtualshelves}       = "OPAC";
307 $tabsysprefs{RequestOnOpac}        = "OPAC";
308 $tabsysprefs{reviewson}            = "OPAC";
309 $tabsysprefs{OpacTopissues}        = "OPAC";
310 $tabsysprefs{OpacAuthorities}      = "OPAC";
311 $tabsysprefs{OpacCloud}            = "OPAC";
312 $tabsysprefs{opacuserlogin}        = "OPAC";
313 $tabsysprefs{AnonSuggestions}      = "OPAC";
314 $tabsysprefs{suggestion}           = "OPAC";
315 $tabsysprefs{OpacTopissue}         = "OPAC";
316 $tabsysprefs{OpacBrowser}          = "OPAC";
317 $tabsysprefs{kohaspsuggest}        = "OPAC";
318 $tabsysprefs{OpacRenewalAllowed}   = "OPAC";
319 $tabsysprefs{OPACItemHolds}        = "OPAC";
320 $tabsysprefs{OPACGroupResults}     = "OPAC";
321 $tabsysprefs{XSLTDetailsDisplay}   = "OPAC";
322 $tabsysprefs{XSLTResultsDisplay}   = "OPAC";
323 $tabsysprefs{OPACShowCheckoutName}   = "OPAC";
324
325 # Serials
326 $tabsysprefs{OPACSerialIssueDisplayCount}  = "Serials";
327 $tabsysprefs{StaffSerialIssueDisplayCount} = "Serials";
328 $tabsysprefs{OPACDisplayExtendedSubInfo}   = "Serials";
329 $tabsysprefs{OPACSubscriptionDisplay}      = "Serials";
330 $tabsysprefs{RenewSerialAddsSuggestion}    = "Serials";
331 $tabsysprefs{SubscriptionHistory}          = "Serials";
332
333 # LOGFeatures
334 $tabsysprefs{CataloguingLog}  = "Logs";
335 $tabsysprefs{BorrowersLog}    = "Logs";
336 $tabsysprefs{SubscriptionLog} = "Logs";
337 $tabsysprefs{IssueLog}        = "Logs";
338 $tabsysprefs{ReturnLog}       = "Logs";
339 $tabsysprefs{LetterLog}       = "Logs";
340 $tabsysprefs{FinesLog}        = "Logs";
341
342 # OAI-PMH variables
343 $tabsysprefs{'OAI-PMH'}           = "OAI-PMH";
344 $tabsysprefs{'OAI-PMH:archiveID'} = "OAI-PMH";
345 $tabsysprefs{'OAI-PMH:MaxCount'}  = "OAI-PMH";
346 $tabsysprefs{'OAI-PMH:Set'}       = "OAI-PMH";
347 $tabsysprefs{'OAI-PMH:Subset'}    = "OAI-PMH";
348
349 sub StringSearch {
350     my ( $searchstring, $type ) = @_;
351     my $dbh = C4::Context->dbh;
352     $searchstring =~ s/\'/\\\'/g;
353     my @data = split( ' ', $searchstring );
354     my $count = @data;
355     my @results;
356     my $cnt = 0;
357     my $sth;
358
359     # used for doing a plain-old sys-pref search
360     if ( $type && $type ne 'all' ) {
361         foreach my $syspref ( sort { lc $a cmp lc $b } keys %tabsysprefs ) {
362             if ( $tabsysprefs{$syspref} eq $type ) {
363                 my $sth = $dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
364                 $sth->execute($syspref);
365                 while ( my $data = $sth->fetchrow_hashref ) {
366                     $data->{shortvalue} = $data->{value};
367                     $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if defined( $data->{value} ) and length( $data->{value} ) > 60;
368                     push( @results, $data );
369                     $cnt++;
370                 }
371                 $sth->finish;
372             }
373         }
374     } else {
375         my $sth;
376
377         if ( $type and $type eq 'all' ) {
378             $sth = $dbh->prepare( "
379             SELECT *
380               FROM systempreferences 
381               WHERE variable LIKE ? OR explanation LIKE ? 
382               ORDER BY VARIABLE" );
383             $sth->execute( "%$searchstring%", "%$searchstring%" );
384         } else {
385             my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable not in (";
386             foreach my $syspref ( keys %tabsysprefs ) {
387                 $strsth .= $dbh->quote($syspref) . ",";
388             }
389             $strsth =~ s/,$/) /;
390             $strsth .= " order by variable";
391             $sth = $dbh->prepare($strsth);
392             $sth->execute();
393         }
394
395         while ( my $data = $sth->fetchrow_hashref ) {
396             $data->{shortvalue} = $data->{value};
397             $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if length( $data->{value} ) > 60;
398             push( @results, $data );
399             $cnt++;
400         }
401
402         $sth->finish;
403     }
404     return ( $cnt, \@results );
405 }
406
407 sub GetPrefParams {
408     my $data   = shift;
409     my $params = $data;
410     my @options;
411
412     if ( defined $data->{'options'} ) {
413         foreach my $option ( split( /\|/, $data->{'options'} ) ) {
414             my $selected = '0';
415             defined( $data->{'value'} ) and $option eq $data->{'value'} and $selected = 1;
416             push @options, { option => $option, selected => $selected };
417         }
418     }
419
420     $params->{'prefoptions'} = $data->{'options'};
421
422     if ( not defined( $data->{'type'} ) ) {
423         $params->{'type-free'} = 1;
424         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 );
425     } elsif ( $data->{'type'} eq 'Choice' ) {
426         $params->{'type-choice'} = 1;
427     } elsif ( $data->{'type'} eq 'YesNo' ) {
428         $params->{'type-yesno'} = 1;
429         $data->{'value'}        = C4::Context->boolean_preference( $data->{'variable'} );
430         if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
431             $params->{'value-yes'} = 1;
432         } else {
433             $params->{'value-no'} = 1;
434         }
435     } elsif ( $data->{'type'} eq 'Integer' || $data->{'type'} eq 'Float' ) {
436         $params->{'type-free'} = 1;
437         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 10;
438     } elsif ( $data->{'type'} eq 'Textarea' ) {
439         $params->{'type-textarea'} = 1;
440         $data->{options} =~ /(.*)\|(.*)/;
441         $params->{'cols'} = $1;
442         $params->{'rows'} = $2;
443     } elsif ( $data->{'type'} eq 'Themes' ) {
444         $params->{'type-choice'} = 1;
445         my $type = '';
446         ( $data->{'variable'} =~ m#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
447         @options = ();
448         my $currently_selected_themes;
449         my $counter = 0;
450         foreach my $theme ( split /\s+/, $data->{'value'} ) {
451             push @options, { option => $theme, counter => $counter };
452             $currently_selected_themes->{$theme} = 1;
453             $counter++;
454         }
455         foreach my $theme ( getallthemes($type) ) {
456             my $selected = '0';
457             next if $currently_selected_themes->{$theme};
458             push @options, { option => $theme, counter => $counter };
459             $counter++;
460         }
461     } elsif ( $data->{'type'} eq 'ClassSources' ) {
462         $params->{'type-choice'} = 1;
463         my $type = '';
464         @options = ();
465         my $sources = GetClassSources();
466         my $counter = 0;
467         foreach my $cn_source ( sort keys %$sources ) {
468             if ( $cn_source eq $data->{'value'} ) {
469                 push @options, { option => $cn_source, counter => $counter, selected => 1 };
470             } else {
471                 push @options, { option => $cn_source, counter => $counter };
472             }
473             $counter++;
474         }
475     } elsif ( $data->{'type'} eq 'Languages' ) {
476         my $currently_selected_languages;
477         foreach my $language ( split /\s+/, $data->{'value'} ) {
478             $currently_selected_languages->{$language} = 1;
479         }
480
481         # current language
482         my $lang = $params->{'lang'};
483         my $theme;
484         my $interface;
485         if ( $data->{'variable'} =~ /opac/ ) {
486
487             # this is the OPAC
488             $interface = 'opac';
489             $theme     = C4::Context->preference('opacthemes');
490         } else {
491
492             # this is the staff client
493             $interface = 'intranet';
494             $theme     = C4::Context->preference('template');
495         }
496         my $languages_loop = getTranslatedLanguages( $interface, $theme, $lang, $currently_selected_languages );
497
498         $params->{'languages_loop'}    = $languages_loop;
499         $params->{'type-langselector'} = 1;
500     } else {
501         $params->{'type-free'} = 1;
502         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 30;
503     }
504
505     if ( $params->{'type-choice'} || $params->{'type-free'} || $params->{'type-yesno'} ) {
506         $params->{'oneline'} = 1;
507     }
508
509     $params->{'preftype'} = $data->{'type'};
510     $params->{'options'}  = \@options;
511
512     return $params;
513 }
514
515 my $input       = new CGI;
516 my $searchfield = $input->param('searchfield') || '';
517 my $Tvalue      = $input->param('Tvalue');
518 my $offset      = $input->param('offset') || 0;
519 my $script_name = "/cgi-bin/koha/admin/systempreferences.pl";
520
521 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
522     {   template_name   => "admin/systempreferences.tmpl",
523         query           => $input,
524         type            => "intranet",
525         authnotrequired => 0,
526         flagsrequired   => { parameters => 1 },
527         debug           => 1,
528     }
529 );
530 my $pagesize = 100;
531 my $op = $input->param('op') || '';
532 $searchfield =~ s/\,//g;
533
534 if ($op) {
535     $template->param(
536         script_name => $script_name,
537         $op         => 1
538     );    # we show only the TMPL_VAR names $op
539 } else {
540     $template->param(
541         script_name => $script_name,
542         else        => 1
543     );    # we show only the TMPL_VAR names $op
544 }
545
546 if ( $op eq 'update_and_reedit' ) {
547     foreach ( $input->param ) {
548     }
549     my $value = '';
550     if ( my $currentorder = $input->param('currentorder') ) {
551         my @currentorder = split /\|/, $currentorder;
552         my $orderchanged = 0;
553         foreach my $param ( $input->param ) {
554             if ( $param =~ m#up-(\d+).x# ) {
555                 my $temp = $currentorder[$1];
556                 $currentorder[$1]       = $currentorder[ $1 - 1 ];
557                 $currentorder[ $1 - 1 ] = $temp;
558                 $orderchanged           = 1;
559                 last;
560             } elsif ( $param =~ m#down-(\d+).x# ) {
561                 my $temp = $currentorder[$1];
562                 $currentorder[$1]       = $currentorder[ $1 + 1 ];
563                 $currentorder[ $1 + 1 ] = $temp;
564                 $orderchanged           = 1;
565                 last;
566             }
567         }
568         $value = join ' ', @currentorder;
569         if ($orderchanged) {
570             $op = 'add_form';
571             $template->param(
572                 script_name => $script_name,
573                 $op         => 1
574             );    # we show only the TMPL_VAR names $op
575         } else {
576             $op          = '';
577             $searchfield = '';
578             $template->param(
579                 script_name => $script_name,
580                 else        => 1
581             );    # we show only the TMPL_VAR names $op
582         }
583     }
584     my $dbh   = C4::Context->dbh;
585     my $query = "select * from systempreferences where variable=?";
586     my $sth   = $dbh->prepare($query);
587     $sth->execute( $input->param('variable') );
588     if ( $sth->rows ) {
589         unless ( C4::Context->config('demo') ) {
590             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
591             $sth->execute( $value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions') );
592             $sth->finish;
593             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
594         }
595     } else {
596         unless ( C4::Context->config('demo') ) {
597             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
598             $sth->execute( $input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
599             $sth->finish;
600             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $input->param('value') );
601         }
602     }
603     $sth->finish;
604
605 }
606
607 ################## ADD_FORM ##################################
608 # called by default. Used to create form to add or  modify a record
609
610 if ( $op eq 'add_form' ) {
611
612     #---- if primkey exists, it's a modify action, so read values to modify...
613     my $data;
614     if ($searchfield) {
615         my $dbh = C4::Context->dbh;
616         my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
617         $sth->execute($searchfield);
618         $data = $sth->fetchrow_hashref;
619         $sth->finish;
620         $template->param( modify => 1 );
621
622         # save tab to return to if user cancels edit
623         $template->param( return_tab => $tabsysprefs{$searchfield} );
624     }
625
626     $data->{'lang'} = $template->param('lang');
627
628     $template->param( GetPrefParams($data) );
629
630     $template->param( searchfield => $searchfield );
631
632 ################## ADD_VALIDATE ##################################
633     # called by add_form, used to insert/modify data in DB
634 } elsif ( $op eq 'add_validate' ) {
635     my $dbh = C4::Context->dbh;
636     my $sth = $dbh->prepare("select * from systempreferences where variable=?");
637     $sth->execute( $input->param('variable') );
638
639     # to handle multiple values
640     my $value;
641
642     # handle multiple value strings (separated by ',')
643     my $params = $input->Vars;
644     if ( defined $params->{'value'} ) {
645         my @values = ();
646         @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
647         if (@values) {
648             $value = "";
649             for my $vl (@values) {
650                 $value .= "$vl,";
651             }
652             $value =~ s/,$//;
653         } else {
654             $value = $params->{'value'};
655         }
656     }
657     if ( $sth->rows ) {
658         unless ( C4::Context->config('demo') ) {
659             my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
660             $sth->execute( $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable') );
661             $sth->finish;
662             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
663         }
664     } else {
665         unless ( C4::Context->config('demo') ) {
666             my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
667             $sth->execute( $input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
668             $sth->finish;
669             logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $value );
670         }
671     }
672     $sth->finish;
673     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=" . $tabsysprefs{ $input->param('variable') } . "\"></html>";
674     exit;
675 ################## DELETE_CONFIRM ##################################
676     # called by default form, used to confirm deletion of data in DB
677 } elsif ( $op eq 'delete_confirm' ) {
678     my $dbh = C4::Context->dbh;
679     my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
680     $sth->execute($searchfield);
681     my $data = $sth->fetchrow_hashref;
682     $sth->finish;
683     $template->param(
684         searchfield => $searchfield,
685         Tvalue      => $data->{'value'},
686     );
687
688     # END $OP eq DELETE_CONFIRM
689 ################## DELETE_CONFIRMED ##################################
690     # called by delete_confirm, used to effectively confirm deletion of data in DB
691 } elsif ( $op eq 'delete_confirmed' ) {
692     my $dbh = C4::Context->dbh;
693     my $sth = $dbh->prepare("delete from systempreferences where variable=?");
694     $sth->execute($searchfield);
695     my $logstring = $searchfield . " | " . $Tvalue;
696     logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring );
697     $sth->finish;
698
699     # END $OP eq DELETE_CONFIRMED
700 ################## DEFAULT ##################################
701 } else {    # DEFAULT
702             #Adding tab management for system preferences
703     my $tab = $input->param('tab');
704     $template->param( $tab => 1 );
705     my ( $count, $results ) = StringSearch( $searchfield, $tab );
706     my @loop_data = ();
707     for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
708         my $row_data = $results->[$i];
709         $row_data->{'lang'} = $template->param('lang');
710         $row_data           = GetPrefParams($row_data);                                                         # get a fresh hash for the row data
711         $row_data->{edit}   = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'variable'};
712         $row_data->{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'variable'};
713         push( @loop_data, $row_data );
714     }
715     $tab = ( $tab ? $tab : "Local Use" );
716     $template->param( loop => \@loop_data, $tab => 1 );
717     if ( $offset > 0 ) {
718         my $prevpage = $offset - $pagesize;
719         $template->param( "<a href=$script_name?offset=" . $prevpage . '&lt;&lt; Prev</a>' );
720     }
721     if ( $offset + $pagesize < $count ) {
722         my $nextpage = $offset + $pagesize;
723         $template->param( "a href=$script_name?offset=" . $nextpage . 'Next &gt;&gt;</a>' );
724     }
725     $template->param( tab => $tab, );
726 }    #---- END $OP eq DEFAULT
727 output_html_with_http_headers $input, $cookie, $template->output;