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