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