Bug 13199: Add missing notices for several installations
[koha.git] / admin / preferences.pl
blobf7910951b1ef1d9e76f80bbd6c0bcd8f117618f3
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 Modern::Perl;
22 use CGI;
23 use Encode;
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::Templates;
32 use C4::Budgets qw(GetCurrency);
33 use File::Spec;
34 use IO::File;
35 use YAML::Syck qw();
36 use List::MoreUtils qw(any);
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( Encode::decode('UTF-8',$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'} }
108 } elsif ( $options{'multiple'} ) {
109 my @values = split /,/, $value;
110 $chunk->{type} = 'multiple';
111 $chunk->{CHOICES} = [
112 sort { $a->{'text'} cmp $b->{'text'} }
113 map {
114 my $option_value = $_;
116 text => $options{multiple}->{$option_value},
117 value => $option_value,
118 selected => grep /^$option_value$/, @values,
121 keys %{ $options{multiple} }
125 $chunk->{ 'type_' . $chunk->{'type'} } = 1;
127 return $chunk;
130 sub TransformPrefsToHTML {
131 my ( $data, $searchfield ) = @_;
133 my @lines;
134 my $dbh = C4::Context->dbh;
135 my $title = ( keys( %$data ) )[0];
136 my $tab = $data->{ $title };
137 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
139 my @override_syspref_names;
140 if ( exists($ENV{OVERRIDE_SYSPREF_NAMES}) &&
141 defined($ENV{OVERRIDE_SYSPREF_NAMES})
143 @override_syspref_names = split /,/, $ENV{OVERRIDE_SYSPREF_NAMES};
146 foreach my $group ( sort keys %$tab ) {
147 if ( $group ) {
148 push @lines, { is_group_title => 1, title => $group };
151 foreach my $line ( @{ $tab->{ $group } } ) {
152 my @chunks;
153 my @names;
155 foreach my $piece ( @$line ) {
156 if ( ref ( $piece ) eq 'HASH' ) {
157 my $name = $piece->{'pref'};
159 if ( $name ) {
160 my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
161 my $value;
162 if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
163 $value = $piece->{'default'};
164 } else {
165 $value = $row->{'value'};
167 my $chunk = _get_chunk( $value, %$piece );
169 # No highlighting of inputs yet, but would be useful
170 $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
172 push @chunks, $chunk;
174 my $name_entry = { name => $name };
175 if ( $searchfield ) {
176 if ( $name =~ /^$searchfield$/i ) {
177 $name_entry->{'jumped'} = 1;
178 } elsif ( $name =~ /$searchfield/i ) {
179 $name_entry->{'highlighted'} = 1;
182 $name_entry->{'overridden'} = 1 if ( any { $name eq $_ } @override_syspref_names );
183 push @names, $name_entry;
184 } else {
185 push @chunks, $piece;
187 } else {
188 push @chunks, { type_text => 1, contents => $piece };
191 push @lines, { CHUNKS => \@chunks, NAMES => \@names, is_group_title => 0 };
195 return $title, \@lines;
198 sub _get_pref_files {
199 my ( $input, $open_files ) = @_;
201 my ( $htdocs, $theme, $lang, undef ) = C4::Templates::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
203 my %results;
205 foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
206 my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
208 $results{$tab} = $open_files ? new IO::File( $file, 'r' ) : '';
211 return %results;
214 sub SearchPrefs {
215 my ( $input, $searchfield ) = @_;
216 my @tabs;
218 my %tab_files = _get_pref_files( $input );
219 our @terms = split( /\s+/, $searchfield );
221 foreach my $tab_name ( keys %tab_files ) {
222 my $data = GetTab( $input, $tab_name );
223 my $title = ( keys( %$data ) )[0];
224 my $tab = $data->{ $title };
225 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
227 my $matched_groups;
229 while ( my ( $group_title, $contents ) = each %$tab ) {
230 if ( matches( $group_title, \@terms ) ) {
231 $matched_groups->{$group_title} = $contents;
232 next;
235 my @new_contents;
237 foreach my $line ( @$contents ) {
238 my $matched;
240 foreach my $piece ( @$line ) {
241 if ( ref( $piece ) eq 'HASH' ) {
242 if ( !$piece->{'pref'} ){
243 next;
245 if ( matches( $piece->{'pref'}, \@terms) ) {
246 $matched = 1;
247 } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_, \@terms ) } values( %{ $piece->{'choices'} } ) ) ) {
248 $matched = 1;
250 } elsif ( matches( $piece, \@terms ) ) {
251 $matched = 1;
253 last if ( $matched );
256 push @new_contents, $line if ( $matched );
259 $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
262 if ( $matched_groups ) {
263 my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
265 push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, };
269 return @tabs;
272 sub matches {
273 my ( $text, $terms ) = @_;
274 if ( $text ) { return !grep( { $text !~ /$_/i } @$terms ); }
277 my $dbh = C4::Context->dbh;
278 our $input = new CGI;
280 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
281 { template_name => "admin/preferences.tt",
282 query => $input,
283 type => "intranet",
284 authnotrequired => 0,
285 flagsrequired => { parameters => 'parameters_remaining_permissions' },
286 debug => 1,
290 $lang = $template->param( 'lang' );
291 my $op = $input->param( 'op' ) || '';
292 my $tab = $input->param( 'tab' );
293 $tab ||= 'acquisitions'; # Ideally this should be "local-use" but preferences.pl
294 # does not presently support local use preferences
296 my $highlighted;
298 if ( $op eq 'save' ) {
299 unless ( C4::Context->config( 'demo' ) ) {
300 foreach my $param ( $input->param() ) {
301 my ( $pref ) = ( $param =~ /pref_(.*)/ );
303 next if ( !defined( $pref ) );
305 my $value = join( ',', $input->param( $param ) );
307 C4::Context->set_preference( $pref, $value );
308 logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $pref . " | " . $value );
312 print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
313 exit;
316 my @TABS;
318 if ( $op eq 'search' ) {
319 my $searchfield = $input->param( 'searchfield' );
321 $searchfield =~ s/\p{IsC}//g;
322 $searchfield =~ s/\s+/ /;
323 $searchfield =~ s/^\s+//;
324 $searchfield =~ s/\s+$//;
326 $template->param( searchfield => $searchfield );
328 @TABS = SearchPrefs( $input, $searchfield );
330 foreach my $tabh ( @TABS ) {
331 $template->param(
332 $tabh->{'tab'} => 1
336 if ( @TABS ) {
337 $tab = ''; # No need to load a particular tab, as we found results
338 $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
339 } else {
340 $template->param(
341 search_not_found => 1,
346 if ( $tab ) {
347 my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
349 push @TABS, { tab_title => $tab_title, LINES => $LINES };
350 $template->param(
351 $tab => 1,
352 tab => $tab,
356 $template->param( TABS => \@TABS );
358 output_html_with_http_headers $input, $cookie, $template->output;