Bug 11944: Set ImplicitUnicode for YAML
[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 Modern::Perl;
21
22 use CGI;
23 use Encode;
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 $YAML::Syck::ImplicitUnicode = 1;
39 our $lang;
40
41 # use Smart::Comments;
42 #
43
44 sub GetTab {
45     my ( $input, $tab ) = @_;
46
47     my $tab_template = C4::Templates::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
48
49     my $active_currency = GetCurrency();
50     my $local_currency;
51     if ($active_currency) {
52         $local_currency = $active_currency->{currency};
53     }
54     $tab_template->param(
55         local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
56     );
57
58     return YAML::Syck::Load( Encode::decode('UTF-8',$tab_template->output()) );
59 }
60
61 sub _get_chunk {
62     my ( $value, %options ) = @_;
63
64     my $name = $options{'pref'};
65     my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
66
67     if ( $options{'class'} && $options{'class'} eq 'password' ) {
68         $chunk->{'input_type'} = 'password';
69     } elsif ( $options{'class'} && $options{'class'} eq 'date' ) {
70         $chunk->{'dateinput'} = 1;
71     } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
72         my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
73
74         my $theme;
75         my $interface;
76         if ( $options{'type'} eq 'opac-languages' ) {
77             # this is the OPAC
78             $interface = 'opac';
79             $theme     = C4::Context->preference('opacthemes');
80         } else {
81             # this is the staff client
82             $interface = 'intranet';
83             $theme     = C4::Context->preference('template');
84         }
85         $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, $lang, $current_languages );
86         $chunk->{'type'} = 'languages';
87     } elsif ( $options{ 'choices' } ) {
88         if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
89             if ( $options{'choices'} eq 'class-sources' ) {
90                 my $sources = GetClassSources();
91                 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
92             } elsif ( $options{'choices'} eq 'opac-templates' ) {
93                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
94             } elsif ( $options{'choices'} eq 'staff-templates' ) {
95                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
96             } else {
97                 die 'Unrecognized source of preference values: ' . $options{'choices'};
98             }
99         }
100
101         $value ||= 0;
102
103         $chunk->{'type'} = 'select';
104         $chunk->{'CHOICES'} = [
105             sort { $a->{'text'} cmp $b->{'text'} }
106             map { { text => $options{'choices'}->{$_}, value => $_, selected => ( $_ eq $value || ( $_ eq '' && ( $value eq '0' || !$value ) ) ) } }
107             keys %{ $options{'choices'} }
108         ];
109     } elsif ( $options{'multiple'} ) {
110         my @values = split /,/, $value;
111         $chunk->{type}    = 'multiple';
112         $chunk->{CHOICES} = [
113             sort { $a->{'text'} cmp $b->{'text'} }
114               map {
115                 my $option_value = $_;
116                 {
117                     text     => $options{multiple}->{$option_value},
118                     value    => $option_value,
119                     selected => grep /^$option_value$/, @values,
120                 }
121               }
122               keys %{ $options{multiple} }
123         ];
124     }
125
126     $chunk->{ 'type_' . $chunk->{'type'} } = 1;
127
128     return $chunk;
129 }
130
131 sub TransformPrefsToHTML {
132     my ( $data, $searchfield ) = @_;
133
134     my @lines;
135     my $dbh = C4::Context->dbh;
136     my $title = ( keys( %$data ) )[0];
137     my $tab = $data->{ $title };
138     $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
139
140     my @override_syspref_names;
141     if ( exists($ENV{OVERRIDE_SYSPREF_NAMES}) &&
142          defined($ENV{OVERRIDE_SYSPREF_NAMES})
143        ) {
144         @override_syspref_names = split /,/, $ENV{OVERRIDE_SYSPREF_NAMES};
145     }
146
147     foreach my $group ( sort keys %$tab ) {
148         if ( $group ) {
149             push @lines, { is_group_title => 1, title => $group };
150         }
151
152         foreach my $line ( @{ $tab->{ $group } } ) {
153             my @chunks;
154             my @names;
155
156             foreach my $piece ( @$line ) {
157                 if ( ref ( $piece ) eq 'HASH' ) {
158                     my $name = $piece->{'pref'};
159
160                     if ( $name ) {
161                         my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
162                         my $value;
163                         if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
164                             $value = $piece->{'default'};
165                         } else {
166                             $value = $row->{'value'};
167                         }
168                         my $chunk = _get_chunk( $value, %$piece );
169
170                         # No highlighting of inputs yet, but would be useful
171                         $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
172
173                         push @chunks, $chunk;
174
175                         my $name_entry = { name => $name };
176                         if ( $searchfield ) {
177                             if ( $name =~ /^$searchfield$/i ) {
178                                 $name_entry->{'jumped'} = 1;
179                             } elsif ( $name =~ /$searchfield/i ) {
180                                 $name_entry->{'highlighted'} = 1;
181                             }
182                         }
183                         $name_entry->{'overridden'} = 1 if ( any { $name eq $_ } @override_syspref_names );
184                         push @names, $name_entry;
185                     } else {
186                         push @chunks, $piece;
187                     }
188                 } else {
189                     push @chunks, { type_text => 1, contents => $piece };
190                 }
191             }
192             push @lines, { CHUNKS => \@chunks, NAMES => \@names, is_group_title => 0 };
193         }
194     }
195
196     return $title, \@lines;
197 }
198
199 sub _get_pref_files {
200     my ( $input, $open_files ) = @_;
201
202     my ( $htdocs, $theme, $lang, undef ) = C4::Templates::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
203
204     my %results;
205
206     foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
207         my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
208
209         $results{$tab} = $open_files ? new IO::File( $file, 'r' ) : '';
210     }
211
212     return %results;
213 }
214
215 sub SearchPrefs {
216     my ( $input, $searchfield ) = @_;
217     my @tabs;
218
219     my %tab_files = _get_pref_files( $input );
220     our @terms = split( /\s+/, $searchfield );
221
222     foreach my $tab_name ( keys %tab_files ) {
223         my $data = GetTab( $input, $tab_name );
224         my $title = ( keys( %$data ) )[0];
225         my $tab = $data->{ $title };
226         $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
227
228         my $matched_groups;
229
230         while ( my ( $group_title, $contents ) = each %$tab ) {
231             if ( matches( $group_title, \@terms ) ) {
232                 $matched_groups->{$group_title} = $contents;
233                 next;
234             }
235
236             my @new_contents;
237
238             foreach my $line ( @$contents ) {
239                 my $matched;
240
241                 foreach my $piece ( @$line ) {
242                     if ( ref( $piece ) eq 'HASH' ) {
243                         if ( !$piece->{'pref'} ){
244                             next;
245                         }
246                         if ( matches( $piece->{'pref'}, \@terms) ) {
247                             $matched = 1;
248                         } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_, \@terms ) } values( %{ $piece->{'choices'} } ) ) ) {
249                             $matched = 1;
250                         }
251                     } elsif ( matches( $piece, \@terms ) ) {
252                         $matched = 1;
253                     }
254                     last if ( $matched );
255                 }
256
257                 push @new_contents, $line if ( $matched );
258             }
259
260             $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
261         }
262
263         if ( $matched_groups ) {
264             my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
265
266             push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, tab_id => $tab_name };
267         }
268     }
269
270     return @tabs;
271 }
272
273 sub matches {
274     my ( $text, $terms ) = @_;
275     if ( $text ) { return !grep( { $text !~ /$_/i } @$terms ); }
276 }
277
278 my $dbh = C4::Context->dbh;
279 our $input = new CGI;
280
281 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
282     {   template_name   => "admin/preferences.tt",
283         query           => $input,
284         type            => "intranet",
285         authnotrequired => 0,
286         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
287         debug           => 1,
288     }
289 );
290
291 $lang = $template->param( 'lang' );
292 my $op = $input->param( 'op' ) || '';
293 my $tab = $input->param( 'tab' );
294 $tab ||= 'acquisitions'; # Ideally this should be "local-use" but preferences.pl
295                          # does not presently support local use preferences
296
297 my $highlighted;
298
299 if ( $op eq 'save' ) {
300     unless ( C4::Context->config( 'demo' ) ) {
301         foreach my $param ( $input->param() ) {
302             my ( $pref ) = ( $param =~ /pref_(.*)/ );
303
304             next if ( !defined( $pref ) );
305
306             my $value = join( ',', $input->param( $param ) );
307
308             C4::Context->set_preference( $pref, $value );
309             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $pref . " | " . $value );
310         }
311     }
312
313     print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
314     exit;
315 }
316
317 my @TABS;
318
319 if ( $op eq 'search' ) {
320     my $searchfield = $input->param( 'searchfield' );
321
322     $searchfield =~ s/\p{IsC}//g;
323     $searchfield =~ s/\s+/ /;
324     $searchfield =~ s/^\s+//;
325     $searchfield =~ s/\s+$//;
326
327     $template->param( searchfield => $searchfield );
328
329     @TABS = SearchPrefs( $input, $searchfield );
330
331     foreach my $tabh ( @TABS ) {
332         $template->param(
333             $tabh->{'tab'} => 1
334         );
335     }
336
337     if ( @TABS ) {
338         $tab = ''; # No need to load a particular tab, as we found results
339         $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
340     } else {
341         $template->param(
342             search_not_found => 1,
343         );
344     }
345 }
346
347 if ( $tab ) {
348     my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
349
350     push @TABS, { tab_title => $tab_title, LINES => $LINES, tab_id => $tab };
351     $template->param(
352         $tab => 1,
353         tab => $tab,
354     );
355 }
356
357 $template->param( TABS => \@TABS );
358
359 output_html_with_http_headers $input, $cookie, $template->output;