Completed
Push — develop ( 7592b8...3eb7c5 )
by Zack
14:44
created

GravityView_Field_List   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 199
ccs 0
cts 56
cp 0
rs 10
c 0
b 0
f 0
wmc 23
lcom 0
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B add_form_fields() 0 44 5
C column_value() 0 33 8
B _filter_field_label() 0 23 5
A get_column_label() 0 12 4
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 214.

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.

Loading history...
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';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
17
18
	/**
19
	 * @var bool
20
	 * @since 1.15.3
21
	 */
22
	var $is_searchable = true;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_searchable.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
23
24
	var $search_operators = array( 'contains' );
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $search_operators.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
25
26
	/**
27
	 * @var bool
28
	 * @since 1.15.3
29
	 */
30
	var $is_sortable = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_sortable.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
31
32
	/** @see GF_Field_List */
33
	var $_gf_field_class_name = 'GF_Field_List';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_gf_field_class_name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
34
35
	var $group = 'advanced';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $group.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
36
37
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $include_parent_field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 ) {
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
73
74
				$input_id = sprintf( '%d.%d', $list_field->id, $key ); // {field_id}.{column_key}
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...
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);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
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 ) ) {
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $entry is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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;