fix silly typo
[koha.git] / admin / preferences.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2009 Jesse Weaver and the Koha Dev Team
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use warnings;
22
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::Budgets qw(GetCurrency);
32 use File::Spec;
33 use IO::File;
34 use YAML::Syck qw();
35 $YAML::Syck::ImplicitTyping = 1;
36 our $lang;
37
38 # use Smart::Comments;
39 #
40
41 sub GetTab {
42     my ( $input, $tab ) = @_;
43
44     my $tab_template = C4::Output::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
45
46     $tab_template->param(
47         local_currency => GetCurrency()->{'currency'}, # currency code is used, because we do not know how a given currency is formatted.
48     );
49
50     return YAML::Syck::Load( $tab_template->output() );
51 }
52
53 sub _get_chunk {
54     my ( $value, %options ) = @_;
55
56     my $name = $options{'pref'};
57     my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
58
59     if ( $options{'class'} && $options{'class'} eq 'password' ) {
60         $chunk->{'input_type'} = 'password';
61     } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
62         my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
63
64         my $theme;
65         my $interface;
66         if ( $options{'type'} eq 'opac-languages' ) {
67             # this is the OPAC
68             $interface = 'opac';
69             $theme     = C4::Context->preference('opacthemes');
70         } else {
71             # this is the staff client
72             $interface = 'intranet';
73             $theme     = C4::Context->preference('template');
74         }
75         $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, $lang, $current_languages );
76         $chunk->{'type'} = 'languages';
77     } elsif ( $options{ 'choices' } ) {
78         if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
79             if ( $options{'choices'} eq 'class-sources' ) {
80                 my $sources = GetClassSources();
81                 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
82             } elsif ( $options{'choices'} eq 'opac-templates' ) {
83                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
84             } elsif ( $options{'choices'} eq 'staff-templates' ) {
85                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
86             } else {
87                 die 'Unrecognized source of preference values: ' . $options{'choices'};
88             }
89         }
90
91         $value ||= 0;
92
93         $chunk->{'type'} = 'select';
94         $chunk->{'CHOICES'} = [
95             sort { $a->{'text'} cmp $b->{'text'} }
96             map { { text => $options{'choices'}->{$_}, value => $_, selected => ( $_ eq $value || ( $_ eq '' && ( $value eq '0' || !$value ) ) ) } }
97             keys %{ $options{'choices'} }
98         ];
99     }
100
101     $chunk->{ 'type_' . $chunk->{'type'} } = 1;
102
103     return $chunk;
104 }
105
106 sub TransformPrefsToHTML {
107     my ( $data, $searchfield ) = @_;
108
109     my @lines;
110     my $dbh = C4::Context->dbh;
111     my $title = ( keys( %$data ) )[0];
112     my $tab = $data->{ $title };
113     $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
114
115     foreach my $group ( sort keys %$tab ) {
116         if ( $group ) {
117             push @lines, { is_group_title => 1, title => $group };
118         }
119
120         foreach my $line ( @{ $tab->{ $group } } ) {
121             my @chunks;
122             my @names;
123
124             foreach my $piece ( @$line ) {
125                 if ( ref ( $piece ) eq 'HASH' ) {
126                     my $name = $piece->{'pref'};
127
128                     if ( $name ) {
129                         my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
130                         my $value;
131                         if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
132                             $value = $piece->{'default'};
133                         } else {
134                             $value = $row->{'value'};
135                         }
136                         my $chunk = _get_chunk( $value, %$piece );
137
138                         # No highlighting of inputs yet, but would be useful
139                         $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
140
141                         push @chunks, $chunk;
142
143                         my $name_entry = { name => $name };
144                         if ( $searchfield ) {
145                             if ( $name =~ /^$searchfield$/i ) {
146                                 $name_entry->{'jumped'} = 1;
147                             } elsif ( $name =~ /$searchfield/i ) {
148                                 $name_entry->{'highlighted'} = 1;
149                             }
150                         }
151                         push @names, $name_entry;
152                     } else {
153                         push @chunks, $piece;
154                     }
155                 } else {
156                     push @chunks, { type_text => 1, contents => $piece };
157                 }
158             }
159
160             push @lines, { CHUNKS => \@chunks, NAMES => \@names };
161         }
162     }
163
164     return $title, \@lines;
165 }
166
167 sub _get_pref_files {
168     my ( $input, $open_files ) = @_;
169
170     my ( $htdocs, $theme, $lang, undef ) = C4::Output::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
171
172     my %results;
173
174     foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
175         my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
176
177         $results{$tab} = $open_files ? new IO::File( $file, 'r' ) : '';
178     }
179
180     return %results;
181 }
182
183 sub SearchPrefs {
184     my ( $input, $searchfield ) = @_;
185     my @tabs;
186
187     my %tab_files = _get_pref_files( $input );
188     our @terms = split( /\s+/, $searchfield );
189
190     sub matches {
191         my ( $text ) = @_;
192
193         return !grep( { $text !~ /$_/i } @terms );
194     }
195
196     foreach my $tab_name ( keys %tab_files ) {
197         my $data = GetTab( $input, $tab_name );
198         my $title = ( keys( %$data ) )[0];
199         my $tab = $data->{ $title };
200         $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
201
202         my $matched_groups;
203
204         while ( my ( $group_title, $contents ) = each %$tab ) {
205             if ( matches( $group_title ) ) {
206                 $matched_groups->{$group_title} = $contents;
207                 next;
208             }
209
210             my @new_contents;
211
212             foreach my $line ( @$contents ) {
213                 my $matched;
214
215                 foreach my $piece ( @$line ) {
216                     if ( ref( $piece ) eq 'HASH' ) {
217                         if ( $piece->{'pref'} =~ /^$searchfield$/i ) {
218                             my ( undef, $LINES ) = TransformPrefsToHTML( $data, $searchfield );
219
220                             return { search_jumped => 1, tab => $tab_name, tab_title => $title, LINES => $LINES };
221                         } elsif ( matches( $piece->{'pref'} ) ) {
222                             $matched = 1;
223                         } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_ ) } values( %{ $piece->{'choices'} } ) ) ) {
224                             $matched = 1;
225                         }
226                     } elsif ( matches( $piece ) ) {
227                         $matched = 1;
228                     }
229                     last if ( $matched );
230                 }
231
232                 push @new_contents, $line if ( $matched );
233             }
234
235             $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
236         }
237
238         if ( $matched_groups ) {
239             my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
240
241             push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, };
242         }
243     }
244
245     return @tabs;
246 }
247
248 my $dbh = C4::Context->dbh;
249 our $input = new CGI;
250
251 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
252     {   template_name   => "admin/preferences.tmpl",
253         query           => $input,
254         type            => "intranet",
255         authnotrequired => 0,
256         flagsrequired   => { parameters => 1 },
257         debug           => 1,
258     }
259 );
260
261 $lang = $template->param( 'lang' );
262 my $op = $input->param( 'op' ) || '';
263 my $tab = $input->param( 'tab' );
264 $tab ||= 'local-use';
265
266 my $highlighted;
267
268 if ( $op eq 'save' ) {
269     unless ( C4::Context->config( 'demo' ) ) {
270         foreach my $param ( $input->param() ) {
271             my ( $pref ) = ( $param =~ /pref_(.*)/ );
272
273             next if ( !defined( $pref ) );
274
275             my $value = join( ',', $input->param( $param ) );
276
277             C4::Context->set_preference( $pref, $value );
278             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $pref . " | " . $value );
279         }
280     }
281
282     print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
283     exit;
284 }
285
286 my @TABS;
287
288 if ( $op eq 'search' ) {
289     my $searchfield = $input->param( 'searchfield' );
290
291     $searchfield =~ s/[^a-zA-Z0-9_ -]//g;
292
293     $template->param( searchfield => $searchfield );
294
295     @TABS = SearchPrefs( $input, $searchfield );
296
297     foreach my $tabh ( @TABS ) {
298         $template->param(
299             $tabh->{'tab'} => 1
300         );
301     }
302
303     if ( @TABS ) {
304         $tab = ''; # No need to load a particular tab, as we found results
305         $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
306     } else {
307         $template->param(
308             search_not_found => 1,
309         );
310     }
311 }
312
313 if ( $tab ) {
314     my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
315
316     push @TABS, { tab_title => $tab_title, LINES => $LINES };
317     $template->param(
318         $tab => 1,
319         tab => $tab,
320     );
321 }
322
323 $template->param( TABS => \@TABS );
324
325 output_html_with_http_headers $input, $cookie, $template->output;