Bug 20912: Rental Fees based on Time Period
[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::Syck qw();
35 use List::MoreUtils qw(any);
36 $YAML::Syck::ImplicitTyping = 1;
37 $YAML::Syck::ImplicitUnicode = 1;
38
39 # use Smart::Comments;
40 #
41
42 sub GetTab {
43     my ( $input, $tab ) = @_;
44
45     my $tab_template = C4::Templates::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
46
47     my $active_currency = Koha::Acquisition::Currencies->get_active;
48     my $local_currency;
49     if ($active_currency) {
50         $local_currency = $active_currency->currency;
51     }
52     $tab_template->param(
53         local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
54     );
55
56     return YAML::Syck::Load( $tab_template->output() );
57 }
58
59 sub _get_chunk {
60     my ( $value, %options ) = @_;
61
62     my $name = $options{'pref'};
63     my $chunk = { name => $name, value => $value, type => $options{'type'} || 'input', class => $options{'class'} };
64
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 ) };
71
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');
82         }
83         $chunk->{'languages'} = getTranslatedLanguages( $interface, $theme, undef, $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'};
96             }
97         }
98
99         $value ||= 0;
100
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'} }
106         ];
107     } elsif ( $options{'multiple'} ) {
108         my @values;
109         @values = split /,/, $value if defined($value);
110         $chunk->{type}    = 'multiple';
111         $chunk->{CHOICES} = [
112             sort { $a->{'text'} cmp $b->{'text'} }
113               map {
114                 my $option_value = $_;
115                 {
116                     text     => $options{multiple}->{$option_value},
117                     value    => $option_value,
118                     selected => (grep /^$option_value$/, @values) ? 1 : 0,
119                 }
120               }
121               keys %{ $options{multiple} }
122         ];
123     }
124
125     $chunk->{ 'type_' . $chunk->{'type'} } = 1;
126
127     return $chunk;
128 }
129
130 sub TransformPrefsToHTML {
131     my ( $data, $searchfield ) = @_;
132
133     my @lines;
134     my $dbh = C4::Context->dbh;
135     my $title = ( keys( %$data ) )[0];
136     my $tab = $data->{ $title };
137     $tab = { '' => $tab } if ( ref( $tab ) eq 'ARRAY' );
138
139     my @override_syspref_names;
140     if ( exists($ENV{OVERRIDE_SYSPREF_NAMES}) &&
141          defined($ENV{OVERRIDE_SYSPREF_NAMES})
142        ) {
143         @override_syspref_names = split /,/, $ENV{OVERRIDE_SYSPREF_NAMES};
144     }
145
146     foreach my $group ( sort keys %$tab ) {
147         if ( $group ) {
148             push @lines, { is_group_title => 1, title => $group };
149         }
150
151         foreach my $line ( @{ $tab->{ $group } } ) {
152             my @chunks;
153             my @names;
154
155             foreach my $piece ( @$line ) {
156                 if ( ref ( $piece ) eq 'HASH' ) {
157                     my $name = $piece->{'pref'};
158
159                     if ( $name ) {
160                         my $row = $dbh->selectrow_hashref( "SELECT value, type FROM systempreferences WHERE variable = ?", {}, $name );
161                         my $value;
162                         if ( ( !defined( $row ) || ( !defined( $row->{'value'} ) && $row->{'type'} ne 'YesNo' ) ) && defined( $piece->{'default'} ) ) {
163                             $value = $piece->{'default'};
164                         } else {
165                             $value = $row->{'value'};
166                         }
167                         my $chunk = _get_chunk( $value, %$piece );
168
169                         # No highlighting of inputs yet, but would be useful
170                         $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
171
172                         push @chunks, $chunk;
173
174                         my $name_entry = { name => $name };
175                         if ( $searchfield ) {
176                             if ( $name =~ /^$searchfield$/i ) {
177                                 $name_entry->{'jumped'} = 1;
178                             } elsif ( $name =~ /$searchfield/i ) {
179                                 $name_entry->{'highlighted'} = 1;
180                             }
181                         }
182                         $name_entry->{'overridden'} = 1 if ( any { $name eq $_ } @override_syspref_names );
183                         push @names, $name_entry;
184                     } else {
185                         push @chunks, $piece;
186                     }
187                 } else {
188                     if ( $piece ) {
189                         my $version = Koha::version();
190                         my ( $major, $minor, $maintenance, $development ) = split( '\.', $version );
191                         if ( $minor % 2 ) {
192                             $piece =~ s|__VERSION__|${major}_${minor}|g;
193                         } else {
194                             $piece =~ s|__VERSION__|master|g;
195                         }
196                     }
197                     push @chunks, { type_text => 1, contents => $piece };
198                 }
199             }
200             push @lines, { CHUNKS => \@chunks, NAMES => \@names, is_group_title => 0 };
201         }
202     }
203
204     return $title, \@lines;
205 }
206
207 sub _get_pref_files {
208     my ( $input, $open_files ) = @_;
209
210     my ( $htdocs, $theme, $lang, undef ) = C4::Templates::_get_template_file( 'admin/preferences/admin.pref', 'intranet', $input );
211
212     my %results;
213
214     foreach my $file ( glob( "$htdocs/$theme/$lang/modules/admin/preferences/*.pref" ) ) {
215         my ( $tab ) = ( $file =~ /([a-z0-9_-]+)\.pref$/ );
216
217         $results{$tab} = $open_files ? new IO::File( $file, 'r' ) : '';
218     }
219
220     return %results;
221 }
222
223 sub SearchPrefs {
224     my ( $input, $searchfield ) = @_;
225     my @tabs;
226
227     my %tab_files = _get_pref_files( $input );
228     our @terms = split( /\s+/, $searchfield );
229
230     foreach my $tab_name ( keys %tab_files ) {
231         # 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
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' );
236
237         my $matched_groups;
238
239         while ( my ( $group_title, $contents ) = each %$tab ) {
240             if ( matches( $group_title, \@terms ) ) {
241                 $matched_groups->{$group_title} = $contents;
242                 next;
243             }
244
245             my @new_contents;
246
247             foreach my $line ( @$contents ) {
248                 my $matched;
249
250                 foreach my $piece ( @$line ) {
251                     if ( ref( $piece ) eq 'HASH' ) {
252                         if ( !$piece->{'pref'} ){
253                             next;
254                         }
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;
259                         }
260                     } elsif ( matches( $piece, \@terms ) ) {
261                         $matched = 1;
262                     }
263                     last if ( $matched );
264                 }
265
266                 push @new_contents, $line if ( $matched );
267             }
268
269             $matched_groups->{$group_title} = \@new_contents if ( @new_contents );
270         }
271
272         if ( $matched_groups ) {
273             my ( $title, $LINES ) = TransformPrefsToHTML( { $title => $matched_groups }, $searchfield );
274
275             push @tabs, { tab => $tab, tab_title => $title, LINES => $LINES, tab_id => $tab_name };
276         }
277     }
278
279     return @tabs;
280 }
281
282 sub matches {
283     my ( $text, $terms ) = @_;
284     if ( $text ) {
285         return !grep(
286             {
287                 my $re = eval{qr|$_|i};
288                 $re = qr|\Q$_\E| if $@;
289                 $text !~ m|$re|;
290             } @$terms
291         )
292     }
293 }
294
295 my $dbh = C4::Context->dbh;
296 our $input = new CGI;
297
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 => 'manage_sysprefs' },
304         debug           => 1,
305     }
306 );
307
308 my $op = $input->param( 'op' ) || '';
309 my $tab = $input->param( 'tab' );
310 $tab ||= 'acquisitions'; # Ideally this should be "local-use" but preferences.pl
311                          # does not presently support local use preferences
312
313 my $highlighted;
314
315 if ( $op eq 'save' ) {
316     foreach my $param ( $input->param() ) {
317         my ( $pref ) = ( $param =~ /pref_(.*)/ );
318
319         next if ( !defined( $pref ) );
320
321         my $value = join( ',', $input->param( $param ) );
322
323         C4::Context->set_preference( $pref, $value );
324     }
325
326     print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
327     exit;
328 }
329
330 my @TABS;
331
332 if ( $op eq 'search' ) {
333     my $searchfield = $input->param( 'searchfield' );
334
335     $searchfield =~ s/\p{IsC}//g;
336     $searchfield =~ s/\s+/ /;
337     $searchfield =~ s/^\s+//;
338     $searchfield =~ s/\s+$//;
339
340     $template->param( searchfield => $searchfield );
341
342     @TABS = SearchPrefs( $input, $searchfield );
343
344     foreach my $tabh ( @TABS ) {
345         $template->param(
346             $tabh->{'tab'} => 1
347         );
348     }
349
350     if ( @TABS ) {
351         $tab = ''; # No need to load a particular tab, as we found results
352         $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
353     } else {
354         $template->param(
355             search_not_found => 1,
356         );
357     }
358 }
359
360 if ( $tab ) {
361     my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
362
363     push @TABS, { tab_title => $tab_title, LINES => $LINES, tab_id => $tab };
364     $template->param(
365         $tab => 1,
366         tab => $tab,
367     );
368 }
369
370 $template->param( TABS => \@TABS );
371
372 output_html_with_http_headers $input, $cookie, $template->output;