Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha.git] / admin / systempreferences.pl
1 #!/usr/bin/perl
2
3 # script to administer the systempref table
4 # written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23
24 =head1 systempreferences.pl
25
26 ALSO :
27  this script use an $op to know what to do.
28  if $op is empty or none of the above values,
29     - the default screen is build (with all records, or filtered datas).
30     - the   user can clic on add, modify or delete record.
31  if $op=add_form
32     - if primkey exists, this is a modification,so we read the $primkey record
33     - builds the add/modify form
34  if $op=add_validate
35     - the user has just send datas, so we create/modify the record
36  if $op=delete_form
37     - we show the record having primkey=$primkey and ask for deletion validation form
38  if $op=delete_confirm
39     - we delete the record having primkey=$primkey
40
41 =cut
42
43 use Modern::Perl;
44
45 use CGI qw ( -utf8 );
46 use MIME::Base64;
47 use C4::Auth;
48 use C4::Context;
49 use C4::Koha;
50 use C4::Languages qw(getTranslatedLanguages);
51 use C4::ClassSource;
52 use C4::Output;
53 use YAML::Syck qw( Dump LoadFile );
54
55 my %tabsysprefs; #we do no longer need to keep track of a tab per pref (yaml)
56
57 sub StringSearch {
58     my ( $searchstring, $tab ) = @_;
59     return (0,[]) if $tab ne 'local_use';
60
61     my $dbh = C4::Context->dbh;
62     $searchstring =~ s/\'/\\\'/g;
63     my @data = split( ' ', $searchstring );
64     my $count = @data;
65     my @results;
66     my $cnt = 0;
67     my $sth;
68
69     my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable in (";
70     my $first = 1;
71     my @sql_bind;
72     for my $name ( get_local_prefs() ) {
73                 $strsth .= ',' unless $first;
74                 $strsth .= "?";
75                 push(@sql_bind,$name);
76                 $first = 0;
77     }
78     $strsth .= ") order by variable";
79     $sth = $dbh->prepare($strsth);
80     $sth->execute(@sql_bind);
81
82     while ( my $data = $sth->fetchrow_hashref ) {
83             unless (defined $data->{value}) { $data->{value} = "";}
84             $data->{shortvalue} = $data->{value};
85             $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if length( $data->{value} ) > 60;
86             push( @results, $data );
87             $cnt++;
88     }
89
90     return ( $cnt, \@results );
91 }
92
93 sub GetPrefParams {
94     my $data   = shift;
95     my $params = $data;
96     my @options;
97
98     if ( defined $data->{'options'} ) {
99         foreach my $option ( split( /\|/, $data->{'options'} ) ) {
100             my $selected = '0';
101             defined( $data->{'value'} ) and $option eq $data->{'value'} and $selected = 1;
102             push @options, { option => $option, selected => $selected };
103         }
104     }
105
106     $params->{'prefoptions'} = $data->{'options'};
107
108     if ( not defined( $data->{'type'} ) ) {
109         $params->{'type_free'} = 1;
110         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 );
111     } elsif ( $data->{'type'} eq 'Upload' ) {
112         $params->{'type_upload'} = 1;
113     } elsif ( $data->{'type'} eq 'Choice' ) {
114         $params->{'type_choice'} = 1;
115     } elsif ( $data->{'type'} eq 'YesNo' ) {
116         $params->{'type_yesno'} = 1;
117         $data->{'value'}        = C4::Context->boolean_preference( $data->{'variable'} );
118         if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
119             $params->{'value_yes'} = 1;
120         } else {
121             $params->{'value_no'} = 1;
122         }
123     } elsif ( $data->{'type'} eq 'Integer' || $data->{'type'} eq 'Float' ) {
124         $params->{'type_free'} = 1;
125         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 10;
126     } elsif ( $data->{'type'} eq 'Textarea' ) {
127         $params->{'type_textarea'} = 1;
128         $data->{options} =~ /(.*)\|(.*)/;
129         $params->{'cols'} = $1;
130         $params->{'rows'} = $2;
131     } elsif ( $data->{'type'} eq 'Htmlarea' ) {
132         $params->{'type_htmlarea'} = 1;
133         $data->{options} =~ /(.*)\|(.*)/;
134         $params->{'cols'} = $1;
135         $params->{'rows'} = $2;
136     } elsif ( $data->{'type'} eq 'Themes' ) {
137         $params->{'type_choice'} = 1;
138         my $type = '';
139         ( $data->{'variable'} =~ m#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
140         @options = ();
141         my $currently_selected_themes;
142         my $counter = 0;
143         foreach my $theme ( split /\s+/, $data->{'value'} ) {
144             push @options, { option => $theme, counter => $counter };
145             $currently_selected_themes->{$theme} = 1;
146             $counter++;
147         }
148         foreach my $theme ( getallthemes($type) ) {
149             my $selected = '0';
150             next if $currently_selected_themes->{$theme};
151             push @options, { option => $theme, counter => $counter };
152             $counter++;
153         }
154     } elsif ( $data->{'type'} eq 'ClassSources' ) {
155         $params->{'type_choice'} = 1;
156         my $type = '';
157         @options = ();
158         my $sources = GetClassSources();
159         my $counter = 0;
160         foreach my $cn_source ( sort keys %$sources ) {
161             if ( $cn_source eq $data->{'value'} ) {
162                 push @options, { option => $cn_source, counter => $counter, selected => 1 };
163             } else {
164                 push @options, { option => $cn_source, counter => $counter };
165             }
166             $counter++;
167         }
168     } elsif ( $data->{'type'} eq 'Languages' ) {
169         my $currently_selected_languages;
170         foreach my $language ( split /\s+/, $data->{'value'} ) {
171             $currently_selected_languages->{$language} = 1;
172         }
173
174         # current language
175         my $lang = $params->{'lang'};
176         my $theme;
177         my $interface;
178         if ( $data->{'variable'} =~ /opac/ ) {
179
180             # this is the OPAC
181             $interface = 'opac';
182             $theme     = C4::Context->preference('opacthemes');
183         } else {
184
185             # this is the staff client
186             $interface = 'intranet';
187             $theme     = C4::Context->preference('template');
188         }
189         my $languages_loop = getTranslatedLanguages( $interface, $theme, $lang, $currently_selected_languages );
190
191         $params->{'languages_loop'}    = $languages_loop;
192         $params->{'type_langselector'} = 1;
193     } else {
194         $params->{'type_free'} = 1;
195         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 30;
196     }
197
198     if ( $params->{'type_choice'} || $params->{'type_free'} || $params->{'type_yesno'} ) {
199         $params->{'oneline'} = 1;
200     }
201
202     $params->{'preftype'} = $data->{'type'};
203     $params->{'options'}  = \@options;
204
205     return $params;
206 }
207
208 my $input       = new CGI;
209 my $searchfield = $input->param('searchfield') || '';
210 my $Tvalue      = $input->param('Tvalue');
211 my $offset      = $input->param('offset') || 0;
212 my $script_name = "/cgi-bin/koha/admin/systempreferences.pl";
213
214 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
215     {   template_name   => "admin/systempreferences.tt",
216         query           => $input,
217         type            => "intranet",
218         authnotrequired => 0,
219         flagsrequired   => { parameters => 'manage_sysprefs' },
220         debug           => 1,
221     }
222 );
223 my $pagesize = 100;
224 my $op = $input->param('op') || '';
225 $searchfield =~ s/\,//g;
226
227 if ($op) {
228     $template->param(
229         script_name => $script_name,
230         $op         => 1
231     );    # we show only the TMPL_VAR names $op
232 } else {
233     $template->param(
234         script_name => $script_name,
235         else        => 1
236     );    # we show only the TMPL_VAR names $op
237 }
238
239 if ( $op eq 'update_and_reedit' ) {
240     foreach ( $input->param ) {
241     }
242     my $value = '';
243     if ( my $currentorder = $input->param('currentorder') ) {
244         my @currentorder = split /\|/, $currentorder;
245         my $orderchanged = 0;
246         foreach my $param ( $input->param ) {
247             if ( $param =~ m#up-(\d+).x# ) {
248                 my $temp = $currentorder[$1];
249                 $currentorder[$1]       = $currentorder[ $1 - 1 ];
250                 $currentorder[ $1 - 1 ] = $temp;
251                 $orderchanged           = 1;
252                 last;
253             } elsif ( $param =~ m#down-(\d+).x# ) {
254                 my $temp = $currentorder[$1];
255                 $currentorder[$1]       = $currentorder[ $1 + 1 ];
256                 $currentorder[ $1 + 1 ] = $temp;
257                 $orderchanged           = 1;
258                 last;
259             }
260         }
261         $value = join ' ', @currentorder;
262         if ($orderchanged) {
263             $op = 'add_form';
264             $template->param(
265                 script_name => $script_name,
266                 $op         => 1
267             );    # we show only the TMPL_VAR names $op
268         } else {
269             $op          = '';
270             $searchfield = '';
271             $template->param(
272                 script_name => $script_name,
273                 else        => 1
274             );    # we show only the TMPL_VAR names $op
275         }
276     }
277     my $variable = $input->param('variable');
278     C4::Context->set_preference($variable, $value);
279 }
280
281 ################## ADD_FORM ##################################
282 # called by default. Used to create form to add or  modify a record
283
284 if ( $op eq 'add_form' ) {
285
286     #---- if primkey exists, it's a modify action, so read values to modify...
287     my $data;
288     if ($searchfield) {
289         my $dbh = C4::Context->dbh;
290         my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
291         $sth->execute($searchfield);
292         $data = $sth->fetchrow_hashref;
293         $template->param( modify => 1 );
294
295         # save tab to return to if user cancels edit
296         $template->param( return_tab => $tabsysprefs{$searchfield} );
297     }
298
299     $data->{'lang'} = $template->param('lang');
300     my $prefparams = GetPrefParams($data);
301     $template->param( %$prefparams );
302     $template->param( searchfield => $searchfield );
303
304 ################## ADD_VALIDATE ##################################
305     # called by add_form, used to insert/modify data in DB
306 } elsif ( $op eq 'add_validate' ) {
307     # to handle multiple values
308     my $value;
309
310     my $variable = $input->param('variable');
311     my $expl     = $input->param('explanation');
312     my $type     = $input->param('preftype');
313     my $options  = $input->param('prefoptions');
314
315     # handle multiple value strings (separated by ',')
316     my $params = $input->Vars;
317     if ( defined $params->{'value'} ) {
318         my @values = ();
319         @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
320         if (@values) {
321             $value = "";
322             for my $vl (@values) {
323                 $value .= "$vl,";
324             }
325             $value =~ s/,$//;
326         } else {
327             $value = $params->{'value'};
328         }
329     }
330
331     if ( $type eq 'Upload' ) {
332         my $lgtfh = $input->upload('value');
333         $value = join '', <$lgtfh>;
334         $value = encode_base64($value);
335     }
336
337     C4::Context->set_preference( $variable, $value, $expl, $type, $options );
338     print $input->redirect("/cgi-bin/koha/admin/systempreferences.pl?tab=");
339     exit;
340 ################## DELETE_CONFIRM ##################################
341     # called by default form, used to confirm deletion of data in DB
342 } elsif ( $op eq 'delete_confirm' ) {
343     my $value = C4::Context->preference($searchfield);
344     $template->param(
345         searchfield => $searchfield,
346         Tvalue      => $value,
347     );
348
349     # END $OP eq DELETE_CONFIRM
350 ################## DELETE_CONFIRMED ##################################
351     # called by delete_confirm, used to effectively confirm deletion of data in DB
352 } elsif ( $op eq 'delete_confirmed' ) {
353     C4::Context->delete_preference($searchfield);
354     # END $OP eq DELETE_CONFIRMED
355 ################## DEFAULT ##################################
356 } else {    # DEFAULT
357             #Adding tab management for system preferences
358     my $tab = $input->param('tab')||'local_use';
359     $template->param( $tab => 1 );
360     my ( $count, $results ) = StringSearch( $searchfield, $tab );
361     my @loop_data = ();
362     for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
363         my $row_data = $results->[$i];
364         $row_data->{'lang'} = $template->param('lang');
365         $row_data           = GetPrefParams($row_data);                                                         # get a fresh hash for the row data
366         $row_data->{edit}   = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'variable'};
367         $row_data->{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'variable'};
368         push( @loop_data, $row_data );
369     }
370     $template->param( loop => \@loop_data );
371     if ( $offset > 0 ) {
372         my $prevpage = $offset - $pagesize;
373         $template->param( "<a href=$script_name?offset=" . $prevpage . '&lt;&lt; Prev</a>' );
374     }
375     if ( $offset + $pagesize < $count ) {
376         my $nextpage = $offset + $pagesize;
377         $template->param( "a href=$script_name?offset=" . $nextpage . 'Next &gt;&gt;</a>' );
378     }
379     $template->param( tab => $tab, );
380 }    #---- END $OP eq DEFAULT
381 output_html_with_http_headers $input, $cookie, $template->output;
382
383
384 # Return an array containing all preferences defined in current Koha instance
385 # .pref files.
386
387 sub get_prefs_from_files {
388     my $context       = C4::Context->new();
389     my $path_pref_en  = $context->config('intrahtdocs') .
390                         '/prog/en/modules/admin/preferences';
391     # Get all .pref file names
392     opendir ( my $fh, $path_pref_en );
393     my @pref_files = grep { /.pref$/ } readdir($fh);
394     close $fh;
395
396     my @names = ();
397     my $append = sub {
398         my $prefs = shift;
399         for my $pref ( @$prefs ) {
400             for my $element ( @$pref ) {
401                 if ( ref( $element) eq 'HASH' ) {
402                     my $name = $element->{pref};
403                     next unless $name;
404                     push @names, $name;
405                     next;
406                 }
407             }
408         }
409     };
410     for my $file (@pref_files) {
411         my $pref = LoadFile( "$path_pref_en/$file" );
412         for my $tab ( keys %$pref ) {
413             my $content = $pref->{$tab};
414             if ( ref($content) eq 'ARRAY' ) {
415                 $append->($content);
416                 next;
417             }
418             for my $section ( keys %$content ) {
419                 my $syspref = $content->{$section};
420                 $append->($syspref);
421             }
422         }
423     }
424     return @names;
425 }
426
427
428 # Return an array containg all preferences defined in DB
429
430 sub get_prefs_from_db {
431     my $dbh = C4::Context->dbh;
432     my $sth = $dbh->prepare("SELECT variable FROM systempreferences");
433     $sth->execute;
434     my @names = ();
435     while ( (my $name) = $sth->fetchrow_array ) {
436         push @names, $name if $name;
437     }
438     return @names;
439 }
440
441
442 # Return an array containing all local preferences: those which are defined in
443 # DB and not defined in Koha .pref files.
444
445 sub get_local_prefs {
446     my @prefs_file = get_prefs_from_files();
447     my @prefs_db = get_prefs_from_db();
448
449     my %prefs_file = map { lc $_ => 1 } @prefs_file;
450     my @names = ();
451     foreach my $name (@prefs_db) {
452         push @names, $name  unless $prefs_file{lc $name};
453     }
454
455     return @names;
456 }
457