|
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
|
|
|
$value = ! empty( $value ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$field_type = 'checkbox'; |
|
123
|
|
|
|
|
124
|
|
|
if ( 'radio' === pods_v( static::$type . '_format_type', $options ) ) { |
|
125
|
|
|
$field_type = 'radio'; |
|
126
|
|
|
} elseif ( 'dropdown' === pods_v( static::$type . '_format_type', $options ) ) { |
|
127
|
|
|
$field_type = 'select'; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) { |
|
131
|
|
|
if ( pods_v( 'read_only', $options, false ) ) { |
|
132
|
|
|
$options['readonly'] = true; |
|
133
|
|
|
} else { |
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
} elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) { |
|
137
|
|
|
$options['readonly'] = true; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if ( 1 === $value || '1' === $value || true === $value ) { |
|
141
|
|
|
$value = 1; |
|
142
|
|
|
} else { |
|
143
|
|
|
$value = 0; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritdoc} |
|
151
|
|
|
*/ |
|
152
|
|
|
public function data( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) { |
|
153
|
|
|
|
|
154
|
|
|
if ( 'checkbox' !== pods_v( static::$type . '_format_type', $options ) ) { |
|
155
|
|
|
$data = array( |
|
156
|
|
|
1 => pods_v( static::$type . '_yes_label', $options ), |
|
157
|
|
|
0 => pods_v( static::$type . '_no_label', $options ), |
|
158
|
|
|
); |
|
159
|
|
|
} else { |
|
160
|
|
|
$data = array( |
|
161
|
|
|
1 => pods_v( static::$type . '_yes_label', $options ), |
|
162
|
|
|
); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $data; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* {@inheritdoc} |
|
170
|
|
|
*/ |
|
171
|
|
|
public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
|
172
|
|
|
|
|
173
|
|
|
return false; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* {@inheritdoc} |
|
178
|
|
|
*/ |
|
179
|
|
|
public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
|
180
|
|
|
|
|
181
|
|
|
return true; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* {@inheritdoc} |
|
186
|
|
|
*/ |
|
187
|
|
|
public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
|
188
|
|
|
|
|
189
|
|
|
$yes = strtolower( pods_v( static::$type . '_yes_label', $options, __( 'Yes', 'pods' ), true ) ); |
|
190
|
|
|
$no = strtolower( pods_v( static::$type . '_no_label', $options, __( 'No', 'pods' ), true ) ); |
|
191
|
|
|
|
|
192
|
|
|
// Only allow 0 / 1 |
|
193
|
|
|
if ( 'yes' === strtolower( $value ) || '1' === (string) $value ) { |
|
194
|
|
|
$value = 1; |
|
195
|
|
|
} elseif ( 'no' === strtolower( $value ) || '0' === (string) $value ) { |
|
196
|
|
|
$value = 0; |
|
197
|
|
|
} elseif ( strtolower( $value ) === $yes ) { |
|
198
|
|
|
$value = 1; |
|
199
|
|
|
} elseif ( strtolower( $value ) === $no ) { |
|
200
|
|
|
$value = 0; |
|
201
|
|
|
} else { |
|
202
|
|
|
$value = ( 0 === (int) $value ? 0 : 1 ); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return $value; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* {@inheritdoc} |
|
210
|
|
|
*/ |
|
211
|
|
|
public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
|
212
|
|
|
|
|
213
|
|
|
$yesno = array( |
|
214
|
|
|
1 => pods_v( static::$type . '_yes_label', $options, __( 'Yes', 'pods' ), true ), |
|
215
|
|
|
0 => pods_v( static::$type . '_no_label', $options, __( 'No', 'pods' ), true ), |
|
216
|
|
|
); |
|
217
|
|
|
|
|
218
|
|
|
if ( isset( $yesno[ (int) $value ] ) ) { |
|
219
|
|
|
$value = strip_tags( $yesno[ (int) $value ], '<strong><a><em><span><img>' ); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
return $value; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|