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 'date' ) {
76 $chunk->{'dateinput'} = 1;
77 } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
78 my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
82 if ( $options{'type'} eq 'opac-languages' ) {
85 $theme = C4::Context->preference('opacthemes');
87 # this is the staff interface
88 $interface = 'intranet';
89 $theme = C4::Context->preference('template');
91 $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, undef, $current_languages );
92 $chunk->{'type'} = 'languages';
93 } elsif ( $options{ 'choices' } ) {
95 if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
96 if ( $options{'choices'} eq 'class-sources' ) {
97 my $sources = GetClassSources();
98 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
99 } elsif ( $options{'choices'} eq 'opac-templates' ) {
100 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
101 } elsif ( $options{'choices'} eq 'staff-templates' ) {
102 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
103 } elsif ( $options{choices} eq 'patron-categories' ) {
104 $options{choices} = { map { $_->categorycode => $_->description } Koha::Patron::Categories->search };
107 die 'Unrecognized source of preference values: ' . $options{'choices'};
113 $chunk->{'type'} = ( $options{class} && $options{class} eq 'multiple' ) ? 'multiple' : 'select';
116 @values = split /,/, $value if defined($value);
117 $chunk->{'CHOICES'} = [
118 sort { $a->{'text'} cmp $b->{'text'} }
122 text => $options{'choices'}->{$c},
125 grep { $_ eq $c || ( $c eq '' && ($value eq '0' || !$value ) ) } @values
129 keys %{ $options{'choices'} }
132 # Add a first blank value if needed
133 unshift @{ $chunk->{CHOICES} }, {
136 } if $add_blank && $chunk->{type} eq 'select';
138 } elsif ( $options{'multiple'} ) {
140 @values = split /,/, $value if defined($value);
141 $chunk->{type} = 'multiple';
142 $chunk->{CHOICES} = [
143 sort { $a->{'text'} cmp $b->{'text'} }
145 my $option_value = $_;
147 text => $options{multiple}->{$option_value},
148 value => $option_value,
149 selected => (grep { $_ eq $option_value } @values) ? 1 : 0,
152 keys %{ $options{multiple} }
156 $chunk->{ 'type_' . $chunk->{'type'} } = 1;
161 sub TransformPrefsToHTML {
162 my ( $data, $searchfield ) = @_;
165 my $dbh = C4::Context->dbh;
166 my $title = ( keys( %$data ) )[0];
167 my $tab = $data->{ $title };
168 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
170 my @override_syspref_names;
171 if ( exists($ENV{OVERRIDE_SYSPREF_NAMES}) &&
172 defined($ENV{OVERRIDE_SYSPREF_NAMES})
174 @override_syspref_names = split /,/, $ENV{OVERRIDE_SYSPREF_NAMES};
177 foreach my $group ( sort keys %$tab ) {
179 push @lines, { is_group_title => 1, title => $group };
182 foreach my $line ( @{ $tab->{ $group } } ) {
186 foreach my $piece ( @$line ) {
187 if ( ref ( $piece ) eq 'HASH' ) {
188 my $name = $piece->{'pref'};
191 my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
193 if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
194 $value = $piece->{'default'};
196 $value = $row->{'value'};
198 my $chunk = _get_chunk( $value, %$piece );
200 # No highlighting of inputs yet, but would be useful
201 $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
203 push @chunks, $chunk;
205 my $name_entry = { name => $name };
206 if ( $searchfield ) {
207 if ( $name =~ /^$searchfield$/i ) {
208 $name_entry->{'jumped'} = 1;
209 } elsif ( $name =~ /$searchfield/i ) {
210 $name_entry->{'highlighted'} = 1;
213 $name_entry->{'overridden'} = 1 if ( any { $name eq $_ } @override_syspref_names );
214 push @names, $name_entry;
216 push @chunks, $piece;
220 my $version = Koha::version();
221 my ( $major, $minor, $maintenance, $development ) = split( '\.', $version );
223 $piece =~ s|__VERSION__|${major}_${minor}|g;
225 $piece =~ s|__VERSION__|master|g;
228 push @chunks, { type_text => 1, contents => $piece };
231 push @lines, { CHUNKS => \@chunks, NAMES => \@names, is_group_title => 0 };
235 return $title, \@lines;
238 sub _get_pref_files {
239 my ( $input, $open_files ) = @_;
241 my ( $htdocs, $theme, $lang, undef ) = C4::Templates::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
245 foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
246 my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
248 $results{$tab} = $open_files ? IO::File->new( $file, 'r' ) : '';
255 my ( $input, $searchfield ) = @_;
258 my %tab_files = _get_pref_files( $input );
259 our @terms = split( /\s+/, $searchfield );
261 foreach my $tab_name ( sort keys %tab_files ) {
263 # 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
264 my ($data) = GetTab( $input, $tab_name );
265 my $title = ( keys( %$data ) )[0];
266 my $tab = $data->{ $title };
267 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
271 while ( my ( $group_title, $contents ) = each %$tab ) {
272 if ( matches( $group_title, \@terms ) ) {
273 $matched_groups->{$group_title} = $contents;
279 foreach my $line ( @$contents ) {
282 foreach my $piece ( @$line ) {
283 if ( ref( $piece ) eq 'HASH' ) {
284 if ( !$piece->{'pref'} ){
287 if ( matches( $piece->{'pref'}, \@terms) ) {
289 } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_, \@terms ) } values( %{ $piece->{'choices'} } ) ) ) {
292 } elsif ( matches( $piece, \@terms ) ) {
295 last if ( $matched );
298 push @new_contents, $line if ( $matched );
301 $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
304 if ( $matched_groups ) {
305 my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
307 push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, tab_id => $tab_name };
315 my ( $text, $terms ) = @_;
319 my $re = eval{qr|$_|i};
320 $re = qr|\Q$_\E| if $@;
327 my $dbh = C4::Context->dbh;
328 our $input = CGI->new;
330 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
331 { template_name => "admin/preferences.tt",
334 flagsrequired => { parameters => 'manage_sysprefs' },
339 my $op = $input->param( 'op' ) || '';
340 my $tab = $input->param( 'tab' );
341 $tab ||= 'accounting'; # Ideally this should be "local-use" but preferences.pl
342 # does not presently support local use preferences
346 if ( $op eq 'save' ) {
347 foreach my $param ( $input->param() ) {
348 my ( $pref ) = ( $param =~ /pref_(.*)/ );
350 next if ( !defined( $pref ) );
352 my $value = join( ',', $input->param( $param ) );
354 C4::Context->set_preference( $pref, $value );
357 print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
363 if ( $op eq 'search' ) {
364 my $searchfield = $input->param( 'searchfield' );
366 $searchfield =~ s/\p{IsC}//g;
367 $searchfield =~ s/\s+/ /;
368 $searchfield =~ s/^\s+//;
369 $searchfield =~ s/\s+$//;
371 $template->param( searchfield => $searchfield );
373 @TABS = SearchPrefs( $input, $searchfield );
375 foreach my $tabh ( @TABS ) {
382 $tab = ''; # No need to load a particular tab, as we found results
383 $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
386 search_not_found => 1,
392 my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
394 push @TABS, { tab_title => $tab_title, LINES => $LINES, tab_id => $tab };
401 $template->param( TABS => \@TABS );
403 output_html_with_http_headers $input, $cookie, $template->output;