Completed
Pull Request — 2.x (#4782)
by Jory
07:18
created

PodsField_Icon::ui()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 6
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package Pods\Fields
4
 */
5
class PodsField_Icon extends PodsField {
6
7
	/**
8
	 * Field Type Identifier
9
	 *
10
	 * @var string
11
	 * @since 2.0
12
	 */
13
	public static $type = 'icon';
14
15
	/**
16
	 * Field Type Label
17
	 *
18
	 * @var string
19
	 * @since 2.0
20
	 */
21
	public static $label = 'Icon Picker';
22
23
	/**
24
	 * Field Type Preparation
25
	 *
26
	 * @var string
27
	 * @since 2.0
28
	 */
29
	public static $prepare = '%s';
30
31
	/**
32
	 * Do things like register/enqueue scripts and stylesheets
33
	 *
34
	 * @since 2.0
35
	 */
36
	public function __construct () {
37
		self::$label = __( 'Icon Picker', 'pods' );
38
39
		// Load the icon picker library
40
		if ( ! class_exists( 'Icon_Picker' ) ) {
41
			require_once PODS_DIR . '/vendor/kucrut/icon-picker/icon-picker.php';
42
		}
43
		$icon_picker = Icon_Picker::instance();
44
	}
45
46
	/**
47
	 * Add options and set defaults to
48
	 *
49
	 *
50
	 * @return array
51
	 * @since 2.0
52
	 */
53
	public function options () {
54
		$options = array(
55
			self::$type . '_repeatable' => array(
56
				'label' => __( 'Repeatable Field', 'pods' ),
57
				'default' => 0,
58
				'type' => 'boolean',
59
				'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' ),
60
				'boolean_yes_label' => '',
61
				'dependency' => true,
62
				'developer_mode' => true
63
			),
64
			'output_options' => array(
65
				'label' => __( 'Icon libraries', 'pods' ),
66
				'group' => array(
67
				)
68
			),
69
		);
70
71
		return $options;
72
	}
73
74
	/**
75
	 * Define the current field's schema for DB table storage
76
	 *
77
	 * @param array $options
78
	 *
79
	 * @return string
80
	 * @since 2.0
81
	 */
82
	public function schema ( $options = null ) {
83
		$schema = 'LONGTEXT';
84
		return $schema;
85
	}
86
87
	/**
88
	 * Change the way the value of the field is displayed with Pods::get
89
	 *
90
	 * @param mixed $value
91
	 * @param string $name
92
	 * @param array $options
93
	 * @param array $pod
94
	 * @param int $id
95
	 *
96
	 * @return mixed|null|string
97
	 * @since 2.0
98
	 */
99
	public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
100
		//$value = $this->strip_html( $value, $options );
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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...
101
		//$value = icon_picker_get_icon_url( $value );
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
102
103
		return $value;
104
	}
105
106
	/**
107
	 * Customize output of the form field
108
	 *
109
	 * @param string $name
110
	 * @param mixed $value
111
	 * @param array $options
112
	 * @param array $pod
113
	 * @param int $id
114
	 *
115
	 * @since 2.0
116
	 */
117
	public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
118
		$options = (array) $options;
119
		$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...
120
121
		if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) {
122
			if ( pods_var( 'read_only', $options, false ) ) {
123
				$options[ 'readonly' ] = true;
124
			} else {
125
				return;
126
			}
127
		} elseif ( ! pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) {
128
			$options[ 'readonly' ] = true;
129
		}
130
131
		pods_view( PODS_DIR . 'ui/fields/icon.php', compact( array_keys( get_defined_vars() ) ) );
132
	}
133
134
	/**
135
	 * Validate a value before it's saved
136
	 *
137
	 * @param mixed $value
138
	 * @param string $name
139
	 * @param array $options
140
	 * @param array $fields
141
	 * @param array $pod
142
	 * @param int $id
143
	 *
144
	 * @param null $params
145
	 * @return array|bool
146
	 * @since 2.0
147
	 */
148
	public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
149
		$errors = array();
150
151
		$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
152
153
		if ( empty( $check ) ) {
154
			if ( pods_var( 'required', $options ) ) {
155
				$errors[] = __( 'This field is required.', 'pods' );
156
			}
157
		}
158
159
		if ( ! empty( $errors ) ) {
160
			return $errors;
161
		}
162
163
		return true;
164
	}
165
166
	/**
167
	 * Change the value or perform actions after validation but before saving to the DB
168
	 *
169
	 * @param mixed $value
170
	 * @param int $id
171
	 * @param string $name
172
	 * @param array $options
173
	 * @param array $fields
174
	 * @param array $pod
175
	 * @param object $params
176
	 *
177
	 * @return mixed|string
178
	 * @since 2.0
179
	 */
180
	public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
181
182
		$value = array_intersect_key( $value, array( 'type' => '', 'icon' => '' ) );
183
184
		if ( empty( $value['type'] ) || empty( $value['icon'] ) ) {
185
			$value = '';
186
		}
187
188
		$value = array_map( 'strip_tags', $value );
189
190
		return $value;
191
	}
192
193
	/**
194
	 * Customize the Pods UI manage table column output
195
	 *
196
	 * @param int $id
197
	 * @param mixed $value
198
	 * @param string $name
199
	 * @param array $options
200
	 * @param array $fields
201
	 * @param array $pod
202
	 *
203
	 * @return mixed|string
204
	 * @since 2.0
205
	 */
206
	public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
207
		/*$value = $this->strip_html( $value, $options );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
208
209
		if ( 0 == pods_var( self::$type . '_allow_html', $options, 0, null, true ) )
210
			$value = wp_trim_words( $value );*/
211
212
		return $value;
213
	}
214
}
215