Bug 27779: (QA follow-up) Fix translation issue with 'against'
[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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use C4::Auth qw( get_template_and_user );
24 use C4::Context;
25 use C4::Koha qw( getallthemes );
26 use C4::Languages qw( getTranslatedLanguages );
27 use C4::ClassSource qw( GetClassSources GetClassSource );
28 use C4::Output qw( output_html_with_http_headers );
29 use C4::Templates;
30 use Koha::Acquisition::Currencies;
31 use Koha::Database::Columns;
32 use IO::File;
33 use YAML::XS;
34 use Encode;
35 use List::MoreUtils qw( any );
36
37 sub GetTab {
38     my ( $input, $tab ) = @_;
39
40     my $tab_template = C4::Templates::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
41
42     my $active_currency = Koha::Acquisition::Currencies->get_active;
43     my $local_currency;
44     if ($active_currency) {
45         $local_currency = $active_currency->currency;
46     }
47     $tab_template->param(
48         local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
49     );
50
51     return YAML::XS::Load( Encode::encode_utf8($tab_template->output()));
52 }
53
54 sub _get_chunk {
55     my ( $value, %options ) = @_;
56
57     my $name = $options{'pref'};
58     my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
59     if( $options{'syntax'} ){
60         $chunk->{'syntax'} = $options{'syntax'};
61     }
62
63     if( $options{'type'} && $options{'type'} eq 'modalselect' ){
64         $chunk->{'source'} = $options{'source'};
65         $chunk->{'exclusions'} = $options{'exclusions'} // "";
66         $chunk->{'required'} = $options{'required'} // "";
67         $chunk->{'type'} = 'modalselect';
68     }
69
70     if ( $options{'class'} && $options{'class'} eq 'password' ) {
71         $chunk->{'input_type'} = 'password';
72     } elsif ( $options{'class'} && $options{'class'} eq 'email' ) {
73         $chunk->{'input_type'} = 'email';
74     } elsif ( $options{'class'} && $options{'class'} eq 'date' ) {
75         $chunk->{'dateinput'} = 1;
76     } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
77         my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
78
79         my $theme;
80         my $interface;
81         if ( $options{'type'} eq 'opac-languages' ) {
82             # this is the OPAC
83             $interface = 'opac';
84             $theme     = C4::Context->preference('opacthemes');
85         } else {
86             # this is the staff interface
87             $interface = 'intranet';
88             $theme     = C4::Context->preference('template');
89         }
90         $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, undef, $current_languages );
91         $chunk->{'type'} = 'languages';
92     } elsif ( $options{ 'choices' } ) {
93         my $add_blank;
94         if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
95             if ( $options{'choices'} eq 'class-sources' ) {
96                 my $sources = GetClassSources();
97                 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
98             } elsif ( $options{'choices'} eq 'opac-templates' ) {
99                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
100             } elsif ( $options{'choices'} eq 'staff-templates' ) {
101                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
102             } elsif ( $options{choices} eq 'patron-categories' ) {
103                 $options{choices} = { map { $_->categorycode => $_->description } Koha::Patron::Categories->search->as_list };
104                 $add_blank = 1;
105             } else {
106                 die 'Unrecognized source of preference values: ' . $options{'choices'};
107             }
108         }
109
110         $value ||= 0;
111
112         $chunk->{'type'} = ( $options{class} && $options{class} eq 'multiple' ) ? 'multiple' : 'select';
113
114         my @values;
115         @values = split /,/, $value if defined($value);
116         $chunk->{'CHOICES'} = [
117             sort { $a->{'text'} cmp $b->{'text'} }
118             map {
119                 my $c = $_;
120                 {
121                     text     => $options{'choices'}->{$c},
122                     value    => $c,
123                     selected => (
124                         grep { $_ eq $c || ( $c eq '' && ($value eq '0' || !$value ) ) } @values
125                     ) ? 1 : 0,
126                 }
127               }
128             keys %{ $options{'choices'} }
129         ];
130
131         # Add a first blank value if needed
132         unshift @{ $chunk->{CHOICES} }, {
133             text  => '',
134             value => '',
135         } if $add_blank && $chunk->{type} eq 'select';
136
137     } elsif ( $options{'multiple'} ) {
138         my @values;
139         @values = split /,/, $value if defined($value);
140         $chunk->{type}    = 'multiple';
141         $chunk->{CHOICES} = [
142             sort { $a->{'text'} cmp $b->{'text'} }
143               map {
144                 my $option_value = $_;
145                 {
146                     text     => $options{multiple}->{$option_value},
147                     value    => $option_value,
148                     selected => (grep { $_ eq $option_value } @values) ? 1 : 0,
149                 }
150               }
151               keys %{ $options{multiple} }
152         ];
153     }
154
155     $chunk->{ 'type_' . $chunk->{'type'} } = 1;
156
157     return $chunk;
158 }
159
160 sub TransformPrefsToHTML {
161     my ( $data, $searchfield ) = @_;
162
163     my @lines;
164     my $dbh = C4::Context->dbh;
165     my $title = ( keys( %$data ) )[0];
166     my $tab = $data->{ $title };
167     $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
168
169     my @override_syspref_names;
170     if ( exists($ENV{OVERRIDE_SYSPREF_NAMES}) &&
171          defined($ENV{OVERRIDE_SYSPREF_NAMES})
172        ) {
173         @override_syspref_names = split /,/, $ENV{OVERRIDE_SYSPREF_NAMES};
174     }
175
176     foreach my $group ( sort keys %$tab ) {
177         if ( $group ) {
178             push @lines, { is_group_title => 1, title => $group };
179         }
180
181         foreach my $line ( @{ $tab->{ $group } } ) {
182             my @chunks;
183             my @names;
184             my @warnings;
185
186             foreach my $piece ( @$line ) {
187                 if ( ref ( $piece ) eq 'HASH' ) {
188                     my $name = $piece->{'pref'};
189
190                     if ( $name ) {
191                         my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
192                         my $value;
193                         if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
194                             $value = $piece->{'default'};
195                         } else {
196                             $value = $row->{'value'};
197                         }
198                         my $chunk = _get_chunk( $value, %$piece );
199
200                         # No highlighting of inputs yet, but would be useful
201                         $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
202
203                         if ( $name eq 'Pseudonymization' && ! C4::Context->config('bcrypt_settings')) {
204                             push @warnings, 'bcrypt_config_not_set';
205                             $chunk->{disabled} = 1 unless $value; # Let disable if enabled
206                         }
207                         push @chunks, $chunk;
208
209                         my $name_entry = { name => $name };
210                         if ( $searchfield ) {
211                             if ( $name =~ /^$searchfield$/i ) {
212                                 $name_entry->{'jumped'} = 1;
213                             } elsif ( $name =~ /$searchfield/i ) {
214                                 $name_entry->{'highlighted'} = 1;
215                             }
216                         }
217                         $name_entry->{'overridden'} = 1 if ( any { $name eq $_ } @override_syspref_names );
218
219                         push @names, $name_entry;
220                     } else {
221                         push @chunks, $piece;
222                     }
223                 } else {
224                     if ( $piece ) {
225                         my $version = Koha::version();
226                         my ( $major, $minor, $maintenance, $development ) = split( '\.', $version );
227                         if ( $minor % 2 ) {
228                             $piece =~ s|__VERSION__|${major}_${minor}|g;
229                         } else {
230                             $piece =~ s|__VERSION__|master|g;
231                         }
232                     }
233                     push @chunks, { type_text => 1, contents => $piece };
234                 }
235             }
236             push @lines, { CHUNKS => \@chunks, NAMES => \@names, WARNINGS => \@warnings, is_group_title => 0 };
237         }
238     }
239
240     return $title, \@lines;
241 }
242
243 sub _get_pref_files {
244     my ( $input, $open_files ) = @_;
245
246     my ( $htdocs, $theme, $lang, undef ) = C4::Templates::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
247
248     my %results;
249
250     foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
251         my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
252
253         $results{$tab} = $open_files ? IO::File->new( $file, 'r' ) : '';
254     }
255
256     return %results;
257 }
258
259 sub SearchPrefs {
260     my ( $input, $searchfield ) = @_;
261     my @tabs;
262
263     my %tab_files = _get_pref_files( $input );
264     our @terms = split( /\s+/, $searchfield );
265
266     foreach my $tab_name ( sort keys %tab_files ) {
267         # FIXME Hum?
268         # Force list context to remove 'uninitialized value in goto' warn coming from YAML::Syck; note that the other GetTab call is in list context too. The actual cause however is the null value for the pref OpacRenewalBranch in opac.pref
269         my ($data) = GetTab( $input, $tab_name );
270         my $title = ( keys( %$data ) )[0];
271         my $tab = $data->{ $title };
272         $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
273
274         my $matched_groups;
275
276         while ( my ( $group_title, $contents ) = each %$tab ) {
277             if ( matches( $group_title, \@terms ) ) {
278                 $matched_groups->{$group_title} = $contents;
279                 next;
280             }
281
282             my @new_contents;
283
284             foreach my $line ( @$contents ) {
285                 my $matched;
286
287                 foreach my $piece ( @$line ) {
288                     if ( ref( $piece ) eq 'HASH' ) {
289                         if ( !$piece->{'pref'} ){
290                             next;
291                         }
292                         if ( matches( $piece->{'pref'}, \@terms) ) {
293                             $matched = 1;
294                         } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_, \@terms ) } values( %{ $piece->{'choices'} } ) ) ) {
295                             $matched = 1;
296                         }
297                     } elsif ( matches( $piece, \@terms ) ) {
298                         $matched = 1;
299                     }
300                     last if ( $matched );
301                 }
302
303                 push @new_contents, $line if ( $matched );
304             }
305
306             $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
307         }
308
309         if ( $matched_groups ) {
310             my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
311
312             push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, tab_id => $tab_name };
313         }
314     }
315
316     return @tabs;
317 }
318
319 sub matches {
320     my ( $text, $terms ) = @_;
321     if ( $text ) {
322         return !grep(
323             {
324                 my $re = eval{qr|$_|i};
325                 $re = qr|\Q$_\E| if $@;
326                 $text !~ m|$re|;
327             } @$terms
328         )
329     }
330 }
331
332 my $dbh = C4::Context->dbh;
333 our $input = CGI->new;
334
335 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
336     {   template_name   => "admin/preferences.tt",
337         query           => $input,
338         type            => "intranet",
339         flagsrequired   => { parameters => 'manage_sysprefs' },
340     }
341 );
342
343 my $op = $input->param( 'op' ) || '';
344 my $tab = $input->param( 'tab' );
345 $tab ||= 'accounting'; # Ideally this should be "local-use" but preferences.pl
346                          # does not presently support local use preferences
347
348 my $highlighted;
349
350 if ( $op eq 'save' ) {
351     foreach my $param ( $input->param() ) {
352         my ( $pref ) = ( $param =~ /pref_(.*)/ );
353
354         next if ( !defined( $pref ) );
355
356         my $value = join( ',', $input->param( $param ) );
357
358         C4::Context->set_preference( $pref, $value );
359     }
360
361     print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
362     exit;
363 }
364
365 my @TABS;
366
367 if ( $op eq 'search' ) {
368     my $searchfield = $input->param( 'searchfield' );
369
370     $searchfield =~ s/\p{IsC}//g;
371     $searchfield =~ s/\s+/ /;
372     $searchfield =~ s/^\s+//;
373     $searchfield =~ s/\s+$//;
374
375     $template->param( searchfield => $searchfield );
376
377     @TABS = SearchPrefs( $input, $searchfield );
378
379     foreach my $tabh ( @TABS ) {
380         $template->param(
381             $tabh->{'tab'} => 1
382         );
383     }
384
385     if ( @TABS ) {
386         $tab = ''; # No need to load a particular tab, as we found results
387         $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
388     } else {
389         $template->param(
390             search_not_found => 1,
391         );
392     }
393 }
394
395 if ( $tab ) {
396     my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
397
398     push @TABS, { tab_title => $tab_title, LINES => $LINES, tab_id => $tab };
399     $template->param(
400         $tab => 1,
401         tab => $tab,
402     );
403 }
404
405 $template->param(
406     TABS => \@TABS,
407     db_columns => Koha::Database::Columns->columns,
408 );
409
410 output_html_with_http_headers $input, $cookie, $template->output;