Revert "Bug 6554 - make Koha internally utf-8 clean"
[koha.git] / admin / preferences.pl
blob7c5b3c01f8738c1b1bb11aa6b98f8d433b52cae2
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;
25 use C4::Auth;
26 use C4::Context;
27 use C4::Koha;
28 use C4::Languages qw(getTranslatedLanguages);
29 use C4::ClassSource;
30 use C4::Log;
31 use C4::Output;
32 use C4::Templates;
33 use C4::Budgets qw(GetCurrency);
34 use File::Spec;
35 use IO::File;
36 use YAML::Syck qw();
37 $YAML::Syck::ImplicitTyping = 1;
38 our $lang;
40 # use Smart::Comments;
43 sub GetTab {
44 my ( $input, $tab ) = @_;
46 my $tab_template = C4::Templates::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
48 my $active_currency = GetCurrency();
49 my $local_currency;
50 if ($active_currency) {
51 $local_currency = $active_currency->{currency};
53 $tab_template->param(
54 local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
57 return YAML::Syck::Load( $tab_template->output() );
60 sub _get_chunk {
61 my ( $value, %options ) = @_;
63 my $name = $options{'pref'};
64 my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
66 if ( $options{'class'} && $options{'class'} eq 'password' ) {
67 $chunk->{'input_type'} = 'password';
68 } elsif ( $options{'class'} && $options{'class'} eq 'date' ) {
69 $chunk->{'dateinput'} = 1;
70 } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
71 my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
73 my $theme;
74 my $interface;
75 if ( $options{'type'} eq 'opac-languages' ) {
76 # this is the OPAC
77 $interface = 'opac';
78 $theme = C4::Context->preference('opacthemes');
79 } else {
80 # this is the staff client
81 $interface = 'intranet';
82 $theme = C4::Context->preference('template');
84 $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, $lang, $current_languages );
85 $chunk->{'type'} = 'languages';
86 } elsif ( $options{ 'choices' } ) {
87 if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
88 if ( $options{'choices'} eq 'class-sources' ) {
89 my $sources = GetClassSources();
90 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
91 } elsif ( $options{'choices'} eq 'opac-templates' ) {
92 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
93 } elsif ( $options{'choices'} eq 'staff-templates' ) {
94 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
95 } else {
96 die 'Unrecognized source of preference values: ' . $options{'choices'};
100 $value ||= 0;
102 $chunk->{'type'} = 'select';
103 $chunk->{'CHOICES'} = [
104 sort { $a->{'text'} cmp $b->{'text'} }
105 map { { text => $options{'choices'}->{$_}, value => $_, selected => ( $_ eq $value || ( $_ eq '' && ( $value eq '0' || !$value ) ) ) } }
106 keys %{ $options{'choices'} }
110 $chunk->{ 'type_' . $chunk->{'type'} } = 1;
112 return $chunk;
115 sub TransformPrefsToHTML {
116 my ( $data, $searchfield ) = @_;
118 my @lines;
119 my $dbh = C4::Context->dbh;
120 my $title = ( keys( %$data ) )[0];
121 my $tab = $data->{ $title };
122 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
124 foreach my $group ( sort keys %$tab ) {
125 if ( $group ) {
126 push @lines, { is_group_title => 1, title => $group };
129 foreach my $line ( @{ $tab->{ $group } } ) {
130 my @chunks;
131 my @names;
133 foreach my $piece ( @$line ) {
134 if ( ref ( $piece ) eq 'HASH' ) {
135 my $name = $piece->{'pref'};
137 if ( $name ) {
138 my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
139 my $value;
140 if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
141 $value = $piece->{'default'};
142 } else {
143 $value = $row->{'value'};
145 my $chunk = _get_chunk( $value, %$piece );
147 # No highlighting of inputs yet, but would be useful
148 $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
150 push @chunks, $chunk;
152 my $name_entry = { name => $name };
153 if ( $searchfield ) {
154 if ( $name =~ /^$searchfield$/i ) {
155 $name_entry->{'jumped'} = 1;
156 } elsif ( $name =~ /$searchfield/i ) {
157 $name_entry->{'highlighted'} = 1;
160 push @names, $name_entry;
161 } else {
162 push @chunks, $piece;
164 } else {
165 push @chunks, { type_text => 1, contents => $piece };
169 push @lines, { CHUNKS => \@chunks, NAMES => \@names, is_group_title => 0 };
173 return $title, \@lines;
176 sub _get_pref_files {
177 my ( $input, $open_files ) = @_;
179 my ( $htdocs, $theme, $lang, undef ) = C4::Templates::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
181 my %results;
183 foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
184 my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
186 $results{$tab} = $open_files ? new IO::File( $file, 'r' ) : '';
189 return %results;
192 sub SearchPrefs {
193 my ( $input, $searchfield ) = @_;
194 my @tabs;
196 my %tab_files = _get_pref_files( $input );
197 our @terms = split( /\s+/, $searchfield );
199 foreach my $tab_name ( keys %tab_files ) {
200 my $data = GetTab( $input, $tab_name );
201 my $title = ( keys( %$data ) )[0];
202 my $tab = $data->{ $title };
203 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
205 my $matched_groups;
207 while ( my ( $group_title, $contents ) = each %$tab ) {
208 if ( matches( $group_title, \@terms ) ) {
209 $matched_groups->{$group_title} = $contents;
210 next;
213 my @new_contents;
215 foreach my $line ( @$contents ) {
216 my $matched;
218 foreach my $piece ( @$line ) {
219 if ( ref( $piece ) eq 'HASH' ) {
220 if ( !$piece->{'pref'} ){ next; }
221 if ( $piece->{'pref'} =~ /^$searchfield$/i ) {
222 my ( undef, $LINES ) = TransformPrefsToHTML( $data, $searchfield );
224 return { search_jumped => 1, tab => $tab_name, tab_title => $title, LINES => $LINES };
225 } elsif ( matches( $piece->{'pref'}, \@terms) ) {
226 $matched = 1;
227 } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_, \@terms ) } values( %{ $piece->{'choices'} } ) ) ) {
228 $matched = 1;
230 } elsif ( matches( $piece, \@terms ) ) {
231 $matched = 1;
233 last if ( $matched );
236 push @new_contents, $line if ( $matched );
239 $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
242 if ( $matched_groups ) {
243 my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
245 push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, };
249 return @tabs;
252 sub matches {
253 my ( $text, $terms ) = @_;
254 if ( $text ) { return !grep( { $text !~ /$_/i } @$terms ); }
257 my $dbh = C4::Context->dbh;
258 our $input = new CGI;
260 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
261 { template_name => "admin/preferences.tmpl",
262 query => $input,
263 type => "intranet",
264 authnotrequired => 0,
265 flagsrequired => { parameters => 'parameters_remaining_permissions' },
266 debug => 1,
270 $lang = $template->param( 'lang' );
271 my $op = $input->param( 'op' ) || '';
272 my $tab = $input->param( 'tab' );
273 $tab ||= 'acquisitions'; # Ideally this should be "local-use" but preferences.pl
274 # does not presently support local use preferences
276 my $highlighted;
278 if ( $op eq 'save' ) {
279 unless ( C4::Context->config( 'demo' ) ) {
280 foreach my $param ( $input->param() ) {
281 my ( $pref ) = ( $param =~ /pref_(.*)/ );
283 next if ( !defined( $pref ) );
285 my $value = join( ',', $input->param( $param ) );
287 C4::Context->set_preference( $pref, $value );
288 logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $pref . " | " . $value );
292 print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
293 exit;
296 my @TABS;
298 if ( $op eq 'search' ) {
299 my $searchfield = $input->param('searchfield');
300 utf8::decode($searchfield);
302 $searchfield =~ s/\p{IsC}//g;
303 $searchfield =~ s/\s+/ /;
304 $searchfield =~ s/^\s+//;
305 $searchfield =~ s/\s+$//;
307 $template->param( searchfield => $searchfield );
309 @TABS = SearchPrefs( $input, $searchfield );
311 foreach my $tabh ( @TABS ) {
312 $template->param(
313 $tabh->{'tab'} => 1
317 if ( @TABS ) {
318 $tab = ''; # No need to load a particular tab, as we found results
319 $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
320 } else {
321 $template->param(
322 search_not_found => 1,
327 if ( $tab ) {
328 my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
330 push @TABS, { tab_title => $tab_title, LINES => $LINES };
331 $template->param(
332 $tab => 1,
333 tab => $tab,
337 $template->param( TABS => \@TABS );
339 output_html_with_http_headers $input, $cookie, $template->output;