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);
54 use YAML::Syck qw( Dump LoadFile );
56 my %tabsysprefs; #we do no longer need to keep track of a tab per pref (yaml)
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 );
70 my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable in (";
73 for my $name ( get_local_prefs() ) {
74 $strsth .= ',' unless $first;
76 push(@sql_bind,$name);
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 );
91 return ( $cnt, \@results );
99 if ( defined $data->{'options'} ) {
100 foreach my $option ( split( /\|/, $data->{'options'} ) ) {
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;
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;
140 ( $data->{'variable'} =~ m#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
142 my $currently_selected_themes;
144 foreach my $theme ( split /\s+/, $data->{'value'} ) {
145 push @options, { option => $theme, counter => $counter };
146 $currently_selected_themes->{$theme} = 1;
149 foreach my $theme ( getallthemes($type) ) {
151 next if $currently_selected_themes->{$theme};
152 push @options, { option => $theme, counter => $counter };
155 } elsif ( $data->{'type'} eq 'ClassSources' ) {
156 $params->{'type_choice'} = 1;
159 my $sources = GetClassSources();
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 };
165 push @options, { option => $cn_source, counter => $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;
176 my $lang = $params->{'lang'};
179 if ( $data->{'variable'} =~ /opac/ ) {
183 $theme = C4::Context->preference('opacthemes');
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;
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;
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",
219 authnotrequired => 0,
220 flagsrequired => { parameters => 'parameters_remaining_permissions' },
225 my $op = $input->param('op') || '';
226 $searchfield =~ s/\,//g;
230 script_name => $script_name,
232 ); # we show only the TMPL_VAR names $op
235 script_name => $script_name,
237 ); # we show only the TMPL_VAR names $op
240 if ( $op eq 'update_and_reedit' ) {
241 foreach ( $input->param ) {
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;
254 } elsif ( $param =~ m#down-(\d+).x# ) {
255 my $temp = $currentorder[$1];
256 $currentorder[$1] = $currentorder[ $1 + 1 ];
257 $currentorder[ $1 + 1 ] = $temp;
262 $value = join ' ', @currentorder;
266 script_name => $script_name,
268 ); # we show only the TMPL_VAR names $op
273 script_name => $script_name,
275 ); # we show only the TMPL_VAR names $op
278 my $variable = $input->param('variable');
279 C4::Context->set_preference($variable, $value);
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...
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
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'} ) {
320 @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
323 for my $vl (@values) {
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 print $input->redirect("/cgi-bin/koha/admin/systempreferences.pl?tab=");
341 ################## DELETE_CONFIRM ##################################
342 # called by default form, used to confirm deletion of data in DB
343 } elsif ( $op eq 'delete_confirm' ) {
344 my $value = C4::Context->preference($searchfield);
346 searchfield => $searchfield,
350 # END $OP eq DELETE_CONFIRM
351 ################## DELETE_CONFIRMED ##################################
352 # called by delete_confirm, used to effectively confirm deletion of data in DB
353 } elsif ( $op eq 'delete_confirmed' ) {
354 C4::Context->delete_preference($searchfield);
355 # END $OP eq DELETE_CONFIRMED
356 ################## DEFAULT ##################################
358 #Adding tab management for system preferences
359 my $tab = $input->param('tab')||'local_use';
360 $template->param( $tab => 1 );
361 my ( $count, $results ) = StringSearch( $searchfield, $tab );
363 for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
364 my $row_data = $results->[$i];
365 $row_data->{'lang'} = $template->param('lang');
366 $row_data = GetPrefParams($row_data); # get a fresh hash for the row data
367 $row_data->{edit} = "$script_name?op=add_form&searchfield=" . $results->[$i]{'variable'};
368 $row_data->{delete} = "$script_name?op=delete_confirm&searchfield=" . $results->[$i]{'variable'};
369 push( @loop_data, $row_data );
371 $template->param( loop => \@loop_data );
373 my $prevpage = $offset - $pagesize;
374 $template->param( "<a href=$script_name?offset=" . $prevpage . '<< Prev</a>' );
376 if ( $offset + $pagesize < $count ) {
377 my $nextpage = $offset + $pagesize;
378 $template->param( "a href=$script_name?offset=" . $nextpage . 'Next >></a>' );
380 $template->param( tab => $tab, );
381 } #---- END $OP eq DEFAULT
382 output_html_with_http_headers $input, $cookie, $template->output;
385 # Return an array containing all preferences defined in current Koha instance
388 sub get_prefs_from_files {
389 my $context = C4::Context->new();
390 my $path_pref_en = $context->config('intrahtdocs') .
391 '/prog/en/modules/admin/preferences';
392 # Get all .pref file names
393 opendir ( my $fh, $path_pref_en );
394 my @pref_files = grep { /.pref$/ } readdir($fh);
400 for my $pref ( @$prefs ) {
401 for my $element ( @$pref ) {
402 if ( ref( $element) eq 'HASH' ) {
403 my $name = $element->{pref};
411 for my $file (@pref_files) {
412 my $pref = LoadFile( "$path_pref_en/$file" );
413 for my $tab ( keys %$pref ) {
414 my $content = $pref->{$tab};
415 if ( ref($content) eq 'ARRAY' ) {
419 for my $section ( keys %$content ) {
420 my $syspref = $content->{$section};
429 # Return an array containg all preferences defined in DB
431 sub get_prefs_from_db {
432 my $dbh = C4::Context->dbh;
433 my $sth = $dbh->prepare("SELECT variable FROM systempreferences");
436 while ( (my $name) = $sth->fetchrow_array ) {
437 push @names, $name if $name;
443 # Return an array containing all local preferences: those which are defined in
444 # DB and not defined in Koha .pref files.
446 sub get_local_prefs {
447 my @prefs_file = get_prefs_from_files();
448 my @prefs_db = get_prefs_from_db();
450 my %prefs_file = map { lc $_ => 1 } @prefs_file;
452 foreach my $name (@prefs_db) {
453 push @names, $name unless $prefs_file{lc $name};