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