|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* @file class-gravityview-field-list.php |
|
4
|
|
|
* @package GravityView |
|
5
|
|
|
* @subpackage includes\fields |
|
6
|
|
|
* @since 1.14 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Add custom options for list fields |
|
11
|
|
|
* |
|
12
|
|
|
* @since 1.14 |
|
13
|
|
|
*/ |
|
14
|
|
|
class GravityView_Field_List extends GravityView_Field { |
|
15
|
|
|
|
|
16
|
|
|
var $name = 'list'; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var bool |
|
20
|
|
|
* @since 1.15.3 |
|
21
|
|
|
*/ |
|
22
|
|
|
var $is_searchable = true; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
var $search_operators = array( 'contains' ); |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var bool |
|
28
|
|
|
* @since 1.15.3 |
|
29
|
|
|
*/ |
|
30
|
|
|
var $is_sortable = false; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** @see GF_Field_List */ |
|
33
|
|
|
var $_gf_field_class_name = 'GF_Field_List'; |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
var $group = 'advanced'; |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
function __construct() { |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
$this->label = esc_html__( 'List', 'gravityview' ); |
|
40
|
|
|
|
|
41
|
|
|
parent::__construct(); |
|
42
|
|
|
|
|
43
|
|
|
add_filter( 'gravityview/template/field_label', array( $this, '_filter_field_label' ), 10, 4 ); |
|
44
|
|
|
|
|
45
|
|
|
add_filter( 'gravityview/common/get_form_fields', array( $this, 'add_form_fields' ), 10, 3 ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* If a form has list fields, add the columns to the field picker |
|
50
|
|
|
* |
|
51
|
|
|
* @since 1.17 |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $fields Associative array of fields, with keys as field type |
|
54
|
|
|
* @param array $form GF Form array |
|
55
|
|
|
* @param bool $include_parent_field Whether to include the parent field when getting a field with inputs |
|
56
|
|
|
* |
|
57
|
|
|
* @return array $fields with list field columns added, if exist. Unmodified if form has no list fields. |
|
58
|
|
|
*/ |
|
59
|
|
|
function add_form_fields( $fields = array(), $form = array(), $include_parent_field = true ) { |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
$list_fields = GFAPI::get_fields_by_type( $form, 'list' ); |
|
62
|
|
|
|
|
63
|
|
|
// Add the list columns |
|
64
|
|
|
foreach ( $list_fields as $list_field ) { |
|
65
|
|
|
|
|
66
|
|
|
if( empty( $list_field->enableColumns ) ) { |
|
67
|
|
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$list_columns = array(); |
|
71
|
|
|
|
|
72
|
|
|
foreach ( (array)$list_field->choices as $key => $input ) { |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
$input_id = sprintf( '%d.%d', $list_field->id, $key ); // {field_id}.{column_key} |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
$list_columns[ $input_id ] = array( |
|
77
|
|
|
'label' => rgar( $input, 'text' ), |
|
78
|
|
|
'customLabel' => '', |
|
79
|
|
|
'parent' => $list_field, |
|
80
|
|
|
'type' => rgar( $list_field, 'type' ), |
|
81
|
|
|
'adminLabel' => rgar( $list_field, 'adminLabel' ), |
|
82
|
|
|
'adminOnly' => rgar( $list_field, 'adminOnly' ), |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// If there are columns, add them under the parent field |
|
87
|
|
|
if( ! empty( $list_columns ) ) { |
|
88
|
|
|
|
|
89
|
|
|
$index = array_search( $list_field->id, array_keys( $fields ) ) + 1; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Merge the $list_columns into the $fields array at $index |
|
93
|
|
|
* @see https://stackoverflow.com/a/1783125 |
|
94
|
|
|
*/ |
|
95
|
|
|
$fields = array_slice( $fields, 0, $index, true) + $list_columns + array_slice( $fields, $index, null, true); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
unset( $list_columns, $index, $input_id ); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $fields; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get the value of a Multiple Column List field for a specific column. |
|
106
|
|
|
* |
|
107
|
|
|
* @since 1.14 |
|
108
|
|
|
* |
|
109
|
|
|
* @see GF_Field_List::get_value_entry_detail() |
|
110
|
|
|
* |
|
111
|
|
|
* @param GF_Field_List $field Gravity Forms field |
|
112
|
|
|
* @param string|array $field_value Serialized or unserialized array value for the field |
|
113
|
|
|
* @param int|string $column_id The numeric key of the column (0-index) or the label of the column |
|
114
|
|
|
* @param string $format If set to 'raw', return an array of values for the column. Otherwise, allow Gravity Forms to render using `html` or `text` |
|
115
|
|
|
* |
|
116
|
|
|
* @return array|string|null Returns null if the $field_value passed wasn't an array or serialized array |
|
117
|
|
|
*/ |
|
118
|
|
|
public static function column_value( GF_Field_List $field, $field_value, $column_id = 0, $format = 'html' ) { |
|
119
|
|
|
|
|
120
|
|
|
$list_rows = maybe_unserialize( $field_value ); |
|
121
|
|
|
|
|
122
|
|
|
if( ! is_array( $list_rows ) ) { |
|
123
|
|
|
do_action( 'gravityview_log_error', __METHOD__ . ' - $field_value did not unserialize', $field_value ); |
|
124
|
|
|
return null; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$column_values = array(); |
|
128
|
|
|
|
|
129
|
|
|
// Each list row |
|
130
|
|
|
foreach ( $list_rows as $list_row ) { |
|
131
|
|
|
$current_column = 0; |
|
132
|
|
|
foreach ( (array) $list_row as $column_key => $column_value ) { |
|
133
|
|
|
|
|
134
|
|
|
// If the label of the column matches $column_id, or the numeric key value matches, add the value |
|
135
|
|
|
if( (string)$column_key === (string)$column_id || ( is_numeric( $column_id ) && (int)$column_id === $current_column ) ) { |
|
|
|
|
|
|
136
|
|
|
$column_values[] = $column_value; |
|
137
|
|
|
} |
|
138
|
|
|
$current_column++; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
// Return the array of values |
|
143
|
|
|
if( 'raw' === $format ) { |
|
144
|
|
|
return $column_values; |
|
145
|
|
|
} |
|
146
|
|
|
// Return the Gravity Forms Field output |
|
147
|
|
|
else { |
|
148
|
|
|
return $field->get_value_entry_detail( serialize( $column_values ), '', false, $format ); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* When showing a single column values, display the label of the column instead of the field |
|
154
|
|
|
* |
|
155
|
|
|
* @since 1.14 |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $label Existing label string |
|
158
|
|
|
* @param array $field GV field settings array, with `id`, `show_label`, `label`, `custom_label`, etc. keys |
|
159
|
|
|
* @param array $form Gravity Forms form array |
|
160
|
|
|
* @param array $entry Gravity Forms entry array |
|
161
|
|
|
* |
|
162
|
|
|
* @return string Existing label if the field isn't |
|
163
|
|
|
*/ |
|
164
|
|
|
public function _filter_field_label( $label, $field, $form, $entry ) { |
|
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
$field_object = RGFormsModel::get_field( $form, $field['id'] ); |
|
167
|
|
|
|
|
168
|
|
|
// Not a list field |
|
169
|
|
|
if( ! $field_object || 'list' !== $field_object->type ) { |
|
170
|
|
|
return $label; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
// Custom label is defined, so use it |
|
174
|
|
|
if( ! empty( $field['custom_label'] ) ) { |
|
175
|
|
|
return $label; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$column_id = gravityview_get_input_id_from_id( $field['id'] ); |
|
179
|
|
|
|
|
180
|
|
|
// Parent field, not column field |
|
181
|
|
|
if( false === $column_id ) { |
|
182
|
|
|
return $label; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return self::get_column_label( $field_object, $column_id, $label ); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Get the column label for the list |
|
190
|
|
|
* |
|
191
|
|
|
* @since 1.14 |
|
192
|
|
|
* |
|
193
|
|
|
* @param GF_Field_List $field Gravity Forms List field |
|
194
|
|
|
* @param int $column_id The key of the column (0-index) |
|
195
|
|
|
* @param string $backup_label Backup label to use. Optional. |
|
196
|
|
|
* |
|
197
|
|
|
* @return string |
|
198
|
|
|
*/ |
|
199
|
|
|
public static function get_column_label( GF_Field_List $field, $column_id, $backup_label = '' ) { |
|
200
|
|
|
|
|
201
|
|
|
// Doesn't have columns enabled |
|
202
|
|
|
if( ! isset( $field->choices ) || ! $field->enableColumns ) { |
|
203
|
|
|
return $backup_label; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
// Get the list of columns, with numeric index keys |
|
207
|
|
|
$columns = wp_list_pluck( $field->choices, 'text' ); |
|
208
|
|
|
|
|
209
|
|
|
return isset( $columns[ $column_id ] ) ? $columns[ $column_id ] : $backup_label; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
new GravityView_Field_List; |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.