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