Ported session storage code to support PostgreSQL
[koha.git] / C4 / Boolean.pm
1 package C4::Boolean;
2
3
4 #package to handle Boolean values in the parameters table
5 # Note: This is just a utility module; it should not be instantiated.
6
7
8 # Copyright 2003 Katipo Communications
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA  02111-1307 USA
24
25 use strict;
26 use POSIX;
27 require Exporter;
28
29 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
30
31 # set the version for version checking
32 $VERSION = 0.01;
33
34 =head1 NAME
35
36 C4::Boolean - Convenience functions to handle boolean values
37 in the parameter table
38
39 =head1 SYNOPSIS
40
41   use C4::Boolean;
42
43 =head1 DESCRIPTION
44
45 In the parameter table, there are various Boolean values that
46 variously require a 0/1, no/yes, false/true, or off/on values.
47 This module aims to provide scripts a means to interpret these
48 Boolean values in a consistent way which makes common sense.
49
50 =head1 FUNCTIONS
51
52 =over 2
53
54 =cut
55
56 @ISA = qw(Exporter);
57 @EXPORT = (
58         &INVALID_BOOLEAN_STRING_EXCEPTION
59     );
60
61 @EXPORT_OK = qw(
62         true_p
63     );
64
65 sub INVALID_BOOLEAN_STRING_EXCEPTION ()
66     { 'The given value does not seem to be interpretable as a Boolean value' }
67
68 use vars qw( %strings );
69
70 %strings = (
71    '0'     => 0,        '1'     => 1,   # C
72                         '-1'    => 1,   # BASIC
73    'nil'   => 0,        't'     => 1,   # LISP
74    'false' => 0,        'true'  => 1,   # Pascal
75    'off'   => 0,        'on'    => 1,
76    'no'    => 0,        'yes'   => 1,
77    'n'     => 0,        'y'     => 1,
78 );
79
80 =item true_p
81
82     if ( C4::Boolean::true_p(C4::Context->preference("insecure")) ) {
83         ...
84     }
85
86 Tries to interpret the passed string as a Boolean value. Returns
87 the value if the string can be interpreted as such; otherwise an
88 exception is thrown.
89
90 =cut
91
92 sub true_p ($) {
93     my($x) = @_;
94     my $it;
95     if (!defined $x || ref($x) ne '') {
96         die INVALID_BOOLEAN_STRING_EXCEPTION;
97     }
98     $x = lc($x);
99     $x =~ s/\s//g;
100     if (defined $strings{$x}) {
101         $it = $strings{$x};
102     } else {
103         die INVALID_BOOLEAN_STRING_EXCEPTION;
104     }
105     return $it;
106 }
107
108
109 #---------------------------------
110
111 END { }       # module clean-up code here (global destructor)
112
113 1;
114 __END__
115
116 =back
117
118 =head1 AUTHOR
119
120 Koha Developement team <info@koha.org>
121
122 =cut