Completed
Pull Request — 2.x (#4569)
by Scott Kingsley
07:09
created

PodsField_Slug::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package Pods\Fields
5
 */
6
class PodsField_Slug extends PodsField {
7
8
	/**
9
	 * {@inheritdoc}
10
	 */
11
	public static $type = 'slug';
12
13
	/**
14
	 * {@inheritdoc}
15
	 */
16
	public static $label = 'Permalink (url-friendly)';
17
18
	/**
19
	 * {@inheritdoc}
20
	 */
21
	public static $prepare = '%s';
22
23
	/**
24
	 * {@inheritdoc}
25
	 */
26
	public static $pod_types = array(
27
		'pod',
28
		'table',
29
	);
30
31
	/**
32
	 * {@inheritdoc}
33
	 */
34
	public function setup() {
35
36
		self::$label = __( 'Permalink (url-friendly)', 'pods' );
37
	}
38
39
	/**
40
	 * {@inheritdoc}
41
	 */
42
	public function options() {
43
44
		$options = array(
45
			static::$type . '_placeholder' => array(
46
				'label'   => __( 'HTML Placeholder', 'pods' ),
47
				'default' => '',
48
				'type'    => 'text',
49
				'help'    => array(
50
					__( 'Placeholders can provide instructions or an example of the required data format for a field. Please note: It is not a replacement for labels or description text, and it is less accessible for people using screen readers.', 'pods' ),
51
					'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text',
52
				),
53
			),
54
		);
55
56
		return $options;
57
58
	}
59
60
	/**
61
	 * {@inheritdoc}
62
	 */
63
	public function schema( $options = null ) {
64
65
		$schema = 'VARCHAR(200)';
66
67
		return $schema;
68
	}
69
70
	/**
71
	 * {@inheritdoc}
72
	 */
73
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
74
75
		$options         = (array) $options;
76
		$form_field_type = PodsForm::$field_type;
77
78
		if ( is_array( $value ) ) {
79
			$value = implode( '-', $value );
80
		}
81
82
		$field_type = 'slug';
83
84
		if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) {
85
			if ( pods_v( 'read_only', $options, false ) ) {
86
				$options['readonly'] = true;
87
88
				$field_type = 'text';
89
			} else {
90
				return;
91
			}
92
		} elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) {
93
			$options['readonly'] = true;
94
95
			$field_type = 'text';
96
		}
97
98
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
99
	}
100
101
	/**
102
	 * {@inheritdoc}
103
	 */
104
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
105
106
		$index = pods_v( 'pod_index', pods_v( 'options', $pod, $pod, true ), 'id', true );
107
108
		if ( empty( $value ) && isset( $fields[ $index ] ) ) {
109
			$value = $fields[ $index ]['value'];
110
		}
111
112
		$value = pods_unique_slug( $value, $name, $pod, 0, $params->id, null, false );
113
114
		return $value;
115
	}
116
}
117