Bug 11135: avoid logging warnings if OVERRIDE_SYSPREF_NAME is not set
[koha.git] / admin / preferences.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2009 Jesse Weaver and the Koha Dev Team
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Koha;
27 use C4::Languages qw(getTranslatedLanguages);
28 use C4::ClassSource;
29 use C4::Log;
30 use C4::Output;
31 use C4::Templates;
32 use C4::Budgets qw(GetCurrency);
33 use File::Spec;
34 use IO::File;
35 use YAML::Syck qw();
36 use List::MoreUtils qw(any);
37 $YAML::Syck::ImplicitTyping = 1;
38 our $lang;
39
40 # use Smart::Comments;
41 #
42
43 sub GetTab {
44     my ( $input, $tab ) = @_;
45
46     my $tab_template = C4::Templates::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
47
48     my $active_currency = GetCurrency();
49     my $local_currency;
50     if ($active_currency) {
51         $local_currency = $active_currency->{currency};
52     }
53     $tab_template->param(
54         local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
55     );
56
57     return YAML::Syck::Load( $tab_template->output() );
58 }
59
60 sub _get_chunk {
61     my ( $value, %options ) = @_;
62
63     my $name = $options{'pref'};
64     my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
65
66     if ( $options{'class'} && $options{'class'} eq 'password' ) {
67         $chunk->{'input_type'} = 'password';
68     } elsif ( $options{'class'} && $options{'class'} eq 'date' ) {
69         $chunk->{'dateinput'} = 1;
70     } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
71         my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
72
73         my $theme;
74         my $interface;
75         if ( $options{'type'} eq 'opac-languages' ) {
76             # this is the OPAC
77             $interface = 'opac';
78             $theme     = C4::Context->preference('opacthemes');
79         } else {
80             # this is the staff client
81             $interface = 'intranet';
82             $theme     = C4::Context->preference('template');
83         }
84         $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, $lang, $current_languages );
85         $chunk->{'type'} = 'languages';
86     } elsif ( $options{ 'choices' } ) {
87         if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
88             if ( $options{'choices'} eq 'class-sources' ) {
89                 my $sources = GetClassSources();
90                 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
91             } elsif ( $options{'choices'} eq 'opac-templates' ) {
92                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
93             } elsif ( $options{'choices'} eq 'staff-templates' ) {
94                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
95             } else {
96                 die 'Unrecognized source of preference values: ' . $options{'choices'};
97             }
98         }
99
100         $value ||= 0;
101
102         $chunk->{'type'} = 'select';
103         $chunk->{'CHOICES'} = [
104             sort { $a->{'text'} cmp $b->{'text'} }
105             map { { text => $options{'choices'}->{$_}, value => $_, selected => ( $_ eq $value || ( $_ eq '' && ( $value eq '0' || !$value ) ) ) } }
106             keys %{ $options{'choices'} }
107         ];
108     }
109
110     $chunk->{ 'type_' . $chunk->{'type'} } = 1;
111
112     return $chunk;
113 }
114
115 sub TransformPrefsToHTML {
116     my ( $data, $searchfield ) = @_;
117
118     my @lines;
119     my $dbh = C4::Context->dbh;
120     my $title = ( keys( %$data ) )[0];
121     my $tab = $data->{ $title };
122     $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
123
124     my @override_syspref_names;
125     if ( $ENV{OVERRIDE_SYSPREF_NAMES} ) {
126         @override_syspref_names = split /,/, $ENV{OVERRIDE_SYSPREF_NAMES};
127     }
128
129     foreach my $group ( sort keys %$tab ) {
130         if ( $group ) {
131             push @lines, { is_group_title => 1, title => $group };
132         }
133
134         foreach my $line ( @{ $tab->{ $group } } ) {
135             my @chunks;
136             my @names;
137
138             foreach my $piece ( @$line ) {
139                 if ( ref ( $piece ) eq 'HASH' ) {
140                     my $name = $piece->{'pref'};
141
142                     if ( $name ) {
143                         my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
144                         my $value;
145                         if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
146                             $value = $piece->{'default'};
147                         } else {
148                             $value = $row->{'value'};
149                         }
150                         my $chunk = _get_chunk( $value, %$piece );
151
152                         # No highlighting of inputs yet, but would be useful
153                         $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
154
155                         push @chunks, $chunk;
156
157                         my $name_entry = { name => $name };
158                         if ( $searchfield ) {
159                             if ( $name =~ /^$searchfield$/i ) {
160                                 $name_entry->{'jumped'} = 1;
161                             } elsif ( $name =~ /$searchfield/i ) {
162                                 $name_entry->{'highlighted'} = 1;
163                             }
164                         }
165                         $name_entry->{'overridden'} = 1 if ( any { $name eq $_ } @override_syspref_names );
166                         push @names, $name_entry;
167                     } else {
168                         push @chunks, $piece;
169                     }
170                 } else {
171                     push @chunks, { type_text => 1, contents => $piece };
172                 }
173             }
174             push @lines, { CHUNKS => \@chunks, NAMES => \@names, is_group_title => 0 };
175         }
176     }
177
178     return $title, \@lines;
179 }
180
181 sub _get_pref_files {
182     my ( $input, $open_files ) = @_;
183
184     my ( $htdocs, $theme, $lang, undef ) = C4::Templates::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
185
186     my %results;
187
188     foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
189         my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
190
191         $results{$tab} = $open_files ? new IO::File( $file, 'r' ) : '';
192     }
193
194     return %results;
195 }
196
197 sub SearchPrefs {
198     my ( $input, $searchfield ) = @_;
199     my @tabs;
200
201     my %tab_files = _get_pref_files( $input );
202     our @terms = split( /\s+/, $searchfield );
203
204     foreach my $tab_name ( keys %tab_files ) {
205         my $data = GetTab( $input, $tab_name );
206         my $title = ( keys( %$data ) )[0];
207         my $tab = $data->{ $title };
208         $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
209
210         my $matched_groups;
211
212         while ( my ( $group_title, $contents ) = each %$tab ) {
213             if ( matches( $group_title, \@terms ) ) {
214                 $matched_groups->{$group_title} = $contents;
215                 next;
216             }
217
218             my @new_contents;
219
220             foreach my $line ( @$contents ) {
221                 my $matched;
222
223                 foreach my $piece ( @$line ) {
224                     if ( ref( $piece ) eq 'HASH' ) {
225                         if ( !$piece->{'pref'} ){
226                             next;
227                         }
228                         if ( matches( $piece->{'pref'}, \@terms) ) {
229                             $matched = 1;
230                         } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_, \@terms ) } values( %{ $piece->{'choices'} } ) ) ) {
231                             $matched = 1;
232                         }
233                     } elsif ( matches( $piece, \@terms ) ) {
234                         $matched = 1;
235                     }
236                     last if ( $matched );
237                 }
238
239                 push @new_contents, $line if ( $matched );
240             }
241
242             $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
243         }
244
245         if ( $matched_groups ) {
246             my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
247
248             push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, };
249         }
250     }
251
252     return @tabs;
253 }
254
255 sub matches {
256     my ( $text, $terms ) = @_;
257     if ( $text ) { return !grep( { $text !~ /$_/i } @$terms ); }
258 }
259
260 my $dbh = C4::Context->dbh;
261 our $input = new CGI;
262
263 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
264     {   template_name   => "admin/preferences.tmpl",
265         query           => $input,
266         type            => "intranet",
267         authnotrequired => 0,
268         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
269         debug           => 1,
270     }
271 );
272
273 $lang = $template->param( 'lang' );
274 my $op = $input->param( 'op' ) || '';
275 my $tab = $input->param( 'tab' );
276 $tab ||= 'acquisitions'; # Ideally this should be "local-use" but preferences.pl
277                          # does not presently support local use preferences
278
279 my $highlighted;
280
281 if ( $op eq 'save' ) {
282     unless ( C4::Context->config( 'demo' ) ) {
283         foreach my $param ( $input->param() ) {
284             my ( $pref ) = ( $param =~ /pref_(.*)/ );
285
286             next if ( !defined( $pref ) );
287
288             my $value = join( ',', $input->param( $param ) );
289
290             C4::Context->set_preference( $pref, $value );
291             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $pref . " | " . $value );
292         }
293     }
294
295     print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
296     exit;
297 }
298
299 my @TABS;
300
301 if ( $op eq 'search' ) {
302     my $searchfield = $input->param( 'searchfield' );
303
304     $searchfield =~ s/\p{IsC}//g;
305     $searchfield =~ s/\s+/ /;
306     $searchfield =~ s/^\s+//;
307     $searchfield =~ s/\s+$//;
308
309     $template->param( searchfield => $searchfield );
310
311     @TABS = SearchPrefs( $input, $searchfield );
312
313     foreach my $tabh ( @TABS ) {
314         $template->param(
315             $tabh->{'tab'} => 1
316         );
317     }
318
319     if ( @TABS ) {
320         $tab = ''; # No need to load a particular tab, as we found results
321         $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
322     } else {
323         $template->param(
324             search_not_found => 1,
325         );
326     }
327 }
328
329 if ( $tab ) {
330     my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
331
332     push @TABS, { tab_title => $tab_title, LINES => $LINES };
333     $template->param(
334         $tab => 1,
335         tab => $tab,
336     );
337 }
338
339 $template->param( TABS => \@TABS );
340
341 output_html_with_http_headers $input, $cookie, $template->output;