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 );
30 use Koha::Acquisition::Currencies;
34 use List::MoreUtils qw( any );
37 my ( $input, $tab ) = @_;
39 my $tab_template = C4::Templates::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
41 my $active_currency = Koha::Acquisition::Currencies->get_active;
43 if ($active_currency) {
44 $local_currency = $active_currency->currency;
47 local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
50 return YAML::XS::Load( Encode::encode_utf8($tab_template->output()));
54 my ( $value, %options ) = @_;
56 my $name = $options{'pref'};
57 my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
58 if( $options{'syntax'} ){
59 $chunk->{'syntax'} = $options{'syntax'};
62 if( $options{'type'} && $options{'type'} eq 'modalselect' ){
63 $chunk->{'source'} = $options{'source'};
64 $chunk->{'exclusions'} = $options{'exclusions'} // "";
65 $chunk->{'type'} = 'modalselect';
68 if ( $options{'class'} && $options{'class'} eq 'password' ) {
69 $chunk->{'input_type'} = 'password';
70 } elsif ( $options{'class'} && $options{'class'} eq 'email' ) {
71 $chunk->{'input_type'} = 'email';
72 } elsif ( $options{'class'} && $options{'class'} eq 'date' ) {
73 $chunk->{'dateinput'} = 1;
74 } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
75 my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
79 if ( $options{'type'} eq 'opac-languages' ) {
82 $theme = C4::Context->preference('opacthemes');
84 # this is the staff interface
85 $interface = 'intranet';
86 $theme = C4::Context->preference('template');
88 $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, undef, $current_languages );
89 $chunk->{'type'} = 'languages';
90 } elsif ( $options{ 'choices' } ) {
92 if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
93 if ( $options{'choices'} eq 'class-sources' ) {
94 my $sources = GetClassSources();
95 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
96 } elsif ( $options{'choices'} eq 'opac-templates' ) {
97 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
98 } elsif ( $options{'choices'} eq 'staff-templates' ) {
99 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
100 } elsif ( $options{choices} eq 'patron-categories' ) {
101 $options{choices} = { map { $_->categorycode => $_->description } Koha::Patron::Categories->search };
104 die 'Unrecognized source of preference values: ' . $options{'choices'};
110 $chunk->{'type'} = ( $options{class} && $options{class} eq 'multiple' ) ? 'multiple' : 'select';
113 @values = split /,/, $value if defined($value);
114 $chunk->{'CHOICES'} = [
115 sort { $a->{'text'} cmp $b->{'text'} }
119 text => $options{'choices'}->{$c},
122 grep { $_ eq $c || ( $c eq '' && ($value eq '0' || !$value ) ) } @values
126 keys %{ $options{'choices'} }
129 # Add a first blank value if needed
130 unshift @{ $chunk->{CHOICES} }, {
133 } if $add_blank && $chunk->{type} eq 'select';
135 } elsif ( $options{'multiple'} ) {
137 @values = split /,/, $value if defined($value);
138 $chunk->{type} = 'multiple';
139 $chunk->{CHOICES} = [
140 sort { $a->{'text'} cmp $b->{'text'} }
142 my $option_value = $_;
144 text => $options{multiple}->{$option_value},
145 value => $option_value,
146 selected => (grep { $_ eq $option_value } @values) ? 1 : 0,
149 keys %{ $options{multiple} }
153 $chunk->{ 'type_' . $chunk->{'type'} } = 1;
158 sub TransformPrefsToHTML {
159 my ( $data, $searchfield ) = @_;
162 my $dbh = C4::Context->dbh;
163 my $title = ( keys( %$data ) )[0];
164 my $tab = $data->{ $title };
165 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
167 my @override_syspref_names;
168 if ( exists($ENV{OVERRIDE_SYSPREF_NAMES}) &&
169 defined($ENV{OVERRIDE_SYSPREF_NAMES})
171 @override_syspref_names = split /,/, $ENV{OVERRIDE_SYSPREF_NAMES};
174 foreach my $group ( sort keys %$tab ) {
176 push @lines, { is_group_title => 1, title => $group };
179 foreach my $line ( @{ $tab->{ $group } } ) {
184 foreach my $piece ( @$line ) {
185 if ( ref ( $piece ) eq 'HASH' ) {
186 my $name = $piece->{'pref'};
189 my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
191 if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
192 $value = $piece->{'default'};
194 $value = $row->{'value'};
196 my $chunk = _get_chunk( $value, %$piece );
198 # No highlighting of inputs yet, but would be useful
199 $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
201 if ( $name eq 'Pseudonymization' && ! C4::Context->config('bcrypt_settings')) {
202 push @warnings, 'bcrypt_config_not_set';
203 $chunk->{disabled} = 1 unless $value; # Let disable if enabled
205 push @chunks, $chunk;
207 my $name_entry = { name => $name };
208 if ( $searchfield ) {
209 if ( $name =~ /^$searchfield$/i ) {
210 $name_entry->{'jumped'} = 1;
211 } elsif ( $name =~ /$searchfield/i ) {
212 $name_entry->{'highlighted'} = 1;
215 $name_entry->{'overridden'} = 1 if ( any { $name eq $_ } @override_syspref_names );
217 push @names, $name_entry;
219 push @chunks, $piece;
223 my $version = Koha::version();
224 my ( $major, $minor, $maintenance, $development ) = split( '\.', $version );
226 $piece =~ s|__VERSION__|${major}_${minor}|g;
228 $piece =~ s|__VERSION__|master|g;
231 push @chunks, { type_text => 1, contents => $piece };
234 push @lines, { CHUNKS => \@chunks, NAMES => \@names, WARNINGS => \@warnings, is_group_title => 0 };
238 return $title, \@lines;
241 sub _get_pref_files {
242 my ( $input, $open_files ) = @_;
244 my ( $htdocs, $theme, $lang, undef ) = C4::Templates::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
248 foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
249 my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
251 $results{$tab} = $open_files ? IO::File->new( $file, 'r' ) : '';
258 my ( $input, $searchfield ) = @_;
261 my %tab_files = _get_pref_files( $input );
262 our @terms = split( /\s+/, $searchfield );
264 foreach my $tab_name ( sort keys %tab_files ) {
266 # 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
267 my ($data) = GetTab( $input, $tab_name );
268 my $title = ( keys( %$data ) )[0];
269 my $tab = $data->{ $title };
270 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
274 while ( my ( $group_title, $contents ) = each %$tab ) {
275 if ( matches( $group_title, \@terms ) ) {
276 $matched_groups->{$group_title} = $contents;
282 foreach my $line ( @$contents ) {
285 foreach my $piece ( @$line ) {
286 if ( ref( $piece ) eq 'HASH' ) {
287 if ( !$piece->{'pref'} ){
290 if ( matches( $piece->{'pref'}, \@terms) ) {
292 } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_, \@terms ) } values( %{ $piece->{'choices'} } ) ) ) {
295 } elsif ( matches( $piece, \@terms ) ) {
298 last if ( $matched );
301 push @new_contents, $line if ( $matched );
304 $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
307 if ( $matched_groups ) {
308 my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
310 push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, tab_id => $tab_name };
318 my ( $text, $terms ) = @_;
322 my $re = eval{qr|$_|i};
323 $re = qr|\Q$_\E| if $@;
330 my $dbh = C4::Context->dbh;
331 our $input = CGI->new;
333 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
334 { template_name => "admin/preferences.tt",
337 flagsrequired => { parameters => 'manage_sysprefs' },
341 my $op = $input->param( 'op' ) || '';
342 my $tab = $input->param( 'tab' );
343 $tab ||= 'accounting'; # Ideally this should be "local-use" but preferences.pl
344 # does not presently support local use preferences
348 if ( $op eq 'save' ) {
349 foreach my $param ( $input->param() ) {
350 my ( $pref ) = ( $param =~ /pref_(.*)/ );
352 next if ( !defined( $pref ) );
354 my $value = join( ',', $input->param( $param ) );
356 C4::Context->set_preference( $pref, $value );
359 print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
365 if ( $op eq 'search' ) {
366 my $searchfield = $input->param( 'searchfield' );
368 $searchfield =~ s/\p{IsC}//g;
369 $searchfield =~ s/\s+/ /;
370 $searchfield =~ s/^\s+//;
371 $searchfield =~ s/\s+$//;
373 $template->param( searchfield => $searchfield );
375 @TABS = SearchPrefs( $input, $searchfield );
377 foreach my $tabh ( @TABS ) {
384 $tab = ''; # No need to load a particular tab, as we found results
385 $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
388 search_not_found => 1,
394 my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
396 push @TABS, { tab_title => $tab_title, LINES => $LINES, tab_id => $tab };
403 $template->param( TABS => \@TABS );
405 output_html_with_http_headers $input, $cookie, $template->output;