Bug 22824: Remove C4::Boolean, true_p, boolean_preference, etc.
It seems that we don't really need all this overhead. YesNo must be a boolean and contain 1 or 0. Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Signed-off-by: Julian Maurice <julian.maurice@biblibre.com> Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org> Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
parent
241a7958f2
commit
592ab10fe5
|
|
@ -1190,7 +1190,7 @@ sub checkauth {
|
|||
$register_name = $register->name if ($register);
|
||||
}
|
||||
my $branches = { map { $_->branchcode => $_->unblessed } Koha::Libraries->search };
|
||||
if ( $type ne 'opac' and C4::Context->boolean_preference('AutoLocation') ) {
|
||||
if ( $type ne 'opac' and C4::Context->preference('AutoLocation') ) {
|
||||
|
||||
# we have to check they are coming from the right ip range
|
||||
my $domain = $branches->{$branchcode}->{'branchip'};
|
||||
|
|
|
|||
105
C4/Boolean.pm
105
C4/Boolean.pm
|
|
@ -1,105 +0,0 @@
|
|||
package C4::Boolean;
|
||||
|
||||
#package to handle Boolean values in the parameters table
|
||||
# Note: This is just a utility module; it should not be instantiated.
|
||||
|
||||
|
||||
# Copyright 2003 Katipo Communications
|
||||
#
|
||||
# This file is part of Koha.
|
||||
#
|
||||
# Koha is free software; you can redistribute it and/or modify it
|
||||
# under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Koha is distributed in the hope that it will be useful, but
|
||||
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Koha; if not, see <http://www.gnu.org/licenses>.
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Carp;
|
||||
use base qw(Exporter);
|
||||
|
||||
our @EXPORT_OK = qw( true_p);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
C4::Boolean - Convenience function to handle boolean values
|
||||
in the parameter table
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
use C4::Boolean;
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
In the parameter table, there are various Boolean values that
|
||||
variously require a 0/1, no/yes, false/true, or off/on values.
|
||||
This module aims to provide scripts a means to interpret these
|
||||
Boolean values in a consistent way which makes common sense.
|
||||
|
||||
=head1 FUNCTIONS
|
||||
|
||||
=over 2
|
||||
|
||||
=cut
|
||||
|
||||
use constant INVALID_BOOLEAN_STRING_EXCEPTION =>
|
||||
q{The given value does not seem to be interpretable as a Boolean value};
|
||||
|
||||
our %strings = (
|
||||
'0' => 0, '1' => 1, # C
|
||||
'-1' => 1, # BASIC
|
||||
'nil' => 0, 't' => 1, # LISP
|
||||
'false' => 0, 'true' => 1, # Pascal
|
||||
'off' => 0, 'on' => 1,
|
||||
'no' => 0, 'yes' => 1,
|
||||
'n' => 0, 'y' => 1,
|
||||
);
|
||||
|
||||
=item true_p
|
||||
|
||||
if ( C4::Boolean::true_p(C4::Context->preference("IndependentBranches")) ) {
|
||||
...
|
||||
}
|
||||
|
||||
Tries to interpret the passed string as a Boolean value. Returns
|
||||
the value if the string can be interpreted as such; otherwise an
|
||||
exception is thrown.
|
||||
|
||||
=cut
|
||||
|
||||
sub true_p {
|
||||
my $x = shift;
|
||||
my $it;
|
||||
if (!defined $x || ref $x ) {
|
||||
carp INVALID_BOOLEAN_STRING_EXCEPTION;
|
||||
return;
|
||||
}
|
||||
$x = lc $x;
|
||||
$x =~ s/\s//g;
|
||||
if (defined $strings{$x}) {
|
||||
$it = $strings{$x};
|
||||
} else {
|
||||
carp INVALID_BOOLEAN_STRING_EXCEPTION;
|
||||
}
|
||||
return $it;
|
||||
}
|
||||
|
||||
1;
|
||||
__END__
|
||||
|
||||
=back
|
||||
|
||||
=head1 AUTHOR
|
||||
|
||||
Koha Development Team <http://koha-community.org/>
|
||||
|
||||
=cut
|
||||
|
|
@ -99,7 +99,6 @@ use POSIX ();
|
|||
use YAML::XS;
|
||||
use ZOOM;
|
||||
|
||||
use C4::Boolean;
|
||||
use C4::Debug;
|
||||
use Koha::Caches;
|
||||
use Koha::Config::SysPref;
|
||||
|
|
@ -427,13 +426,6 @@ sub preference {
|
|||
return $value;
|
||||
}
|
||||
|
||||
sub boolean_preference {
|
||||
my $self = shift;
|
||||
my $var = shift; # The system preference to return
|
||||
my $it = preference($self, $var);
|
||||
return defined($it)? C4::Boolean::true_p($it): undef;
|
||||
}
|
||||
|
||||
=head2 yaml_preference
|
||||
|
||||
Retrieves the required system preference value, and converts it
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ sub GetPrefParams {
|
|||
$params->{'type_choice'} = 1;
|
||||
} elsif ( $data->{'type'} eq 'YesNo' ) {
|
||||
$params->{'type_yesno'} = 1;
|
||||
$data->{'value'} = C4::Context->boolean_preference( $data->{'variable'} );
|
||||
$data->{'value'} = C4::Context->preference( $data->{'variable'} );
|
||||
if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
|
||||
$params->{'value_yes'} = 1;
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -26,7 +26,6 @@ use Plack::Request;
|
|||
use Mojo::Server::PSGI;
|
||||
|
||||
# Pre-load libraries
|
||||
use C4::Boolean;
|
||||
use C4::Koha;
|
||||
use C4::Languages;
|
||||
use C4::Letters;
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@
|
|||
|
||||
use Modern::Perl;
|
||||
use Koha::Script;
|
||||
use C4::Boolean;
|
||||
use C4::Context;
|
||||
use C4::Debug;
|
||||
use C4::Log;
|
||||
|
|
@ -56,27 +55,6 @@ sub _debug {
|
|||
print STDERR $message . "\n" if ( $C4::Debug::debug );
|
||||
}
|
||||
|
||||
sub _normalize_value {
|
||||
my ($row) = @_;
|
||||
|
||||
# AFAIK, there are no sysprefs where NULL has a different meaning than ''.
|
||||
return ( $row->{'value'} || '' ) unless ( $row->{'type'} eq 'YesNo' );
|
||||
|
||||
#
|
||||
# In theory, YesNo sysprefs should always contain 1/0.
|
||||
# In practice, however, they don't.
|
||||
# - Yogi Berra
|
||||
#
|
||||
my $bool_value = eval {
|
||||
return C4::Boolean::true_p( $row->{'value'} ) ? 1 : 0;
|
||||
};
|
||||
if ( $@ ) {
|
||||
return 0;
|
||||
} else {
|
||||
return $bool_value;
|
||||
}
|
||||
}
|
||||
|
||||
sub _set_preference {
|
||||
my ( $preference, $value ) = @_;
|
||||
|
||||
|
|
@ -89,7 +67,7 @@ sub GetPreferences {
|
|||
my $dbh = C4::Context->dbh;
|
||||
|
||||
return {
|
||||
map { $_->{'variable'}, _normalize_value($_) }
|
||||
map { $_->{'variable'}, $_ }
|
||||
@{ $dbh->selectall_arrayref( "
|
||||
SELECT
|
||||
variable, value, type
|
||||
|
|
@ -113,11 +91,6 @@ sub SetPreferences {
|
|||
|
||||
exit 2 if ( scalar( @$current_state ) != scalar( keys %preferences ) );
|
||||
|
||||
foreach my $row ( @$current_state ) {
|
||||
# YAML::Syck encodes no as '', so deal with that
|
||||
$preferences{$row->{'variable'}} = $preferences{$row->{'variable'}} eq '' ? 0 : C4::Boolean::true_p( $preferences{$row->{'variable'}} ) if ( $row->{'type'} eq 'YesNo' );
|
||||
}
|
||||
|
||||
# Iterate through again, now that we've checked all of the YesNo sysprefs
|
||||
|
||||
foreach my $row ( @$current_state ) {
|
||||
|
|
@ -151,9 +124,7 @@ sub _fetch_preference {
|
|||
sub GetPreference {
|
||||
my ( $preference ) = @_;
|
||||
|
||||
my $row = _fetch_preference( $preference );
|
||||
|
||||
return _normalize_value( $row );
|
||||
return _fetch_preference( $preference );
|
||||
}
|
||||
|
||||
sub SetPreference {
|
||||
|
|
@ -161,9 +132,7 @@ sub SetPreference {
|
|||
|
||||
my $row = _fetch_preference( $preference );
|
||||
|
||||
$value = C4::Boolean::true_p($value) ? 1 : 0 if ( $row->{'type'} eq 'YesNo' );
|
||||
|
||||
exit 3 if ( $value eq $row->{'value'} );
|
||||
exit 3 if ( $value eq $row->{'value'} ); #FIXME exit??
|
||||
|
||||
_set_preference( $preference, $value );
|
||||
}
|
||||
|
|
|
|||
|
|
@ -163,7 +163,7 @@ if ( $action eq 'create' ) {
|
|||
}
|
||||
else {
|
||||
if (
|
||||
C4::Context->boolean_preference(
|
||||
C4::Context->preference(
|
||||
'PatronSelfRegistrationVerifyByEmail')
|
||||
)
|
||||
{
|
||||
|
|
@ -401,7 +401,7 @@ sub GetMandatoryFields {
|
|||
|
||||
if ( $action eq 'create' || $action eq 'new' ) {
|
||||
$mandatory_fields{'email'} = 1
|
||||
if C4::Context->boolean_preference(
|
||||
if C4::Context->preference(
|
||||
'PatronSelfRegistrationVerifyByEmail');
|
||||
}
|
||||
|
||||
|
|
|
|||
39
t/Boolean.t
39
t/Boolean.t
|
|
@ -1,39 +0,0 @@
|
|||
|
||||
use Modern::Perl;
|
||||
|
||||
use Test::More tests => 22;
|
||||
use Test::Warn;
|
||||
|
||||
BEGIN { use_ok( 'C4::Boolean', qw( true_p ) ); }
|
||||
|
||||
is( true_p('0'), '0', 'recognizes \'0\' as false' );
|
||||
is( true_p('nil'), '0', 'recognizes \'nil\' as false' );
|
||||
is( true_p('false'), '0', 'recognizes \'false\' as false' );
|
||||
is( true_p('off'), '0', 'recognizes \'off\' as false' );
|
||||
is( true_p('no'), '0', 'recognizes \'no\' as false' );
|
||||
is( true_p('n'), '0', 'recognizes \'n\' as false' );
|
||||
is( true_p('NO'), '0', 'verified case insensitivity' );
|
||||
|
||||
is( true_p('1'), '1', 'recognizes \'1\' as true' );
|
||||
is( true_p('-1'), '1', 'recognizes \'-1\' as true' );
|
||||
is( true_p('t'), '1', 'recognizes \'t\' as true' );
|
||||
is( true_p('true'), '1', 'recognizes \'true\' as true' );
|
||||
is( true_p('on'), '1', 'recognizes \'on\' as true' );
|
||||
is( true_p('yes'), '1', 'recognizes \'yes\' as true' );
|
||||
is( true_p('y'), '1', 'recognizes \'y\' as true' );
|
||||
is( true_p('YES'), '1', 'verified case insensitivity' );
|
||||
|
||||
my $result;
|
||||
warning_like { $result = true_p(undef) }
|
||||
qr/^The given value does not seem to be interpretable as a Boolean value/,
|
||||
'Invalid boolean (undef) raises warning';
|
||||
is( $result, undef, 'recognizes undefined as not boolean' );
|
||||
warning_like { $result = true_p('foo') }
|
||||
qr/^The given value does not seem to be interpretable as a Boolean value/,
|
||||
'Invalid boolean (\'foo\') raises warning';
|
||||
is( $result, undef, 'recognizes \'foo\' as not boolean' );
|
||||
warning_like { $result = true_p([]) }
|
||||
qr/^The given value does not seem to be interpretable as a Boolean value/,
|
||||
'Invalid boolean (reference) raises warning';
|
||||
is( $result, undef, 'recognizes a reference as not a boolean' );
|
||||
|
||||
Loading…
Reference in New Issue