Bug 8945: Reports Help Files
[koha.git] / admin / preferences.pl
blobdb91e76e7da0c0f8db333818a828ba929da1b3bc
1 #!/usr/bin/perl
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
10 # version.
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.
20 use strict;
21 use warnings;
23 use CGI;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Koha;
27 use C4::Languages qw(getTranslatedLanguages);
28 use C4::ClassSource;
29 use C4::Log;
30 use C4::Output;
31 use C4::Templates;
32 use C4::Budgets qw(GetCurrency);
33 use File::Spec;
34 use IO::File;
35 use YAML::Syck qw();
36 $YAML::Syck::ImplicitTyping = 1;
37 our $lang;
39 # use Smart::Comments;
42 sub GetTab {
43 my ( $input, $tab ) = @_;
45 my $tab_template = C4::Templates::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
47 my $active_currency = GetCurrency();
48 my $local_currency;
49 if ($active_currency) {
50 $local_currency = $active_currency->{currency};
52 $tab_template->param(
53 local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
56 return YAML::Syck::Load( $tab_template->output() );
59 sub _get_chunk {
60 my ( $value, %options ) = @_;
62 my $name = $options{'pref'};
63 my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
65 if ( $options{'class'} && $options{'class'} eq 'password' ) {
66 $chunk->{'input_type'} = 'password';
67 } elsif ( $options{'class'} && $options{'class'} eq 'date' ) {
68 $chunk->{'dateinput'} = 1;
69 } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
70 my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
72 my $theme;
73 my $interface;
74 if ( $options{'type'} eq 'opac-languages' ) {
75 # this is the OPAC
76 $interface = 'opac';
77 $theme = C4::Context->preference('opacthemes');
78 } else {
79 # this is the staff client
80 $interface = 'intranet';
81 $theme = C4::Context->preference('template');
83 $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, $lang, $current_languages );
84 $chunk->{'type'} = 'languages';
85 } elsif ( $options{ 'choices' } ) {
86 if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
87 if ( $options{'choices'} eq 'class-sources' ) {
88 my $sources = GetClassSources();
89 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
90 } elsif ( $options{'choices'} eq 'opac-templates' ) {
91 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
92 } elsif ( $options{'choices'} eq 'staff-templates' ) {
93 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
94 } else {
95 die 'Unrecognized source of preference values: ' . $options{'choices'};
99 $value ||= 0;
101 $chunk->{'type'} = 'select';
102 $chunk->{'CHOICES'} = [
103 sort { $a->{'text'} cmp $b->{'text'} }
104 map { { text => $options{'choices'}->{$_}, value => $_, selected => ( $_ eq $value || ( $_ eq '' && ( $value eq '0' || !$value ) ) ) } }
105 keys %{ $options{'choices'} }
109 $chunk->{ 'type_' . $chunk->{'type'} } = 1;
111 return $chunk;
114 sub TransformPrefsToHTML {
115 my ( $data, $searchfield ) = @_;
117 my @lines;
118 my $dbh = C4::Context->dbh;
119 my $title = ( keys( %$data ) )[0];
120 my $tab = $data->{ $title };
121 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
123 foreach my $group ( sort keys %$tab ) {
124 if ( $group ) {
125 push @lines, { is_group_title => 1, title => $group };
128 foreach my $line ( @{ $tab->{ $group } } ) {
129 my @chunks;
130 my @names;
132 foreach my $piece ( @$line ) {
133 if ( ref ( $piece ) eq 'HASH' ) {
134 my $name = $piece->{'pref'};
136 if ( $name ) {
137 my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
138 my $value;
139 if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
140 $value = $piece->{'default'};
141 } else {
142 $value = $row->{'value'};
144 my $chunk = _get_chunk( $value, %$piece );
146 # No highlighting of inputs yet, but would be useful
147 $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
149 push @chunks, $chunk;
151 my $name_entry = { name => $name };
152 if ( $searchfield ) {
153 if ( $name =~ /^$searchfield$/i ) {
154 $name_entry->{'jumped'} = 1;
155 } elsif ( $name =~ /$searchfield/i ) {
156 $name_entry->{'highlighted'} = 1;
159 push @names, $name_entry;
160 } else {
161 push @chunks, $piece;
163 } else {
164 push @chunks, { type_text => 1, contents => $piece };
168 push @lines, { CHUNKS => \@chunks, NAMES => \@names, is_group_title => 0 };
172 return $title, \@lines;
175 sub _get_pref_files {
176 my ( $input, $open_files ) = @_;
178 my ( $htdocs, $theme, $lang, undef ) = C4::Templates::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
180 my %results;
182 foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
183 my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
185 $results{$tab} = $open_files ? new IO::File( $file, 'r' ) : '';
188 return %results;
191 sub SearchPrefs {
192 my ( $input, $searchfield ) = @_;
193 my @tabs;
195 my %tab_files = _get_pref_files( $input );
196 our @terms = split( /\s+/, $searchfield );
198 foreach my $tab_name ( keys %tab_files ) {
199 my $data = GetTab( $input, $tab_name );
200 my $title = ( keys( %$data ) )[0];
201 my $tab = $data->{ $title };
202 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
204 my $matched_groups;
206 while ( my ( $group_title, $contents ) = each %$tab ) {
207 if ( matches( $group_title, \@terms ) ) {
208 $matched_groups->{$group_title} = $contents;
209 next;
212 my @new_contents;
214 foreach my $line ( @$contents ) {
215 my $matched;
217 foreach my $piece ( @$line ) {
218 if ( ref( $piece ) eq 'HASH' ) {
219 if ( !$piece->{'pref'} ){ next; }
220 if ( $piece->{'pref'} =~ /^$searchfield$/i ) {
221 my ( undef, $LINES ) = TransformPrefsToHTML( $data, $searchfield );
223 return { search_jumped => 1, tab => $tab_name, tab_title => $title, LINES => $LINES };
224 } elsif ( matches( $piece->{'pref'}, \@terms) ) {
225 $matched = 1;
226 } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_, \@terms ) } values( %{ $piece->{'choices'} } ) ) ) {
227 $matched = 1;
229 } elsif ( matches( $piece, \@terms ) ) {
230 $matched = 1;
232 last if ( $matched );
235 push @new_contents, $line if ( $matched );
238 $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
241 if ( $matched_groups ) {
242 my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
244 push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, };
248 return @tabs;
251 sub matches {
252 my ( $text, $terms ) = @_;
253 if ( $text ) { return !grep( { $text !~ /$_/i } @$terms ); }
256 my $dbh = C4::Context->dbh;
257 our $input = new CGI;
259 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
260 { template_name => "admin/preferences.tmpl",
261 query => $input,
262 type => "intranet",
263 authnotrequired => 0,
264 flagsrequired => { parameters => 'parameters_remaining_permissions' },
265 debug => 1,
269 $lang = $template->param( 'lang' );
270 my $op = $input->param( 'op' ) || '';
271 my $tab = $input->param( 'tab' );
272 $tab ||= 'acquisitions'; # Ideally this should be "local-use" but preferences.pl
273 # does not presently support local use preferences
275 my $highlighted;
277 if ( $op eq 'save' ) {
278 unless ( C4::Context->config( 'demo' ) ) {
279 foreach my $param ( $input->param() ) {
280 my ( $pref ) = ( $param =~ /pref_(.*)/ );
282 next if ( !defined( $pref ) );
284 my $value = join( ',', $input->param( $param ) );
286 C4::Context->set_preference( $pref, $value );
287 logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $pref . " | " . $value );
291 print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
292 exit;
295 my @TABS;
297 if ( $op eq 'search' ) {
298 my $searchfield = $input->param( 'searchfield' );
300 $searchfield =~ s/\p{IsC}//g;
301 $searchfield =~ s/\s+/ /;
302 $searchfield =~ s/^\s+//;
303 $searchfield =~ s/\s+$//;
305 $template->param( searchfield => $searchfield );
307 @TABS = SearchPrefs( $input, $searchfield );
309 foreach my $tabh ( @TABS ) {
310 $template->param(
311 $tabh->{'tab'} => 1
315 if ( @TABS ) {
316 $tab = ''; # No need to load a particular tab, as we found results
317 $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
318 } else {
319 $template->param(
320 search_not_found => 1,
325 if ( $tab ) {
326 my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
328 push @TABS, { tab_title => $tab_title, LINES => $LINES };
329 $template->param(
330 $tab => 1,
331 tab => $tab,
335 $template->param( TABS => \@TABS );
337 output_html_with_http_headers $input, $cookie, $template->output;