Fix for Bug 4485, System preferences page uses non-standard warning style
[koha.git] / admin / preferences.pl
blobb5bcde8131db4205e922dd49ff91ec2267602881
1 #!/usr/bin/perl
3 # Copyright 2009 Jesse Weaver and the Koha Dev Team
5 # This file is part of Koha.
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.
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.
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.
20 use strict;
21 use warnings;
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;
38 # use Smart::Comments;
41 sub GetTab {
42 my ( $input, $tab ) = @_;
44 my $tab_template = C4::Output::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
46 my $active_currency = GetCurrency();
47 my $local_currency;
48 if ($active_currency) {
49 $local_currency = $active_currency->{currency};
51 $tab_template->param(
52 local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
55 return YAML::Syck::Load( $tab_template->output() );
58 sub _get_chunk {
59 my ( $value, %options ) = @_;
61 my $name = $options{'pref'};
62 my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
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 ) };
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');
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'};
96 $value ||= 0;
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'} }
106 $chunk->{ 'type_' . $chunk->{'type'} } = 1;
108 return $chunk;
111 sub TransformPrefsToHTML {
112 my ( $data, $searchfield ) = @_;
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' );
120 foreach my $group ( sort keys %$tab ) {
121 if ( $group ) {
122 push @lines, { is_group_title => 1, title => $group };
125 foreach my $line ( @{ $tab->{ $group } } ) {
126 my @chunks;
127 my @names;
129 foreach my $piece ( @$line ) {
130 if ( ref ( $piece ) eq 'HASH' ) {
131 my $name = $piece->{'pref'};
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'};
141 my $chunk = _get_chunk( $value, %$piece );
143 # No highlighting of inputs yet, but would be useful
144 $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
146 push @chunks, $chunk;
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;
156 push @names, $name_entry;
157 } else {
158 push @chunks, $piece;
160 } else {
161 push @chunks, { type_text => 1, contents => $piece };
165 push @lines, { CHUNKS => \@chunks, NAMES => \@names };
169 return $title, \@lines;
172 sub _get_pref_files {
173 my ( $input, $open_files ) = @_;
175 my ( $htdocs, $theme, $lang, undef ) = C4::Output::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
177 my %results;
179 foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
180 my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
182 $results{$tab} = $open_files ? new IO::File( $file, 'r' ) : '';
185 return %results;
188 sub SearchPrefs {
189 my ( $input, $searchfield ) = @_;
190 my @tabs;
192 my %tab_files = _get_pref_files( $input );
193 our @terms = split( /\s+/, $searchfield );
195 sub matches {
196 my ( $text ) = @_;
198 return !grep( { $text !~ /$_/i } @terms );
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' );
207 my $matched_groups;
209 while ( my ( $group_title, $contents ) = each %$tab ) {
210 if ( matches( $group_title ) ) {
211 $matched_groups->{$group_title} = $contents;
212 next;
215 my @new_contents;
217 foreach my $line ( @$contents ) {
218 my $matched;
220 foreach my $piece ( @$line ) {
221 if ( ref( $piece ) eq 'HASH' ) {
222 if ( $piece->{'pref'} =~ /^$searchfield$/i ) {
223 my ( undef, $LINES ) = TransformPrefsToHTML( $data, $searchfield );
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;
231 } elsif ( matches( $piece ) ) {
232 $matched = 1;
234 last if ( $matched );
237 push @new_contents, $line if ( $matched );
240 $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
243 if ( $matched_groups ) {
244 my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
246 push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, };
250 return @tabs;
253 my $dbh = C4::Context->dbh;
254 our $input = new CGI;
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,
266 $lang = $template->param( 'lang' );
267 my $op = $input->param( 'op' ) || '';
268 my $tab = $input->param( 'tab' );
269 $tab ||= 'local-use';
271 my $highlighted;
273 if ( $op eq 'save' ) {
274 unless ( C4::Context->config( 'demo' ) ) {
275 foreach my $param ( $input->param() ) {
276 my ( $pref ) = ( $param =~ /pref_(.*)/ );
278 next if ( !defined( $pref ) );
280 my $value = join( ',', $input->param( $param ) );
282 C4::Context->set_preference( $pref, $value );
283 logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $pref . " | " . $value );
287 print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
288 exit;
291 my @TABS;
293 if ( $op eq 'search' ) {
294 my $searchfield = $input->param( 'searchfield' );
296 $searchfield =~ s/[^a-zA-Z0-9_ -]//g;
298 $template->param( searchfield => $searchfield );
300 @TABS = SearchPrefs( $input, $searchfield );
302 foreach my $tabh ( @TABS ) {
303 $template->param(
304 $tabh->{'tab'} => 1
308 if ( @TABS ) {
309 $tab = ''; # No need to load a particular tab, as we found results
310 $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
311 } else {
312 $template->param(
313 search_not_found => 1,
318 if ( $tab ) {
319 my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
321 push @TABS, { tab_title => $tab_title, LINES => $LINES };
322 $template->param(
323 $tab => 1,
324 tab => $tab,
328 $template->param( TABS => \@TABS );
330 output_html_with_http_headers $input, $cookie, $template->output;