1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Handles boolean field type data and operations. |
5
|
|
|
* |
6
|
|
|
* @package Pods\Fields |
7
|
|
|
*/ |
8
|
|
|
class PodsField_Boolean extends PodsField { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* {@inheritdoc} |
12
|
|
|
*/ |
13
|
|
|
public static $type = 'boolean'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
|
|
public static $label = 'Yes / No'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public static $prepare = '%s'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function setup() { |
29
|
|
|
|
30
|
|
|
self::$label = __( 'Yes / No', 'pods' ); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function options() { |
37
|
|
|
|
38
|
|
|
$options = array( |
39
|
|
|
static::$type . '_format_type' => array( |
40
|
|
|
'label' => __( 'Input Type', 'pods' ), |
41
|
|
|
'default' => 'checkbox', |
42
|
|
|
'type' => 'pick', |
43
|
|
|
'data' => array( |
44
|
|
|
'checkbox' => __( 'Checkbox', 'pods' ), |
45
|
|
|
'radio' => __( 'Radio Buttons', 'pods' ), |
46
|
|
|
'dropdown' => __( 'Drop Down', 'pods' ), |
47
|
|
|
), |
48
|
|
|
'dependency' => true, |
49
|
|
|
), |
50
|
|
|
static::$type . '_yes_label' => array( |
51
|
|
|
'label' => __( 'Yes Label', 'pods' ), |
52
|
|
|
'default' => __( 'Yes', 'pods' ), |
53
|
|
|
'type' => 'text', |
54
|
|
|
), |
55
|
|
|
static::$type . '_no_label' => array( |
56
|
|
|
'label' => __( 'No Label', 'pods' ), |
57
|
|
|
'default' => __( 'No', 'pods' ), |
58
|
|
|
'type' => 'text', |
59
|
|
|
), |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
return $options; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function schema( $options = null ) { |
69
|
|
|
|
70
|
|
|
$schema = 'BOOL DEFAULT 0'; |
71
|
|
|
|
72
|
|
|
return $schema; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function is_empty( $value = null ) { |
79
|
|
|
|
80
|
|
|
$is_empty = false; |
81
|
|
|
|
82
|
|
|
$value = filter_var( $value, FILTER_VALIDATE_BOOLEAN ); |
83
|
|
|
|
84
|
|
|
if ( ! $value ) { |
85
|
|
|
$is_empty = true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $is_empty; |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
96
|
|
|
|
97
|
|
|
$yesno = array( |
98
|
|
|
1 => pods_v( static::$type . '_yes_label', $options ), |
99
|
|
|
0 => pods_v( static::$type . '_no_label', $options ), |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
// Deprecated handling for 1.x |
103
|
|
|
if ( ! parent::$deprecated && isset( $yesno[ (int) $value ] ) ) { |
104
|
|
|
$value = $yesno[ (int) $value ]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $value; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
114
|
|
|
|
115
|
|
|
$options = (array) $options; |
116
|
|
|
$form_field_type = PodsForm::$field_type; |
117
|
|
|
|
118
|
|
|
if ( is_array( $value ) ) { |
119
|
|
|
if ( ! empty( $value ) ) { |
120
|
|
|
$value = true; |
121
|
|
|
} else { |
122
|
|
|
$value = false; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$field_type = 'checkbox'; |
127
|
|
|
|
128
|
|
|
if ( 'radio' === pods_v( static::$type . '_format_type', $options ) ) { |
129
|
|
|
$field_type = 'radio'; |
130
|
|
|
} elseif ( 'dropdown' === pods_v( static::$type . '_format_type', $options ) ) { |
131
|
|
|
$field_type = 'select'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) { |
135
|
|
|
if ( pods_v( 'read_only', $options, false ) ) { |
136
|
|
|
$options['readonly'] = true; |
137
|
|
|
} else { |
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
} elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) { |
141
|
|
|
$options['readonly'] = true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ( 1 === $value || '1' === $value || true === $value ) { |
145
|
|
|
$value = 1; |
146
|
|
|
} else { |
147
|
|
|
$value = 0; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function data( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) { |
157
|
|
|
|
158
|
|
|
if ( 'checkbox' !== pods_v( static::$type . '_format_type', $options ) ) { |
159
|
|
|
$data = array( |
160
|
|
|
1 => pods_v( static::$type . '_yes_label', $options ), |
161
|
|
|
0 => pods_v( static::$type . '_no_label', $options ), |
162
|
|
|
); |
163
|
|
|
} else { |
164
|
|
|
$data = array( |
165
|
|
|
1 => pods_v( static::$type . '_yes_label', $options ), |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $data; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
|
public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
176
|
|
|
|
177
|
|
|
$yes = strtolower( pods_v( static::$type . '_yes_label', $options, __( 'Yes', 'pods' ), true ) ); |
178
|
|
|
$no = strtolower( pods_v( static::$type . '_no_label', $options, __( 'No', 'pods' ), true ) ); |
|
|
|
|
179
|
|
|
|
180
|
|
|
// Only allow 0 / 1 |
181
|
|
|
if ( 'yes' === strtolower( $value ) || '1' === (string) $value ) { |
182
|
|
|
$value = 1; |
183
|
|
|
} elseif ( 'no' === strtolower( $value ) || '0' === (string) $value ) { |
184
|
|
|
$value = 0; |
185
|
|
|
} elseif ( strtolower( $value ) === $yes ) { |
186
|
|
|
$value = 1; |
187
|
|
|
} elseif ( strtolower( $value ) === $no ) { |
188
|
|
|
$value = 0; |
189
|
|
|
} elseif ( 0 !== (int) $value ) { |
190
|
|
|
$value = 1; |
191
|
|
|
} else { |
192
|
|
|
$value = 0; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $value; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritdoc} |
200
|
|
|
*/ |
201
|
|
|
public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
|
|
|
|
202
|
|
|
|
203
|
|
|
$yesno = array( |
204
|
|
|
1 => pods_v( static::$type . '_yes_label', $options, __( 'Yes', 'pods' ), true ), |
205
|
|
|
0 => pods_v( static::$type . '_no_label', $options, __( 'No', 'pods' ), true ), |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
if ( isset( $yesno[ (int) $value ] ) ) { |
209
|
|
|
$value = strip_tags( $yesno[ (int) $value ], '<strong><a><em><span><img>' ); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $value; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.