PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.7.31.4
Pods – Custom Content Types and Fields v2.7.31.4
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / classes / fields / boolean.php
pods / classes / fields Last commit date
avatar.php 3 days ago boolean.php 3 days ago code.php 3 days ago color.php 3 days ago comment.php 3 days ago currency.php 3 days ago date.php 3 days ago datetime.php 3 days ago email.php 3 days ago file.php 3 days ago html.php 3 days ago link.php 3 days ago number.php 3 days ago oembed.php 3 days ago paragraph.php 3 days ago password.php 3 days ago phone.php 3 days ago pick.php 3 days ago slug.php 3 days ago taxonomy.php 3 days ago text.php 3 days ago time.php 3 days ago website.php 3 days ago wysiwyg.php 3 days ago
boolean.php
246 lines
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 // is_empty() is used for if/else statements. Value should be true to pass.
83 $value = $this->pre_save( $value );
84
85 if ( ! $value ) {
86 $is_empty = true;
87 }
88
89 return $is_empty;
90
91 }
92
93 /**
94 * {@inheritdoc}
95 */
96 public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
97
98 $yesno = array(
99 1 => pods_v( static::$type . '_yes_label', $options ),
100 0 => pods_v( static::$type . '_no_label', $options ),
101 );
102
103 // Deprecated handling for 1.x
104 if ( ! parent::$deprecated && isset( $yesno[ (int) $value ] ) ) {
105 $value = $yesno[ (int) $value ];
106 }
107
108 return $value;
109 }
110
111 /**
112 * {@inheritdoc}
113 */
114 public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
115
116 $options = (array) $options;
117 $form_field_type = PodsForm::$field_type;
118
119 if ( is_array( $value ) ) {
120 if ( ! empty( $value ) ) {
121 $value = true;
122 } else {
123 $value = false;
124 }
125 }
126
127 $field_type = 'checkbox';
128
129 if ( 'radio' === pods_v( static::$type . '_format_type', $options ) ) {
130 $field_type = 'radio';
131 } elseif ( 'dropdown' === pods_v( static::$type . '_format_type', $options ) ) {
132 $field_type = 'select';
133 }
134
135 if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) {
136 if ( pods_v( 'read_only', $options, false ) ) {
137 $options['readonly'] = true;
138 } else {
139 return;
140 }
141 } elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) {
142 $options['readonly'] = true;
143 }
144
145 if ( 1 === $value || '1' === $value || true === $value ) {
146 $value = 1;
147 } else {
148 $value = 0;
149 }
150
151 pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
152 }
153
154 /**
155 * {@inheritdoc}
156 */
157 public function data( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) {
158
159 if ( 'checkbox' !== pods_v( static::$type . '_format_type', $options ) ) {
160 $data = array(
161 1 => pods_v( static::$type . '_yes_label', $options ),
162 0 => pods_v( static::$type . '_no_label', $options ),
163 );
164 } else {
165 $data = array(
166 1 => pods_v( static::$type . '_yes_label', $options ),
167 );
168 }
169
170 return $data;
171 }
172
173 /**
174 * {@inheritdoc}
175 */
176 public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
177 $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params );
178
179 if ( ! $this->is_required( $options ) ) {
180 // Any value can be parsed to boolean.
181 return $validate;
182 }
183
184 $errors = array();
185
186 if ( is_array( $validate ) ) {
187 $errors = $validate;
188 }
189
190 $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
191
192 $yes_required = ( 'checkbox' === pods_v( static::$type . '_format_type', $options ) );
193
194 if ( $yes_required && ! $check ) {
195 $errors[] = __( 'This field is required.', 'pods' );
196 }
197
198 if ( ! empty( $errors ) ) {
199 return $errors;
200 }
201
202 return $validate;
203 }
204
205 /**
206 * Replicates filter_var() with `FILTER_VALIDATE_BOOLEAN` and adds custom input for yes/no values.
207 *
208 * {@inheritdoc}
209 */
210 public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
211
212 $yes = strtolower( pods_v( static::$type . '_yes_label', $options, __( 'Yes', 'pods' ), true ) );
213 $no = strtolower( pods_v( static::$type . '_no_label', $options, __( 'No', 'pods' ), true ) );
214
215 if ( is_string( $value ) ) {
216 $value = strtolower( $value );
217 }
218
219 if ( $yes === $value ) {
220 $value = 1;
221 } else {
222 // Validate: 1, "1", true, "true", "on", and "yes" as 1, all others are 0.
223 $value = (int) filter_var( $value, FILTER_VALIDATE_BOOLEAN );
224 }
225
226 return $value;
227 }
228
229 /**
230 * {@inheritdoc}
231 */
232 public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
233
234 $yesno = array(
235 1 => pods_v( static::$type . '_yes_label', $options, __( 'Yes', 'pods' ), true ),
236 0 => pods_v( static::$type . '_no_label', $options, __( 'No', 'pods' ), true ),
237 );
238
239 if ( isset( $yesno[ (int) $value ] ) ) {
240 $value = strip_tags( $yesno[ (int) $value ], '<strong><a><em><span><img>' );
241 }
242
243 return $value;
244 }
245 }
246