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
50 use C4
::Languages
qw(getTranslatedLanguages);
53 use YAML
::Syck
qw( Dump LoadFile );
55 my %tabsysprefs; #we do no longer need to keep track of a tab per pref (yaml)
58 my ( $searchstring, $tab ) = @_;
59 return (0,[]) if $tab ne 'local_use';
61 my $dbh = C4
::Context
->dbh;
62 $searchstring =~ s/\'/\\\'/g;
63 my @data = split( ' ', $searchstring );
69 my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable in (";
72 for my $name ( get_local_prefs
() ) {
73 $strsth .= ',' unless $first;
75 push(@sql_bind,$name);
78 $strsth .= ") order by variable";
79 $sth = $dbh->prepare($strsth);
80 $sth->execute(@sql_bind);
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 );
90 return ( $cnt, \
@results );
98 if ( defined $data->{'options'} ) {
99 foreach my $option ( split( /\|/, $data->{'options'} ) ) {
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;
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 '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;
139 ( $data->{'variable'} =~ m
#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
141 my $currently_selected_themes;
143 foreach my $theme ( split /\s+/, $data->{'value'} ) {
144 push @options, { option
=> $theme, counter
=> $counter };
145 $currently_selected_themes->{$theme} = 1;
148 foreach my $theme ( getallthemes
($type) ) {
150 next if $currently_selected_themes->{$theme};
151 push @options, { option
=> $theme, counter
=> $counter };
154 } elsif ( $data->{'type'} eq 'ClassSources' ) {
155 $params->{'type_choice'} = 1;
158 my $sources = GetClassSources
();
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 };
164 push @options, { option
=> $cn_source, counter
=> $counter };
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;
175 my $lang = $params->{'lang'};
178 if ( $data->{'variable'} =~ /opac/ ) {
182 $theme = C4
::Context
->preference('opacthemes');
185 # this is the staff interface
186 $interface = 'intranet';
187 $theme = C4
::Context
->preference('template');
189 my $languages_loop = getTranslatedLanguages
( $interface, $theme, $lang, $currently_selected_languages );
191 $params->{'languages_loop'} = $languages_loop;
192 $params->{'type_langselector'} = 1;
194 $params->{'type_free'} = 1;
195 $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ?
$data->{'options'} : 30;
198 if ( $params->{'type_choice'} || $params->{'type_free'} || $params->{'type_yesno'} ) {
199 $params->{'oneline'} = 1;
202 $params->{'preftype'} = $data->{'type'};
203 $params->{'options'} = \
@options;
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";
214 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
215 { template_name
=> "admin/systempreferences.tt",
218 flagsrequired
=> { parameters
=> 'manage_sysprefs' },
223 my $op = $input->param('op') || '';
224 $searchfield =~ s/\,//g;
228 script_name
=> $script_name,
230 ); # we show only the TMPL_VAR names $op
233 script_name
=> $script_name,
235 ); # we show only the TMPL_VAR names $op
238 if ( $op eq 'update_and_reedit' ) {
239 foreach ( $input->param ) {
242 if ( my $currentorder = $input->param('currentorder') ) {
243 my @currentorder = split /\|/, $currentorder;
244 my $orderchanged = 0;
245 foreach my $param ( $input->param ) {
246 if ( $param =~ m
#up-(\d+).x# ) {
247 my $temp = $currentorder[$1];
248 $currentorder[$1] = $currentorder[ $1 - 1 ];
249 $currentorder[ $1 - 1 ] = $temp;
252 } elsif ( $param =~ m
#down-(\d+).x# ) {
253 my $temp = $currentorder[$1];
254 $currentorder[$1] = $currentorder[ $1 + 1 ];
255 $currentorder[ $1 + 1 ] = $temp;
260 $value = join ' ', @currentorder;
264 script_name
=> $script_name,
266 ); # we show only the TMPL_VAR names $op
271 script_name
=> $script_name,
273 ); # we show only the TMPL_VAR names $op
276 my $variable = $input->param('variable');
277 C4
::Context
->set_preference($variable, $value);
280 ################## ADD_FORM ##################################
281 # called by default. Used to create form to add or modify a record
283 if ( $op eq 'add_form' ) {
285 #---- if primkey exists, it's a modify action, so read values to modify...
288 my $dbh = C4
::Context
->dbh;
289 my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
290 $sth->execute($searchfield);
291 $data = $sth->fetchrow_hashref;
292 $template->param( modify
=> 1 );
294 # save tab to return to if user cancels edit
295 $template->param( return_tab
=> $tabsysprefs{$searchfield} );
298 $data->{'lang'} = $template->param('lang');
299 my $prefparams = GetPrefParams
($data);
300 $template->param( %$prefparams );
301 $template->param( searchfield
=> $searchfield );
303 ################## ADD_VALIDATE ##################################
304 # called by add_form, used to insert/modify data in DB
305 } elsif ( $op eq 'add_validate' ) {
306 # to handle multiple values
309 my $variable = $input->param('variable');
310 my $expl = $input->param('explanation');
311 my $type = $input->param('preftype');
312 my $options = $input->param('prefoptions');
314 # handle multiple value strings (separated by ',')
315 my $params = $input->Vars;
316 if ( defined $params->{'value'} ) {
318 @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
321 for my $vl (@values) {
326 $value = $params->{'value'};
330 if ( $type eq 'Upload' ) {
331 my $lgtfh = $input->upload('value');
332 $value = join '', <$lgtfh>;
333 $value = encode_base64
($value);
336 C4
::Context
->set_preference( $variable, $value, $expl, $type, $options );
337 print $input->redirect("/cgi-bin/koha/admin/systempreferences.pl?tab=");
339 ################## DELETE_CONFIRM ##################################
340 # called by default form, used to confirm deletion of data in DB
341 } elsif ( $op eq 'delete_confirm' ) {
342 my $value = C4
::Context
->preference($searchfield);
344 searchfield
=> $searchfield,
348 # END $OP eq DELETE_CONFIRM
349 ################## DELETE_CONFIRMED ##################################
350 # called by delete_confirm, used to effectively confirm deletion of data in DB
351 } elsif ( $op eq 'delete_confirmed' ) {
352 C4
::Context
->delete_preference($searchfield);
353 # END $OP eq DELETE_CONFIRMED
354 ################## DEFAULT ##################################
356 #Adding tab management for system preferences
357 my $tab = $input->param('tab')||'local_use';
358 $template->param( $tab => 1 );
359 my ( $count, $results ) = StringSearch
( $searchfield, $tab );
361 for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ?
$offset + $pagesize : $count ) ; $i++ ) {
362 my $row_data = $results->[$i];
363 $row_data->{'lang'} = $template->param('lang');
364 $row_data = GetPrefParams
($row_data); # get a fresh hash for the row data
365 $row_data->{edit
} = "$script_name?op=add_form&searchfield=" . $results->[$i]{'variable'};
366 $row_data->{delete} = "$script_name?op=delete_confirm&searchfield=" . $results->[$i]{'variable'};
367 push( @loop_data, $row_data );
369 $template->param( loop => \
@loop_data );
371 my $prevpage = $offset - $pagesize;
372 $template->param( "<a href=$script_name?offset=" . $prevpage . '<< Prev</a>' );
374 if ( $offset + $pagesize < $count ) {
375 my $nextpage = $offset + $pagesize;
376 $template->param( "a href=$script_name?offset=" . $nextpage . 'Next >></a>' );
378 $template->param( tab
=> $tab, );
379 } #---- END $OP eq DEFAULT
380 output_html_with_http_headers
$input, $cookie, $template->output;
383 # Return an array containing all preferences defined in current Koha instance
386 sub get_prefs_from_files
{
387 my $context = C4
::Context
->new();
388 my $path_pref_en = $context->config('intrahtdocs') .
389 '/prog/en/modules/admin/preferences';
390 # Get all .pref file names
391 opendir ( my $fh, $path_pref_en );
392 my @pref_files = grep { /.pref$/ } readdir($fh);
398 for my $pref ( @
$prefs ) {
399 for my $element ( @
$pref ) {
400 if ( ref( $element) eq 'HASH' ) {
401 my $name = $element->{pref
};
409 for my $file (@pref_files) {
410 my $pref = LoadFile
( "$path_pref_en/$file" );
411 for my $tab ( keys %$pref ) {
412 my $content = $pref->{$tab};
413 if ( ref($content) eq 'ARRAY' ) {
417 for my $section ( keys %$content ) {
418 my $syspref = $content->{$section};
427 # Return an array containg all preferences defined in DB
429 sub get_prefs_from_db
{
430 my $dbh = C4
::Context
->dbh;
431 my $sth = $dbh->prepare("SELECT variable FROM systempreferences");
434 while ( (my $name) = $sth->fetchrow_array ) {
435 push @names, $name if $name;
441 # Return an array containing all local preferences: those which are defined in
442 # DB and not defined in Koha .pref files.
444 sub get_local_prefs
{
445 my @prefs_file = get_prefs_from_files
();
446 my @prefs_db = get_prefs_from_db
();
448 my %prefs_file = map { lc $_ => 1 } @prefs_file;
450 foreach my $name (@prefs_db) {
451 push @names, $name unless $prefs_file{lc $name};