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 client
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;
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 authnotrequired => 0,
219 flagsrequired => { parameters => 'manage_sysprefs' },
224 my $op = $input->param('op') || '';
225 $searchfield =~ s/\,//g;
229 script_name => $script_name,
231 ); # we show only the TMPL_VAR names $op
234 script_name => $script_name,
236 ); # we show only the TMPL_VAR names $op
239 if ( $op eq 'update_and_reedit' ) {
240 foreach ( $input->param ) {
243 if ( my $currentorder = $input->param('currentorder') ) {
244 my @currentorder = split /\|/, $currentorder;
245 my $orderchanged = 0;
246 foreach my $param ( $input->param ) {
247 if ( $param =~ m#up-(\d+).x# ) {
248 my $temp = $currentorder[$1];
249 $currentorder[$1] = $currentorder[ $1 - 1 ];
250 $currentorder[ $1 - 1 ] = $temp;
253 } elsif ( $param =~ m#down-(\d+).x# ) {
254 my $temp = $currentorder[$1];
255 $currentorder[$1] = $currentorder[ $1 + 1 ];
256 $currentorder[ $1 + 1 ] = $temp;
261 $value = join ' ', @currentorder;
265 script_name => $script_name,
267 ); # we show only the TMPL_VAR names $op
272 script_name => $script_name,
274 ); # we show only the TMPL_VAR names $op
277 my $variable = $input->param('variable');
278 C4::Context->set_preference($variable, $value);
281 ################## ADD_FORM ##################################
282 # called by default. Used to create form to add or modify a record
284 if ( $op eq 'add_form' ) {
286 #---- if primkey exists, it's a modify action, so read values to modify...
289 my $dbh = C4::Context->dbh;
290 my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
291 $sth->execute($searchfield);
292 $data = $sth->fetchrow_hashref;
293 $template->param( modify => 1 );
295 # save tab to return to if user cancels edit
296 $template->param( return_tab => $tabsysprefs{$searchfield} );
299 $data->{'lang'} = $template->param('lang');
300 my $prefparams = GetPrefParams($data);
301 $template->param( %$prefparams );
302 $template->param( searchfield => $searchfield );
304 ################## ADD_VALIDATE ##################################
305 # called by add_form, used to insert/modify data in DB
306 } elsif ( $op eq 'add_validate' ) {
307 # to handle multiple values
310 my $variable = $input->param('variable');
311 my $expl = $input->param('explanation');
312 my $type = $input->param('preftype');
313 my $options = $input->param('prefoptions');
315 # handle multiple value strings (separated by ',')
316 my $params = $input->Vars;
317 if ( defined $params->{'value'} ) {
319 @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
322 for my $vl (@values) {
327 $value = $params->{'value'};
331 if ( $type eq 'Upload' ) {
332 my $lgtfh = $input->upload('value');
333 $value = join '', <$lgtfh>;
334 $value = encode_base64($value);
337 C4::Context->set_preference( $variable, $value, $expl, $type, $options );
338 print $input->redirect("/cgi-bin/koha/admin/systempreferences.pl?tab=");
340 ################## DELETE_CONFIRM ##################################
341 # called by default form, used to confirm deletion of data in DB
342 } elsif ( $op eq 'delete_confirm' ) {
343 my $value = C4::Context->preference($searchfield);
345 searchfield => $searchfield,
349 # END $OP eq DELETE_CONFIRM
350 ################## DELETE_CONFIRMED ##################################
351 # called by delete_confirm, used to effectively confirm deletion of data in DB
352 } elsif ( $op eq 'delete_confirmed' ) {
353 C4::Context->delete_preference($searchfield);
354 # END $OP eq DELETE_CONFIRMED
355 ################## DEFAULT ##################################
357 #Adding tab management for system preferences
358 my $tab = $input->param('tab')||'local_use';
359 $template->param( $tab => 1 );
360 my ( $count, $results ) = StringSearch( $searchfield, $tab );
362 for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
363 my $row_data = $results->[$i];
364 $row_data->{'lang'} = $template->param('lang');
365 $row_data = GetPrefParams($row_data); # get a fresh hash for the row data
366 $row_data->{edit} = "$script_name?op=add_form&searchfield=" . $results->[$i]{'variable'};
367 $row_data->{delete} = "$script_name?op=delete_confirm&searchfield=" . $results->[$i]{'variable'};
368 push( @loop_data, $row_data );
370 $template->param( loop => \@loop_data );
372 my $prevpage = $offset - $pagesize;
373 $template->param( "<a href=$script_name?offset=" . $prevpage . '<< Prev</a>' );
375 if ( $offset + $pagesize < $count ) {
376 my $nextpage = $offset + $pagesize;
377 $template->param( "a href=$script_name?offset=" . $nextpage . 'Next >></a>' );
379 $template->param( tab => $tab, );
380 } #---- END $OP eq DEFAULT
381 output_html_with_http_headers $input, $cookie, $template->output;
384 # Return an array containing all preferences defined in current Koha instance
387 sub get_prefs_from_files {
388 my $context = C4::Context->new();
389 my $path_pref_en = $context->config('intrahtdocs') .
390 '/prog/en/modules/admin/preferences';
391 # Get all .pref file names
392 opendir ( my $fh, $path_pref_en );
393 my @pref_files = grep { /.pref$/ } readdir($fh);
399 for my $pref ( @$prefs ) {
400 for my $element ( @$pref ) {
401 if ( ref( $element) eq 'HASH' ) {
402 my $name = $element->{pref};
410 for my $file (@pref_files) {
411 my $pref = LoadFile( "$path_pref_en/$file" );
412 for my $tab ( keys %$pref ) {
413 my $content = $pref->{$tab};
414 if ( ref($content) eq 'ARRAY' ) {
418 for my $section ( keys %$content ) {
419 my $syspref = $content->{$section};
428 # Return an array containg all preferences defined in DB
430 sub get_prefs_from_db {
431 my $dbh = C4::Context->dbh;
432 my $sth = $dbh->prepare("SELECT variable FROM systempreferences");
435 while ( (my $name) = $sth->fetchrow_array ) {
436 push @names, $name if $name;
442 # Return an array containing all local preferences: those which are defined in
443 # DB and not defined in Koha .pref files.
445 sub get_local_prefs {
446 my @prefs_file = get_prefs_from_files();
447 my @prefs_db = get_prefs_from_db();
449 my %prefs_file = map { lc $_ => 1 } @prefs_file;
451 foreach my $name (@prefs_db) {
452 push @names, $name unless $prefs_file{lc $name};