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>.
26 use C4::Languages qw(getTranslatedLanguages);
31 use Koha::Acquisition::Currencies;
36 use List::MoreUtils qw(any);
38 # use Smart::Comments;
42 my ( $input, $tab ) = @_;
44 my $tab_template = C4::Templates::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
46 my $active_currency = Koha::Acquisition::Currencies->get_active;
48 if ($active_currency) {
49 $local_currency = $active_currency->currency;
52 local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
55 return YAML::XS::Load( Encode::encode_utf8($tab_template->output()));
59 my ( $value, %options ) = @_;
61 my $name = $options{'pref'};
62 my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
63 if( $options{'syntax'} ){
64 $chunk->{'syntax'} = $options{'syntax'};
67 if( $options{'type'} && $options{'type'} eq 'modalselect' ){
68 $chunk->{'source'} = $options{'source'};
69 $chunk->{'exclusions'} = $options{'exclusions'} // "";
70 $chunk->{'type'} = 'modalselect';
73 if ( $options{'class'} && $options{'class'} eq 'password' ) {
74 $chunk->{'input_type'} = 'password';
75 } elsif ( $options{'class'} && $options{'class'} eq 'email' ) {
76 $chunk->{'input_type'} = 'email';
77 } elsif ( $options{'class'} && $options{'class'} eq 'date' ) {
78 $chunk->{'dateinput'} = 1;
79 } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
80 my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
84 if ( $options{'type'} eq 'opac-languages' ) {
87 $theme = C4::Context->preference('opacthemes');
89 # this is the staff interface
90 $interface = 'intranet';
91 $theme = C4::Context->preference('template');
93 $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, undef, $current_languages );
94 $chunk->{'type'} = 'languages';
95 } elsif ( $options{ 'choices' } ) {
97 if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
98 if ( $options{'choices'} eq 'class-sources' ) {
99 my $sources = GetClassSources();
100 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
101 } elsif ( $options{'choices'} eq 'opac-templates' ) {
102 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
103 } elsif ( $options{'choices'} eq 'staff-templates' ) {
104 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
105 } elsif ( $options{choices} eq 'patron-categories' ) {
106 $options{choices} = { map { $_->categorycode => $_->description } Koha::Patron::Categories->search };
109 die 'Unrecognized source of preference values: ' . $options{'choices'};
115 $chunk->{'type'} = ( $options{class} && $options{class} eq 'multiple' ) ? 'multiple' : 'select';
118 @values = split /,/, $value if defined($value);
119 $chunk->{'CHOICES'} = [
120 sort { $a->{'text'} cmp $b->{'text'} }
124 text => $options{'choices'}->{$c},
127 grep { $_ eq $c || ( $c eq '' && ($value eq '0' || !$value ) ) } @values
131 keys %{ $options{'choices'} }
134 # Add a first blank value if needed
135 unshift @{ $chunk->{CHOICES} }, {
138 } if $add_blank && $chunk->{type} eq 'select';
140 } elsif ( $options{'multiple'} ) {
142 @values = split /,/, $value if defined($value);
143 $chunk->{type} = 'multiple';
144 $chunk->{CHOICES} = [
145 sort { $a->{'text'} cmp $b->{'text'} }
147 my $option_value = $_;
149 text => $options{multiple}->{$option_value},
150 value => $option_value,
151 selected => (grep { $_ eq $option_value } @values) ? 1 : 0,
154 keys %{ $options{multiple} }
158 $chunk->{ 'type_' . $chunk->{'type'} } = 1;
163 sub TransformPrefsToHTML {
164 my ( $data, $searchfield ) = @_;
167 my $dbh = C4::Context->dbh;
168 my $title = ( keys( %$data ) )[0];
169 my $tab = $data->{ $title };
170 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
172 my @override_syspref_names;
173 if ( exists($ENV{OVERRIDE_SYSPREF_NAMES}) &&
174 defined($ENV{OVERRIDE_SYSPREF_NAMES})
176 @override_syspref_names = split /,/, $ENV{OVERRIDE_SYSPREF_NAMES};
179 foreach my $group ( sort keys %$tab ) {
181 push @lines, { is_group_title => 1, title => $group };
184 foreach my $line ( @{ $tab->{ $group } } ) {
188 foreach my $piece ( @$line ) {
189 if ( ref ( $piece ) eq 'HASH' ) {
190 my $name = $piece->{'pref'};
193 my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
195 if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
196 $value = $piece->{'default'};
198 $value = $row->{'value'};
200 my $chunk = _get_chunk( $value, %$piece );
202 # No highlighting of inputs yet, but would be useful
203 $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
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 );
216 push @names, $name_entry;
218 push @chunks, $piece;
222 my $version = Koha::version();
223 my ( $major, $minor, $maintenance, $development ) = split( '\.', $version );
225 $piece =~ s|__VERSION__|${major}_${minor}|g;
227 $piece =~ s|__VERSION__|master|g;
230 push @chunks, { type_text => 1, contents => $piece };
233 push @lines, { CHUNKS => \@chunks, NAMES => \@names, is_group_title => 0 };
237 return $title, \@lines;
240 sub _get_pref_files {
241 my ( $input, $open_files ) = @_;
243 my ( $htdocs, $theme, $lang, undef ) = C4::Templates::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
247 foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
248 my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
250 $results{$tab} = $open_files ? IO::File->new( $file, 'r' ) : '';
257 my ( $input, $searchfield ) = @_;
260 my %tab_files = _get_pref_files( $input );
261 our @terms = split( /\s+/, $searchfield );
263 foreach my $tab_name ( sort keys %tab_files ) {
265 # 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
266 my ($data) = GetTab( $input, $tab_name );
267 my $title = ( keys( %$data ) )[0];
268 my $tab = $data->{ $title };
269 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
273 while ( my ( $group_title, $contents ) = each %$tab ) {
274 if ( matches( $group_title, \@terms ) ) {
275 $matched_groups->{$group_title} = $contents;
281 foreach my $line ( @$contents ) {
284 foreach my $piece ( @$line ) {
285 if ( ref( $piece ) eq 'HASH' ) {
286 if ( !$piece->{'pref'} ){
289 if ( matches( $piece->{'pref'}, \@terms) ) {
291 } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_, \@terms ) } values( %{ $piece->{'choices'} } ) ) ) {
294 } elsif ( matches( $piece, \@terms ) ) {
297 last if ( $matched );
300 push @new_contents, $line if ( $matched );
303 $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
306 if ( $matched_groups ) {
307 my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
309 push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, tab_id => $tab_name };
317 my ( $text, $terms ) = @_;
321 my $re = eval{qr|$_|i};
322 $re = qr|\Q$_\E| if $@;
329 my $dbh = C4::Context->dbh;
330 our $input = CGI->new;
332 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
333 { template_name => "admin/preferences.tt",
336 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;