Bug 4440 Follow-up for English typography and alignment
[koha.git] / admin / preferences.pl
bloba161203739e697b5133c9ed8d0c321b80b63dbf5
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{'class'} && $options{'class'} eq 'date' ) {
67 $chunk->{'dateinput'} = 1;
68 } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
69 my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
71 my $theme;
72 my $interface;
73 if ( $options{'type'} eq 'opac-languages' ) {
74 # this is the OPAC
75 $interface = 'opac';
76 $theme = C4::Context->preference('opacthemes');
77 } else {
78 # this is the staff client
79 $interface = 'intranet';
80 $theme = C4::Context->preference('template');
82 $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, $lang, $current_languages );
83 $chunk->{'type'} = 'languages';
84 } elsif ( $options{ 'choices' } ) {
85 if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
86 if ( $options{'choices'} eq 'class-sources' ) {
87 my $sources = GetClassSources();
88 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
89 } elsif ( $options{'choices'} eq 'opac-templates' ) {
90 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
91 } elsif ( $options{'choices'} eq 'staff-templates' ) {
92 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
93 } else {
94 die 'Unrecognized source of preference values: ' . $options{'choices'};
98 $value ||= 0;
100 $chunk->{'type'} = 'select';
101 $chunk->{'CHOICES'} = [
102 sort { $a->{'text'} cmp $b->{'text'} }
103 map { { text => $options{'choices'}->{$_}, value => $_, selected => ( $_ eq $value || ( $_ eq '' && ( $value eq '0' || !$value ) ) ) } }
104 keys %{ $options{'choices'} }
108 $chunk->{ 'type_' . $chunk->{'type'} } = 1;
110 return $chunk;
113 sub TransformPrefsToHTML {
114 my ( $data, $searchfield ) = @_;
116 my @lines;
117 my $dbh = C4::Context->dbh;
118 my $title = ( keys( %$data ) )[0];
119 my $tab = $data->{ $title };
120 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
122 foreach my $group ( sort keys %$tab ) {
123 if ( $group ) {
124 push @lines, { is_group_title => 1, title => $group };
127 foreach my $line ( @{ $tab->{ $group } } ) {
128 my @chunks;
129 my @names;
131 foreach my $piece ( @$line ) {
132 if ( ref ( $piece ) eq 'HASH' ) {
133 my $name = $piece->{'pref'};
135 if ( $name ) {
136 my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
137 my $value;
138 if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
139 $value = $piece->{'default'};
140 } else {
141 $value = $row->{'value'};
143 my $chunk = _get_chunk( $value, %$piece );
145 # No highlighting of inputs yet, but would be useful
146 $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
148 push @chunks, $chunk;
150 my $name_entry = { name => $name };
151 if ( $searchfield ) {
152 if ( $name =~ /^$searchfield$/i ) {
153 $name_entry->{'jumped'} = 1;
154 } elsif ( $name =~ /$searchfield/i ) {
155 $name_entry->{'highlighted'} = 1;
158 push @names, $name_entry;
159 } else {
160 push @chunks, $piece;
162 } else {
163 push @chunks, { type_text => 1, contents => $piece };
167 push @lines, { CHUNKS => \@chunks, NAMES => \@names };
171 return $title, \@lines;
174 sub _get_pref_files {
175 my ( $input, $open_files ) = @_;
177 my ( $htdocs, $theme, $lang, undef ) = C4::Output::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
179 my %results;
181 foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
182 my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
184 $results{$tab} = $open_files ? new IO::File( $file, 'r' ) : '';
187 return %results;
190 sub SearchPrefs {
191 my ( $input, $searchfield ) = @_;
192 my @tabs;
194 my %tab_files = _get_pref_files( $input );
195 our @terms = split( /\s+/, $searchfield );
197 sub matches {
198 my ( $text ) = @_;
200 return !grep( { $text !~ /$_/i } @terms );
203 foreach my $tab_name ( keys %tab_files ) {
204 my $data = GetTab( $input, $tab_name );
205 my $title = ( keys( %$data ) )[0];
206 my $tab = $data->{ $title };
207 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
209 my $matched_groups;
211 while ( my ( $group_title, $contents ) = each %$tab ) {
212 if ( matches( $group_title ) ) {
213 $matched_groups->{$group_title} = $contents;
214 next;
217 my @new_contents;
219 foreach my $line ( @$contents ) {
220 my $matched;
222 foreach my $piece ( @$line ) {
223 if ( ref( $piece ) eq 'HASH' ) {
224 if ( $piece->{'pref'} =~ /^$searchfield$/i ) {
225 my ( undef, $LINES ) = TransformPrefsToHTML( $data, $searchfield );
227 return { search_jumped => 1, tab => $tab_name, tab_title => $title, LINES => $LINES };
228 } elsif ( matches( $piece->{'pref'} ) ) {
229 $matched = 1;
230 } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_ ) } values( %{ $piece->{'choices'} } ) ) ) {
231 $matched = 1;
233 } elsif ( matches( $piece ) ) {
234 $matched = 1;
236 last if ( $matched );
239 push @new_contents, $line if ( $matched );
242 $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
245 if ( $matched_groups ) {
246 my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
248 push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, };
252 return @tabs;
255 my $dbh = C4::Context->dbh;
256 our $input = new CGI;
258 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
259 { template_name => "admin/preferences.tmpl",
260 query => $input,
261 type => "intranet",
262 authnotrequired => 0,
263 flagsrequired => { parameters => 1 },
264 debug => 1,
268 $lang = $template->param( 'lang' );
269 my $op = $input->param( 'op' ) || '';
270 my $tab = $input->param( 'tab' );
271 $tab ||= 'acquisitions'; # Ideally this should be "local-use" but preferences.pl
272 # does not presently support local use preferences
274 my $highlighted;
276 if ( $op eq 'save' ) {
277 unless ( C4::Context->config( 'demo' ) ) {
278 foreach my $param ( $input->param() ) {
279 my ( $pref ) = ( $param =~ /pref_(.*)/ );
281 next if ( !defined( $pref ) );
283 my $value = join( ',', $input->param( $param ) );
285 C4::Context->set_preference( $pref, $value );
286 logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $pref . " | " . $value );
290 print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
291 exit;
294 my @TABS;
296 if ( $op eq 'search' ) {
297 my $searchfield = $input->param( 'searchfield' );
299 $searchfield =~ s/[^a-zA-Z0-9_ -]//g;
301 $template->param( searchfield => $searchfield );
303 @TABS = SearchPrefs( $input, $searchfield );
305 foreach my $tabh ( @TABS ) {
306 $template->param(
307 $tabh->{'tab'} => 1
311 if ( @TABS ) {
312 $tab = ''; # No need to load a particular tab, as we found results
313 $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
314 } else {
315 $template->param(
316 search_not_found => 1,
321 if ( $tab ) {
322 my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
324 push @TABS, { tab_title => $tab_title, LINES => $LINES };
325 $template->param(
326 $tab => 1,
327 tab => $tab,
331 $template->param( TABS => \@TABS );
333 output_html_with_http_headers $input, $cookie, $template->output;