Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 331 lines (262 sloc) 10.772 kB
fdd82e8 @pianohacker Add new system preferences editor
pianohacker authored
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 #
2c67656 Fix FSF address in directory admin/
Lars Wirzenius authored
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.
fdd82e8 @pianohacker Add new system preferences editor
pianohacker authored
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;
2c9e28e @gmcharlt fix silly typo
gmcharlt authored
31 use C4::Budgets qw(GetCurrency);
fdd82e8 @pianohacker Add new system preferences editor
pianohacker authored
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
0c3fde5 @gmcharlt handle error if no active currency is defined
gmcharlt authored
46 my $active_currency = GetCurrency();
47 my $local_currency;
b6c0431 @gmcharlt final fix for sysprefs merge, hopefully
gmcharlt authored
48 if ($active_currency) {
0c3fde5 @gmcharlt handle error if no active currency is defined
gmcharlt authored
49 $local_currency = $active_currency->{currency};
50 }
fdd82e8 @pianohacker Add new system preferences editor
pianohacker authored
51 $tab_template->param(
0c3fde5 @gmcharlt handle error if no active currency is defined
gmcharlt authored
52 local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
fdd82e8 @pianohacker Add new system preferences editor
pianohacker authored
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 {
cdb3d52 @pianohacker Move jumping functionality to search
pianohacker authored
112 my ( $data, $searchfield ) = @_;
fdd82e8 @pianohacker Add new system preferences editor
pianohacker authored
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 ) {
870abd7 @pianohacker Fix handling of pref defaults (#3747, #3588)
pianohacker authored
134 my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
fdd82e8 @pianohacker Add new system preferences editor
pianohacker authored
135 my $value;
870abd7 @pianohacker Fix handling of pref defaults (#3747, #3588)
pianohacker authored
136 if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
fdd82e8 @pianohacker Add new system preferences editor
pianohacker authored
137 $value = $piece->{'default'};
138 } else {
139 $value = $row->{'value'};
140 }
141 my $chunk = _get_chunk( $value, %$piece );
142
cdb3d52 @pianohacker Move jumping functionality to search
pianohacker authored
143 # No highlighting of inputs yet, but would be useful
144 $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
fdd82e8 @pianohacker Add new system preferences editor
pianohacker authored
145
146 push @chunks, $chunk;
cdb3d52 @pianohacker Move jumping functionality to search
pianohacker authored
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;
fdd82e8 @pianohacker Add new system preferences editor
pianohacker authored
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' ) {
cdb3d52 @pianohacker Move jumping functionality to search
pianohacker authored
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 };
fdd82e8 @pianohacker Add new system preferences editor
pianohacker authored
226 } elsif ( matches( $piece->{'pref'} ) ) {
227 $matched = 1;
cdb3d52 @pianohacker Move jumping functionality to search
pianohacker authored
228 } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_ ) } values( %{ $piece->{'choices'} } ) ) ) {
229 $matched = 1;
fdd82e8 @pianohacker Add new system preferences editor
pianohacker authored
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 ||= 'local-use';
270
271 my $highlighted;
272
273 if ( $op eq 'save' ) {
274 unless ( C4::Context->config( 'demo' ) ) {
275 foreach my $param ( $input->param() ) {
276 my ( $pref ) = ( $param =~ /pref_(.*)/ );
277
278 next if ( !defined( $pref ) );
279
280 my $value = join( ',', $input->param( $param ) );
281
282 C4::Context->set_preference( $pref, $value );
283 logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $pref . " | " . $value );
284 }
285 }
286
287 print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
288 exit;
289 }
290
291 my @TABS;
292
293 if ( $op eq 'search' ) {
294 my $searchfield = $input->param( 'searchfield' );
295
296 $searchfield =~ s/[^a-zA-Z0-9_ -]//g;
297
298 $template->param( searchfield => $searchfield );
299
300 @TABS = SearchPrefs( $input, $searchfield );
301
302 foreach my $tabh ( @TABS ) {
303 $template->param(
304 $tabh->{'tab'} => 1
305 );
306 }
307
308 if ( @TABS ) {
309 $tab = ''; # No need to load a particular tab, as we found results
cdb3d52 @pianohacker Move jumping functionality to search
pianohacker authored
310 $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
fdd82e8 @pianohacker Add new system preferences editor
pianohacker authored
311 } else {
312 $template->param(
313 search_not_found => 1,
314 );
315 }
316 }
317
318 if ( $tab ) {
cdb3d52 @pianohacker Move jumping functionality to search
pianohacker authored
319 my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
fdd82e8 @pianohacker Add new system preferences editor
pianohacker authored
320
321 push @TABS, { tab_title => $tab_title, LINES => $LINES };
322 $template->param(
323 $tab => 1,
324 tab => $tab,
325 );
326 }
327
328 $template->param( TABS => \@TABS );
329
330 output_html_with_http_headers $input, $cookie, $template->output;
Something went wrong with that request. Please try again.