Bug 30477: Add new UNIMARC installer translation files
[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 qw( encode_base64 );
47 use C4::Auth qw( get_template_and_user );
48 use C4::Context;
49 use C4::Koha qw( getallthemes );
50 use C4::Languages qw( getTranslatedLanguages );
51 use C4::ClassSource qw( GetClassSources GetClassSource );
52 use C4::Output qw( output_html_with_http_headers );
53 use YAML::XS;
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->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 interface
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       = CGI->new;
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         flagsrequired   => { parameters => 'manage_sysprefs' },
219     }
220 );
221 my $pagesize = 100;
222 my $op = $input->param('op') || '';
223 $searchfield =~ s/\,//g;
224
225 if ($op) {
226     $template->param(
227         script_name => $script_name,
228         $op         => 1
229     );    # we show only the TMPL_VAR names $op
230 } else {
231     $template->param(
232         script_name => $script_name,
233         else        => 1
234     );    # we show only the TMPL_VAR names $op
235 }
236
237 if ( $op eq 'update_and_reedit' ) {
238     foreach ( $input->param ) {
239     }
240     my $value = '';
241     if ( my $currentorder = $input->param('currentorder') ) {
242         my @currentorder = split /\|/, $currentorder;
243         my $orderchanged = 0;
244         foreach my $param ( $input->param ) {
245             if ( $param =~ m#up-(\d+).x# ) {
246                 my $temp = $currentorder[$1];
247                 $currentorder[$1]       = $currentorder[ $1 - 1 ];
248                 $currentorder[ $1 - 1 ] = $temp;
249                 $orderchanged           = 1;
250                 last;
251             } elsif ( $param =~ m#down-(\d+).x# ) {
252                 my $temp = $currentorder[$1];
253                 $currentorder[$1]       = $currentorder[ $1 + 1 ];
254                 $currentorder[ $1 + 1 ] = $temp;
255                 $orderchanged           = 1;
256                 last;
257             }
258         }
259         $value = join ' ', @currentorder;
260         if ($orderchanged) {
261             $op = 'add_form';
262             $template->param(
263                 script_name => $script_name,
264                 $op         => 1
265             );    # we show only the TMPL_VAR names $op
266         } else {
267             $op          = '';
268             $searchfield = '';
269             $template->param(
270                 script_name => $script_name,
271                 else        => 1
272             );    # we show only the TMPL_VAR names $op
273         }
274     }
275     my $variable = $input->param('variable');
276     C4::Context->set_preference($variable, $value);
277 }
278
279 ################## ADD_FORM ##################################
280 # called by default. Used to create form to add or  modify a record
281
282 if ( $op eq 'add_form' ) {
283
284     #---- if primkey exists, it's a modify action, so read values to modify...
285     my $data;
286     if ($searchfield) {
287         my $dbh = C4::Context->dbh;
288         my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
289         $sth->execute($searchfield);
290         $data = $sth->fetchrow_hashref;
291         $template->param( modify => 1 );
292
293         # save tab to return to if user cancels edit
294         $template->param( return_tab => $tabsysprefs{$searchfield} );
295     }
296
297     $data->{'lang'} = $template->param('lang');
298     my $prefparams = GetPrefParams($data);
299     $template->param( %$prefparams );
300     $template->param( searchfield => $searchfield );
301
302 ################## ADD_VALIDATE ##################################
303     # called by add_form, used to insert/modify data in DB
304 } elsif ( $op eq 'add_validate' ) {
305     # to handle multiple values
306     my $value;
307
308     my $variable = $input->param('variable');
309     my $expl     = $input->param('explanation');
310     my $type     = $input->param('preftype');
311     my $options  = $input->param('prefoptions');
312
313     # handle multiple value strings (separated by ',')
314     my $params = $input->Vars;
315     if ( defined $params->{'value'} ) {
316         my @values = ();
317         @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
318         if (@values) {
319             $value = "";
320             for my $vl (@values) {
321                 $value .= "$vl,";
322             }
323             $value =~ s/,$//;
324         } else {
325             $value = $params->{'value'};
326         }
327     }
328
329     if ( $type eq 'Upload' ) {
330         my $lgtfh = $input->upload('value');
331         $value = join '', <$lgtfh>;
332         $value = encode_base64($value);
333     }
334
335     C4::Context->set_preference( $variable, $value, $expl, $type, $options );
336     print $input->redirect("/cgi-bin/koha/admin/systempreferences.pl?tab=");
337     exit;
338 ################## DELETE_CONFIRM ##################################
339     # called by default form, used to confirm deletion of data in DB
340 } elsif ( $op eq 'delete_confirm' ) {
341     my $value = C4::Context->preference($searchfield);
342     $template->param(
343         searchfield => $searchfield,
344         Tvalue      => $value,
345     );
346
347     # END $OP eq DELETE_CONFIRM
348 ################## DELETE_CONFIRMED ##################################
349     # called by delete_confirm, used to effectively confirm deletion of data in DB
350 } elsif ( $op eq 'delete_confirmed' ) {
351     C4::Context->delete_preference($searchfield);
352     # END $OP eq DELETE_CONFIRMED
353 ################## DEFAULT ##################################
354 } else {    # DEFAULT
355             #Adding tab management for system preferences
356     my $tab = $input->param('tab')||'local_use';
357     $template->param( $tab => 1 );
358     my ( $count, $results ) = StringSearch( $searchfield, $tab );
359     my @loop_data = ();
360     for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
361         my $row_data = $results->[$i];
362         $row_data->{'lang'} = $template->param('lang');
363         $row_data           = GetPrefParams($row_data);                                                         # get a fresh hash for the row data
364         $row_data->{edit}   = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'variable'};
365         $row_data->{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'variable'};
366         push( @loop_data, $row_data );
367     }
368     $template->param( loop => \@loop_data );
369     if ( $offset > 0 ) {
370         my $prevpage = $offset - $pagesize;
371         $template->param( "<a href=$script_name?offset=" . $prevpage . '&lt;&lt; Prev</a>' );
372     }
373     if ( $offset + $pagesize < $count ) {
374         my $nextpage = $offset + $pagesize;
375         $template->param( "a href=$script_name?offset=" . $nextpage . 'Next &gt;&gt;</a>' );
376     }
377     $template->param( tab => $tab, );
378 }    #---- END $OP eq DEFAULT
379 output_html_with_http_headers $input, $cookie, $template->output;
380
381
382 # Return an array containing all preferences defined in current Koha instance
383 # .pref files.
384
385 sub get_prefs_from_files {
386     my $path_pref_en  = C4::Context->config('intrahtdocs') .
387                         '/prog/en/modules/admin/preferences';
388     # Get all .pref file names
389     opendir ( my $fh, $path_pref_en );
390     my @pref_files = grep { /.pref$/ } readdir($fh);
391     close $fh;
392
393     my @names = ();
394     my $append = sub {
395         my $prefs = shift;
396         for my $pref ( @$prefs ) {
397             for my $element ( @$pref ) {
398                 if ( ref( $element) eq 'HASH' ) {
399                     my $name = $element->{pref};
400                     next unless $name;
401                     push @names, $name;
402                     next;
403                 }
404             }
405         }
406     };
407     for my $file (@pref_files) {
408         my $pref = YAML::XS::LoadFile( "$path_pref_en/$file" );
409         for my $tab ( keys %$pref ) {
410             my $content = $pref->{$tab};
411             if ( ref($content) eq 'ARRAY' ) {
412                 $append->($content);
413                 next;
414             }
415             for my $section ( keys %$content ) {
416                 my $syspref = $content->{$section};
417                 $append->($syspref);
418             }
419         }
420     }
421     return @names;
422 }
423
424
425 # Return an array containg all preferences defined in DB
426
427 sub get_prefs_from_db {
428     my $dbh = C4::Context->dbh;
429     my $sth = $dbh->prepare("SELECT variable FROM systempreferences");
430     $sth->execute;
431     my @names = ();
432     while ( (my $name) = $sth->fetchrow_array ) {
433         push @names, $name if $name;
434     }
435     return @names;
436 }
437
438
439 # Return an array containing all local preferences: those which are defined in
440 # DB and not defined in Koha .pref files.
441
442 sub get_local_prefs {
443     my @prefs_file = get_prefs_from_files();
444     my @prefs_db = get_prefs_from_db();
445
446     my %prefs_file = map { lc $_ => 1 } @prefs_file;
447     my @names = ();
448     foreach my $name (@prefs_db) {
449         push @names, $name  unless $prefs_file{lc $name};
450     }
451
452     return @names;
453 }
454