Completed
Push — master ( ac7d8e...ec16eb )
by Stephanie
05:53 queued 02:59
created

FrmEntryValues   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 274
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 274
rs 9.8
c 0
b 0
f 0
wmc 31
lcom 1
cbo 4

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A init_entry() 0 3 1
A init_form_id() 0 3 1
C init_include_fields() 0 23 7
A init_exclude_fields() 0 3 1
A prepare_array_property() 0 14 4
A init_fields() 0 3 1
A init_field_values() 0 7 3
A get_field_values() 0 3 1
B init_user_info() 0 31 2
A get_user_info() 0 3 1
A is_field_included() 0 11 3
A is_field_in_array() 0 3 2
A add_field_values() 0 3 1
1
<?php
2
3
/**
4
 * @since 2.04
5
 */
6
class FrmEntryValues {
7
8
	/**
9
	 * @var stdClass
10
	 */
11
	protected $entry = null;
12
13
	/**
14
	 * @var int
15
	 */
16
	protected $form_id;
17
18
	/**
19
	 * @var array
20
	 */
21
	protected $fields = array();
22
23
	/**
24
	 * @var FrmFieldValue[]
25
	 */
26
	protected $field_values = array();
27
28
	/**
29
	 * @var array
30
	 */
31
	protected $user_info = array();
32
33
	/**
34
	 * @var array
35
	 */
36
	protected $include_fields = array();
37
38
	/**
39
	 * @var array
40
	 */
41
	protected $exclude_fields = array();
42
43
	/**
44
	 * FrmEntryValues constructor
45
	 *
46
	 * @since 2.04
47
	 *
48
	 * @param int|string $entry_id
49
	 * @param array $atts
50
	 */
51
	public function __construct( $entry_id, $atts = array() ) {
52
		$this->init_entry( $entry_id );
53
54
		if ( $this->entry === null || $this->entry === false ) {
55
			return;
56
		}
57
58
		$this->init_form_id();
59
		$this->init_include_fields( $atts );
60
		$this->init_exclude_fields( $atts );
61
		$this->init_fields();
62
		$this->init_field_values();
63
		$this->init_user_info();
64
	}
65
66
	/**
67
	 * Set the entry property
68
	 *
69
	 * @since 2.04
70
	 *
71
	 * @param int|string $entry_id
72
	 */
73
	protected function init_entry( $entry_id ) {
74
		$this->entry = FrmEntry::getOne( $entry_id, true );
75
	}
76
77
	/**
78
	 * Set the form_id property
79
	 *
80
	 * @since 2.04
81
	 */
82
	protected function init_form_id() {
83
		$this->form_id = (int) $this->entry->form_id;
84
	}
85
86
	/**
87
	 * Set the include_fields property
88
	 *
89
	 * @since 2.04
90
	 *
91
	 * @param array $atts
92
	 */
93
	protected function init_include_fields( $atts ) {
94
95
		// For reverse compatibility with the fields parameter
96
		if ( ! isset( $atts['include_fields'] ) || empty( $atts['include_fields'] ) ) {
97
98
			if ( isset( $atts['fields'] ) && ! empty( $atts['fields'] ) ) {
99
100
				if ( ! is_array( $atts['fields'] ) ) {
101
					$atts['include_fields'] = $atts['fields'];
102
				} else {
103
					$atts['include_fields'] = '';
104
105
					foreach ( $atts['fields'] as $included_field ) {
106
						$atts['include_fields'] .= $included_field->id . ',';
107
					}
108
109
					$atts['include_fields'] = rtrim( $atts['include_fields'], ',' );
110
				}
111
			}
112
		}
113
114
		$this->include_fields = $this->prepare_array_property( 'include_fields', $atts );
115
	}
116
117
	/**
118
	 * Set the exclude_fields property
119
	 *
120
	 * @since 2.04
121
	 *
122
	 * @param array $atts
123
	 */
124
	protected function init_exclude_fields( $atts ) {
125
		$this->exclude_fields = $this->prepare_array_property( 'exclude_fields', $atts );
126
	}
127
128
	/**
129
	 * Prepare an array property value, such as include_fields and exclude_fields
130
	 *
131
	 * @since 2.04
132
	 *
133
	 * @param string $index
134
	 * @param array $atts
135
	 *
136
	 * @return array
137
	 */
138
	private function prepare_array_property( $index, $atts ) {
139
		if ( isset( $atts[ $index ] ) && ! empty( $atts[ $index ] ) ) {
140
141
			if ( is_array( $atts[ $index ] ) ) {
142
				$property = $atts[ $index ];
143
			} else {
144
				$property = explode( ',', $atts[ $index ] );
145
			}
146
		} else {
147
			$property = array();
148
		}
149
150
		return $property;
151
	}
152
153
	/**
154
	 * Set the fields property
155
	 *
156
	 * @since 2.04
157
	 */
158
	protected function init_fields() {
159
		$this->fields = FrmField::get_all_for_form( $this->form_id, '', 'exclude', 'exclude' );
160
	}
161
162
	/**
163
	 * Set the field_values property
164
	 *
165
	 * @since 2.04
166
	 */
167
	protected function init_field_values() {
168
		foreach ( $this->fields as $field ) {
169
			if ( $this->is_field_included( $field ) ) {
170
				$this->add_field_values( $field );
171
			}
172
		}
173
	}
174
175
	/**
176
	 * Get the field_values property
177
	 *
178
	 * @since 2.04
179
	 *
180
	 * @return array
181
	 */
182
	public function get_field_values() {
183
		return $this->field_values;
184
	}
185
186
	/**
187
	 * Set the user_info property
188
	 *
189
	 * @since 2.04
190
	 */
191
	protected function init_user_info() {
192
		if ( isset( $this->entry->description ) ) {
193
			$entry_description = (array) maybe_unserialize( $this->entry->description );
194
		} else {
195
			$entry_description = array(
196
				'browser' => '',
197
				'referrer' => '',
198
			);
199
		}
200
201
		$ip = array(
202
			'label' => __( 'IP Address', 'formidable' ),
203
			'value' => $this->entry->ip,
204
		);
205
206
		$browser = array(
207
			'label' => __( 'User-Agent (Browser/OS)', 'formidable' ),
208
			'value'   => FrmEntriesHelper::get_browser( $entry_description['browser'] ),
209
		);
210
211
		$referrer = array(
212
			'label' => __( 'Referrer', 'formidable' ),
213
			'value' => $entry_description['referrer'],
214
		);
215
216
		$this->user_info = array(
217
			'ip' => $ip,
218
			'browser' => $browser,
219
			'referrer' => $referrer,
220
		);
221
	}
222
223
	/**
224
	 * Get the user_info property
225
	 *
226
	 * @since 2.04
227
	 *
228
	 * @return array
229
	 */
230
	public function get_user_info() {
231
		return $this->user_info;
232
	}
233
234
	/**
235
	 * Check if a field should be included in the values
236
	 *
237
	 * @since 2.04
238
	 *
239
	 * @param stdClass $field
240
	 *
241
	 * @return bool
242
	 */
243
	protected function is_field_included( $field ) {
244
		if ( ! empty( $this->include_fields ) ) {
245
			$is_included = $this->is_field_in_array( $field, $this->include_fields );
246
		} else if ( ! empty( $this->exclude_fields ) ) {
247
			$is_included = ! $this->is_field_in_array( $field, $this->exclude_fields );
248
		} else {
249
			$is_included = true;
250
		}
251
252
		return $is_included;
253
	}
254
255
	/**
256
	 * Check if a field is in the include fields or exclude fields array
257
	 *
258
	 * @since 2.04
259
	 *
260
	 * @param stdClass $field
261
	 * @param array $array
262
	 *
263
	 * @return bool
264
	 */
265
	protected function is_field_in_array( $field, $array ) {
266
		return in_array( $field->id, $array ) || in_array( $field->field_key, $array );
267
	}
268
269
	/**
270
	 * Add a field's values to the field_values property
271
	 *
272
	 * @since 2.04
273
	 *
274
	 * @param stdClass $field
275
	 */
276
	protected function add_field_values( $field ) {
277
		$this->field_values[ $field->id ] = new FrmFieldValue( $field, $this->entry );
278
	}
279
}
280