forked from pods-framework/pods
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PodsField.php
384 lines (347 loc) · 9.03 KB
/
PodsField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
/**
* @package Pods
*/
class PodsField {
/**
* Whether this field is running under 1.x deprecated forms
*
* @var bool
* @since 2.0
*/
public static $deprecated = false;
/**
* Field Type Identifier
*
* @var string
* @since 2.0
*/
public static $type = 'text';
/**
* Field Type Label
*
* @var string
* @since 2.0
*/
public static $label = 'Unknown';
/**
* Field Type Preparation
*
* @var string
* @since 2.0
*/
public static $prepare = '%s';
/**
* Pod Types supported on (true for all, false for none, or give array of specific types supported)
*
* @var array|bool
* @since 2.1
*/
public static $pod_types = true;
/**
* API caching for fields that need it during validate/save
*
* @var \PodsAPI
* @since 2.3
*/
private static $api = false;
/**
* Do things like register/enqueue scripts and stylesheets
*
* @return \PodsField
*
* @since 2.0
*/
public function __construct () {
}
/**
* Add options and set defaults for field type, shows in admin area
*
* @return array $options
*
* @since 2.0
* @see PodsField::ui_options
*/
public function options () {
$options = array( /*
'option_name' => array(
'label' => 'Option Label',
'depends-on' => array( 'another_option' => 'specific-value' ),
'default' => 'default-value',
'type' => 'field_type',
'data' => array(
'value1' => 'Label 1',
// Group your options together
'Option Group' => array(
'gvalue1' => 'Option Label 1',
'gvalue2' => 'Option Label 2'
),
// below is only if the option_name above is the "{$fieldtype}_format_type"
'value2' => array(
'label' => 'Label 2',
'regex' => '[a-zA-Z]' // Uses JS regex validation for the value saved if this option selected
)
),
// below is only for a boolean group
'group' => array(
'option_boolean1' => array(
'label' => 'Option boolean 1?',
'default' => 1,
'type' => 'boolean'
),
'option_boolean2' => array(
'label' => 'Option boolean 2?',
'default' => 0,
'type' => 'boolean'
)
)
) */
);
return $options;
}
/**
* Options for the Admin area, defaults to $this->options()
*
* @return array $options
*
* @since 2.0
* @see PodsField::options
*/
public function ui_options () {
return $this->options();
}
/**
* Define the current field's schema for DB table storage
*
* @param array $options
*
* @return string
* @since 2.0
*/
public function schema ( $options = null ) {
$schema = 'VARCHAR(255)';
return $schema;
}
/**
* Define the current field's preparation for sprintf
*
* @param array $options
*
* @return array
* @since 2.0
*/
public function prepare ( $options = null ) {
$format = self::$prepare;
return $format;
}
/**
* Change the value of the field
*
* @param mixed $value
* @param string $name
* @param array $options
* @param array $pod
* @param int $id
*
* @return mixed|null|string
* @since 2.3
*/
public function value ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
return $value;
}
/**
* Change the way the value of the field is displayed with Pods::get
*
* @param mixed $value
* @param string $name
* @param array $options
* @param array $pod
* @param int $id
*
* @return mixed|null|string
* @since 2.0
*/
public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
return $value;
}
/**
* Customize output of the form field
*
* @param string $name
* @param mixed $value
* @param array $options
* @param array $pod
* @param int $id
*
* @return void
*
* @since 2.0
*/
public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
$options = (array) $options;
$form_field_type = PodsForm::$field_type;
if ( is_array( $value ) )
$value = implode( ' ', $value );
pods_view( PODS_DIR . 'ui/fields/text.php', compact( array_keys( get_defined_vars() ) ) );
}
/**
* Get the data from the field
*
* @param string $name The name of the field
* @param string|array $value The value of the field
* @param array $options
* @param array $pod
* @param int $id
* @param boolean $in_form
*
* @return array Array of possible field data
*
* @since 2.0
*/
public function data ( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) {
return (array) $value;
}
/**
* Build regex necessary for JS validation
*
* @param mixed $value
* @param string $name
* @param array $options
* @param string $pod
* @param int $id
*
* @return bool
* @since 2.0
*/
public function regex ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
return false;
}
/**
* Validate a value before it's saved
*
* @param mixed $value
* @param string $name
* @param array $options
* @param array $fields
* @param array $pod
* @param int $id
* @param array $params
*
* @return bool
* @since 2.0
*/
public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
return true;
}
/**
* Change the value or perform actions after validation but before saving to the DB
*
* @param mixed $value
* @param int $id
* @param string $name
* @param array $options
* @param array $fields
* @param array $pod
* @param object $params
*
* @return mixed
* @since 2.0
*/
public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
return $value;
}
/**
* Save the value to the DB
*
* @param mixed $value
* @param int $id
* @param string $name
* @param array $options
* @param array $fields
* @param array $pod
* @param object $params
*
* @return bool|void Whether the value was saved
*
* @since 2.3
*/
public function save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
return null;
}
/**
* Perform actions after saving to the DB
*
* @param mixed $value
* @param int $id
* @param string $name
* @param array $options
* @param array $fields
* @param array $pod
* @param object $params
*
* @since void
*
* @since 2.0
*/
public function post_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
}
/**
* Perform actions before deleting from the DB
*
* @param int $id
* @param string $name
* @param null $options
* @param string $pod
*
* @since void
*
* @since 2.0
*/
public function pre_delete ( $id = null, $name = null, $options = null, $pod = null ) {
}
/**
* Delete the value from the DB
*
* @param int $id
* @param string $name
* @param array $options
* @param array $pod
*
* @since void
*
* @since 2.3
*/
public function delete ( $id = null, $name = null, $options = null, $pod = null ) {
}
/**
* Perform actions after deleting from the DB
*
* @param int $id
* @param string $name
* @param array $options
* @param array $pod
*
* @since void
*
* @since 2.0
*/
public function post_delete ( $id = null, $name = null, $options = null, $pod = null ) {
}
/**
* Customize the Pods UI manage table column output
*
* @param int $id
* @param mixed $value
* @param string $name
* @param array $options
* @param array $fields
* @param array $pod
*
* @since string Value to be shown in the UI
*
* @since 2.0
*/
public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
return $value;
}
}