Merge remote branch 'koha-fbc/k_bug_5247' into to-push
[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 use C4::Auth;
25 use C4::Context;
26 use C4::Koha;
27 use C4::Languages qw(getTranslatedLanguages);
28 use C4::ClassSource;
29 use C4::Log;
30 use C4::Output;
31 use C4::Budgets qw(GetCurrency);
32 use File::Spec;
33 use IO::File;
34 use YAML::Syck qw();
35 $YAML::Syck::ImplicitTyping = 1;
36 our $lang;
37
38 # use Smart::Comments;
39 #
40
41 sub GetTab {
42     my ( $input, $tab ) = @_;
43
44     my $tab_template = C4::Output::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
45
46     my $active_currency = GetCurrency();
47     my $local_currency;
48     if ($active_currency) {
49         $local_currency = $active_currency->{currency};
50     }
51     $tab_template->param(
52         local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
53     );
54
55     return YAML::Syck::Load( $tab_template->output() );
56 }
57
58 sub _get_chunk {
59     my ( $value, %options ) = @_;
60
61     my $name = $options{'pref'};
62     my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
63
64     if ( $options{'class'} && $options{'class'} eq 'password' ) {
65         $chunk->{'input_type'} = 'password';
66     } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
67         my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
68
69         my $theme;
70         my $interface;
71         if ( $options{'type'} eq 'opac-languages' ) {
72             # this is the OPAC
73             $interface = 'opac';
74             $theme     = C4::Context->preference('opacthemes');
75         } else {
76             # this is the staff client
77             $interface = 'intranet';
78             $theme     = C4::Context->preference('template');
79         }
80         $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, $lang, $current_languages );
81         $chunk->{'type'} = 'languages';
82     } elsif ( $options{ 'choices' } ) {
83         if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
84             if ( $options{'choices'} eq 'class-sources' ) {
85                 my $sources = GetClassSources();
86                 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
87             } elsif ( $options{'choices'} eq 'opac-templates' ) {
88                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
89             } elsif ( $options{'choices'} eq 'staff-templates' ) {
90                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
91             } else {
92                 die 'Unrecognized source of preference values: ' . $options{'choices'};
93             }
94         }
95
96         $value ||= 0;
97
98         $chunk->{'type'} = 'select';
99         $chunk->{'CHOICES'} = [
100             sort { $a->{'text'} cmp $b->{'text'} }
101             map { { text => $options{'choices'}->{$_}, value => $_, selected => ( $_ eq $value || ( $_ eq '' && ( $value eq '0' || !$value ) ) ) } }
102             keys %{ $options{'choices'} }
103         ];
104     }
105
106     $chunk->{ 'type_' . $chunk->{'type'} } = 1;
107
108     return $chunk;
109 }
110
111 sub TransformPrefsToHTML {
112     my ( $data, $searchfield ) = @_;
113
114     my @lines;
115     my $dbh = C4::Context->dbh;
116     my $title = ( keys( %$data ) )[0];
117     my $tab = $data->{ $title };
118     $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
119
120     foreach my $group ( sort keys %$tab ) {
121         if ( $group ) {
122             push @lines, { is_group_title => 1, title => $group };
123         }
124
125         foreach my $line ( @{ $tab->{ $group } } ) {
126             my @chunks;
127             my @names;
128
129             foreach my $piece ( @$line ) {
130                 if ( ref ( $piece ) eq 'HASH' ) {
131                     my $name = $piece->{'pref'};
132
133                     if ( $name ) {
134                         my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
135                         my $value;
136                         if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
137                             $value = $piece->{'default'};
138                         } else {
139                             $value = $row->{'value'};
140                         }
141                         my $chunk = _get_chunk( $value, %$piece );
142
143                         # No highlighting of inputs yet, but would be useful
144                         $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
145
146                         push @chunks, $chunk;
147
148                         my $name_entry = { name => $name };
149                         if ( $searchfield ) {
150                             if ( $name =~ /^$searchfield$/i ) {
151                                 $name_entry->{'jumped'} = 1;
152                             } elsif ( $name =~ /$searchfield/i ) {
153                                 $name_entry->{'highlighted'} = 1;
154                             }
155                         }
156                         push @names, $name_entry;
157                     } else {
158                         push @chunks, $piece;
159                     }
160                 } else {
161                     push @chunks, { type_text => 1, contents => $piece };
162                 }
163             }
164
165             push @lines, { CHUNKS => \@chunks, NAMES => \@names };
166         }
167     }
168
169     return $title, \@lines;
170 }
171
172 sub _get_pref_files {
173     my ( $input, $open_files ) = @_;
174
175     my ( $htdocs, $theme, $lang, undef ) = C4::Output::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
176
177     my %results;
178
179     foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
180         my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
181
182         $results{$tab} = $open_files ? new IO::File( $file, 'r' ) : '';
183     }
184
185     return %results;
186 }
187
188 sub SearchPrefs {
189     my ( $input, $searchfield ) = @_;
190     my @tabs;
191
192     my %tab_files = _get_pref_files( $input );
193     our @terms = split( /\s+/, $searchfield );
194
195     sub matches {
196         my ( $text ) = @_;
197
198         return !grep( { $text !~ /$_/i } @terms );
199     }
200
201     foreach my $tab_name ( keys %tab_files ) {
202         my $data = GetTab( $input, $tab_name );
203         my $title = ( keys( %$data ) )[0];
204         my $tab = $data->{ $title };
205         $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
206
207         my $matched_groups;
208
209         while ( my ( $group_title, $contents ) = each %$tab ) {
210             if ( matches( $group_title ) ) {
211                 $matched_groups->{$group_title} = $contents;
212                 next;
213             }
214
215             my @new_contents;
216
217             foreach my $line ( @$contents ) {
218                 my $matched;
219
220                 foreach my $piece ( @$line ) {
221                     if ( ref( $piece ) eq 'HASH' ) {
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'} ) ) {
227                             $matched = 1;
228                         } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_ ) } values( %{ $piece->{'choices'} } ) ) ) {
229                             $matched = 1;
230                         }
231                     } elsif ( matches( $piece ) ) {
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 my $dbh = C4::Context->dbh;
254 our $input = new CGI;
255
256 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
257     {   template_name   => "admin/preferences.tmpl",
258         query           => $input,
259         type            => "intranet",
260         authnotrequired => 0,
261         flagsrequired   => { parameters => 1 },
262         debug           => 1,
263     }
264 );
265
266 $lang = $template->param( 'lang' );
267 my $op = $input->param( 'op' ) || '';
268 my $tab = $input->param( 'tab' );
269 $tab ||= 'acquisitions'; # Ideally this should be "local-use" but preferences.pl
270                          # does not presently support local use preferences
271
272 my $highlighted;
273
274 if ( $op eq 'save' ) {
275     unless ( C4::Context->config( 'demo' ) ) {
276         foreach my $param ( $input->param() ) {
277             my ( $pref ) = ( $param =~ /pref_(.*)/ );
278
279             next if ( !defined( $pref ) );
280
281             my $value = join( ',', $input->param( $param ) );
282
283             C4::Context->set_preference( $pref, $value );
284             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $pref . " | " . $value );
285         }
286     }
287
288     print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
289     exit;
290 }
291
292 my @TABS;
293
294 if ( $op eq 'search' ) {
295     my $searchfield = $input->param( 'searchfield' );
296
297     $searchfield =~ s/[^a-zA-Z0-9_ -]//g;
298
299     $template->param( searchfield => $searchfield );
300
301     @TABS = SearchPrefs( $input, $searchfield );
302
303     foreach my $tabh ( @TABS ) {
304         $template->param(
305             $tabh->{'tab'} => 1
306         );
307     }
308
309     if ( @TABS ) {
310         $tab = ''; # No need to load a particular tab, as we found results
311         $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
312     } else {
313         $template->param(
314             search_not_found => 1,
315         );
316     }
317 }
318
319 if ( $tab ) {
320     my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
321
322     push @TABS, { tab_title => $tab_title, LINES => $LINES };
323     $template->param(
324         $tab => 1,
325         tab => $tab,
326     );
327 }
328
329 $template->param( TABS => \@TABS );
330
331 output_html_with_http_headers $input, $cookie, $template->output;