Completed
Push — 2.x ( 971a6c...befcfa )
by Scott Kingsley
08:24 queued 03:02
created

PodsField::input()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 5
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Pods Field class for common type-specific methods.
5
 *
6
 * @package Pods
7
 */
8
class PodsField {
9
10
	/**
11
	 * Whether this field is running under 1.x deprecated forms
12
	 *
13
	 * @var bool
14
	 * @since 2.0
15
	 */
16
	public static $deprecated = false;
17
18
	/**
19
	 * Field Type Identifier
20
	 *
21
	 * @var string
22
	 * @since 2.0
23
	 */
24
	public static $type = 'text';
25
26
	/**
27
	 * Field Type Label
28
	 *
29
	 * @var string
30
	 * @since 2.0
31
	 */
32
	public static $label = 'Unknown';
33
34
	/**
35
	 * Field Type Preparation
36
	 *
37
	 * @var string
38
	 * @since 2.0
39
	 */
40
	public static $prepare = '%s';
41
42
	/**
43
	 * Pod Types supported on (true for all, false for none, or give array of specific types supported)
44
	 *
45
	 * @var array|bool
46
	 * @since 2.1
47
	 */
48
	public static $pod_types = true;
49
50
	/**
51
	 * API caching for fields that need it during validate/save
52
	 *
53
	 * @var \PodsAPI
54
	 * @since 2.3
55
	 */
56
	private static $api = false;
0 ignored issues
show
Unused Code introduced by
The property $api is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
57
58
	/**
59
	 * Do things like register/enqueue scripts and stylesheets
60
	 *
61
	 * @since 2.0
62
	 */
63
	public function __construct() {
64
65
		// Subclasses utilize this method if needed.
66
67
	}
68
69
	/**
70
	 * Add options and set defaults for field type, shows in admin area
71
	 *
72
	 * @return array $options
73
	 *
74
	 * @since 2.0
75
	 * @see   PodsField::ui_options
76
	 */
77
	public function options() {
78
79
		$options = array(
80
			/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
            'option_name' => array(
82
                'label' => 'Option Label',
83
                'depends-on' => array( 'another_option' => 'specific-value' ),
84
                'default' => 'default-value',
85
                'type' => 'field_type',
86
                'data' => array(
87
                    'value1' => 'Label 1',
88
89
                    // Group your options together
90
                    'Option Group' => array(
91
                        'gvalue1' => 'Option Label 1',
92
                        'gvalue2' => 'Option Label 2'
93
                    ),
94
95
                    // below is only if the option_name above is the "{$fieldtype}_format_type"
96
                    'value2' => array(
97
                        'label' => 'Label 2',
98
                        'regex' => '[a-zA-Z]' // Uses JS regex validation for the value saved if this option selected
99
                    )
100
                ),
101
102
                // below is only for a boolean group
103
                'group' => array(
104
                    'option_boolean1' => array(
105
                        'label' => 'Option boolean 1?',
106
                        'default' => 1,
107
                        'type' => 'boolean'
108
                    ),
109
                    'option_boolean2' => array(
110
                        'label' => 'Option boolean 2?',
111
                        'default' => 0,
112
                        'type' => 'boolean'
113
                    )
114
                )
115
            )
116
            */
117
		);
118
119
		return $options;
120
121
	}
122
123
	/**
124
	 * Options for the Admin area, defaults to $this->options()
125
	 *
126
	 * @return array $options
127
	 *
128
	 * @since 2.0
129
	 * @see   PodsField::options
130
	 */
131
	public function ui_options() {
132
133
		return $this->options();
134
135
	}
136
137
	/**
138
	 * Define the current field's schema for DB table storage
139
	 *
140
	 * @param array|null $options
141
	 *
142
	 * @return string
143
	 *
144
	 * @since 2.0
145
	 */
146
	public function schema( $options = null ) {
147
148
		$schema = 'VARCHAR(255)';
149
150
		return $schema;
151
152
	}
153
154
	/**
155
	 * Define the current field's preparation for sprintf
156
	 *
157
	 * @param array|null $options
158
	 *
159
	 * @return string
160
	 *
161
	 * @since 2.0
162
	 */
163
	public function prepare( $options = null ) {
164
165
		$format = self::$prepare;
166
167
		return $format;
168
169
	}
170
171
	/**
172
	 * Change the value of the field
173
	 *
174
	 * @param mixed|null  $value
175
	 * @param string|null $name
176
	 * @param array|null  $options
177
	 * @param array|null  $pod
178
	 * @param int|null    $id
179
	 *
180
	 * @return mixed|null|string
181
	 *
182
	 * @since 2.3
183
	 */
184
	public function value( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
185
186
		return $value;
187
188
	}
189
190
	/**
191
	 * Change the way the value of the field is displayed with Pods::get
192
	 *
193
	 * @param mixed|null  $value
194
	 * @param string|null $name
195
	 * @param array|null  $options
196
	 * @param array|null  $pod
197
	 * @param int|null    $id
198
	 *
199
	 * @return mixed|null|string
200
	 *
201
	 * @since 2.0
202
	 */
203
	public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
204
205
		return $value;
206
207
	}
208
209
	/**
210
	 * Customize output of the form field
211
	 *
212
	 * @param string     $name
213
	 * @param mixed|null $value
214
	 * @param array|null $options
215
	 * @param array|null $pod
216
	 * @param int|null   $id
217
	 *
218
	 * @since 2.0
219
	 */
220
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
221
222
		$options = (array) $options;
223
224
		$form_field_type = PodsForm::$field_type;
0 ignored issues
show
Bug introduced by
The property field_type cannot be accessed from this context as it is declared private in class PodsForm.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
225
226
		if ( is_array( $value ) ) {
227
			$value = implode( ' ', $value );
228
		}
229
230
		pods_view( PODS_DIR . 'ui/fields/text.php', compact( array_keys( get_defined_vars() ) ) );
231
232
		return;
233
234
		// @todo Eventually use this code
235
		$options = (array) $options;
0 ignored issues
show
Unused Code introduced by
// @todo Eventually use ...ons = (array) $options; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
236
237
		$type = pods_v( 'type', $options, static::$type );
238
239
		$args = compact( array_keys( get_defined_vars() ) );
240
		$args = (object) $args;
241
242
		$this->render_input_script( $args );
243
244
	}
245
246
	/**
247
	 * Render input script for Pods DFV
248
	 *
249
	 * @param array|object $args {
250
	 *     Field information arguments.
251
	 *
252
	 *     @type string     $name    Field name
253
	 *     @type string     $type    Field type
254
	 *     @type array      $options Field options
255
	 *     @type mixed      $value   Current value
256
	 *     @type array      $pod     Pod information
257
	 *     @type int|string $id      Current item ID
258
	 * }
259
	 */
260
	public function render_input_script( $args ) {
261
262
		if ( is_array( $args ) ) {
263
			$args = (object) $args;
264
		}
265
266
		$script_content = json_encode( $this->build_dfv_field_data( $args ), JSON_HEX_TAG );;
0 ignored issues
show
Unused Code introduced by
The call to json_encode() has too many arguments starting with JSON_HEX_TAG.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
267
	?>
268
		<div class="pods-form-ui-field pods-dfv-field">
269
			<script type="application/json" class="pods-dfv-field-data"><?php echo $script_content ?></script>
270
		</div>
271
	<?php
272
273
	}
274
275
	/**
276
	 * Build field data for Pods DFV
277
	 *
278
	 * @param object $args {
279
	 *     Field information arguments.
280
	 *
281
	 *     @type string     $name            Field name
282
	 *     @type string     $type            Pod field type
283
	 *     @type string     $form_field_type HTML field type
284
	 *     @type array      $options         Field options
285
	 *     @type mixed      $value           Current value
286
	 *     @type array      $pod             Pod information
287
	 *     @type int|string $id              Current item ID
288
	 * }
289
	 *
290
	 * @return array
291
	 */
292
	public function build_dfv_field_data( $args ) {
293
294
		// Handle DFV options.
295
		$args->options = $this->build_dfv_field_options( $args->options, $args );
296
297
		// Handle DFV attributes.
298
		$attributes = PodsForm::merge_attributes( array(), $args->name, $args->type, $args->options );
299
		$attributes = $this->build_dfv_field_attributes( $attributes, $args );
300
		$attributes = array_map( 'esc_attr', $attributes );
301
302
		// Build DFV field data.
303
		$data = array(
304
			'htmlAttr'       => array(
305
				'id'         => $attributes['id'],
306
				'class'      => $attributes['class'],
307
				'name'       => $attributes['name'],
308
				'name_clean' => $attributes['data-name-clean'],
309
			),
310
			'fieldType'      => $args->type,
311
			'fieldItemData'  => $this->build_dfv_field_item_data( $args ),
312
			'fieldConfig'    => $this->build_dfv_field_config( $args ),
313
		);
314
315
		/**
316
		 * Filter Pods DFV field data to further customize functionality.
317
		 *
318
		 * @since 2.7
319
		 *
320
		 * @param array  $data DFV field data
321
		 * @param object $args {
322
		 *     Field information arguments.
323
		 *
324
		 *     @type string     $name            Field name
325
		 *     @type string     $type            Pod field type
326
		 *     @type string     $form_field_type HTML field type
327
		 *     @type array      $options         Field options
328
		 *     @type mixed      $value           Current value
329
		 *     @type array      $pod             Pod information
330
		 *     @type int|string $id              Current item ID
331
		 * }
332
		 * @param array  $attributes HTML attributes
333
		 */
334
		$data = apply_filters( 'pods_field_dfv_data', $data, $args, $attributes );
335
336
		return $data;
337
338
	}
339
340
	/**
341
	 * Build field options and handle any validation/customization for Pods DFV
342
	 *
343
	 * @param array  $options
344
	 * @param object $args {
345
	 *     Field information arguments.
346
	 *
347
	 *     @type string     $name    Field name
348
	 *     @type string     $type    Field type
349
	 *     @type array      $options Field options
350
	 *     @type mixed      $value   Current value
351
	 *     @type array      $pod     Pod information
352
	 *     @type int|string $id      Current item ID
353
	 * }
354
	 *
355
	 * @return array
356
	 */
357
	public function build_dfv_field_options( $options, $args ) {
358
359
		return $options;
360
361
	}
362
363
	/**
364
	 * Build field HTML attributes for Pods DFV.
365
	 *
366
	 * @param array  $attributes Default HTML attributes from field and PodsForm::merge_attributes.
367
	 * @param object $args {
368
	 *     Field information arguments.
369
	 *
370
	 *     @type string     $name    Field name
371
	 *     @type string     $type    Field type
372
	 *     @type array      $options Field options
373
	 *     @type mixed      $value   Current value
374
	 *     @type array      $pod     Pod information
375
	 *     @type int|string $id      Current item ID
376
	 * }
377
	 *
378
	 * @return array
379
	 */
380
	public function build_dfv_field_attributes( $attributes, $args ) {
381
382
		return $attributes;
383
384
	}
385
386
	/**
387
	 * Build field config for Pods DFV using field options.
388
	 *
389
	 * This is for customizing the options and adding output-specific config values.
390
	 *
391
	 * @param object $args {
392
	 *     Field information arguments.
393
	 *
394
	 *     @type string     $name    Field name
395
	 *     @type string     $type    Field type
396
	 *     @type array      $options Field options
397
	 *     @type mixed      $value   Current value
398
	 *     @type array      $pod     Pod information
399
	 *     @type int|string $id      Current item ID
400
	 * }
401
	 *
402
	 * @return array
403
	 */
404
	public function build_dfv_field_config( $args ) {
405
406
		$config = $args->options;
407
408
		unset( $config['data'] );
409
410
		$config['item_id'] = (int) $args->id;
411
412
		return $config;
413
414
	}
415
416
	/**
417
	 * Build array of item data for Pods DFV
418
	 *
419
	 * @param object $args {
420
	 *     Field information arguments.
421
	 *
422
	 *     @type string     $name    Field name
423
	 *     @type string     $type    Field type
424
	 *     @type array      $options Field options
425
	 *     @type mixed      $value   Current value
426
	 *     @type array      $pod     Pod information
427
	 *     @type int|string $id      Current item ID
428
	 * }
429
	 *
430
	 * @return array
431
	 */
432
	public function build_dfv_field_item_data( $args ) {
433
434
		$data = array();
435
436
		if ( ! empty( $args->options['data'] ) && is_array( $args->options['data'] ) ) {
437
			$data = $args->options['data'];
438
		}
439
440
		return $data;
441
442
	}
443
444
	/**
445
	 * Get the data from the field
446
	 *
447
	 * @param string            $name  The name of the field
448
	 * @param string|array|null $value The value of the field
449
	 * @param array|null        $options
450
	 * @param array|null        $pod
451
	 * @param int|null          $id
452
	 * @param boolean           $in_form
453
	 *
454
	 * @return array Array of possible field data
455
	 *
456
	 * @since 2.0
457
	 */
458
	public function data( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) {
0 ignored issues
show
Unused Code introduced by
The parameter $in_form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
459
460
		return (array) $value;
461
462
	}
463
464
	/**
465
	 * Build regex necessary for JS validation
466
	 *
467
	 * @param mixed|null  $value
468
	 * @param string|null $name
469
	 * @param array|null  $options
470
	 * @param string|null $pod
471
	 * @param int|null    $id
472
	 *
473
	 * @return bool
474
	 *
475
	 * @since 2.0
476
	 */
477
	public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
478
479
		return false;
480
481
	}
482
483
	/**
484
	 * Validate a value before it's saved
485
	 *
486
	 * @param mixed       $value
487
	 * @param string|null $name
488
	 * @param array|null  $options
489
	 * @param array|null  $fields
490
	 * @param array|null  $pod
491
	 * @param int|null    $id
492
	 * @param array|null  $params
493
	 *
494
	 * @return bool
495
	 *
496
	 * @since 2.0
497
	 */
498
	public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
499
500
		return true;
501
502
	}
503
504
	/**
505
	 * Change the value or perform actions after validation but before saving to the DB
506
	 *
507
	 * @param mixed       $value
508
	 * @param int|null    $id
509
	 * @param string|null $name
510
	 * @param array|null  $options
511
	 * @param array|null  $fields
512
	 * @param array|null  $pod
513
	 * @param object|null $params
514
	 *
515
	 * @return mixed
516
	 *
517
	 * @since 2.0
518
	 */
519
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
520
521
		return $value;
522
523
	}
524
525
	/**
526
	 * Save the value to the DB
527
	 *
528
	 * @param mixed       $value
529
	 * @param int|null    $id
530
	 * @param string|null $name
531
	 * @param array|null  $options
532
	 * @param array|null  $fields
533
	 * @param array|null  $pod
534
	 * @param object|null $params
535
	 *
536
	 * @return bool|null Whether the value was saved, returning null means no save needed to occur
537
	 *
538
	 * @since 2.3
539
	 */
540
	public function save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
541
542
		return null;
543
544
	}
545
546
	/**
547
	 * Perform actions after saving to the DB
548
	 *
549
	 * @param mixed       $value
550
	 * @param int|null    $id
551
	 * @param string|null $name
552
	 * @param array|null  $options
553
	 * @param array|null  $fields
554
	 * @param array|null  $pod
555
	 * @param object|null $params
556
	 *
557
	 * @since 2.0
558
	 */
559
	public function post_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
560
561
		// Subclasses utilize this method if needed.
562
563
	}
564
565
	/**
566
	 * Perform actions before deleting from the DB
567
	 *
568
	 * @param int|null    $id
569
	 * @param string|null $name
570
	 * @param array|null  $options
571
	 * @param string|null $pod
572
	 *
573
	 * @since 2.0
574
	 */
575
	public function pre_delete( $id = null, $name = null, $options = null, $pod = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
576
577
		// Subclasses utilize this method if needed.
578
579
	}
580
581
	/**
582
	 * Delete the value from the DB
583
	 *
584
	 * @param int|null    $id
585
	 * @param string|null $name
586
	 * @param array|null  $options
587
	 * @param array|null  $pod
588
	 *
589
	 * @since 2.3
590
	 */
591
	public function delete( $id = null, $name = null, $options = null, $pod = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
592
593
		// Subclasses utilize this method if needed.
594
595
	}
596
597
	/**
598
	 * Perform actions after deleting from the DB
599
	 *
600
	 * @param int|null    $id
601
	 * @param string|null $name
602
	 * @param array|null  $options
603
	 * @param array|null  $pod
604
	 *
605
	 * @since 2.0
606
	 */
607
	public function post_delete( $id = null, $name = null, $options = null, $pod = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
608
609
		// Subclasses utilize this method if needed.
610
611
	}
612
613
	/**
614
	 * Customize the Pods UI manage table column output
615
	 *
616
	 * @param int         $id
617
	 * @param mixed       $value
618
	 * @param string|null $name
619
	 * @param array|null  $options
620
	 * @param array|null  $fields
621
	 * @param array|null  $pod
622
	 *
623
	 * @return string Value to be shown in the UI
624
	 *
625
	 * @since 2.0
626
	 */
627
	public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
628
629
		return $value;
630
631
	}
632
633
	/**
634
	 * Placeholder function to allow var_export() use with classes
635
	 *
636
	 * @param array $properties
637
	 *
638
	 * @return object|void
639
	 */
640
	public static function __set_state( $properties ) {
641
642
		return;
643
644
	}
645
646
}
647