Bug 27857: Add handling for globally mandatory attributes
[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
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
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::XS;
35 use Encode;
36 use List::MoreUtils qw(any);
37
38 # use Smart::Comments;
39 #
40
41 sub GetTab {
42     my ( $input, $tab ) = @_;
43
44     my $tab_template = C4::Templates::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
45
46     my $active_currency = Koha::Acquisition::Currencies->get_active;
47     my $local_currency;
48     if ($active_currency) {
49         $local_currency = $active_currency->currency;
50     }
51     $tab_template->param(
52         local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
53     );
54
55     return YAML::XS::Load( Encode::encode_utf8($tab_template->output()));
56 }
57
58 sub _get_chunk {
59     my ( $value, %options ) = @_;
60
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'};
65     }
66
67     if( $options{'type'} && $options{'type'} eq 'modalselect' ){
68         $chunk->{'source'} = $options{'source'};
69         $chunk->{'exclusions'} = $options{'exclusions'} // "";
70         $chunk->{'type'} = 'modalselect';
71     }
72
73     if ( $options{'class'} && $options{'class'} eq 'password' ) {
74         $chunk->{'input_type'} = 'password';
75     } elsif ( $options{'class'} && $options{'class'} eq 'email' ) {
76         $chunk->{'input_type'} = 'email';
77     } elsif ( $options{'class'} && $options{'class'} eq 'date' ) {
78         $chunk->{'dateinput'} = 1;
79     } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
80         my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
81
82         my $theme;
83         my $interface;
84         if ( $options{'type'} eq 'opac-languages' ) {
85             # this is the OPAC
86             $interface = 'opac';
87             $theme     = C4::Context->preference('opacthemes');
88         } else {
89             # this is the staff interface
90             $interface = 'intranet';
91             $theme     = C4::Context->preference('template');
92         }
93         $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, undef, $current_languages );
94         $chunk->{'type'} = 'languages';
95     } elsif ( $options{ 'choices' } ) {
96         my $add_blank;
97         if ( $options{'choices'} && ref( $options{ 'choices' } ) eq '' ) {
98             if ( $options{'choices'} eq 'class-sources' ) {
99                 my $sources = GetClassSources();
100                 $options{'choices'} = { map { $_ => $sources->{$_}->{'description'} } keys %$sources };
101             } elsif ( $options{'choices'} eq 'opac-templates' ) {
102                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'opac' ) }
103             } elsif ( $options{'choices'} eq 'staff-templates' ) {
104                 $options{'choices'} = { map { $_ => $_ } getallthemes( 'intranet' ) }
105             } elsif ( $options{choices} eq 'patron-categories' ) {
106                 $options{choices} = { map { $_->categorycode => $_->description } Koha::Patron::Categories->search };
107                 $add_blank = 1;
108             } else {
109                 die 'Unrecognized source of preference values: ' . $options{'choices'};
110             }
111         }
112
113         $value ||= 0;
114
115         $chunk->{'type'} = ( $options{class} && $options{class} eq 'multiple' ) ? 'multiple' : 'select';
116
117         my @values;
118         @values = split /,/, $value if defined($value);
119         $chunk->{'CHOICES'} = [
120             sort { $a->{'text'} cmp $b->{'text'} }
121             map {
122                 my $c = $_;
123                 {
124                     text     => $options{'choices'}->{$c},
125                     value    => $c,
126                     selected => (
127                         grep { $_ eq $c || ( $c eq '' && ($value eq '0' || !$value ) ) } @values
128                     ) ? 1 : 0,
129                 }
130               }
131             keys %{ $options{'choices'} }
132         ];
133
134         # Add a first blank value if needed
135         unshift @{ $chunk->{CHOICES} }, {
136             text  => '',
137             value => '',
138         } if $add_blank && $chunk->{type} eq 'select';
139
140     } elsif ( $options{'multiple'} ) {
141         my @values;
142         @values = split /,/, $value if defined($value);
143         $chunk->{type}    = 'multiple';
144         $chunk->{CHOICES} = [
145             sort { $a->{'text'} cmp $b->{'text'} }
146               map {
147                 my $option_value = $_;
148                 {
149                     text     => $options{multiple}->{$option_value},
150                     value    => $option_value,
151                     selected => (grep { $_ eq $option_value } @values) ? 1 : 0,
152                 }
153               }
154               keys %{ $options{multiple} }
155         ];
156     }
157
158     $chunk->{ 'type_' . $chunk->{'type'} } = 1;
159
160     return $chunk;
161 }
162
163 sub TransformPrefsToHTML {
164     my ( $data, $searchfield ) = @_;
165
166     my @lines;
167     my $dbh = C4::Context->dbh;
168     my $title = ( keys( %$data ) )[0];
169     my $tab = $data->{ $title };
170     $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
171
172     my @override_syspref_names;
173     if ( exists($ENV{OVERRIDE_SYSPREF_NAMES}) &&
174          defined($ENV{OVERRIDE_SYSPREF_NAMES})
175        ) {
176         @override_syspref_names = split /,/, $ENV{OVERRIDE_SYSPREF_NAMES};
177     }
178
179     foreach my $group ( sort keys %$tab ) {
180         if ( $group ) {
181             push @lines, { is_group_title => 1, title => $group };
182         }
183
184         foreach my $line ( @{ $tab->{ $group } } ) {
185             my @chunks;
186             my @names;
187
188             foreach my $piece ( @$line ) {
189                 if ( ref ( $piece ) eq 'HASH' ) {
190                     my $name = $piece->{'pref'};
191
192                     if ( $name ) {
193                         my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
194                         my $value;
195                         if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
196                             $value = $piece->{'default'};
197                         } else {
198                             $value = $row->{'value'};
199                         }
200                         my $chunk = _get_chunk( $value, %$piece );
201
202                         # No highlighting of inputs yet, but would be useful
203                         $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
204
205                         push @chunks, $chunk;
206
207                         my $name_entry = { name => $name };
208                         if ( $searchfield ) {
209                             if ( $name =~ /^$searchfield$/i ) {
210                                 $name_entry->{'jumped'} = 1;
211                             } elsif ( $name =~ /$searchfield/i ) {
212                                 $name_entry->{'highlighted'} = 1;
213                             }
214                         }
215                         $name_entry->{'overridden'} = 1 if ( any { $name eq $_ } @override_syspref_names );
216                         push @names, $name_entry;
217                     } else {
218                         push @chunks, $piece;
219                     }
220                 } else {
221                     if ( $piece ) {
222                         my $version = Koha::version();
223                         my ( $major, $minor, $maintenance, $development ) = split( '\.', $version );
224                         if ( $minor % 2 ) {
225                             $piece =~ s|__VERSION__|${major}_${minor}|g;
226                         } else {
227                             $piece =~ s|__VERSION__|master|g;
228                         }
229                     }
230                     push @chunks, { type_text => 1, contents => $piece };
231                 }
232             }
233             push @lines, { CHUNKS => \@chunks, NAMES => \@names, is_group_title => 0 };
234         }
235     }
236
237     return $title, \@lines;
238 }
239
240 sub _get_pref_files {
241     my ( $input, $open_files ) = @_;
242
243     my ( $htdocs, $theme, $lang, undef ) = C4::Templates::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
244
245     my %results;
246
247     foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
248         my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
249
250         $results{$tab} = $open_files ? IO::File->new( $file, 'r' ) : '';
251     }
252
253     return %results;
254 }
255
256 sub SearchPrefs {
257     my ( $input, $searchfield ) = @_;
258     my @tabs;
259
260     my %tab_files = _get_pref_files( $input );
261     our @terms = split( /\s+/, $searchfield );
262
263     foreach my $tab_name ( sort keys %tab_files ) {
264         # FIXME Hum?
265         # 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
266         my ($data) = GetTab( $input, $tab_name );
267         my $title = ( keys( %$data ) )[0];
268         my $tab = $data->{ $title };
269         $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
270
271         my $matched_groups;
272
273         while ( my ( $group_title, $contents ) = each %$tab ) {
274             if ( matches( $group_title, \@terms ) ) {
275                 $matched_groups->{$group_title} = $contents;
276                 next;
277             }
278
279             my @new_contents;
280
281             foreach my $line ( @$contents ) {
282                 my $matched;
283
284                 foreach my $piece ( @$line ) {
285                     if ( ref( $piece ) eq 'HASH' ) {
286                         if ( !$piece->{'pref'} ){
287                             next;
288                         }
289                         if ( matches( $piece->{'pref'}, \@terms) ) {
290                             $matched = 1;
291                         } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_, \@terms ) } values( %{ $piece->{'choices'} } ) ) ) {
292                             $matched = 1;
293                         }
294                     } elsif ( matches( $piece, \@terms ) ) {
295                         $matched = 1;
296                     }
297                     last if ( $matched );
298                 }
299
300                 push @new_contents, $line if ( $matched );
301             }
302
303             $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
304         }
305
306         if ( $matched_groups ) {
307             my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
308
309             push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, tab_id => $tab_name };
310         }
311     }
312
313     return @tabs;
314 }
315
316 sub matches {
317     my ( $text, $terms ) = @_;
318     if ( $text ) {
319         return !grep(
320             {
321                 my $re = eval{qr|$_|i};
322                 $re = qr|\Q$_\E| if $@;
323                 $text !~ m|$re|;
324             } @$terms
325         )
326     }
327 }
328
329 my $dbh = C4::Context->dbh;
330 our $input = CGI->new;
331
332 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
333     {   template_name   => "admin/preferences.tt",
334         query           => $input,
335         type            => "intranet",
336         flagsrequired   => { parameters => 'manage_sysprefs' },
337         debug           => 1,
338     }
339 );
340
341 my $op = $input->param( 'op' ) || '';
342 my $tab = $input->param( 'tab' );
343 $tab ||= 'accounting'; # Ideally this should be "local-use" but preferences.pl
344                          # does not presently support local use preferences
345
346 my $highlighted;
347
348 if ( $op eq 'save' ) {
349     foreach my $param ( $input->param() ) {
350         my ( $pref ) = ( $param =~ /pref_(.*)/ );
351
352         next if ( !defined( $pref ) );
353
354         my $value = join( ',', $input->param( $param ) );
355
356         C4::Context->set_preference( $pref, $value );
357     }
358
359     print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
360     exit;
361 }
362
363 my @TABS;
364
365 if ( $op eq 'search' ) {
366     my $searchfield = $input->param( 'searchfield' );
367
368     $searchfield =~ s/\p{IsC}//g;
369     $searchfield =~ s/\s+/ /;
370     $searchfield =~ s/^\s+//;
371     $searchfield =~ s/\s+$//;
372
373     $template->param( searchfield => $searchfield );
374
375     @TABS = SearchPrefs( $input, $searchfield );
376
377     foreach my $tabh ( @TABS ) {
378         $template->param(
379             $tabh->{'tab'} => 1
380         );
381     }
382
383     if ( @TABS ) {
384         $tab = ''; # No need to load a particular tab, as we found results
385         $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
386     } else {
387         $template->param(
388             search_not_found => 1,
389         );
390     }
391 }
392
393 if ( $tab ) {
394     my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
395
396     push @TABS, { tab_title => $tab_title, LINES => $LINES, tab_id => $tab };
397     $template->param(
398         $tab => 1,
399         tab => $tab,
400     );
401 }
402
403 $template->param( TABS => \@TABS );
404
405 output_html_with_http_headers $input, $cookie, $template->output;