Add new system preferences editor
[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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 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::Bookfund qw(GetLocalCurrency);
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     $tab_template->param(
47         local_currency => GetLocalCurrency()->{'currency'}, # currency code is used, because we do not know how a given currency is formatted.
48     );
49
50     return YAML::Syck::Load( $tab_template->output() );
51 }
52
53 sub _get_chunk {
54     my ( $value, %options ) = @_;
55
56     my $name = $options{'pref'};
57     my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
58
59     if ( $options{'class'} && $options{'class'} eq 'password' ) {
60         $chunk->{'input_type'} = 'password';
61     } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
62         my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
63
64         my $theme;
65         my $interface;
66         if ( $options{'type'} eq 'opac-languages' ) {
67             # this is the OPAC
68             $interface = 'opac';
69             $theme     = C4::Context->preference('opacthemes');
70         } else {
71             # this is the staff client
72             $interface = 'intranet';
73             $theme     = C4::Context->preference('template');
74         }
75         $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, $lang, $current_languages );
76         $chunk->{'type'} = 'languages';
77     } elsif ( $options{ 'choices' } ) {
78         if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
79             if ( $options{'choices'} eq 'class-sources' ) {
80                 my $sources = GetClassSources();
81                 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
82             } elsif ( $options{'choices'} eq 'opac-templates' ) {
83                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
84             } elsif ( $options{'choices'} eq 'staff-templates' ) {
85                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
86             } else {
87                 die 'Unrecognized source of preference values: ' . $options{'choices'};
88             }
89         }
90
91         $value ||= 0;
92
93         $chunk->{'type'} = 'select';
94         $chunk->{'CHOICES'} = [
95             sort { $a->{'text'} cmp $b->{'text'} }
96             map { { text => $options{'choices'}->{$_}, value => $_, selected => ( $_ eq $value || ( $_ eq '' && ( $value eq '0' || !$value ) ) ) } }
97             keys %{ $options{'choices'} }
98         ];
99     }
100
101     $chunk->{ 'type_' . $chunk->{'type'} } = 1;
102
103     return $chunk;
104 }
105
106 sub TransformPrefsToHTML {
107     my ( $data, $highlighted_pref ) = @_;
108
109     my @lines;
110     my $dbh = C4::Context->dbh;
111     my $title = ( keys( %$data ) )[0];
112     my $tab = $data->{ $title };
113     $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
114
115     foreach my $group ( sort keys %$tab ) {
116         if ( $group ) {
117             push @lines, { is_group_title => 1, title => $group };
118         }
119
120         foreach my $line ( @{ $tab->{ $group } } ) {
121             my @chunks;
122             my @names;
123
124             foreach my $piece ( @$line ) {
125                 if ( ref ( $piece ) eq 'HASH' ) {
126                     my $name = $piece->{'pref'};
127
128                     if ( $name ) {
129                         my $row = $dbh->selectrow_hashref( "SELECT value FROM systempreferences WHERE variable = ?", {}, $name );
130                         my $value;
131                         if ( !defined( $row ) && defined( $piece->{'default'} ) ) {
132                             $value = $piece->{'default'};
133                         } else {
134                             $value = $row->{'value'};
135                         }
136                         my $chunk = _get_chunk( $value, %$piece );
137
138                         $chunk->{'highlighted'} = 1 if ( $highlighted_pref && $name =~ /$highlighted_pref/ );
139
140                         push @chunks, $chunk;
141                         push @names, { name => $name, highlighted => ( $highlighted_pref && ( $name =~ /$highlighted_pref/i ? 1 : 0 ) ) };
142                     } else {
143                         push @chunks, $piece;
144                     }
145                 } else {
146                     push @chunks, { type_text => 1, contents => $piece };
147                 }
148             }
149
150             push @lines, { CHUNKS => \@chunks, NAMES => \@names };
151         }
152     }
153
154     return $title, \@lines;
155 }
156
157 sub _get_pref_files {
158     my ( $input, $open_files ) = @_;
159
160     my ( $htdocs, $theme, $lang, undef ) = C4::Output::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
161
162     my %results;
163
164     foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
165         my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
166
167         $results{$tab} = $open_files ? new IO::File( $file, 'r' ) : '';
168     }
169
170     return %results;
171 }
172
173 sub JumpPref {
174     my ( $input, $tab, $jumpfield ) = @_;
175
176     return ( $tab ) if ( $jumpfield !~ /^[a-zA-Z_0-9-]+$/ );
177
178     my %tab_files = _get_pref_files( $input, 1 );
179
180     while ( my ( $tab, $tabfile ) = each %tab_files ) {
181         while ( <$tabfile> ) {
182             return ( $tab, $1 ) if ( /pref: ($jumpfield)/i );
183         }
184
185         close $tabfile;
186     }
187
188     return ( "", "" );
189 }
190
191 sub SearchPrefs {
192     my ( $input, $searchfield ) = @_;
193     my @tabs;
194
195     my %tab_files = _get_pref_files( $input );
196     our @terms = split( /\s+/, $searchfield );
197
198     sub matches {
199         my ( $text ) = @_;
200
201         return !grep( { $text !~ /$_/i } @terms );
202     }
203
204     foreach my $tab_name ( keys %tab_files ) {
205         my $data = GetTab( $input, $tab_name );
206         my $title = ( keys( %$data ) )[0];
207         my $tab = $data->{ $title };
208         $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
209
210         my $matched_groups;
211
212         while ( my ( $group_title, $contents ) = each %$tab ) {
213             if ( matches( $group_title ) ) {
214                 $matched_groups->{$group_title} = $contents;
215                 next;
216             }
217
218             my @new_contents;
219
220             foreach my $line ( @$contents ) {
221                 my $matched;
222
223                 foreach my $piece ( @$line ) {
224                     if ( ref( $piece ) eq 'HASH' ) {
225                         if ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_ ) } values( %{ $piece->{'choices'} } ) ) ) {
226                             $matched = 1;
227                         } elsif ( matches( $piece->{'pref'} ) ) {
228                             $matched = 1;
229                         }
230                     } elsif ( matches( $piece ) ) {
231                         $matched = 1;
232                     }
233                     last if ( $matched );
234                 }
235
236                 push @new_contents, $line if ( $matched );
237             }
238
239             $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
240         }
241
242         if ( $matched_groups ) {
243             my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
244
245             push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, };
246         }
247     }
248
249     return @tabs;
250 }
251
252 my $dbh = C4::Context->dbh;
253 our $input = new CGI;
254
255 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
256     {   template_name   => "admin/preferences.tmpl",
257         query           => $input,
258         type            => "intranet",
259         authnotrequired => 0,
260         flagsrequired   => { parameters => 1 },
261         debug           => 1,
262     }
263 );
264
265 $lang = $template->param( 'lang' );
266 my $op = $input->param( 'op' ) || '';
267 my $tab = $input->param( 'tab' );
268 $tab ||= 'local-use';
269
270 my $highlighted;
271
272 if ( $op eq 'save' ) {
273     unless ( C4::Context->config( 'demo' ) ) {
274         foreach my $param ( $input->param() ) {
275             my ( $pref ) = ( $param =~ /pref_(.*)/ );
276
277             next if ( !defined( $pref ) );
278
279             my $value = join( ',', $input->param( $param ) );
280
281             C4::Context->set_preference( $pref, $value );
282             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $pref . " | " . $value );
283         }
284     }
285
286     print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
287     exit;
288 } elsif ( $op eq 'jump' ) {
289     my $jumpfield = $input->param( 'jumpfield' );
290     $template->param( jumpfield => $jumpfield );
291
292     my $new_tab;
293     ( $new_tab, $highlighted ) = JumpPref( $input, $tab, $jumpfield );
294
295     if ( $highlighted ) {
296         $tab = $new_tab;
297     } else {
298         $template->param( jump_not_found => 1 );
299     }
300 }
301
302 my @TABS;
303
304 if ( $op eq 'search' ) {
305     my $searchfield = $input->param( 'searchfield' );
306
307     $searchfield =~ s/[^a-zA-Z0-9_ -]//g;
308
309     $template->param( searchfield => $searchfield );
310
311     @TABS = SearchPrefs( $input, $searchfield );
312
313     foreach my $tabh ( @TABS ) {
314         $template->param(
315             $tabh->{'tab'} => 1
316         );
317     }
318
319     if ( @TABS ) {
320         $tab = ''; # No need to load a particular tab, as we found results
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, ( $op eq 'jump' ) );
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;