Bug 18999: (QA followup) Minor fixes for tests
[koha.git] / admin / systempreferences.pl
blob67e063895b1714cdbfa9a2858ba2a5f5d7f15b97
1 #!/usr/bin/perl
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)
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
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.
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.
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>.
24 =head1 systempreferences.pl
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
41 =cut
43 use strict;
44 use warnings;
46 use CGI qw ( -utf8 );
47 use MIME::Base64;
48 use C4::Auth;
49 use C4::Context;
50 use C4::Koha;
51 use C4::Languages qw(getTranslatedLanguages);
52 use C4::ClassSource;
53 use C4::Output;
54 use YAML::Syck qw( Dump LoadFile );
56 my %tabsysprefs; #we do no longer need to keep track of a tab per pref (yaml)
58 sub StringSearch {
59 my ( $searchstring, $tab ) = @_;
60 return (0,[]) if $tab ne 'local_use';
62 my $dbh = C4::Context->dbh;
63 $searchstring =~ s/\'/\\\'/g;
64 my @data = split( ' ', $searchstring );
65 my $count = @data;
66 my @results;
67 my $cnt = 0;
68 my $sth;
70 my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable in (";
71 my $first = 1;
72 my @sql_bind;
73 for my $name ( get_local_prefs() ) {
74 $strsth .= ',' unless $first;
75 $strsth .= "?";
76 push(@sql_bind,$name);
77 $first = 0;
79 $strsth .= ") order by variable";
80 $sth = $dbh->prepare($strsth);
81 $sth->execute(@sql_bind);
83 while ( my $data = $sth->fetchrow_hashref ) {
84 unless (defined $data->{value}) { $data->{value} = "";}
85 $data->{shortvalue} = $data->{value};
86 $data->{shortvalue} = substr( $data->{value}, 0, 60 ) . "..." if length( $data->{value} ) > 60;
87 push( @results, $data );
88 $cnt++;
91 return ( $cnt, \@results );
94 sub GetPrefParams {
95 my $data = shift;
96 my $params = $data;
97 my @options;
99 if ( defined $data->{'options'} ) {
100 foreach my $option ( split( /\|/, $data->{'options'} ) ) {
101 my $selected = '0';
102 defined( $data->{'value'} ) and $option eq $data->{'value'} and $selected = 1;
103 push @options, { option => $option, selected => $selected };
107 $params->{'prefoptions'} = $data->{'options'};
109 if ( not defined( $data->{'type'} ) ) {
110 $params->{'type_free'} = 1;
111 $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 );
112 } elsif ( $data->{'type'} eq 'Upload' ) {
113 $params->{'type_upload'} = 1;
114 } elsif ( $data->{'type'} eq 'Choice' ) {
115 $params->{'type_choice'} = 1;
116 } elsif ( $data->{'type'} eq 'YesNo' ) {
117 $params->{'type_yesno'} = 1;
118 $data->{'value'} = C4::Context->boolean_preference( $data->{'variable'} );
119 if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
120 $params->{'value_yes'} = 1;
121 } else {
122 $params->{'value_no'} = 1;
124 } elsif ( $data->{'type'} eq 'Integer' || $data->{'type'} eq 'Float' ) {
125 $params->{'type_free'} = 1;
126 $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 10;
127 } elsif ( $data->{'type'} eq 'Textarea' ) {
128 $params->{'type_textarea'} = 1;
129 $data->{options} =~ /(.*)\|(.*)/;
130 $params->{'cols'} = $1;
131 $params->{'rows'} = $2;
132 } elsif ( $data->{'type'} eq 'Htmlarea' ) {
133 $params->{'type_htmlarea'} = 1;
134 $data->{options} =~ /(.*)\|(.*)/;
135 $params->{'cols'} = $1;
136 $params->{'rows'} = $2;
137 } elsif ( $data->{'type'} eq 'Themes' ) {
138 $params->{'type_choice'} = 1;
139 my $type = '';
140 ( $data->{'variable'} =~ m#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
141 @options = ();
142 my $currently_selected_themes;
143 my $counter = 0;
144 foreach my $theme ( split /\s+/, $data->{'value'} ) {
145 push @options, { option => $theme, counter => $counter };
146 $currently_selected_themes->{$theme} = 1;
147 $counter++;
149 foreach my $theme ( getallthemes($type) ) {
150 my $selected = '0';
151 next if $currently_selected_themes->{$theme};
152 push @options, { option => $theme, counter => $counter };
153 $counter++;
155 } elsif ( $data->{'type'} eq 'ClassSources' ) {
156 $params->{'type_choice'} = 1;
157 my $type = '';
158 @options = ();
159 my $sources = GetClassSources();
160 my $counter = 0;
161 foreach my $cn_source ( sort keys %$sources ) {
162 if ( $cn_source eq $data->{'value'} ) {
163 push @options, { option => $cn_source, counter => $counter, selected => 1 };
164 } else {
165 push @options, { option => $cn_source, counter => $counter };
167 $counter++;
169 } elsif ( $data->{'type'} eq 'Languages' ) {
170 my $currently_selected_languages;
171 foreach my $language ( split /\s+/, $data->{'value'} ) {
172 $currently_selected_languages->{$language} = 1;
175 # current language
176 my $lang = $params->{'lang'};
177 my $theme;
178 my $interface;
179 if ( $data->{'variable'} =~ /opac/ ) {
181 # this is the OPAC
182 $interface = 'opac';
183 $theme = C4::Context->preference('opacthemes');
184 } else {
186 # this is the staff client
187 $interface = 'intranet';
188 $theme = C4::Context->preference('template');
190 my $languages_loop = getTranslatedLanguages( $interface, $theme, $lang, $currently_selected_languages );
192 $params->{'languages_loop'} = $languages_loop;
193 $params->{'type_langselector'} = 1;
194 } else {
195 $params->{'type_free'} = 1;
196 $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 30;
199 if ( $params->{'type_choice'} || $params->{'type_free'} || $params->{'type_yesno'} ) {
200 $params->{'oneline'} = 1;
203 $params->{'preftype'} = $data->{'type'};
204 $params->{'options'} = \@options;
206 return $params;
209 my $input = new CGI;
210 my $searchfield = $input->param('searchfield') || '';
211 my $Tvalue = $input->param('Tvalue');
212 my $offset = $input->param('offset') || 0;
213 my $script_name = "/cgi-bin/koha/admin/systempreferences.pl";
215 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
216 { template_name => "admin/systempreferences.tt",
217 query => $input,
218 type => "intranet",
219 authnotrequired => 0,
220 flagsrequired => { parameters => 'parameters_remaining_permissions' },
221 debug => 1,
224 my $pagesize = 100;
225 my $op = $input->param('op') || '';
226 $searchfield =~ s/\,//g;
228 if ($op) {
229 $template->param(
230 script_name => $script_name,
231 $op => 1
232 ); # we show only the TMPL_VAR names $op
233 } else {
234 $template->param(
235 script_name => $script_name,
236 else => 1
237 ); # we show only the TMPL_VAR names $op
240 if ( $op eq 'update_and_reedit' ) {
241 foreach ( $input->param ) {
243 my $value = '';
244 if ( my $currentorder = $input->param('currentorder') ) {
245 my @currentorder = split /\|/, $currentorder;
246 my $orderchanged = 0;
247 foreach my $param ( $input->param ) {
248 if ( $param =~ m#up-(\d+).x# ) {
249 my $temp = $currentorder[$1];
250 $currentorder[$1] = $currentorder[ $1 - 1 ];
251 $currentorder[ $1 - 1 ] = $temp;
252 $orderchanged = 1;
253 last;
254 } elsif ( $param =~ m#down-(\d+).x# ) {
255 my $temp = $currentorder[$1];
256 $currentorder[$1] = $currentorder[ $1 + 1 ];
257 $currentorder[ $1 + 1 ] = $temp;
258 $orderchanged = 1;
259 last;
262 $value = join ' ', @currentorder;
263 if ($orderchanged) {
264 $op = 'add_form';
265 $template->param(
266 script_name => $script_name,
267 $op => 1
268 ); # we show only the TMPL_VAR names $op
269 } else {
270 $op = '';
271 $searchfield = '';
272 $template->param(
273 script_name => $script_name,
274 else => 1
275 ); # we show only the TMPL_VAR names $op
278 my $variable = $input->param('variable');
279 C4::Context->set_preference($variable, $value) unless C4::Context->config('demo');
282 ################## ADD_FORM ##################################
283 # called by default. Used to create form to add or modify a record
285 if ( $op eq 'add_form' ) {
287 #---- if primkey exists, it's a modify action, so read values to modify...
288 my $data;
289 if ($searchfield) {
290 my $dbh = C4::Context->dbh;
291 my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
292 $sth->execute($searchfield);
293 $data = $sth->fetchrow_hashref;
294 $template->param( modify => 1 );
296 # save tab to return to if user cancels edit
297 $template->param( return_tab => $tabsysprefs{$searchfield} );
300 $data->{'lang'} = $template->param('lang');
301 my $prefparams = GetPrefParams($data);
302 $template->param( %$prefparams );
303 $template->param( searchfield => $searchfield );
305 ################## ADD_VALIDATE ##################################
306 # called by add_form, used to insert/modify data in DB
307 } elsif ( $op eq 'add_validate' ) {
308 # to handle multiple values
309 my $value;
311 my $variable = $input->param('variable');
312 my $expl = $input->param('explanation');
313 my $type = $input->param('preftype');
314 my $options = $input->param('prefoptions');
316 # handle multiple value strings (separated by ',')
317 my $params = $input->Vars;
318 if ( defined $params->{'value'} ) {
319 my @values = ();
320 @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
321 if (@values) {
322 $value = "";
323 for my $vl (@values) {
324 $value .= "$vl,";
326 $value =~ s/,$//;
327 } else {
328 $value = $params->{'value'};
332 if ( $type eq 'Upload' ) {
333 my $lgtfh = $input->upload('value');
334 $value = join '', <$lgtfh>;
335 $value = encode_base64($value);
338 C4::Context->set_preference( $variable, $value, $expl, $type, $options )
339 unless C4::Context->config('demo');
340 print $input->redirect("/cgi-bin/koha/admin/systempreferences.pl?tab=");
341 exit;
342 ################## DELETE_CONFIRM ##################################
343 # called by default form, used to confirm deletion of data in DB
344 } elsif ( $op eq 'delete_confirm' ) {
345 my $value = C4::Context->preference($searchfield);
346 $template->param(
347 searchfield => $searchfield,
348 Tvalue => $value,
351 # END $OP eq DELETE_CONFIRM
352 ################## DELETE_CONFIRMED ##################################
353 # called by delete_confirm, used to effectively confirm deletion of data in DB
354 } elsif ( $op eq 'delete_confirmed' ) {
355 C4::Context->delete_preference($searchfield);
356 # END $OP eq DELETE_CONFIRMED
357 ################## DEFAULT ##################################
358 } else { # DEFAULT
359 #Adding tab management for system preferences
360 my $tab = $input->param('tab')||'local_use';
361 $template->param( $tab => 1 );
362 my ( $count, $results ) = StringSearch( $searchfield, $tab );
363 my @loop_data = ();
364 for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
365 my $row_data = $results->[$i];
366 $row_data->{'lang'} = $template->param('lang');
367 $row_data = GetPrefParams($row_data); # get a fresh hash for the row data
368 $row_data->{edit} = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'variable'};
369 $row_data->{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'variable'};
370 push( @loop_data, $row_data );
372 $template->param( loop => \@loop_data );
373 if ( $offset > 0 ) {
374 my $prevpage = $offset - $pagesize;
375 $template->param( "<a href=$script_name?offset=" . $prevpage . '&lt;&lt; Prev</a>' );
377 if ( $offset + $pagesize < $count ) {
378 my $nextpage = $offset + $pagesize;
379 $template->param( "a href=$script_name?offset=" . $nextpage . 'Next &gt;&gt;</a>' );
381 $template->param( tab => $tab, );
382 } #---- END $OP eq DEFAULT
383 output_html_with_http_headers $input, $cookie, $template->output;
386 # Return an array containing all preferences defined in current Koha instance
387 # .pref files.
389 sub get_prefs_from_files {
390 my $context = C4::Context->new();
391 my $path_pref_en = $context->config('intrahtdocs') .
392 '/prog/en/modules/admin/preferences';
393 # Get all .pref file names
394 opendir ( my $fh, $path_pref_en );
395 my @pref_files = grep { /.pref/ } readdir($fh);
396 close $fh;
398 my @names = ();
399 my $append = sub {
400 my $prefs = shift;
401 for my $pref ( @$prefs ) {
402 for my $element ( @$pref ) {
403 if ( ref( $element) eq 'HASH' ) {
404 my $name = $element->{pref};
405 next unless $name;
406 push @names, $name;
407 next;
412 for my $file (@pref_files) {
413 my $pref = LoadFile( "$path_pref_en/$file" );
414 for my $tab ( keys %$pref ) {
415 my $content = $pref->{$tab};
416 if ( ref($content) eq 'ARRAY' ) {
417 $append->($content);
418 next;
420 for my $section ( keys %$content ) {
421 my $syspref = $content->{$section};
422 $append->($syspref);
426 return @names;
430 # Return an array containg all preferences defined in DB
432 sub get_prefs_from_db {
433 my $dbh = C4::Context->dbh;
434 my $sth = $dbh->prepare("SELECT variable FROM systempreferences");
435 $sth->execute;
436 my @names = ();
437 while ( (my $name) = $sth->fetchrow_array ) {
438 push @names, $name if $name;
440 return @names;
444 # Return an array containing all local preferences: those which are defined in
445 # DB and not defined in Koha .pref files.
447 sub get_local_prefs {
448 my @prefs_file = get_prefs_from_files();
449 my @prefs_db = get_prefs_from_db();
451 my %prefs_file = map { lc $_ => 1 } @prefs_file;
452 my @names = ();
453 foreach my $name (@prefs_db) {
454 push @names, $name unless $prefs_file{lc $name};
457 return @names;