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