class-gf-field-multiselect.php
11.1 KB
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
<?php
// If the GF_Field class isn't available, bail.
if ( ! class_exists( 'GFForms' ) ) {
die();
}
/**
* Class GF_Field_MultiSelect
*
* Allows the creation of multiselect fields.
*
* @since Unknown
*
* @uses GF_Field
*/
class GF_Field_MultiSelect extends GF_Field {
public $type = 'multiselect';
/**
* Returns the field title.
*
* @since Unknown
* @access public
*
* @return string The field title. Escaped.
*/
public function get_form_editor_field_title() {
return esc_attr__( 'Multi Select', 'gravityforms' );
}
/**
* Returns the class names of the settings which should be available on the field in the form editor.
*
* @since Unknown
* @access public
*
* @return array Settings available within the field editor.
*/
function get_form_editor_field_settings() {
return array(
'conditional_logic_field_setting',
'prepopulate_field_setting',
'error_message_setting',
'enable_enhanced_ui_setting',
'label_setting',
'label_placement_setting',
'admin_label_setting',
'size_setting',
'choices_setting',
'rules_setting',
'visibility_setting',
'description_setting',
'css_class_setting',
);
}
/**
* Indicates this field type can be used when configuring conditional logic rules.
*
* @return bool
*/
public function is_conditional_logic_supported() {
return true;
}
/**
* Returns the field inner markup.
*
* @since Unknown
* @access public
*
* @uses GF_Field_MultiSelect::is_entry_detail()
* @uses GF_Field_MultiSelect::is_form_editor()
* @uses GF_Field_MultiSelect::get_conditional_logic_event()
* @uses GF_Field_MultiSelect::get_tabindex()
*
* @param array $form The Form Object currently being processed.
* @param string|array $value The field value. From default/dynamic population, $_POST, or a resumed incomplete submission.
* @param null|array $entry Null or the Entry Object currently being edited.
*
* @return string The field input HTML markup.
*/
public function get_field_input( $form, $value = '', $entry = null ) {
$form_id = absint( $form['id'] );
$is_entry_detail = $this->is_entry_detail();
$is_form_editor = $this->is_form_editor();
$id = $this->id;
$field_id = $is_entry_detail || $is_form_editor || $form_id == 0 ? "input_$id" : 'input_' . $form_id . "_$id";
$logic_event = $this->get_conditional_logic_event( 'keyup' );
$size = $this->size;
$class_suffix = $is_entry_detail ? '_admin' : '';
$class = $size . $class_suffix;
$css_class = trim( esc_attr( $class ) . ' gfield_select' );
$tabindex = $this->get_tabindex();
$disabled_text = $is_form_editor ? 'disabled="disabled"' : '';
/**
* Allow the placeholder used by the enhanced ui to be overridden
*
* @since 1.9.14 Third parameter containing the field ID was added.
* @since Unknown
*
* @param string $placeholder The placeholder text.
* @param integer $form_id The ID of the current form.
*/
$placeholder = gf_apply_filters( array(
'gform_multiselect_placeholder',
$form_id,
$this->id
), __( 'Click to select...', 'gravityforms' ), $form_id, $this );
$placeholder = $this->enableEnhancedUI ? "data-placeholder='" . esc_attr( $placeholder ) . "'" : '';
$size = $this->multiSelectSize;
if ( empty( $size ) ) {
$size = 7;
}
return sprintf( "<div class='ginput_container ginput_container_multiselect'><select multiple='multiple' {$placeholder} size='{$size}' name='input_%d[]' id='%s' {$logic_event} class='%s' $tabindex %s>%s</select></div>", $id, esc_attr( $field_id ), $css_class, $disabled_text, $this->get_choices( $value ) );
}
/**
* Helper for retrieving the markup for the choices.
*
* @since Unknown
* @access public
*
* @uses GFCommon::get_select_choices()
*
* @param string|array $value The field value. From default/dynamic population, $_POST, or a resumed incomplete submission.
*
* @return string Returns the choices available within the multi-select field.
*/
public function get_choices( $value ) {
return GFCommon::get_select_choices( $this, $value, false );
}
/**
* Format the entry value for display on the entries list page.
*
* @since Unknown
* @access public
*
* @param string|array $value The field value.
* @param array $entry The Entry Object currently being processed.
* @param string $field_id The field or input ID currently being processed.
* @param array $columns The properties for the columns being displayed on the entry list page.
* @param array $form The Form Object currently being processed.
*
* @return string $value The value of the field. Escaped.
*/
public function get_value_entry_list( $value, $entry, $field_id, $columns, $form ) {
// Add space after comma-delimited values.
$value = implode( ', ', explode( ',', $value ) );
return esc_html( $value );
}
/**
* Format the entry value for display on the entry detail page and for the {all_fields} merge tag.
*
* @since Unknown
* @access public
*
* @uses GFCommon::selection_display()
*
* @param string|array $value The field value.
* @param string $currency The entry currency code.
* @param bool|false $use_text When processing choice based fields should the choice text be returned instead of the value.
* @param string $format The format requested for the location the merge is being used. Possible values: html, text or url.
* @param string $media The location where the value will be displayed. Possible values: screen or email.
*
* @return string The list items, stored within an unordered list.
*/
public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
if ( empty( $value ) || $format == 'text' ) {
return $value;
}
$value = explode( ',', $value );
$items = '';
foreach ( $value as $item ) {
$item_value = GFCommon::selection_display( $item, $this, $currency, $use_text );
$items .= '<li>' . esc_html( $item_value ) . '</li>';
}
return "<ul class='bulleted'>{$items}</ul>";
}
/**
* Format the value before it is saved to the Entry Object.
*
* @since Unknown
* @access public
*
* @uses GF_Field_MultiSelect::sanitize_entry_value()
*
* @param array|string $value The value to be saved.
* @param array $form The Form Object currently being processed.
* @param string $input_name The input name used when accessing the $_POST.
* @param int $lead_id The ID of the Entry currently being processed.
* @param array $lead The Entry Object currently being processed.
*
* @return string $value The field value. Comma separated if an array.
*/
public function get_value_save_entry( $value, $form, $input_name, $lead_id, $lead ) {
if ( is_array( $value ) ) {
foreach ( $value as &$v ) {
$v = $this->sanitize_entry_value( $v, $form['id'] );
}
} else {
$value = $this->sanitize_entry_value( $value, $form['id'] );
}
return empty( $value ) ? '' : is_array( $value ) ? implode( ',', $value ) : $value;
}
/**
* Format the entry value for when the field/input merge tag is processed.
*
* @since Unknown
* @access public
*
* @uses GFCommon::format_post_category()
* @uses GFCommon::format_variable_value()
* @uses GFCommon::selection_display()
* @uses GFCommon::implode_non_blank()
*
* @param string|array $value The field value. Depending on the location the merge tag is being used the following functions may have already been applied to the value: esc_html, nl2br, and urlencode.
* @param string $input_id The field or input ID from the merge tag currently being processed.
* @param array $entry The Entry Object currently being processed.
* @param array $form The Form Object currently being processed.
* @param string $modifier The merge tag modifier. e.g. value
* @param string|array $raw_value The raw field value from before any formatting was applied to $value.
* @param bool $url_encode Indicates if the urlencode function may have been applied to the $value.
* @param bool $esc_html Indicates if the esc_html function may have been applied to the $value.
* @param string $format The format requested for the location the merge is being used. Possible values: html, text or url.
* @param bool $nl2br Indicates if the nl2br function may have been applied to the $value.
*
* @return string $return The merge tag value.
*/
public function get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format, $nl2br ) {
$items = explode( ',', $raw_value );
if ( $this->type == 'post_category' ) {
$use_id = $modifier == 'id';
if ( is_array( $items ) ) {
foreach ( $items as &$item ) {
$cat = GFCommon::format_post_category( $item, $use_id );
$item = GFCommon::format_variable_value( $cat, $url_encode, $esc_html, $format );
}
}
} elseif ( $modifier != 'value' ) {
foreach ( $items as &$item ) {
$item = GFCommon::selection_display( $item, $this, rgar( $entry, 'currency' ), true );
$item = GFCommon::format_variable_value( $item, $url_encode, $esc_html, $format );
}
}
$return = GFCommon::implode_non_blank( ', ', $items );
if ( $format == 'html' || $esc_html ) {
$return = esc_html( $return );
}
return $return;
}
/**
* Format the entry value before it is used in entry exports and by framework add-ons using GFAddOn::get_field_value().
*
* @since Unknown
* @access public
*
* @uses GFCommon::selection_display()
* @uses GFCommon::implode_non_blank()
*
* @param array $entry The entry currently being processed.
* @param string $input_id The field or input ID.
* @param bool|false $use_text When processing choice based fields should the choice text be returned instead of the value.
* @param bool|false $is_csv Is the value going to be used in the .csv entries export?
*
* @return string $value The value of a field from an export file.
*/
public function get_value_export( $entry, $input_id = '', $use_text = false, $is_csv = false ) {
if ( empty( $input_id ) ) {
$input_id = $this->id;
}
$value = rgar( $entry, $input_id );
if ( ! empty( $value ) && ! $is_csv ) {
$items = explode( ',', $value );
foreach ( $items as &$item ) {
$item = GFCommon::selection_display( $item, $this, rgar( $entry, 'currency' ), $use_text );
}
$value = GFCommon::implode_non_blank( ', ', $items );
}
return $value;
}
/**
* Forces settings into expected values while saving the form object.
*
* No escaping should be done at this stage to prevent double escaping on output.
*
* Currently called only for forms created after version 1.9.6.10.
*
* @since Unknown
* @access public
*
* @return void
*
*/
public function sanitize_settings() {
parent::sanitize_settings();
$this->enableEnhancedUI = (bool) $this->enableEnhancedUI;
if ( $this->type === 'post_category' ) {
$this->displayAllCategories = (bool) $this->displayAllCategories;
}
}
}
// Register the new field type.
GF_Fields::register( new GF_Field_MultiSelect() );