View | Details | Raw Unified | Return to bug 22824
Collapse All | Expand All

(-)a/C4/Auth.pm (-1 / +1 lines)
Lines 1190-1196 sub checkauth { Link Here
1190
                        $register_name = $register->name if ($register);
1190
                        $register_name = $register->name if ($register);
1191
                    }
1191
                    }
1192
                    my $branches = { map { $_->branchcode => $_->unblessed } Koha::Libraries->search };
1192
                    my $branches = { map { $_->branchcode => $_->unblessed } Koha::Libraries->search };
1193
                    if ( $type ne 'opac' and C4::Context->boolean_preference('AutoLocation') ) {
1193
                    if ( $type ne 'opac' and C4::Context->preference('AutoLocation') ) {
1194
1194
1195
                        # we have to check they are coming from the right ip range
1195
                        # we have to check they are coming from the right ip range
1196
                        my $domain = $branches->{$branchcode}->{'branchip'};
1196
                        my $domain = $branches->{$branchcode}->{'branchip'};
(-)a/C4/Boolean.pm (-105 lines)
Lines 1-105 Link Here
1
package C4::Boolean;
2
3
#package to handle Boolean values in the parameters table
4
# Note: This is just a utility module; it should not be instantiated.
5
6
7
# Copyright 2003 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
use strict;
25
use warnings;
26
27
use Carp;
28
use base qw(Exporter);
29
30
our    @EXPORT_OK = qw( true_p);
31
32
=head1 NAME
33
34
C4::Boolean - Convenience function to handle boolean values
35
in the parameter table
36
37
=head1 SYNOPSIS
38
39
  use C4::Boolean;
40
41
=head1 DESCRIPTION
42
43
In the parameter table, there are various Boolean values that
44
variously require a 0/1, no/yes, false/true, or off/on values.
45
This module aims to provide scripts a means to interpret these
46
Boolean values in a consistent way which makes common sense.
47
48
=head1 FUNCTIONS
49
50
=over 2
51
52
=cut
53
54
use constant INVALID_BOOLEAN_STRING_EXCEPTION =>
55
  q{The given value does not seem to be interpretable as a Boolean value};
56
57
our %strings = (
58
   '0'     => 0,    '1'     => 1,    # C
59
                    '-1'    => 1,    # BASIC
60
   'nil'   => 0,    't'     => 1,    # LISP
61
   'false' => 0,    'true'  => 1,    # Pascal
62
   'off'   => 0,    'on'    => 1,
63
   'no'    => 0,    'yes'   => 1,
64
   'n'     => 0,    'y'     => 1,
65
);
66
67
=item true_p
68
69
    if ( C4::Boolean::true_p(C4::Context->preference("IndependentBranches")) ) {
70
    ...
71
    }
72
73
Tries to interpret the passed string as a Boolean value. Returns
74
the value if the string can be interpreted as such; otherwise an
75
exception is thrown.
76
77
=cut
78
79
sub true_p  {
80
    my $x = shift;
81
    my $it;
82
    if (!defined $x || ref $x ) {
83
        carp INVALID_BOOLEAN_STRING_EXCEPTION;
84
        return;
85
    }
86
    $x = lc $x;
87
    $x =~ s/\s//g;
88
    if (defined $strings{$x}) {
89
        $it = $strings{$x};
90
    } else {
91
        carp INVALID_BOOLEAN_STRING_EXCEPTION;
92
    }
93
    return $it;
94
}
95
96
1;
97
__END__
98
99
=back
100
101
=head1 AUTHOR
102
103
Koha Development Team <http://koha-community.org/>
104
105
=cut
(-)a/C4/Context.pm (-8 lines)
Lines 99-105 use POSIX (); Link Here
99
use YAML::XS;
99
use YAML::XS;
100
use ZOOM;
100
use ZOOM;
101
101
102
use C4::Boolean;
103
use C4::Debug;
102
use C4::Debug;
104
use Koha::Caches;
103
use Koha::Caches;
105
use Koha::Config::SysPref;
104
use Koha::Config::SysPref;
Lines 427-439 sub preference { Link Here
427
    return $value;
426
    return $value;
428
}
427
}
429
428
430
sub boolean_preference {
431
    my $self = shift;
432
    my $var = shift;        # The system preference to return
433
    my $it = preference($self, $var);
434
    return defined($it)? C4::Boolean::true_p($it): undef;
435
}
436
437
=head2 yaml_preference
429
=head2 yaml_preference
438
430
439
Retrieves the required system preference value, and converts it
431
Retrieves the required system preference value, and converts it
(-)a/admin/systempreferences.pl (-1 / +1 lines)
Lines 114-120 sub GetPrefParams { Link Here
114
        $params->{'type_choice'} = 1;
114
        $params->{'type_choice'} = 1;
115
    } elsif ( $data->{'type'} eq 'YesNo' ) {
115
    } elsif ( $data->{'type'} eq 'YesNo' ) {
116
        $params->{'type_yesno'} = 1;
116
        $params->{'type_yesno'} = 1;
117
        $data->{'value'}        = C4::Context->boolean_preference( $data->{'variable'} );
117
        $data->{'value'}        = C4::Context->preference( $data->{'variable'} );
118
        if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
118
        if ( defined( $data->{'value'} ) and $data->{'value'} eq '1' ) {
119
            $params->{'value_yes'} = 1;
119
            $params->{'value_yes'} = 1;
120
        } else {
120
        } else {
(-)a/debian/templates/plack.psgi (-1 lines)
Lines 26-32 use Plack::Request; Link Here
26
use Mojo::Server::PSGI;
26
use Mojo::Server::PSGI;
27
27
28
# Pre-load libraries
28
# Pre-load libraries
29
use C4::Boolean;
30
use C4::Koha;
29
use C4::Koha;
31
use C4::Languages;
30
use C4::Languages;
32
use C4::Letters;
31
use C4::Letters;
(-)a/misc/admin/koha-preferences (-34 / +3 lines)
Lines 20-26 Link Here
20
20
21
use Modern::Perl;
21
use Modern::Perl;
22
use Koha::Script;
22
use Koha::Script;
23
use C4::Boolean;
24
use C4::Context;
23
use C4::Context;
25
use C4::Debug;
24
use C4::Debug;
26
use C4::Log;
25
use C4::Log;
Lines 56-82 sub _debug { Link Here
56
    print STDERR $message . "\n" if ( $C4::Debug::debug );
55
    print STDERR $message . "\n" if ( $C4::Debug::debug );
57
}
56
}
58
57
59
sub _normalize_value {
60
    my ($row) = @_;
61
62
    # AFAIK, there are no sysprefs where NULL has a different meaning than ''.
63
    return ( $row->{'value'} || '' ) unless ( $row->{'type'} eq 'YesNo' );
64
65
    #
66
    # In theory, YesNo sysprefs should always contain 1/0.
67
    # In practice, however, they don't.
68
    # - Yogi Berra
69
    #
70
    my $bool_value = eval {
71
        return C4::Boolean::true_p( $row->{'value'} ) ? 1 : 0;
72
    };
73
    if ( $@ ) {
74
        return 0;
75
    } else {
76
        return $bool_value;
77
    }
78
}
79
80
sub _set_preference {
58
sub _set_preference {
81
    my ( $preference, $value ) = @_;
59
    my ( $preference, $value ) = @_;
82
60
Lines 89-95 sub GetPreferences { Link Here
89
    my $dbh = C4::Context->dbh;
67
    my $dbh = C4::Context->dbh;
90
68
91
    return {
69
    return {
92
        map { $_->{'variable'},  _normalize_value($_) }
70
        map { $_->{'variable'},  $_ }
93
        @{ $dbh->selectall_arrayref( "
71
        @{ $dbh->selectall_arrayref( "
94
            SELECT
72
            SELECT
95
              variable, value, type
73
              variable, value, type
Lines 113-123 sub SetPreferences { Link Here
113
91
114
    exit 2 if ( scalar( @$current_state ) != scalar( keys %preferences ) );
92
    exit 2 if ( scalar( @$current_state ) != scalar( keys %preferences ) );
115
93
116
    foreach my $row ( @$current_state ) {
117
        # YAML::Syck encodes no as '', so deal with that
118
        $preferences{$row->{'variable'}} = $preferences{$row->{'variable'}} eq '' ? 0 : C4::Boolean::true_p( $preferences{$row->{'variable'}} ) if ( $row->{'type'} eq 'YesNo' );
119
    }
120
121
    # Iterate through again, now that we've checked all of the YesNo sysprefs
94
    # Iterate through again, now that we've checked all of the YesNo sysprefs
122
95
123
    foreach my $row ( @$current_state ) {
96
    foreach my $row ( @$current_state ) {
Lines 151-159 sub _fetch_preference { Link Here
151
sub GetPreference {
124
sub GetPreference {
152
    my ( $preference ) = @_;
125
    my ( $preference ) = @_;
153
126
154
    my $row = _fetch_preference( $preference );
127
    return _fetch_preference( $preference );
155
156
    return _normalize_value( $row );
157
}
128
}
158
129
159
sub SetPreference {
130
sub SetPreference {
Lines 161-169 sub SetPreference { Link Here
161
132
162
    my $row = _fetch_preference( $preference );
133
    my $row = _fetch_preference( $preference );
163
134
164
    $value = C4::Boolean::true_p($value) ? 1 : 0 if ( $row->{'type'} eq 'YesNo' );
135
    exit 3 if ( $value eq $row->{'value'} ); #FIXME exit??
165
166
    exit 3 if ( $value eq $row->{'value'} );
167
136
168
    _set_preference( $preference, $value );
137
    _set_preference( $preference, $value );
169
}
138
}
(-)a/opac/opac-memberentry.pl (-2 / +2 lines)
Lines 162-168 if ( $action eq 'create' ) { Link Here
162
    }
162
    }
163
    else {
163
    else {
164
        if (
164
        if (
165
            C4::Context->boolean_preference(
165
            C4::Context->preference(
166
                'PatronSelfRegistrationVerifyByEmail')
166
                'PatronSelfRegistrationVerifyByEmail')
167
          )
167
          )
168
        {
168
        {
Lines 401-407 sub GetMandatoryFields { Link Here
401
401
402
    if ( $action eq 'create' || $action eq 'new' ) {
402
    if ( $action eq 'create' || $action eq 'new' ) {
403
        $mandatory_fields{'email'} = 1
403
        $mandatory_fields{'email'} = 1
404
          if C4::Context->boolean_preference(
404
          if C4::Context->preference(
405
            'PatronSelfRegistrationVerifyByEmail');
405
            'PatronSelfRegistrationVerifyByEmail');
406
    }
406
    }
407
407
(-)a/t/Boolean.t (-40 lines)
Lines 1-39 Link Here
1
2
use Modern::Perl;
3
4
use Test::More tests => 22;
5
use Test::Warn;
6
7
BEGIN { use_ok( 'C4::Boolean', qw( true_p ) ); }
8
9
is( true_p('0'),     '0', 'recognizes \'0\' as false' );
10
is( true_p('nil'),   '0', 'recognizes \'nil\' as false' );
11
is( true_p('false'), '0', 'recognizes \'false\' as false' );
12
is( true_p('off'),   '0', 'recognizes \'off\' as false' );
13
is( true_p('no'),    '0', 'recognizes \'no\' as false' );
14
is( true_p('n'),     '0', 'recognizes \'n\' as false' );
15
is( true_p('NO'),    '0', 'verified case insensitivity' );
16
17
is( true_p('1'),    '1', 'recognizes \'1\' as true' );
18
is( true_p('-1'),   '1', 'recognizes \'-1\' as true' );
19
is( true_p('t'),    '1', 'recognizes \'t\' as true' );
20
is( true_p('true'), '1', 'recognizes \'true\' as true' );
21
is( true_p('on'),   '1', 'recognizes \'on\' as true' );
22
is( true_p('yes'),  '1', 'recognizes \'yes\' as true' );
23
is( true_p('y'),    '1', 'recognizes \'y\' as true' );
24
is( true_p('YES'),  '1', 'verified case insensitivity' );
25
26
my $result;
27
warning_like { $result = true_p(undef) }
28
             qr/^The given value does not seem to be interpretable as a Boolean value/,
29
             'Invalid boolean (undef) raises warning';
30
is( $result, undef, 'recognizes undefined as not boolean' );
31
warning_like { $result = true_p('foo') }
32
             qr/^The given value does not seem to be interpretable as a Boolean value/,
33
             'Invalid boolean (\'foo\') raises warning';
34
is( $result, undef, 'recognizes \'foo\' as not boolean' );
35
warning_like { $result = true_p([]) }
36
             qr/^The given value does not seem to be interpretable as a Boolean value/,
37
             'Invalid boolean (reference) raises warning';
38
is( $result, undef, 'recognizes a reference as not a boolean' );
39
40
- 

Return to bug 22824