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

PodsField_Email::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_Email extends PodsField {
7
8
	/**
9
	 * {@inheritdoc}
10
	 */
11
	public static $group = 'Text';
12
13
	/**
14
	 * {@inheritdoc}
15
	 */
16
	public static $type = 'email';
17
18
	/**
19
	 * {@inheritdoc}
20
	 */
21
	public static $label = 'E-mail';
22
23
	/**
24
	 * {@inheritdoc}
25
	 */
26
	public static $prepare = '%s';
27
28
	/**
29
	 * {@inheritdoc}
30
	 */
31
	public function setup() {
32
33
		self::$label = __( 'E-mail', 'pods' );
34
	}
35
36
	/**
37
	 * {@inheritdoc}
38
	 */
39
	public function options() {
40
41
		$options = array(
42
			static::$type . '_repeatable'  => array(
43
				'label'             => __( 'Repeatable Field', 'pods' ),
44
				'default'           => 0,
45
				'type'              => 'boolean',
46
				'help'              => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ),
47
				'boolean_yes_label' => '',
48
				'dependency'        => true,
49
				'developer_mode'    => true,
50
			),
51
			static::$type . '_max_length'  => array(
52
				'label'   => __( 'Maximum Length', 'pods' ),
53
				'default' => 255,
54
				'type'    => 'number',
55
				'help'    => __( 'Set to -1 for no limit', 'pods' ),
56
			),
57
			static::$type . '_html5'       => array(
58
				'label'   => __( 'Enable HTML5 Input Field?', 'pods' ),
59
				'default' => apply_filters( 'pods_form_ui_field_html5', 0, static::$type ),
60
				'type'    => 'boolean',
61
			),
62
			static::$type . '_placeholder' => array(
63
				'label'   => __( 'HTML Placeholder', 'pods' ),
64
				'default' => '',
65
				'type'    => 'text',
66
				'help'    => array(
67
					__( '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' ),
68
					'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text',
69
				),
70
			),
71
		);
72
73
		return $options;
74
	}
75
76
	/**
77
	 * {@inheritdoc}
78
	 */
79 View Code Duplication
	public function schema( $options = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
81
		$length = (int) pods_v( static::$type . '_max_length', $options, 255 );
82
83
		$schema = 'VARCHAR(' . $length . ')';
84
85
		if ( 255 < $length || $length < 1 ) {
86
			$schema = 'LONGTEXT';
87
		}
88
89
		return $schema;
90
	}
91
92
	/**
93
	 * {@inheritdoc}
94
	 */
95 View Code Duplication
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
97
		$options         = (array) $options;
98
		$form_field_type = PodsForm::$field_type;
99
100
		if ( is_array( $value ) ) {
101
			$value = implode( ' ', $value );
102
		}
103
104
		$field_type = 'email';
105
106
		if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) {
107
			if ( pods_v( 'read_only', $options, false ) ) {
108
				$options['readonly'] = true;
109
110
				$field_type = 'text';
111
			} else {
112
				return;
113
			}
114
		} elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) {
115
			$options['readonly'] = true;
116
117
			$field_type = 'text';
118
		}
119
120
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
121
	}
122
123
	/**
124
	 * {@inheritdoc}
125
	 */
126
	public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
127
128
		return false;
129
	}
130
131
	/**
132
	 * {@inheritdoc}
133
	 */
134 View Code Duplication
	public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
136
		$errors = array();
137
138
		$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
139
140
		if ( is_array( $check ) ) {
141
			$errors = $check;
142
		} else {
143
			if ( 0 < strlen( $value ) && '' === $check ) {
144
				$label = pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) );
145
146
				if ( 1 === (int) pods_v( 'required', $options ) ) {
147
					$errors[] = sprintf( __( '%s is required', 'pods' ), $label );
148
				} else {
149
					$errors[] = sprintf( __( 'Invalid e-mail provided for %s', 'pods' ), $label );
150
				}
151
			}
152
		}
153
154
		if ( ! empty( $errors ) ) {
155
			return $errors;
156
		}
157
158
		return true;
159
	}
160
161
	/**
162
	 * {@inheritdoc}
163
	 */
164
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
165
166
		$options = (array) $options;
167
168
		if ( ! is_email( $value ) ) {
169
			$value = '';
170
		}
171
172
		$length = (int) pods_v( static::$type . '_max_length', $options, 255 );
173
174
		if ( 0 < $length && $length < pods_mb_strlen( $value ) ) {
175
			$value = pods_mb_substr( $value, 0, $length );
176
		}
177
178
		return $value;
179
	}
180
181
	/**
182
	 * {@inheritdoc}
183
	 */
184
	public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
185
186
		return '<a href="mailto:' . esc_attr( $value ) . '">' . $value . '</a>';
187
	}
188
}
189