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