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