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
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.
27 use C4
::Languages
qw(getTranslatedLanguages);
31 use C4
::Budgets
qw(GetCurrency);
35 $YAML::Syck
::ImplicitTyping
= 1;
38 # use Smart::Comments;
42 my ( $input, $tab ) = @_;
44 my $tab_template = C4
::Output
::gettemplate
( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
46 my $active_currency = GetCurrency
();
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
::Syck
::Load
( $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'} };
64 if ( $options{'class'} && $options{'class'} eq 'password' ) {
65 $chunk->{'input_type'} = 'password';
66 } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
67 my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
71 if ( $options{'type'} eq 'opac-languages' ) {
74 $theme = C4
::Context
->preference('opacthemes');
76 # this is the staff client
77 $interface = 'intranet';
78 $theme = C4
::Context
->preference('template');
80 $chunk->{'languages'} = getTranslatedLanguages
( $interface, $theme, $lang, $current_languages );
81 $chunk->{'type'} = 'languages';
82 } elsif ( $options{ 'choices' } ) {
83 if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
84 if ( $options{'choices'} eq 'class-sources' ) {
85 my $sources = GetClassSources
();
86 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
87 } elsif ( $options{'choices'} eq 'opac-templates' ) {
88 $options{'choices'} = { map { $_ => $_ } getallthemes
( 'opac' ) }
89 } elsif ( $options{'choices'} eq 'staff-templates' ) {
90 $options{'choices'} = { map { $_ => $_ } getallthemes
( 'intranet' ) }
92 die 'Unrecognized source of preference values: ' . $options{'choices'};
98 $chunk->{'type'} = 'select';
99 $chunk->{'CHOICES'} = [
100 sort { $a->{'text'} cmp $b->{'text'} }
101 map { { text
=> $options{'choices'}->{$_}, value
=> $_, selected
=> ( $_ eq $value || ( $_ eq '' && ( $value eq '0' || !$value ) ) ) } }
102 keys %{ $options{'choices'} }
106 $chunk->{ 'type_' . $chunk->{'type'} } = 1;
111 sub TransformPrefsToHTML
{
112 my ( $data, $searchfield ) = @_;
115 my $dbh = C4
::Context
->dbh;
116 my $title = ( keys( %$data ) )[0];
117 my $tab = $data->{ $title };
118 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
120 foreach my $group ( sort keys %$tab ) {
122 push @lines, { is_group_title
=> 1, title
=> $group };
125 foreach my $line ( @
{ $tab->{ $group } } ) {
129 foreach my $piece ( @
$line ) {
130 if ( ref ( $piece ) eq 'HASH' ) {
131 my $name = $piece->{'pref'};
134 my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
136 if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
137 $value = $piece->{'default'};
139 $value = $row->{'value'};
141 my $chunk = _get_chunk
( $value, %$piece );
143 # No highlighting of inputs yet, but would be useful
144 $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
146 push @chunks, $chunk;
148 my $name_entry = { name
=> $name };
149 if ( $searchfield ) {
150 if ( $name =~ /^$searchfield$/i ) {
151 $name_entry->{'jumped'} = 1;
152 } elsif ( $name =~ /$searchfield/i ) {
153 $name_entry->{'highlighted'} = 1;
156 push @names, $name_entry;
158 push @chunks, $piece;
161 push @chunks, { type_text
=> 1, contents
=> $piece };
165 push @lines, { CHUNKS
=> \
@chunks, NAMES
=> \
@names };
169 return $title, \
@lines;
172 sub _get_pref_files
{
173 my ( $input, $open_files ) = @_;
175 my ( $htdocs, $theme, $lang, undef ) = C4
::Output
::_get_template_file
( 'admin/preferences/admin.pref', 'intranet', $input );
179 foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
180 my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
182 $results{$tab} = $open_files ? new IO
::File
( $file, 'r' ) : '';
189 my ( $input, $searchfield ) = @_;
192 my %tab_files = _get_pref_files
( $input );
193 our @terms = split( /\s+/, $searchfield );
198 return !grep( { $text !~ /$_/i } @terms );
201 foreach my $tab_name ( keys %tab_files ) {
202 my $data = GetTab
( $input, $tab_name );
203 my $title = ( keys( %$data ) )[0];
204 my $tab = $data->{ $title };
205 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
209 while ( my ( $group_title, $contents ) = each %$tab ) {
210 if ( matches
( $group_title ) ) {
211 $matched_groups->{$group_title} = $contents;
217 foreach my $line ( @
$contents ) {
220 foreach my $piece ( @
$line ) {
221 if ( ref( $piece ) eq 'HASH' ) {
222 if ( $piece->{'pref'} =~ /^$searchfield$/i ) {
223 my ( undef, $LINES ) = TransformPrefsToHTML
( $data, $searchfield );
225 return { search_jumped
=> 1, tab
=> $tab_name, tab_title
=> $title, LINES
=> $LINES };
226 } elsif ( matches
( $piece->{'pref'} ) ) {
228 } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches
( $_ ) } values( %{ $piece->{'choices'} } ) ) ) {
231 } elsif ( matches
( $piece ) ) {
234 last if ( $matched );
237 push @new_contents, $line if ( $matched );
240 $matched_groups->{$group_title} = \
@new_contents if ( @new_contents );
243 if ( $matched_groups ) {
244 my ( $title, $LINES ) = TransformPrefsToHTML
( { $title => $matched_groups }, $searchfield );
246 push @tabs, { tab
=> $tab, tab_title
=> $title, LINES
=> $LINES, };
253 my $dbh = C4
::Context
->dbh;
254 our $input = new CGI
;
256 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
257 { template_name
=> "admin/preferences.tmpl",
260 authnotrequired
=> 0,
261 flagsrequired
=> { parameters
=> 1 },
266 $lang = $template->param( 'lang' );
267 my $op = $input->param( 'op' ) || '';
268 my $tab = $input->param( 'tab' );
269 $tab ||= 'local-use';
273 if ( $op eq 'save' ) {
274 unless ( C4
::Context
->config( 'demo' ) ) {
275 foreach my $param ( $input->param() ) {
276 my ( $pref ) = ( $param =~ /pref_(.*)/ );
278 next if ( !defined( $pref ) );
280 my $value = join( ',', $input->param( $param ) );
282 C4
::Context
->set_preference( $pref, $value );
283 logaction
( 'SYSTEMPREFERENCE', 'MODIFY', undef, $pref . " | " . $value );
287 print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
293 if ( $op eq 'search' ) {
294 my $searchfield = $input->param( 'searchfield' );
296 $searchfield =~ s/[^a-zA-Z0-9_ -]//g;
298 $template->param( searchfield
=> $searchfield );
300 @TABS = SearchPrefs
( $input, $searchfield );
302 foreach my $tabh ( @TABS ) {
309 $tab = ''; # No need to load a particular tab, as we found results
310 $template->param( search_jumped
=> 1 ) if ( $TABS[0]->{'search_jumped'} );
313 search_not_found
=> 1,
319 my ( $tab_title, $LINES ) = TransformPrefsToHTML
( GetTab
( $input, $tab ), $highlighted );
321 push @TABS, { tab_title
=> $tab_title, LINES
=> $LINES };
328 $template->param( TABS
=> \
@TABS );
330 output_html_with_http_headers
$input, $cookie, $template->output;