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