Bug 9302: Use patron-title.inc
[koha.git] / admin / preferences.pl
blob1e12b1e9a075cbf220a2617329f01958ca7daa1e
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
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>.
20 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Auth;
24 use C4::Context;
25 use C4::Koha;
26 use C4::Languages qw(getTranslatedLanguages);
27 use C4::ClassSource;
28 use C4::Log;
29 use C4::Output;
30 use C4::Templates;
31 use Koha::Acquisition::Currencies;
32 use File::Spec;
33 use IO::File;
34 use YAML::Syck qw();
35 use List::MoreUtils qw(any);
36 $YAML::Syck::ImplicitTyping = 1;
37 $YAML::Syck::ImplicitUnicode = 1;
38 our $lang;
40 # use Smart::Comments;
43 sub GetTab {
44 my ( $input, $tab ) = @_;
46 my $tab_template = C4::Templates::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
48 my $active_currency = Koha::Acquisition::Currencies->get_active;
49 my $local_currency;
50 if ($active_currency) {
51 $local_currency = $active_currency->currency;
53 $tab_template->param(
54 local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
57 return YAML::Syck::Load( $tab_template->output() );
60 sub _get_chunk {
61 my ( $value, %options ) = @_;
63 my $name = $options{'pref'};
64 my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
66 if ( $options{'class'} && $options{'class'} eq 'password' ) {
67 $chunk->{'input_type'} = 'password';
68 } elsif ( $options{'class'} && $options{'class'} eq 'date' ) {
69 $chunk->{'dateinput'} = 1;
70 } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
71 my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
73 my $theme;
74 my $interface;
75 if ( $options{'type'} eq 'opac-languages' ) {
76 # this is the OPAC
77 $interface = 'opac';
78 $theme = C4::Context->preference('opacthemes');
79 } else {
80 # this is the staff client
81 $interface = 'intranet';
82 $theme = C4::Context->preference('template');
84 $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, $lang, $current_languages );
85 $chunk->{'type'} = 'languages';
86 } elsif ( $options{ 'choices' } ) {
87 if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
88 if ( $options{'choices'} eq 'class-sources' ) {
89 my $sources = GetClassSources();
90 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
91 } elsif ( $options{'choices'} eq 'opac-templates' ) {
92 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
93 } elsif ( $options{'choices'} eq 'staff-templates' ) {
94 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
95 } else {
96 die 'Unrecognized source of preference values: ' . $options{'choices'};
100 $value ||= 0;
102 $chunk->{'type'} = 'select';
103 $chunk->{'CHOICES'} = [
104 sort { $a->{'text'} cmp $b->{'text'} }
105 map { { text => $options{'choices'}->{$_}, value => $_, selected => ( $_ eq $value || ( $_ eq '' && ( $value eq '0' || !$value ) ) ) } }
106 keys %{ $options{'choices'} }
108 } elsif ( $options{'multiple'} ) {
109 my @values;
110 @values = split /,/, $value if defined($value);
111 $chunk->{type} = 'multiple';
112 $chunk->{CHOICES} = [
113 sort { $a->{'text'} cmp $b->{'text'} }
114 map {
115 my $option_value = $_;
117 text => $options{multiple}->{$option_value},
118 value => $option_value,
119 selected => (grep /^$option_value$/, @values) ? 1 : 0,
122 keys %{ $options{multiple} }
126 $chunk->{ 'type_' . $chunk->{'type'} } = 1;
128 return $chunk;
131 sub TransformPrefsToHTML {
132 my ( $data, $searchfield ) = @_;
134 my @lines;
135 my $dbh = C4::Context->dbh;
136 my $title = ( keys( %$data ) )[0];
137 my $tab = $data->{ $title };
138 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
140 my @override_syspref_names;
141 if ( exists($ENV{OVERRIDE_SYSPREF_NAMES}) &&
142 defined($ENV{OVERRIDE_SYSPREF_NAMES})
144 @override_syspref_names = split /,/, $ENV{OVERRIDE_SYSPREF_NAMES};
147 foreach my $group ( sort keys %$tab ) {
148 if ( $group ) {
149 push @lines, { is_group_title => 1, title => $group };
152 foreach my $line ( @{ $tab->{ $group } } ) {
153 my @chunks;
154 my @names;
156 foreach my $piece ( @$line ) {
157 if ( ref ( $piece ) eq 'HASH' ) {
158 my $name = $piece->{'pref'};
160 if ( $name ) {
161 my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
162 my $value;
163 if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
164 $value = $piece->{'default'};
165 } else {
166 $value = $row->{'value'};
168 my $chunk = _get_chunk( $value, %$piece );
170 # No highlighting of inputs yet, but would be useful
171 $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
173 push @chunks, $chunk;
175 my $name_entry = { name => $name };
176 if ( $searchfield ) {
177 if ( $name =~ /^$searchfield$/i ) {
178 $name_entry->{'jumped'} = 1;
179 } elsif ( $name =~ /$searchfield/i ) {
180 $name_entry->{'highlighted'} = 1;
183 $name_entry->{'overridden'} = 1 if ( any { $name eq $_ } @override_syspref_names );
184 push @names, $name_entry;
185 } else {
186 push @chunks, $piece;
188 } else {
189 if ( $piece ) {
190 my $version = Koha::version();
191 my ( $major, $minor, $maintenance, $development ) = split( '\.', $version );
192 if ( $minor % 2 ) {
193 $piece =~ s|__VERSION__|${major}_${minor}|g;
194 } else {
195 $piece =~ s|__VERSION__|master|g;
198 push @chunks, { type_text => 1, contents => $piece };
201 push @lines, { CHUNKS => \@chunks, NAMES => \@names, is_group_title => 0 };
205 return $title, \@lines;
208 sub _get_pref_files {
209 my ( $input, $open_files ) = @_;
211 my ( $htdocs, $theme, $lang, undef ) = C4::Templates::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
213 my %results;
215 foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
216 my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
218 $results{$tab} = $open_files ? new IO::File( $file, 'r' ) : '';
221 return %results;
224 sub SearchPrefs {
225 my ( $input, $searchfield ) = @_;
226 my @tabs;
228 my %tab_files = _get_pref_files( $input );
229 our @terms = split( /\s+/, $searchfield );
231 foreach my $tab_name ( keys %tab_files ) {
232 my $data = GetTab( $input, $tab_name );
233 my $title = ( keys( %$data ) )[0];
234 my $tab = $data->{ $title };
235 $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
237 my $matched_groups;
239 while ( my ( $group_title, $contents ) = each %$tab ) {
240 if ( matches( $group_title, \@terms ) ) {
241 $matched_groups->{$group_title} = $contents;
242 next;
245 my @new_contents;
247 foreach my $line ( @$contents ) {
248 my $matched;
250 foreach my $piece ( @$line ) {
251 if ( ref( $piece ) eq 'HASH' ) {
252 if ( !$piece->{'pref'} ){
253 next;
255 if ( matches( $piece->{'pref'}, \@terms) ) {
256 $matched = 1;
257 } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_, \@terms ) } values( %{ $piece->{'choices'} } ) ) ) {
258 $matched = 1;
260 } elsif ( matches( $piece, \@terms ) ) {
261 $matched = 1;
263 last if ( $matched );
266 push @new_contents, $line if ( $matched );
269 $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
272 if ( $matched_groups ) {
273 my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
275 push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, tab_id => $tab_name };
279 return @tabs;
282 sub matches {
283 my ( $text, $terms ) = @_;
284 if ( $text ) {
285 return !grep(
287 my $re = eval{qr|$_|i};
288 $re = qr|\Q$_\E| if $@;
289 $text !~ m|$re|;
290 } @$terms
295 my $dbh = C4::Context->dbh;
296 our $input = new CGI;
298 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
299 { template_name => "admin/preferences.tt",
300 query => $input,
301 type => "intranet",
302 authnotrequired => 0,
303 flagsrequired => { parameters => 'parameters_remaining_permissions' },
304 debug => 1,
308 $lang = $template->param( 'lang' );
309 my $op = $input->param( 'op' ) || '';
310 my $tab = $input->param( 'tab' );
311 $tab ||= 'acquisitions'; # Ideally this should be "local-use" but preferences.pl
312 # does not presently support local use preferences
314 my $highlighted;
316 if ( $op eq 'save' ) {
317 foreach my $param ( $input->param() ) {
318 my ( $pref ) = ( $param =~ /pref_(.*)/ );
320 next if ( !defined( $pref ) );
322 my $value = join( ',', $input->param( $param ) );
324 C4::Context->set_preference( $pref, $value );
327 print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
328 exit;
331 my @TABS;
333 if ( $op eq 'search' ) {
334 my $searchfield = $input->param( 'searchfield' );
336 $searchfield =~ s/\p{IsC}//g;
337 $searchfield =~ s/\s+/ /;
338 $searchfield =~ s/^\s+//;
339 $searchfield =~ s/\s+$//;
341 $template->param( searchfield => $searchfield );
343 @TABS = SearchPrefs( $input, $searchfield );
345 foreach my $tabh ( @TABS ) {
346 $template->param(
347 $tabh->{'tab'} => 1
351 if ( @TABS ) {
352 $tab = ''; # No need to load a particular tab, as we found results
353 $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
354 } else {
355 $template->param(
356 search_not_found => 1,
361 if ( $tab ) {
362 my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
364 push @TABS, { tab_title => $tab_title, LINES => $LINES, tab_id => $tab };
365 $template->param(
366 $tab => 1,
367 tab => $tab,
371 $template->param( TABS => \@TABS );
373 output_html_with_http_headers $input, $cookie, $template->output;