Bug 13177: Help page for Rotating Collections
[koha.git] / admin / systempreferences.pl
blob01c0300ff3ab105a86737790416bbf301b1acfd6
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
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
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 qw ( -utf8 );
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 my @sql_bind;
74 for my $name ( get_local_prefs() ) {
75 $strsth .= ',' unless $first;
76 $strsth .= "?";
77 push(@sql_bind,$name);
78 $first = 0;
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 );
89 $cnt++;
92 return ( $cnt, \@results );
95 sub GetPrefParams {
96 my $data = shift;
97 my $params = $data;
98 my @options;
100 if ( defined $data->{'options'} ) {
101 foreach my $option ( split( /\|/, $data->{'options'} ) ) {
102 my $selected = '0';
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;
122 } else {
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 'Htmlarea' ) {
134 $params->{'type_htmlarea'} = 1;
135 $data->{options} =~ /(.*)\|(.*)/;
136 $params->{'cols'} = $1;
137 $params->{'rows'} = $2;
138 } elsif ( $data->{'type'} eq 'Themes' ) {
139 $params->{'type_choice'} = 1;
140 my $type = '';
141 ( $data->{'variable'} =~ m#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
142 @options = ();
143 my $currently_selected_themes;
144 my $counter = 0;
145 foreach my $theme ( split /\s+/, $data->{'value'} ) {
146 push @options, { option => $theme, counter => $counter };
147 $currently_selected_themes->{$theme} = 1;
148 $counter++;
150 foreach my $theme ( getallthemes($type) ) {
151 my $selected = '0';
152 next if $currently_selected_themes->{$theme};
153 push @options, { option => $theme, counter => $counter };
154 $counter++;
156 } elsif ( $data->{'type'} eq 'ClassSources' ) {
157 $params->{'type_choice'} = 1;
158 my $type = '';
159 @options = ();
160 my $sources = GetClassSources();
161 my $counter = 0;
162 foreach my $cn_source ( sort keys %$sources ) {
163 if ( $cn_source eq $data->{'value'} ) {
164 push @options, { option => $cn_source, counter => $counter, selected => 1 };
165 } else {
166 push @options, { option => $cn_source, counter => $counter };
168 $counter++;
170 } elsif ( $data->{'type'} eq 'Languages' ) {
171 my $currently_selected_languages;
172 foreach my $language ( split /\s+/, $data->{'value'} ) {
173 $currently_selected_languages->{$language} = 1;
176 # current language
177 my $lang = $params->{'lang'};
178 my $theme;
179 my $interface;
180 if ( $data->{'variable'} =~ /opac/ ) {
182 # this is the OPAC
183 $interface = 'opac';
184 $theme = C4::Context->preference('opacthemes');
185 } else {
187 # this is the staff client
188 $interface = 'intranet';
189 $theme = C4::Context->preference('template');
191 my $languages_loop = getTranslatedLanguages( $interface, $theme, $lang, $currently_selected_languages );
193 $params->{'languages_loop'} = $languages_loop;
194 $params->{'type_langselector'} = 1;
195 } else {
196 $params->{'type_free'} = 1;
197 $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 30;
200 if ( $params->{'type_choice'} || $params->{'type_free'} || $params->{'type_yesno'} ) {
201 $params->{'oneline'} = 1;
204 $params->{'preftype'} = $data->{'type'};
205 $params->{'options'} = \@options;
207 return $params;
210 my $input = new CGI;
211 my $searchfield = $input->param('searchfield') || '';
212 my $Tvalue = $input->param('Tvalue');
213 my $offset = $input->param('offset') || 0;
214 my $script_name = "/cgi-bin/koha/admin/systempreferences.pl";
216 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
217 { template_name => "admin/systempreferences.tt",
218 query => $input,
219 type => "intranet",
220 authnotrequired => 0,
221 flagsrequired => { parameters => 'parameters_remaining_permissions' },
222 debug => 1,
225 my $pagesize = 100;
226 my $op = $input->param('op') || '';
227 $searchfield =~ s/\,//g;
229 if ($op) {
230 $template->param(
231 script_name => $script_name,
232 $op => 1
233 ); # we show only the TMPL_VAR names $op
234 } else {
235 $template->param(
236 script_name => $script_name,
237 else => 1
238 ); # we show only the TMPL_VAR names $op
241 if ( $op eq 'update_and_reedit' ) {
242 foreach ( $input->param ) {
244 my $value = '';
245 if ( my $currentorder = $input->param('currentorder') ) {
246 my @currentorder = split /\|/, $currentorder;
247 my $orderchanged = 0;
248 foreach my $param ( $input->param ) {
249 if ( $param =~ m#up-(\d+).x# ) {
250 my $temp = $currentorder[$1];
251 $currentorder[$1] = $currentorder[ $1 - 1 ];
252 $currentorder[ $1 - 1 ] = $temp;
253 $orderchanged = 1;
254 last;
255 } elsif ( $param =~ m#down-(\d+).x# ) {
256 my $temp = $currentorder[$1];
257 $currentorder[$1] = $currentorder[ $1 + 1 ];
258 $currentorder[ $1 + 1 ] = $temp;
259 $orderchanged = 1;
260 last;
263 $value = join ' ', @currentorder;
264 if ($orderchanged) {
265 $op = 'add_form';
266 $template->param(
267 script_name => $script_name,
268 $op => 1
269 ); # we show only the TMPL_VAR names $op
270 } else {
271 $op = '';
272 $searchfield = '';
273 $template->param(
274 script_name => $script_name,
275 else => 1
276 ); # we show only the TMPL_VAR names $op
279 my $dbh = C4::Context->dbh;
280 my $query = "select * from systempreferences where variable=?";
281 my $sth = $dbh->prepare($query);
282 $sth->execute( $input->param('variable') );
283 if ( $sth->rows ) {
284 unless ( C4::Context->config('demo') ) {
285 my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
286 $sth->execute( $value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions') );
287 logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
289 } else {
290 unless ( C4::Context->config('demo') ) {
291 my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
292 $sth->execute( $input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
293 logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $input->param('value') );
299 ################## ADD_FORM ##################################
300 # called by default. Used to create form to add or modify a record
302 if ( $op eq 'add_form' ) {
304 #---- if primkey exists, it's a modify action, so read values to modify...
305 my $data;
306 if ($searchfield) {
307 my $dbh = C4::Context->dbh;
308 my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
309 $sth->execute($searchfield);
310 $data = $sth->fetchrow_hashref;
311 $template->param( modify => 1 );
313 # save tab to return to if user cancels edit
314 $template->param( return_tab => $tabsysprefs{$searchfield} );
317 $data->{'lang'} = $template->param('lang');
318 my $prefparams = GetPrefParams($data);
319 $template->param( %$prefparams );
320 $template->param( searchfield => $searchfield );
322 ################## ADD_VALIDATE ##################################
323 # called by add_form, used to insert/modify data in DB
324 } elsif ( $op eq 'add_validate' ) {
325 my $dbh = C4::Context->dbh;
326 my $sth = $dbh->prepare("select * from systempreferences where variable=?");
327 $sth->execute( $input->param('variable') );
329 # to handle multiple values
330 my $value;
332 # handle multiple value strings (separated by ',')
333 my $params = $input->Vars;
334 if ( defined $params->{'value'} ) {
335 my @values = ();
336 @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
337 if (@values) {
338 $value = "";
339 for my $vl (@values) {
340 $value .= "$vl,";
342 $value =~ s/,$//;
343 } else {
344 $value = $params->{'value'};
348 if ( $input->param('preftype') eq 'Upload' ) {
349 my $lgtfh = $input->upload('value');
350 $value = join '', <$lgtfh>;
351 $value = encode_base64($value);
354 if ( $sth->rows ) {
355 unless ( C4::Context->config('demo') ) {
356 my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
357 $sth->execute( $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable') );
358 logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
360 } else {
361 unless ( C4::Context->config('demo') ) {
362 my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
363 $sth->execute( $input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
364 logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $value );
367 print $input->redirect("/cgi-bin/koha/admin/systempreferences.pl?tab=");
368 exit;
369 ################## DELETE_CONFIRM ##################################
370 # called by default form, used to confirm deletion of data in DB
371 } elsif ( $op eq 'delete_confirm' ) {
372 my $dbh = C4::Context->dbh;
373 my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
374 $sth->execute($searchfield);
375 my $data = $sth->fetchrow_hashref;
376 $template->param(
377 searchfield => $searchfield,
378 Tvalue => $data->{'value'},
381 # END $OP eq DELETE_CONFIRM
382 ################## DELETE_CONFIRMED ##################################
383 # called by delete_confirm, used to effectively confirm deletion of data in DB
384 } elsif ( $op eq 'delete_confirmed' ) {
385 my $dbh = C4::Context->dbh;
386 my $sth = $dbh->prepare("delete from systempreferences where variable=?");
387 $sth->execute($searchfield);
388 my $logstring = $searchfield . " | " . $Tvalue;
389 logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring );
391 # END $OP eq DELETE_CONFIRMED
392 ################## DEFAULT ##################################
393 } else { # DEFAULT
394 #Adding tab management for system preferences
395 my $tab = $input->param('tab')||'local_use';
396 $template->param( $tab => 1 );
397 my ( $count, $results ) = StringSearch( $searchfield, $tab );
398 my @loop_data = ();
399 for ( my $i = $offset ; $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ; $i++ ) {
400 my $row_data = $results->[$i];
401 $row_data->{'lang'} = $template->param('lang');
402 $row_data = GetPrefParams($row_data); # get a fresh hash for the row data
403 $row_data->{edit} = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'variable'};
404 $row_data->{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'variable'};
405 push( @loop_data, $row_data );
407 $template->param( loop => \@loop_data );
408 if ( $offset > 0 ) {
409 my $prevpage = $offset - $pagesize;
410 $template->param( "<a href=$script_name?offset=" . $prevpage . '&lt;&lt; Prev</a>' );
412 if ( $offset + $pagesize < $count ) {
413 my $nextpage = $offset + $pagesize;
414 $template->param( "a href=$script_name?offset=" . $nextpage . 'Next &gt;&gt;</a>' );
416 $template->param( tab => $tab, );
417 } #---- END $OP eq DEFAULT
418 output_html_with_http_headers $input, $cookie, $template->output;
421 # Return an array containing all preferences defined in current Koha instance
422 # .pref files.
424 sub get_prefs_from_files {
425 my $context = C4::Context->new();
426 my $path_pref_en = $context->config('intrahtdocs') .
427 '/prog/en/modules/admin/preferences';
428 # Get all .pref file names
429 opendir ( my $fh, $path_pref_en );
430 my @pref_files = grep { /.pref/ } readdir($fh);
431 close $fh;
433 my @names = ();
434 my $append = sub {
435 my $prefs = shift;
436 for my $pref ( @$prefs ) {
437 for my $element ( @$pref ) {
438 if ( ref( $element) eq 'HASH' ) {
439 my $name = $element->{pref};
440 next unless $name;
441 push @names, $name;
442 next;
447 for my $file (@pref_files) {
448 my $pref = LoadFile( "$path_pref_en/$file" );
449 for my $tab ( keys %$pref ) {
450 my $content = $pref->{$tab};
451 if ( ref($content) eq 'ARRAY' ) {
452 $append->($content);
453 next;
455 for my $section ( keys %$content ) {
456 my $syspref = $content->{$section};
457 $append->($syspref);
461 return @names;
465 # Return an array containg all preferences defined in DB
467 sub get_prefs_from_db {
468 my $dbh = C4::Context->dbh;
469 my $sth = $dbh->prepare("SELECT variable FROM systempreferences");
470 $sth->execute;
471 my @names = ();
472 while ( (my $name) = $sth->fetchrow_array ) {
473 push @names, $name if $name;
475 return @names;
479 # Return an array containing all local preferences: those which are defined in
480 # DB and not defined in Koha .pref files.
482 sub get_local_prefs {
483 my @prefs_file = get_prefs_from_files();
484 my @prefs_db = get_prefs_from_db();
486 my %prefs_file = map { lc $_ => 1 } @prefs_file;
487 my @names = ();
488 foreach my $name (@prefs_db) {
489 push @names, $name unless $prefs_file{lc $name};
492 return @names;