Bug 6554: Followup for preferences.pl
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use CGI;
24
25 use C4::Auth;
26 use C4::Context;
27 use C4::Koha;
28 use C4::Languages qw(getTranslatedLanguages);
29 use C4::ClassSource;
30 use C4::Log;
31 use C4::Output;
32 use C4::Templates;
33 use C4::Budgets qw(GetCurrency);
34 use File::Spec;
35 use IO::File;
36 use YAML::Syck qw();
37 $YAML::Syck::ImplicitTyping = 1;
38 $YAML::Syck::ImplicitUnicode = 1; # force utf-8 for preference encoding
39 our $lang;
40
41 # use Smart::Comments;
42 #
43
44 sub GetTab {
45     my ( $input, $tab ) = @_;
46
47     my $tab_template = C4::Templates::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
48
49     my $active_currency = GetCurrency();
50     my $local_currency;
51     if ($active_currency) {
52         $local_currency = $active_currency->{currency};
53     }
54     $tab_template->param(
55         local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
56     );
57
58     return YAML::Syck::Load( $tab_template->output() );
59 }
60
61 sub _get_chunk {
62     my ( $value, %options ) = @_;
63
64     my $name = $options{'pref'};
65     my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
66
67     if ( $options{'class'} && $options{'class'} eq 'password' ) {
68         $chunk->{'input_type'} = 'password';
69     } elsif ( $options{'class'} && $options{'class'} eq 'date' ) {
70         $chunk->{'dateinput'} = 1;
71     } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
72         my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
73
74         my $theme;
75         my $interface;
76         if ( $options{'type'} eq 'opac-languages' ) {
77             # this is the OPAC
78             $interface = 'opac';
79             $theme     = C4::Context->preference('opacthemes');
80         } else {
81             # this is the staff client
82             $interface = 'intranet';
83             $theme     = C4::Context->preference('template');
84         }
85         $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, $lang, $current_languages );
86         $chunk->{'type'} = 'languages';
87     } elsif ( $options{ 'choices' } ) {
88         if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
89             if ( $options{'choices'} eq 'class-sources' ) {
90                 my $sources = GetClassSources();
91                 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
92             } elsif ( $options{'choices'} eq 'opac-templates' ) {
93                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
94             } elsif ( $options{'choices'} eq 'staff-templates' ) {
95                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
96             } else {
97                 die 'Unrecognized source of preference values: ' . $options{'choices'};
98             }
99         }
100
101         $value ||= 0;
102
103         $chunk->{'type'} = 'select';
104         $chunk->{'CHOICES'} = [
105             sort { $a->{'text'} cmp $b->{'text'} }
106             map { { text => $options{'choices'}->{$_}, value => $_, selected => ( $_ eq $value || ( $_ eq '' && ( $value eq '0' || !$value ) ) ) } }
107             keys %{ $options{'choices'} }
108         ];
109     }
110
111     $chunk->{ 'type_' . $chunk->{'type'} } = 1;
112
113     return $chunk;
114 }
115
116 sub TransformPrefsToHTML {
117     my ( $data, $searchfield ) = @_;
118
119     my @lines;
120     my $dbh = C4::Context->dbh;
121     my $title = ( keys( %$data ) )[0];
122     my $tab = $data->{ $title };
123     $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
124
125     foreach my $group ( sort keys %$tab ) {
126         if ( $group ) {
127             push @lines, { is_group_title => 1, title => $group };
128         }
129
130         foreach my $line ( @{ $tab->{ $group } } ) {
131             my @chunks;
132             my @names;
133
134             foreach my $piece ( @$line ) {
135                 if ( ref ( $piece ) eq 'HASH' ) {
136                     my $name = $piece->{'pref'};
137
138                     if ( $name ) {
139                         my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
140                         my $value;
141                         if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
142                             $value = $piece->{'default'};
143                         } else {
144                             $value = $row->{'value'};
145                         }
146                         my $chunk = _get_chunk( $value, %$piece );
147
148                         # No highlighting of inputs yet, but would be useful
149                         $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
150
151                         push @chunks, $chunk;
152
153                         my $name_entry = { name => $name };
154                         if ( $searchfield ) {
155                             if ( $name =~ /^$searchfield$/i ) {
156                                 $name_entry->{'jumped'} = 1;
157                             } elsif ( $name =~ /$searchfield/i ) {
158                                 $name_entry->{'highlighted'} = 1;
159                             }
160                         }
161                         push @names, $name_entry;
162                     } else {
163                         push @chunks, $piece;
164                     }
165                 } else {
166                     push @chunks, { type_text => 1, contents => $piece };
167                 }
168             }
169
170             push @lines, { CHUNKS => \@chunks, NAMES => \@names, is_group_title => 0 };
171         }
172     }
173
174     return $title, \@lines;
175 }
176
177 sub _get_pref_files {
178     my ( $input, $open_files ) = @_;
179
180     my ( $htdocs, $theme, $lang, undef ) = C4::Templates::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
181
182     my %results;
183
184     foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
185         my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
186
187         $results{$tab} = $open_files ? new IO::File( $file, 'r' ) : '';
188     }
189
190     return %results;
191 }
192
193 sub SearchPrefs {
194     my ( $input, $searchfield ) = @_;
195     my @tabs;
196
197     my %tab_files = _get_pref_files( $input );
198     our @terms = split( /\s+/, $searchfield );
199
200     foreach my $tab_name ( keys %tab_files ) {
201         my $data = GetTab( $input, $tab_name );
202         my $title = ( keys( %$data ) )[0];
203         my $tab = $data->{ $title };
204         $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
205
206         my $matched_groups;
207
208         while ( my ( $group_title, $contents ) = each %$tab ) {
209             if ( matches( $group_title, \@terms ) ) {
210                 $matched_groups->{$group_title} = $contents;
211                 next;
212             }
213
214             my @new_contents;
215
216             foreach my $line ( @$contents ) {
217                 my $matched;
218
219                 foreach my $piece ( @$line ) {
220                     if ( ref( $piece ) eq 'HASH' ) {
221                         if ( !$piece->{'pref'} ){ next; }
222                         if ( $piece->{'pref'} =~ /^$searchfield$/i ) {
223                             my ( undef, $LINES ) = TransformPrefsToHTML( $data, $searchfield );
224
225                             return { search_jumped => 1, tab => $tab_name, tab_title => $title, LINES => $LINES };
226                         } elsif ( matches( $piece->{'pref'}, \@terms) ) {
227                             $matched = 1;
228                         } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_, \@terms ) } values( %{ $piece->{'choices'} } ) ) ) {
229                             $matched = 1;
230                         }
231                     } elsif ( matches( $piece, \@terms ) ) {
232                         $matched = 1;
233                     }
234                     last if ( $matched );
235                 }
236
237                 push @new_contents, $line if ( $matched );
238             }
239
240             $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
241         }
242
243         if ( $matched_groups ) {
244             my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
245
246             push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, };
247         }
248     }
249
250     return @tabs;
251 }
252
253 sub matches {
254     my ( $text, $terms ) = @_;
255     if ( $text ) { return !grep( { $text !~ /$_/i } @$terms ); }
256 }
257
258 my $dbh = C4::Context->dbh;
259 our $input = new CGI;
260
261 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
262     {   template_name   => "admin/preferences.tmpl",
263         query           => $input,
264         type            => "intranet",
265         authnotrequired => 0,
266         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
267         debug           => 1,
268     }
269 );
270
271 $lang = $template->param( 'lang' );
272 my $op = $input->param( 'op' ) || '';
273 my $tab = $input->param( 'tab' );
274 $tab ||= 'acquisitions'; # Ideally this should be "local-use" but preferences.pl
275                          # does not presently support local use preferences
276
277 my $highlighted;
278
279 if ( $op eq 'save' ) {
280     unless ( C4::Context->config( 'demo' ) ) {
281         foreach my $param ( $input->param() ) {
282             my ( $pref ) = ( $param =~ /pref_(.*)/ );
283
284             next if ( !defined( $pref ) );
285
286             my $value = join( ',', $input->param( $param ) );
287
288             C4::Context->set_preference( $pref, $value );
289             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $pref . " | " . $value );
290         }
291     }
292
293     print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
294     exit;
295 }
296
297 my @TABS;
298
299 if ( $op eq 'search' ) {
300     my $searchfield = $input->param('searchfield');
301     utf8::decode($searchfield);
302
303     $searchfield =~ s/\p{IsC}//g;
304     $searchfield =~ s/\s+/ /;
305     $searchfield =~ s/^\s+//;
306     $searchfield =~ s/\s+$//;
307
308     $template->param( searchfield => $searchfield );
309
310     @TABS = SearchPrefs( $input, $searchfield );
311
312     foreach my $tabh ( @TABS ) {
313         $template->param(
314             $tabh->{'tab'} => 1
315         );
316     }
317
318     if ( @TABS ) {
319         $tab = ''; # No need to load a particular tab, as we found results
320         $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
321     } else {
322         $template->param(
323             search_not_found => 1,
324         );
325     }
326 }
327
328 if ( $tab ) {
329     my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
330
331     push @TABS, { tab_title => $tab_title, LINES => $LINES };
332     $template->param(
333         $tab => 1,
334         tab => $tab,
335     );
336 }
337
338 $template->param( TABS => \@TABS );
339
340 output_html_with_http_headers $input, $cookie, $template->output;