Bug 12164: (followup) MoveOrders description corrected
[koha.git] / admin / preferences.pl
blob2d0bac5711d87c1904b653535371132a973e49ba
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 Encode;
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 use List::MoreUtils qw(any);
38 $YAML::Syck::ImplicitTyping = 1;
39 our $lang;
41 # use Smart::Comments;
44 sub GetTab {
45 my ( $input, $tab ) = @_;
47 my $tab_template = C4::Templates::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
49 my $active_currency = GetCurrency();
50 my $local_currency;
51 if ($active_currency) {
52 $local_currency = $active_currency->{currency};
54 $tab_template->param(
55 local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
58 return YAML::Syck::Load( Encode::decode('UTF-8',$tab_template->output()) );
61 sub _get_chunk {
62 my ( $value, %options ) = @_;
64 my $name = $options{'pref'};
65 my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
67 if ( $options{'class'} && $options{'class'} eq 'password' ) {
68 $chunk->{'input_type'} = 'password';
69 } elsif ( $options{'class'} && $options{'class'} eq 'date' ) {
70 $chunk->{'dateinput'} = 1;
71 } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
72 my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
74 my $theme;
75 my $interface;
76 if ( $options{'type'} eq 'opac-languages' ) {
77 # this is the OPAC
78 $interface = 'opac';
79 $theme = C4::Context->preference('opacthemes');
80 } else {
81 # this is the staff client
82 $interface = 'intranet';
83 $theme = C4::Context->preference('template');
85 $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, $lang, $current_languages );
86 $chunk->{'type'} = 'languages';
87 } elsif ( $options{ 'choices' } ) {
88 if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
89 if ( $options{'choices'} eq 'class-sources' ) {
90 my $sources = GetClassSources();
91 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
92 } elsif ( $options{'choices'} eq 'opac-templates' ) {
93 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
94 } elsif ( $options{'choices'} eq 'staff-templates' ) {
95 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
96 } else {
97 die 'Unrecognized source of preference values: ' . $options{'choices'};
101 $value ||= 0;
103 $chunk->{'type'} = 'select';
104 $chunk->{'CHOICES'} = [
105 sort { $a->{'text'} cmp $b->{'text'} }
106 map { { text => $options{'choices'}->{$_}, value => $_, selected => ( $_ eq $value || ( $_ eq '' && ( $value eq '0' || !$value ) ) ) } }
107 keys %{ $options{'choices'} }
111 $chunk->{ 'type_' . $chunk->{'type'} } = 1;
113 return $chunk;
116 sub TransformPrefsToHTML {
117 my ( $data, $searchfield ) = @_;
119 my @lines;
120 my $dbh = C4::Context->dbh;
121 my $title = ( keys( %$data ) )[0];
122 my $tab = $data->{ $title };
123 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
125 my @override_syspref_names;
126 if ( exists($ENV{OVERRIDE_SYSPREF_NAMES}) &&
127 defined($ENV{OVERRIDE_SYSPREF_NAMES})
129 @override_syspref_names = split /,/, $ENV{OVERRIDE_SYSPREF_NAMES};
132 foreach my $group ( sort keys %$tab ) {
133 if ( $group ) {
134 push @lines, { is_group_title => 1, title => $group };
137 foreach my $line ( @{ $tab->{ $group } } ) {
138 my @chunks;
139 my @names;
141 foreach my $piece ( @$line ) {
142 if ( ref ( $piece ) eq 'HASH' ) {
143 my $name = $piece->{'pref'};
145 if ( $name ) {
146 my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
147 my $value;
148 if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
149 $value = $piece->{'default'};
150 } else {
151 $value = $row->{'value'};
153 my $chunk = _get_chunk( $value, %$piece );
155 # No highlighting of inputs yet, but would be useful
156 $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
158 push @chunks, $chunk;
160 my $name_entry = { name => $name };
161 if ( $searchfield ) {
162 if ( $name =~ /^$searchfield$/i ) {
163 $name_entry->{'jumped'} = 1;
164 } elsif ( $name =~ /$searchfield/i ) {
165 $name_entry->{'highlighted'} = 1;
168 $name_entry->{'overridden'} = 1 if ( any { $name eq $_ } @override_syspref_names );
169 push @names, $name_entry;
170 } else {
171 push @chunks, $piece;
173 } else {
174 push @chunks, { type_text => 1, contents => $piece };
177 push @lines, { CHUNKS => \@chunks, NAMES => \@names, is_group_title => 0 };
181 return $title, \@lines;
184 sub _get_pref_files {
185 my ( $input, $open_files ) = @_;
187 my ( $htdocs, $theme, $lang, undef ) = C4::Templates::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
189 my %results;
191 foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
192 my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
194 $results{$tab} = $open_files ? new IO::File( $file, 'r' ) : '';
197 return %results;
200 sub SearchPrefs {
201 my ( $input, $searchfield ) = @_;
202 my @tabs;
204 my %tab_files = _get_pref_files( $input );
205 our @terms = split( /\s+/, $searchfield );
207 foreach my $tab_name ( keys %tab_files ) {
208 my $data = GetTab( $input, $tab_name );
209 my $title = ( keys( %$data ) )[0];
210 my $tab = $data->{ $title };
211 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
213 my $matched_groups;
215 while ( my ( $group_title, $contents ) = each %$tab ) {
216 if ( matches( $group_title, \@terms ) ) {
217 $matched_groups->{$group_title} = $contents;
218 next;
221 my @new_contents;
223 foreach my $line ( @$contents ) {
224 my $matched;
226 foreach my $piece ( @$line ) {
227 if ( ref( $piece ) eq 'HASH' ) {
228 if ( !$piece->{'pref'} ){
229 next;
231 if ( matches( $piece->{'pref'}, \@terms) ) {
232 $matched = 1;
233 } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_, \@terms ) } values( %{ $piece->{'choices'} } ) ) ) {
234 $matched = 1;
236 } elsif ( matches( $piece, \@terms ) ) {
237 $matched = 1;
239 last if ( $matched );
242 push @new_contents, $line if ( $matched );
245 $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
248 if ( $matched_groups ) {
249 my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
251 push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, };
255 return @tabs;
258 sub matches {
259 my ( $text, $terms ) = @_;
260 if ( $text ) { return !grep( { $text !~ /$_/i } @$terms ); }
263 my $dbh = C4::Context->dbh;
264 our $input = new CGI;
266 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
267 { template_name => "admin/preferences.tt",
268 query => $input,
269 type => "intranet",
270 authnotrequired => 0,
271 flagsrequired => { parameters => 'parameters_remaining_permissions' },
272 debug => 1,
276 $lang = $template->param( 'lang' );
277 my $op = $input->param( 'op' ) || '';
278 my $tab = $input->param( 'tab' );
279 $tab ||= 'acquisitions'; # Ideally this should be "local-use" but preferences.pl
280 # does not presently support local use preferences
282 my $highlighted;
284 if ( $op eq 'save' ) {
285 unless ( C4::Context->config( 'demo' ) ) {
286 foreach my $param ( $input->param() ) {
287 my ( $pref ) = ( $param =~ /pref_(.*)/ );
289 next if ( !defined( $pref ) );
291 my $value = join( ',', $input->param( $param ) );
293 C4::Context->set_preference( $pref, $value );
294 logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $pref . " | " . $value );
298 print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
299 exit;
302 my @TABS;
304 if ( $op eq 'search' ) {
305 my $searchfield = $input->param( 'searchfield' );
307 $searchfield =~ s/\p{IsC}//g;
308 $searchfield =~ s/\s+/ /;
309 $searchfield =~ s/^\s+//;
310 $searchfield =~ s/\s+$//;
312 $template->param( searchfield => $searchfield );
314 @TABS = SearchPrefs( $input, $searchfield );
316 foreach my $tabh ( @TABS ) {
317 $template->param(
318 $tabh->{'tab'} => 1
322 if ( @TABS ) {
323 $tab = ''; # No need to load a particular tab, as we found results
324 $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
325 } else {
326 $template->param(
327 search_not_found => 1,
332 if ( $tab ) {
333 my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
335 push @TABS, { tab_title => $tab_title, LINES => $LINES };
336 $template->param(
337 $tab => 1,
338 tab => $tab,
342 $template->param( TABS => \@TABS );
344 output_html_with_http_headers $input, $cookie, $template->output;