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
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.
32 - if primkey exists, this is a modification,so we read the $primkey record
33 - builds the add/modify form
35 - the user has just send datas, so we create/modify the record
37 - we show the record having primkey=$primkey and ask for deletion validation form
39 - we delete the record having primkey=$primkey
51 use C4
::Languages
qw(getTranslatedLanguages);
55 use YAML
::Syck
qw( Dump LoadFile );
57 my %tabsysprefs; #we do no longer need to keep track of a tab per pref (yaml)
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 );
71 my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable in (";
74 for my $name ( get_local_prefs
() ) {
75 $strsth .= ',' unless $first;
77 push(@sql_bind,$name);
80 $strsth .= ") order by variable";
81 $sth = $dbh->prepare($strsth);
82 $sth->execute(@sql_bind);
84 while ( my $data = $sth->fetchrow_hashref ) {
85 unless (defined $data->{value
}) { $data->{value
} = "";}
86 $data->{shortvalue
} = $data->{value
};
87 $data->{shortvalue
} = substr( $data->{value
}, 0, 60 ) . "..." if length( $data->{value
} ) > 60;
88 push( @results, $data );
92 return ( $cnt, \
@results );
100 if ( defined $data->{'options'} ) {
101 foreach my $option ( split( /\|/, $data->{'options'} ) ) {
103 defined( $data->{'value'} ) and $option eq $data->{'value'} and $selected = 1;
104 push @options, { option
=> $option, selected
=> $selected };
108 $params->{'prefoptions'} = $data->{'options'};
110 if ( not defined( $data->{'type'} ) ) {
111 $params->{'type_free'} = 1;
112 $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 );
113 } elsif ( $data->{'type'} eq 'Upload' ) {
114 $params->{'type_upload'} = 1;
115 } elsif ( $data->{'type'} eq 'Choice' ) {
116 $params->{'type_choice'} = 1;
117 } elsif ( $data->{'type'} eq 'YesNo' ) {
118 $params->{'type_yesno'} = 1;
119 $data->{'value'} = C4
::Context
->boolean_preference( $data->{'variable'} );
120 if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
121 $params->{'value_yes'} = 1;
123 $params->{'value_no'} = 1;
125 } elsif ( $data->{'type'} eq 'Integer' || $data->{'type'} eq 'Float' ) {
126 $params->{'type_free'} = 1;
127 $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ?
$data->{'options'} : 10;
128 } elsif ( $data->{'type'} eq 'Textarea' ) {
129 $params->{'type_textarea'} = 1;
130 $data->{options
} =~ /(.*)\|(.*)/;
131 $params->{'cols'} = $1;
132 $params->{'rows'} = $2;
133 } elsif ( $data->{'type'} eq 'Themes' ) {
134 $params->{'type_choice'} = 1;
136 ( $data->{'variable'} =~ m
#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
138 my $currently_selected_themes;
140 foreach my $theme ( split /\s+/, $data->{'value'} ) {
141 push @options, { option
=> $theme, counter
=> $counter };
142 $currently_selected_themes->{$theme} = 1;
145 foreach my $theme ( getallthemes
($type) ) {
147 next if $currently_selected_themes->{$theme};
148 push @options, { option
=> $theme, counter
=> $counter };
151 } elsif ( $data->{'type'} eq 'ClassSources' ) {
152 $params->{'type_choice'} = 1;
155 my $sources = GetClassSources
();
157 foreach my $cn_source ( sort keys %$sources ) {
158 if ( $cn_source eq $data->{'value'} ) {
159 push @options, { option
=> $cn_source, counter
=> $counter, selected
=> 1 };
161 push @options, { option
=> $cn_source, counter
=> $counter };
165 } elsif ( $data->{'type'} eq 'Languages' ) {
166 my $currently_selected_languages;
167 foreach my $language ( split /\s+/, $data->{'value'} ) {
168 $currently_selected_languages->{$language} = 1;
172 my $lang = $params->{'lang'};
175 if ( $data->{'variable'} =~ /opac/ ) {
179 $theme = C4
::Context
->preference('opacthemes');
182 # this is the staff client
183 $interface = 'intranet';
184 $theme = C4
::Context
->preference('template');
186 my $languages_loop = getTranslatedLanguages
( $interface, $theme, $lang, $currently_selected_languages );
188 $params->{'languages_loop'} = $languages_loop;
189 $params->{'type_langselector'} = 1;
191 $params->{'type_free'} = 1;
192 $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ?
$data->{'options'} : 30;
195 if ( $params->{'type_choice'} || $params->{'type_free'} || $params->{'type_yesno'} ) {
196 $params->{'oneline'} = 1;
199 $params->{'preftype'} = $data->{'type'};
200 $params->{'options'} = \
@options;
206 my $searchfield = $input->param('searchfield') || '';
207 my $Tvalue = $input->param('Tvalue');
208 my $offset = $input->param('offset') || 0;
209 my $script_name = "/cgi-bin/koha/admin/systempreferences.pl";
211 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
212 { template_name
=> "admin/systempreferences.tt",
215 authnotrequired
=> 0,
216 flagsrequired
=> { parameters
=> 'parameters_remaining_permissions' },
221 my $op = $input->param('op') || '';
222 $searchfield =~ s/\,//g;
226 script_name
=> $script_name,
228 ); # we show only the TMPL_VAR names $op
231 script_name
=> $script_name,
233 ); # we show only the TMPL_VAR names $op
236 if ( $op eq 'update_and_reedit' ) {
237 foreach ( $input->param ) {
240 if ( my $currentorder = $input->param('currentorder') ) {
241 my @currentorder = split /\|/, $currentorder;
242 my $orderchanged = 0;
243 foreach my $param ( $input->param ) {
244 if ( $param =~ m
#up-(\d+).x# ) {
245 my $temp = $currentorder[$1];
246 $currentorder[$1] = $currentorder[ $1 - 1 ];
247 $currentorder[ $1 - 1 ] = $temp;
250 } elsif ( $param =~ m
#down-(\d+).x# ) {
251 my $temp = $currentorder[$1];
252 $currentorder[$1] = $currentorder[ $1 + 1 ];
253 $currentorder[ $1 + 1 ] = $temp;
258 $value = join ' ', @currentorder;
262 script_name
=> $script_name,
264 ); # we show only the TMPL_VAR names $op
269 script_name
=> $script_name,
271 ); # we show only the TMPL_VAR names $op
274 my $dbh = C4
::Context
->dbh;
275 my $query = "select * from systempreferences where variable=?";
276 my $sth = $dbh->prepare($query);
277 $sth->execute( $input->param('variable') );
279 unless ( C4
::Context
->config('demo') ) {
280 my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
281 $sth->execute( $value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions') );
282 logaction
( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
285 unless ( C4
::Context
->config('demo') ) {
286 my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
287 $sth->execute( $input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
288 logaction
( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $input->param('value') );
294 ################## ADD_FORM ##################################
295 # called by default. Used to create form to add or modify a record
297 if ( $op eq 'add_form' ) {
299 #---- if primkey exists, it's a modify action, so read values to modify...
302 my $dbh = C4
::Context
->dbh;
303 my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
304 $sth->execute($searchfield);
305 $data = $sth->fetchrow_hashref;
306 $template->param( modify
=> 1 );
308 # save tab to return to if user cancels edit
309 $template->param( return_tab
=> $tabsysprefs{$searchfield} );
312 $data->{'lang'} = $template->param('lang');
313 my $prefparams = GetPrefParams
($data);
314 $template->param( %$prefparams );
315 $template->param( searchfield
=> $searchfield );
317 ################## ADD_VALIDATE ##################################
318 # called by add_form, used to insert/modify data in DB
319 } elsif ( $op eq 'add_validate' ) {
320 my $dbh = C4
::Context
->dbh;
321 my $sth = $dbh->prepare("select * from systempreferences where variable=?");
322 $sth->execute( $input->param('variable') );
324 # to handle multiple values
327 # handle multiple value strings (separated by ',')
328 my $params = $input->Vars;
329 if ( defined $params->{'value'} ) {
331 @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
334 for my $vl (@values) {
339 $value = $params->{'value'};
343 if ( $input->param('preftype') eq 'Upload' ) {
344 my $lgtfh = $input->upload('value');
345 $value = join '', <$lgtfh>;
346 $value = encode_base64
($value);
350 unless ( C4
::Context
->config('demo') ) {
351 my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
352 $sth->execute( $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable') );
353 logaction
( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
356 unless ( C4
::Context
->config('demo') ) {
357 my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
358 $sth->execute( $input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
359 logaction
( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $value );
362 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=\"></html>";
364 ################## DELETE_CONFIRM ##################################
365 # called by default form, used to confirm deletion of data in DB
366 } elsif ( $op eq 'delete_confirm' ) {
367 my $dbh = C4
::Context
->dbh;
368 my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
369 $sth->execute($searchfield);
370 my $data = $sth->fetchrow_hashref;
372 searchfield
=> $searchfield,
373 Tvalue
=> $data->{'value'},
376 # END $OP eq DELETE_CONFIRM
377 ################## DELETE_CONFIRMED ##################################
378 # called by delete_confirm, used to effectively confirm deletion of data in DB
379 } elsif ( $op eq 'delete_confirmed' ) {
380 my $dbh = C4
::Context
->dbh;
381 my $sth = $dbh->prepare("delete from systempreferences where variable=?");
382 $sth->execute($searchfield);
383 my $logstring = $searchfield . " | " . $Tvalue;
384 logaction
( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring );
386 # END $OP eq DELETE_CONFIRMED
387 ################## DEFAULT ##################################
389 #Adding tab management for system preferences
390 my $tab = $input->param('tab')||'local_use';
391 $template->param( $tab => 1 );
392 my ( $count, $results ) = StringSearch
( $searchfield, $tab );
394 for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ?
$offset + $pagesize : $count ) ; $i++ ) {
395 my $row_data = $results->[$i];
396 $row_data->{'lang'} = $template->param('lang');
397 $row_data = GetPrefParams
($row_data); # get a fresh hash for the row data
398 $row_data->{edit
} = "$script_name?op=add_form&searchfield=" . $results->[$i]{'variable'};
399 $row_data->{delete} = "$script_name?op=delete_confirm&searchfield=" . $results->[$i]{'variable'};
400 push( @loop_data, $row_data );
402 $template->param( loop => \
@loop_data );
404 my $prevpage = $offset - $pagesize;
405 $template->param( "<a href=$script_name?offset=" . $prevpage . '<< Prev</a>' );
407 if ( $offset + $pagesize < $count ) {
408 my $nextpage = $offset + $pagesize;
409 $template->param( "a href=$script_name?offset=" . $nextpage . 'Next >></a>' );
411 $template->param( tab
=> $tab, );
412 } #---- END $OP eq DEFAULT
413 output_html_with_http_headers
$input, $cookie, $template->output;
416 # Return an array containing all preferences defined in current Koha instance
419 sub get_prefs_from_files
{
420 my $context = C4
::Context
->new();
421 my $path_pref_en = $context->config('intrahtdocs') .
422 '/prog/en/modules/admin/preferences';
423 # Get all .pref file names
424 opendir ( my $fh, $path_pref_en );
425 my @pref_files = grep { /.pref/ } readdir($fh);
431 for my $pref ( @
$prefs ) {
432 for my $element ( @
$pref ) {
433 if ( ref( $element) eq 'HASH' ) {
434 my $name = $element->{pref
};
442 for my $file (@pref_files) {
443 my $pref = LoadFile
( "$path_pref_en/$file" );
444 for my $tab ( keys %$pref ) {
445 my $content = $pref->{$tab};
446 if ( ref($content) eq 'ARRAY' ) {
450 for my $section ( keys %$content ) {
451 my $syspref = $content->{$section};
460 # Return an array containg all preferences defined in DB
462 sub get_prefs_from_db
{
463 my $dbh = C4
::Context
->dbh;
464 my $sth = $dbh->prepare("SELECT variable FROM systempreferences");
467 while ( (my $name) = $sth->fetchrow_array ) {
468 push @names, $name if $name;
474 # Return an array containing all local preferences: those which are defined in
475 # DB and not defined in Koha .pref files.
477 sub get_local_prefs
{
478 my @prefs_file = get_prefs_from_files
();
479 my @prefs_db = get_prefs_from_db
();
481 my %prefs_file = map { lc $_ => 1 } @prefs_file;
483 foreach my $name (@prefs_db) {
484 push @names, $name unless $prefs_file{lc $name};