Bug 13199: Add missing notices for several installations
[koha.git] / admin / systempreferences.pl
blobf41fe76c9620bb6f9a01ef1941b637bea37cd3af
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 under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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;
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::Log;
54 use C4::Output;
55 use YAML::Syck qw( Dump LoadFile );
57 my %tabsysprefs; #we do no longer need to keep track of a tab per pref (yaml)
59 sub StringSearch {
60 my ( $searchstring, $tab ) = @_;
61 return (0,[]) if $tab ne 'local_use';
63 my $dbh = C4::Context->dbh;
64 $searchstring =~ s/\'/\\\'/g;
65 my @data = split( ' ', $searchstring );
66 my $count = @data;
67 my @results;
68 my $cnt = 0;
69 my $sth;
71 my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable in (";
72 my $first = 1;
73 for my $name ( get_local_prefs() ) {
74 $strsth .= ',' unless $first;
75 $strsth .= "'$name'";
76 $first = 0;
78 $strsth .= ") order by variable";
79 $sth = $dbh->prepare($strsth);
80 $sth->execute();
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++;
90 return ( $cnt, \@results );
93 sub GetPrefParams {
94 my $data = shift;
95 my $params = $data;
96 my @options;
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 };
106 $params->{'prefoptions'} = $data->{'options'};
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;
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 'Themes' ) {
132 $params->{'type_choice'} = 1;
133 my $type = '';
134 ( $data->{'variable'} =~ m#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
135 @options = ();
136 my $currently_selected_themes;
137 my $counter = 0;
138 foreach my $theme ( split /\s+/, $data->{'value'} ) {
139 push @options, { option => $theme, counter => $counter };
140 $currently_selected_themes->{$theme} = 1;
141 $counter++;
143 foreach my $theme ( getallthemes($type) ) {
144 my $selected = '0';
145 next if $currently_selected_themes->{$theme};
146 push @options, { option => $theme, counter => $counter };
147 $counter++;
149 } elsif ( $data->{'type'} eq 'ClassSources' ) {
150 $params->{'type_choice'} = 1;
151 my $type = '';
152 @options = ();
153 my $sources = GetClassSources();
154 my $counter = 0;
155 foreach my $cn_source ( sort keys %$sources ) {
156 if ( $cn_source eq $data->{'value'} ) {
157 push @options, { option => $cn_source, counter => $counter, selected => 1 };
158 } else {
159 push @options, { option => $cn_source, counter => $counter };
161 $counter++;
163 } elsif ( $data->{'type'} eq 'Languages' ) {
164 my $currently_selected_languages;
165 foreach my $language ( split /\s+/, $data->{'value'} ) {
166 $currently_selected_languages->{$language} = 1;
169 # current language
170 my $lang = $params->{'lang'};
171 my $theme;
172 my $interface;
173 if ( $data->{'variable'} =~ /opac/ ) {
175 # this is the OPAC
176 $interface = 'opac';
177 $theme = C4::Context->preference('opacthemes');
178 } else {
180 # this is the staff client
181 $interface = 'intranet';
182 $theme = C4::Context->preference('template');
184 my $languages_loop = getTranslatedLanguages( $interface, $theme, $lang, $currently_selected_languages );
186 $params->{'languages_loop'} = $languages_loop;
187 $params->{'type_langselector'} = 1;
188 } else {
189 $params->{'type_free'} = 1;
190 $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 30;
193 if ( $params->{'type_choice'} || $params->{'type_free'} || $params->{'type_yesno'} ) {
194 $params->{'oneline'} = 1;
197 $params->{'preftype'} = $data->{'type'};
198 $params->{'options'} = \@options;
200 return $params;
203 my $input = new CGI;
204 my $searchfield = $input->param('searchfield') || '';
205 my $Tvalue = $input->param('Tvalue');
206 my $offset = $input->param('offset') || 0;
207 my $script_name = "/cgi-bin/koha/admin/systempreferences.pl";
209 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
210 { template_name => "admin/systempreferences.tt",
211 query => $input,
212 type => "intranet",
213 authnotrequired => 0,
214 flagsrequired => { parameters => 'parameters_remaining_permissions' },
215 debug => 1,
218 my $pagesize = 100;
219 my $op = $input->param('op') || '';
220 $searchfield =~ s/\,//g;
222 if ($op) {
223 $template->param(
224 script_name => $script_name,
225 $op => 1
226 ); # we show only the TMPL_VAR names $op
227 } else {
228 $template->param(
229 script_name => $script_name,
230 else => 1
231 ); # we show only the TMPL_VAR names $op
234 if ( $op eq 'update_and_reedit' ) {
235 foreach ( $input->param ) {
237 my $value = '';
238 if ( my $currentorder = $input->param('currentorder') ) {
239 my @currentorder = split /\|/, $currentorder;
240 my $orderchanged = 0;
241 foreach my $param ( $input->param ) {
242 if ( $param =~ m#up-(\d+).x# ) {
243 my $temp = $currentorder[$1];
244 $currentorder[$1] = $currentorder[ $1 - 1 ];
245 $currentorder[ $1 - 1 ] = $temp;
246 $orderchanged = 1;
247 last;
248 } elsif ( $param =~ m#down-(\d+).x# ) {
249 my $temp = $currentorder[$1];
250 $currentorder[$1] = $currentorder[ $1 + 1 ];
251 $currentorder[ $1 + 1 ] = $temp;
252 $orderchanged = 1;
253 last;
256 $value = join ' ', @currentorder;
257 if ($orderchanged) {
258 $op = 'add_form';
259 $template->param(
260 script_name => $script_name,
261 $op => 1
262 ); # we show only the TMPL_VAR names $op
263 } else {
264 $op = '';
265 $searchfield = '';
266 $template->param(
267 script_name => $script_name,
268 else => 1
269 ); # we show only the TMPL_VAR names $op
272 my $dbh = C4::Context->dbh;
273 my $query = "select * from systempreferences where variable=?";
274 my $sth = $dbh->prepare($query);
275 $sth->execute( $input->param('variable') );
276 if ( $sth->rows ) {
277 unless ( C4::Context->config('demo') ) {
278 my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
279 $sth->execute( $value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions') );
280 logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
282 } else {
283 unless ( C4::Context->config('demo') ) {
284 my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
285 $sth->execute( $input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
286 logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $input->param('value') );
292 ################## ADD_FORM ##################################
293 # called by default. Used to create form to add or modify a record
295 if ( $op eq 'add_form' ) {
297 #---- if primkey exists, it's a modify action, so read values to modify...
298 my $data;
299 if ($searchfield) {
300 my $dbh = C4::Context->dbh;
301 my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
302 $sth->execute($searchfield);
303 $data = $sth->fetchrow_hashref;
304 $template->param( modify => 1 );
306 # save tab to return to if user cancels edit
307 $template->param( return_tab => $tabsysprefs{$searchfield} );
310 $data->{'lang'} = $template->param('lang');
311 my $prefparams = GetPrefParams($data);
312 $template->param( %$prefparams );
313 $template->param( searchfield => $searchfield );
315 ################## ADD_VALIDATE ##################################
316 # called by add_form, used to insert/modify data in DB
317 } elsif ( $op eq 'add_validate' ) {
318 my $dbh = C4::Context->dbh;
319 my $sth = $dbh->prepare("select * from systempreferences where variable=?");
320 $sth->execute( $input->param('variable') );
322 # to handle multiple values
323 my $value;
325 # handle multiple value strings (separated by ',')
326 my $params = $input->Vars;
327 if ( defined $params->{'value'} ) {
328 my @values = ();
329 @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
330 if (@values) {
331 $value = "";
332 for my $vl (@values) {
333 $value .= "$vl,";
335 $value =~ s/,$//;
336 } else {
337 $value = $params->{'value'};
341 if ( $input->param('preftype') eq 'Upload' ) {
342 my $lgtfh = $input->upload('value');
343 $value = join '', <$lgtfh>;
344 $value = encode_base64($value);
347 if ( $sth->rows ) {
348 unless ( C4::Context->config('demo') ) {
349 my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
350 $sth->execute( $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable') );
351 logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
353 } else {
354 unless ( C4::Context->config('demo') ) {
355 my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
356 $sth->execute( $input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
357 logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $value );
360 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=\"></html>";
361 exit;
362 ################## DELETE_CONFIRM ##################################
363 # called by default form, used to confirm deletion of data in DB
364 } elsif ( $op eq 'delete_confirm' ) {
365 my $dbh = C4::Context->dbh;
366 my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
367 $sth->execute($searchfield);
368 my $data = $sth->fetchrow_hashref;
369 $template->param(
370 searchfield => $searchfield,
371 Tvalue => $data->{'value'},
374 # END $OP eq DELETE_CONFIRM
375 ################## DELETE_CONFIRMED ##################################
376 # called by delete_confirm, used to effectively confirm deletion of data in DB
377 } elsif ( $op eq 'delete_confirmed' ) {
378 my $dbh = C4::Context->dbh;
379 my $sth = $dbh->prepare("delete from systempreferences where variable=?");
380 $sth->execute($searchfield);
381 my $logstring = $searchfield . " | " . $Tvalue;
382 logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring );
384 # END $OP eq DELETE_CONFIRMED
385 ################## DEFAULT ##################################
386 } else { # DEFAULT
387 #Adding tab management for system preferences
388 my $tab = $input->param('tab')||'local_use';
389 $template->param( $tab => 1 );
390 my ( $count, $results ) = StringSearch( $searchfield, $tab );
391 my @loop_data = ();
392 for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
393 my $row_data = $results->[$i];
394 $row_data->{'lang'} = $template->param('lang');
395 $row_data = GetPrefParams($row_data); # get a fresh hash for the row data
396 $row_data->{edit} = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'variable'};
397 $row_data->{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'variable'};
398 push( @loop_data, $row_data );
400 $template->param( loop => \@loop_data );
401 if ( $offset > 0 ) {
402 my $prevpage = $offset - $pagesize;
403 $template->param( "<a href=$script_name?offset=" . $prevpage . '&lt;&lt; Prev</a>' );
405 if ( $offset + $pagesize < $count ) {
406 my $nextpage = $offset + $pagesize;
407 $template->param( "a href=$script_name?offset=" . $nextpage . 'Next &gt;&gt;</a>' );
409 $template->param( tab => $tab, );
410 } #---- END $OP eq DEFAULT
411 output_html_with_http_headers $input, $cookie, $template->output;
414 # Return an array containing all preferences defined in current Koha instance
415 # .pref files.
417 sub get_prefs_from_files {
418 my $context = C4::Context->new();
419 my $path_pref_en = $context->config('intrahtdocs') .
420 '/prog/en/modules/admin/preferences';
421 # Get all .pref file names
422 opendir ( my $fh, $path_pref_en );
423 my @pref_files = grep { /.pref/ } readdir($fh);
424 close $fh;
426 my @names = ();
427 my $append = sub {
428 my $prefs = shift;
429 for my $pref ( @$prefs ) {
430 for my $element ( @$pref ) {
431 if ( ref( $element) eq 'HASH' ) {
432 my $name = $element->{pref};
433 next unless $name;
434 push @names, $name;
435 next;
440 for my $file (@pref_files) {
441 my $pref = LoadFile( "$path_pref_en/$file" );
442 for my $tab ( keys %$pref ) {
443 my $content = $pref->{$tab};
444 if ( ref($content) eq 'ARRAY' ) {
445 $append->($content);
446 next;
448 for my $section ( keys %$content ) {
449 my $syspref = $content->{$section};
450 $append->($syspref);
454 return @names;
458 # Return an array containg all preferences defined in DB
460 sub get_prefs_from_db {
461 my $dbh = C4::Context->dbh;
462 my $sth = $dbh->prepare("SELECT variable FROM systempreferences");
463 $sth->execute;
464 my @names = ();
465 while ( (my $name) = $sth->fetchrow_array ) {
466 push @names, $name if $name;
468 return @names;
472 # Return an array containing all local preferences: those which are defined in
473 # DB and not defined in Koha .pref files.
475 sub get_local_prefs {
476 my @prefs_file = get_prefs_from_files();
477 my @prefs_db = get_prefs_from_db();
479 my %prefs_file = map { lc $_ => 1 } @prefs_file;
480 my @names = ();
481 foreach my $name (@prefs_db) {
482 push @names, $name unless $prefs_file{lc $name};
485 return @names;