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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use C4::Auth qw( get_template_and_user );
25 use C4::Koha qw( getallthemes );
26 use C4::Languages qw( getTranslatedLanguages );
27 use C4::ClassSource qw( GetClassSources GetClassSource );
28 use C4::Output qw( output_html_with_http_headers output_and_exit_if_error );
30 use Koha::Acquisition::Currencies;
31 use Koha::Database::Columns;
35 use List::MoreUtils qw( any uniq );
38 my ( $input, $tab ) = @_;
40 my $tab_template = C4::Templates::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
42 my $active_currency = Koha::Acquisition::Currencies->get_active;
44 if ($active_currency) {
45 $local_currency = $active_currency->currency;
48 local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
51 return YAML::XS::Load( Encode::encode_utf8($tab_template->output()));
55 my ( $value, %options ) = @_;
57 my $name = $options{'pref'};
58 my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
59 if( $options{'syntax'} ){
60 $chunk->{'syntax'} = $options{'syntax'};
62 if ( $options{'type'} ) {
63 if ( $options{'type'} eq 'modalselect' ) {
64 $chunk->{'source'} = $options{'source'};
65 $chunk->{'exclusions'} = $options{'exclusions'} // "";
66 $chunk->{'inclusions'} = $options{'inclusions'} // "";
67 $chunk->{'required'} = $options{'required'} // "";
68 $chunk->{'type'} = 'modalselect';
69 } elsif ( $options{'type'} eq 'modaljs' ) {
70 $chunk->{'type'} = 'modaljs';
71 $chunk->{'initiator'} = $options{'initiator'};
72 $chunk->{'processor'} = $options{'processor'};
76 if ( $options{'class'} && $options{'class'} eq 'password' ) {
77 $chunk->{'input_type'} = 'password';
78 } elsif ( $options{'class'} && $options{'class'} eq 'email' ) {
79 $chunk->{'input_type'} = 'email';
80 } elsif ( $options{'class'} && $options{'class'} eq 'date' ) {
81 $chunk->{'dateinput'} = 1;
82 } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
83 my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
87 if ( $options{'type'} eq 'opac-languages' ) {
90 $theme = C4::Context->preference('opacthemes');
92 # this is the staff interface
93 $interface = 'intranet';
94 $theme = C4::Context->preference('template');
96 $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, undef, $current_languages );
97 $chunk->{'type'} = 'languages';
98 } elsif ( $options{ 'choices' } ) {
100 if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
101 if ( $options{'choices'} eq 'class-sources' ) {
102 my $sources = GetClassSources();
103 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
104 } elsif ( $options{'choices'} eq 'opac-templates' ) {
105 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
106 } elsif ( $options{'choices'} eq 'staff-templates' ) {
107 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
108 } elsif ( $options{choices} eq 'patron-categories' ) {
109 $options{choices} = { map { $_->categorycode => $_->description } Koha::Patron::Categories->search->as_list };
111 } elsif ( $options{'choices'} eq 'authval' ){
112 if( $options{'source'} ){
113 $options{'choices'} = { map { $_->authorised_value => $_->lib } Koha::AuthorisedValues->search( { category => $options{'source'} } )->as_list };
117 die 'Unrecognized source of preference values: ' . $options{'choices'};
123 $chunk->{'type'} = ( $options{class} && $options{class} eq 'multiple' ) ? 'multiple' : 'select';
126 @values = split /,/, $value if defined($value);
127 $chunk->{'CHOICES'} = [
128 sort { $a->{'text'} cmp $b->{'text'} }
132 text => $options{'choices'}->{$c},
135 grep { $_ eq $c || ( $c eq '' && ($value eq '0' || !$value ) ) } @values
139 keys %{ $options{'choices'} }
142 # Add a first blank value if needed
143 unshift @{ $chunk->{CHOICES} }, {
146 } if $add_blank && $chunk->{type} eq 'select';
148 } elsif ( $options{'multiple'} ) {
150 @values = split /,/, $value if defined($value);
151 $chunk->{type} = 'multiple';
152 $chunk->{CHOICES} = [
153 sort { $a->{'text'} cmp $b->{'text'} }
155 my $option_value = $_;
157 text => $options{multiple}->{$option_value},
158 value => $option_value,
159 selected => (grep { $_ eq $option_value } @values) ? 1 : 0,
162 keys %{ $options{multiple} }
164 } elsif ( $options{'multiple_sortable'} ) {
166 @values = split /,/, $value if defined($value);
167 $chunk->{type} = 'multiple_sortable';
168 my @options = sort keys %{ $options{multiple_sortable} };
169 $chunk->{CHOICES} = [
171 my $option_value = $_;
173 text => $options{multiple_sortable}->{$option_value},
174 value => $option_value,
175 selected => (grep { $_ eq $option_value } @values) ? 1 : 0,
178 uniq(@values, @options)
183 $chunk->{ 'type_' . $chunk->{'type'} } = 1;
188 sub TransformPrefsToHTML {
189 my ( $data, $searchfield ) = @_;
192 my $dbh = C4::Context->dbh;
193 my $title = ( keys( %$data ) )[0];
194 my $tab = $data->{ $title };
195 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
197 my @override_syspref_names;
198 if ( exists($ENV{OVERRIDE_SYSPREF_NAMES}) &&
199 defined($ENV{OVERRIDE_SYSPREF_NAMES})
201 @override_syspref_names = split /,/, $ENV{OVERRIDE_SYSPREF_NAMES};
204 foreach my $group ( sort keys %$tab ) {
206 push @lines, { is_group_title => 1, title => $group };
209 foreach my $line ( @{ $tab->{ $group } } ) {
214 foreach my $piece ( @$line ) {
215 if ( ref ( $piece ) eq 'HASH' ) {
216 my $name = $piece->{'pref'};
219 my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
221 if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
222 $value = $piece->{'default'};
224 $value = $row->{'value'};
226 my $chunk = _get_chunk( $value, %$piece );
228 # No highlighting of inputs yet, but would be useful
229 $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
231 if ( $name eq 'Pseudonymization' && ! C4::Context->config('bcrypt_settings')) {
232 push @warnings, 'bcrypt_config_not_set';
233 $chunk->{disabled} = 1 unless $value; # Let disable if enabled
235 push @chunks, $chunk;
237 my $name_entry = { name => $name };
238 if ( $searchfield ) {
239 if ( $name =~ /^$searchfield$/i ) {
240 $name_entry->{'jumped'} = 1;
241 } elsif ( $name =~ /$searchfield/i ) {
242 $name_entry->{'highlighted'} = 1;
245 $name_entry->{'overridden'} = 1 if ( any { $name eq $_ } @override_syspref_names );
247 push @names, $name_entry;
249 push @chunks, $piece;
253 my $version = Koha::version();
254 my ( $major, $minor, $maintenance, $development ) = split( '\.', $version );
256 $piece =~ s|__VERSION__|${major}_${minor}|g;
258 $piece =~ s|__VERSION__|main|g;
261 push @chunks, { type_text => 1, contents => $piece };
264 push @lines, { CHUNKS => \@chunks, NAMES => \@names, WARNINGS => \@warnings, is_group_title => 0 };
268 return $title, \@lines;
271 sub _get_pref_files {
272 my ( $input, $open_files ) = @_;
274 my ( $htdocs, $theme, $lang, undef ) = C4::Templates::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
278 foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
279 my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
281 $results{$tab} = $open_files ? IO::File->new( $file, 'r' ) : '';
288 my ( $input, $searchfield ) = @_;
291 my %tab_files = _get_pref_files( $input );
292 our @terms = split( /\s+/, $searchfield );
294 foreach my $tab_name ( sort keys %tab_files ) {
296 # Force list context to remove 'uninitialized value in goto' warn coming from YAML::Syck; note that the other GetTab call is in list context too. The actual cause however is the null value for the pref OpacRenewalBranch in opac.pref
297 my ($data) = GetTab( $input, $tab_name );
298 my $title = ( keys( %$data ) )[0];
299 my $tab = $data->{ $title };
300 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
304 while ( my ( $group_title, $contents ) = each %$tab ) {
305 if ( matches( $group_title, \@terms ) ) {
306 $matched_groups->{$group_title} = $contents;
312 foreach my $line ( @$contents ) {
315 foreach my $piece ( @$line ) {
316 if ( ref( $piece ) eq 'HASH' ) {
317 if ( !$piece->{'pref'} ){
320 if ( matches( $piece->{'pref'}, \@terms) ) {
322 } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_, \@terms ) } values( %{ $piece->{'choices'} } ) ) ) {
325 } elsif ( matches( $piece, \@terms ) ) {
328 last if ( $matched );
331 push @new_contents, $line if ( $matched );
334 $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
337 if ( $matched_groups ) {
338 my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
340 push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, tab_id => $tab_name };
348 my ( $text, $terms ) = @_;
352 my $re = eval{qr|$_|i};
353 $re = qr|\Q$_\E| if $@;
360 my $dbh = C4::Context->dbh;
361 our $input = CGI->new;
363 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
364 { template_name => "admin/preferences.tt",
367 flagsrequired => { parameters => 'manage_sysprefs' },
371 my $op = $input->param( 'op' ) || '';
372 my $tab = $input->param( 'tab' );
373 $tab ||= 'accounting'; # Ideally this should be "local-use" but preferences.pl
374 # does not presently support local use preferences
378 if ( $op eq 'cud-save' ) {
379 output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
380 foreach my $param ( $input->param() ) {
381 my ( $pref ) = ( $param =~ /pref_(.*)/ );
383 next if ( !defined( $pref ) );
385 my $value = join( ',', $input->param( $param ) );
387 C4::Context->set_preference( $pref, $value );
390 print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
396 if ( $op eq 'search' ) {
397 my $searchfield = $input->param( 'searchfield' );
399 $searchfield =~ s/\p{IsC}//g;
400 $searchfield =~ s/\s+/ /;
401 $searchfield =~ s/^\s+//;
402 $searchfield =~ s/\s+$//;
404 $template->param( searchfield => $searchfield );
406 @TABS = SearchPrefs( $input, $searchfield );
408 foreach my $tabh ( @TABS ) {
415 $tab = ''; # No need to load a particular tab, as we found results
416 $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
419 search_not_found => 1,
425 my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
427 push @TABS, { tab_title => $tab_title, LINES => $LINES, tab_id => $tab };
436 db_columns => Koha::Database::Columns->columns,
439 output_html_with_http_headers $input, $cookie, $template->output;