Bug 27070: Add cross_fields type to our searches
[koha.git] / admin / systempreferences.pl
1 #!/usr/bin/perl
2
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)
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
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.
15 #
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.
20 #
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>.
23
24 =head1 systempreferences.pl
25
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
40
41 =cut
42
43 use Modern::Perl;
44
45 use CGI qw ( -utf8 );
46 use MIME::Base64;
47 use C4::Auth;
48 use C4::Context;
49 use C4::Koha;
50 use C4::Languages qw(getTranslatedLanguages);
51 use C4::ClassSource;
52 use C4::Output;
53 use YAML::Syck qw( Dump LoadFile );
54
55 my %tabsysprefs; #we do no longer need to keep track of a tab per pref (yaml)
56
57 sub StringSearch {
58     my ( $searchstring, $tab ) = @_;
59     return (0,[]) if $tab ne 'local_use';
60
61     my $dbh = C4::Context->dbh;
62     $searchstring =~ s/\'/\\\'/g;
63     my @data = split( ' ', $searchstring );
64     my $count = @data;
65     my @results;
66     my $cnt = 0;
67     my $sth;
68
69     my $strsth = "Select variable,value,explanation,type,options from systempreferences where variable in (";
70     my $first = 1;
71     my @sql_bind;
72     for my $name ( get_local_prefs() ) {
73                 $strsth .= ',' unless $first;
74                 $strsth .= "?";
75                 push(@sql_bind,$name);
76                 $first = 0;
77     }
78     $strsth .= ") order by variable";
79     $sth = $dbh->prepare($strsth);
80     $sth->execute(@sql_bind);
81
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 );
87             $cnt++;
88     }
89
90     return ( $cnt, \@results );
91 }
92
93 sub GetPrefParams {
94     my $data   = shift;
95     my $params = $data;
96     my @options;
97
98     if ( defined $data->{'options'} ) {
99         foreach my $option ( split( /\|/, $data->{'options'} ) ) {
100             my $selected = '0';
101             defined( $data->{'value'} ) and $option eq $data->{'value'} and $selected = 1;
102             push @options, { option => $option, selected => $selected };
103         }
104     }
105
106     $params->{'prefoptions'} = $data->{'options'};
107
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;
120         } else {
121             $params->{'value_no'} = 1;
122         }
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;
138         my $type = '';
139         ( $data->{'variable'} =~ m#opac#i ) ? ( $type = 'opac' ) : ( $type = 'intranet' );
140         @options = ();
141         my $currently_selected_themes;
142         my $counter = 0;
143         foreach my $theme ( split /\s+/, $data->{'value'} ) {
144             push @options, { option => $theme, counter => $counter };
145             $currently_selected_themes->{$theme} = 1;
146             $counter++;
147         }
148         foreach my $theme ( getallthemes($type) ) {
149             my $selected = '0';
150             next if $currently_selected_themes->{$theme};
151             push @options, { option => $theme, counter => $counter };
152             $counter++;
153         }
154     } elsif ( $data->{'type'} eq 'ClassSources' ) {
155         $params->{'type_choice'} = 1;
156         my $type = '';
157         @options = ();
158         my $sources = GetClassSources();
159         my $counter = 0;
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 };
163             } else {
164                 push @options, { option => $cn_source, counter => $counter };
165             }
166             $counter++;
167         }
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;
172         }
173
174         # current language
175         my $lang = $params->{'lang'};
176         my $theme;
177         my $interface;
178         if ( $data->{'variable'} =~ /opac/ ) {
179
180             # this is the OPAC
181             $interface = 'opac';
182             $theme     = C4::Context->preference('opacthemes');
183         } else {
184
185             # this is the staff interface
186             $interface = 'intranet';
187             $theme     = C4::Context->preference('template');
188         }
189         my $languages_loop = getTranslatedLanguages( $interface, $theme, $lang, $currently_selected_languages );
190
191         $params->{'languages_loop'}    = $languages_loop;
192         $params->{'type_langselector'} = 1;
193     } else {
194         $params->{'type_free'} = 1;
195         $params->{'fieldlength'} = ( defined( $data->{'options'} ) and $data->{'options'} and $data->{'options'} > 0 ) ? $data->{'options'} : 30;
196     }
197
198     if ( $params->{'type_choice'} || $params->{'type_free'} || $params->{'type_yesno'} ) {
199         $params->{'oneline'} = 1;
200     }
201
202     $params->{'preftype'} = $data->{'type'};
203     $params->{'options'}  = \@options;
204
205     return $params;
206 }
207
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";
213
214 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
215     {   template_name   => "admin/systempreferences.tt",
216         query           => $input,
217         type            => "intranet",
218         flagsrequired   => { parameters => 'manage_sysprefs' },
219         debug           => 1,
220     }
221 );
222 my $pagesize = 100;
223 my $op = $input->param('op') || '';
224 $searchfield =~ s/\,//g;
225
226 if ($op) {
227     $template->param(
228         script_name => $script_name,
229         $op         => 1
230     );    # we show only the TMPL_VAR names $op
231 } else {
232     $template->param(
233         script_name => $script_name,
234         else        => 1
235     );    # we show only the TMPL_VAR names $op
236 }
237
238 if ( $op eq 'update_and_reedit' ) {
239     foreach ( $input->param ) {
240     }
241     my $value = '';
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;
250                 $orderchanged           = 1;
251                 last;
252             } elsif ( $param =~ m#down-(\d+).x# ) {
253                 my $temp = $currentorder[$1];
254                 $currentorder[$1]       = $currentorder[ $1 + 1 ];
255                 $currentorder[ $1 + 1 ] = $temp;
256                 $orderchanged           = 1;
257                 last;
258             }
259         }
260         $value = join ' ', @currentorder;
261         if ($orderchanged) {
262             $op = 'add_form';
263             $template->param(
264                 script_name => $script_name,
265                 $op         => 1
266             );    # we show only the TMPL_VAR names $op
267         } else {
268             $op          = '';
269             $searchfield = '';
270             $template->param(
271                 script_name => $script_name,
272                 else        => 1
273             );    # we show only the TMPL_VAR names $op
274         }
275     }
276     my $variable = $input->param('variable');
277     C4::Context->set_preference($variable, $value);
278 }
279
280 ################## ADD_FORM ##################################
281 # called by default. Used to create form to add or  modify a record
282
283 if ( $op eq 'add_form' ) {
284
285     #---- if primkey exists, it's a modify action, so read values to modify...
286     my $data;
287     if ($searchfield) {
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 );
293
294         # save tab to return to if user cancels edit
295         $template->param( return_tab => $tabsysprefs{$searchfield} );
296     }
297
298     $data->{'lang'} = $template->param('lang');
299     my $prefparams = GetPrefParams($data);
300     $template->param( %$prefparams );
301     $template->param( searchfield => $searchfield );
302
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
307     my $value;
308
309     my $variable = $input->param('variable');
310     my $expl     = $input->param('explanation');
311     my $type     = $input->param('preftype');
312     my $options  = $input->param('prefoptions');
313
314     # handle multiple value strings (separated by ',')
315     my $params = $input->Vars;
316     if ( defined $params->{'value'} ) {
317         my @values = ();
318         @values = split( "\0", $params->{'value'} ) if defined( $params->{'value'} );
319         if (@values) {
320             $value = "";
321             for my $vl (@values) {
322                 $value .= "$vl,";
323             }
324             $value =~ s/,$//;
325         } else {
326             $value = $params->{'value'};
327         }
328     }
329
330     if ( $type eq 'Upload' ) {
331         my $lgtfh = $input->upload('value');
332         $value = join '', <$lgtfh>;
333         $value = encode_base64($value);
334     }
335
336     C4::Context->set_preference( $variable, $value, $expl, $type, $options );
337     print $input->redirect("/cgi-bin/koha/admin/systempreferences.pl?tab=");
338     exit;
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);
343     $template->param(
344         searchfield => $searchfield,
345         Tvalue      => $value,
346     );
347
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 ##################################
355 } else {    # 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 );
360     my @loop_data = ();
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&amp;searchfield=" . $results->[$i]{'variable'};
366         $row_data->{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'variable'};
367         push( @loop_data, $row_data );
368     }
369     $template->param( loop => \@loop_data );
370     if ( $offset > 0 ) {
371         my $prevpage = $offset - $pagesize;
372         $template->param( "<a href=$script_name?offset=" . $prevpage . '&lt;&lt; Prev</a>' );
373     }
374     if ( $offset + $pagesize < $count ) {
375         my $nextpage = $offset + $pagesize;
376         $template->param( "a href=$script_name?offset=" . $nextpage . 'Next &gt;&gt;</a>' );
377     }
378     $template->param( tab => $tab, );
379 }    #---- END $OP eq DEFAULT
380 output_html_with_http_headers $input, $cookie, $template->output;
381
382
383 # Return an array containing all preferences defined in current Koha instance
384 # .pref files.
385
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);
393     close $fh;
394
395     my @names = ();
396     my $append = sub {
397         my $prefs = shift;
398         for my $pref ( @$prefs ) {
399             for my $element ( @$pref ) {
400                 if ( ref( $element) eq 'HASH' ) {
401                     my $name = $element->{pref};
402                     next unless $name;
403                     push @names, $name;
404                     next;
405                 }
406             }
407         }
408     };
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' ) {
414                 $append->($content);
415                 next;
416             }
417             for my $section ( keys %$content ) {
418                 my $syspref = $content->{$section};
419                 $append->($syspref);
420             }
421         }
422     }
423     return @names;
424 }
425
426
427 # Return an array containg all preferences defined in DB
428
429 sub get_prefs_from_db {
430     my $dbh = C4::Context->dbh;
431     my $sth = $dbh->prepare("SELECT variable FROM systempreferences");
432     $sth->execute;
433     my @names = ();
434     while ( (my $name) = $sth->fetchrow_array ) {
435         push @names, $name if $name;
436     }
437     return @names;
438 }
439
440
441 # Return an array containing all local preferences: those which are defined in
442 # DB and not defined in Koha .pref files.
443
444 sub get_local_prefs {
445     my @prefs_file = get_prefs_from_files();
446     my @prefs_db = get_prefs_from_db();
447
448     my %prefs_file = map { lc $_ => 1 } @prefs_file;
449     my @names = ();
450     foreach my $name (@prefs_db) {
451         push @names, $name  unless $prefs_file{lc $name};
452     }
453
454     return @names;
455 }
456