Completed
Push — master ( ba1cd7...836330 )
by Jamie
02:56
created

FrmEntryFormatter::init_format()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 1
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @since 2.03.11
5
 */
6
class FrmEntryFormatter {
7
8
	/**
9
	 * @var stdClass
10
	 * @since 2.03.11
11
	 */
12
	protected $entry = null;
13
14
	/**
15
	 * @var FrmEntryValues
16
	 * @since 2.03.11
17
	 */
18
	protected $entry_values = null;
19
20
	/**
21
	 * @var bool
22
	 * @since 2.03.11
23
	 */
24
	protected $is_plain_text = false;
25
26
	/**
27
	 * @var bool
28
	 * @since 2.03.11
29
	 */
30
	protected $include_user_info = false;
31
32
	/**
33
	 * @var bool
34
	 * @since 2.03.11
35
	 */
36
	protected $include_blank = false;
37
38
	/**
39
	 * @var string
40
	 * @since 2.03.11
41
	 */
42
	protected $format = 'text';
43
44
	/**
45
	 * @var string
46
	 * @since 2.03.11
47
	 */
48
	protected $direction = 'ltr';
49
50
	/**
51
	 * @var FrmTableHTMLGenerator
52
	 * @since 2.03.11
53
	 */
54
	protected $table_generator = null;
55
56
	/**
57
	 * @var bool
58
	 * @since 2.03.11
59
	 */
60
	protected $is_clickable = false;
61
62
	/**
63
	 * @var array
64
	 * @since 2.03.11
65
	 */
66
	protected $include_extras = array();
67
68
	/**
69
	 * @var array
70
	 * @since 2.03.11
71
	 */
72
	protected $skip_fields = array( 'captcha' );
73
74
	/**
75
	 * FrmEntryFormat constructor
76
	 *
77
	 * @since 2.03.11
78
	 *
79
	 * @param $atts
80
	 */
81
	public function __construct( $atts ) {
82
		$this->init_entry( $atts );
83
84
		if ( $this->entry === null || $this->entry === false ) {
85
			return;
86
		}
87
88
		$this->init_is_plain_text( $atts );
89
		$this->init_format( $atts );
90
		$this->init_include_blank( $atts );
91
		$this->init_direction( $atts );
92
		$this->init_include_user_info( $atts );
93
		$this->init_entry_values( $atts );
94
95
		if ( $this->format === 'table' ) {
96
			$this->init_table_generator( $atts );
97
			$this->init_is_clickable( $atts );
98
		}
99
	}
100
101
	/**
102
	 * Set the entry property
103
	 *
104
	 * @since 2.03.11
105
	 *
106
	 * @param array $atts
107
	 */
108
	protected function init_entry( $atts ) {
109
		if ( is_object( $atts['entry'] ) ) {
110
111
			if ( isset( $atts['entry']->metas ) ) {
112
				$this->entry = $atts[ 'entry' ];
0 ignored issues
show
introduced by
Array keys should NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
113
			} else {
114
				$this->entry = FrmEntry::getOne( $atts['entry']->id, true );
115
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
116
117
		} else if ( $atts['id'] ) {
118
			$this->entry = FrmEntry::getOne( $atts['id'], true );
119
		}
120
	}
121
122
	/**
123
	 * Set the entry values property
124
	 *
125
	 * @since 2.03.11
126
	 *
127
	 * @param array $atts
128
	 */
129
	protected function init_entry_values( $atts ) {
130
		$atts['source'] = 'entry_formatter';
131
		$this->entry_values = new FrmEntryValues( $this->entry->id, $atts );
132
	}
133
134
	/**
135
	 * Set the format property
136
	 *
137
	 * @since 2.03.11
138
	 *
139
	 * @param array $atts
140
	 */
141
	protected function init_format( $atts ) {
142
		if ( $atts['format'] === 'array' ) {
143
144
			$this->format = 'array';
145
146
		} else if ( $atts['format'] === 'json' ) {
147
148
			$this->format = 'json';
149
150
		} else if ( $atts['format'] === 'text' ) {
151
152
			if ( $this->is_plain_text === true ) {
153
				$this->format = 'plain_text_block';
154
			} else {
155
				$this->format = 'table';
156
			}
157
		}
158
	}
159
160
	/**
161
	 * Set the is_plain_text property
162
	 *
163
	 * @since 2.03.11
164
	 *
165
	 * @param array $atts
166
	 */
167
	protected function init_is_plain_text( $atts ) {
168
		if ( isset( $atts['plain_text'] ) && $atts['plain_text'] ) {
169
			$this->is_plain_text = true;
170
		} else if ( $atts['format'] !== 'text' ) {
171
			$this->is_plain_text = true;
172
		}
173
	}
174
175
	/**
176
	 * Set the include_blank property
177
	 *
178
	 * @since 2.03.11
179
	 *
180
	 * @param array $atts
181
	 */
182
	protected function init_include_blank( $atts ) {
183
		if ( isset( $atts['include_blank'] ) && $atts['include_blank'] ) {
184
			$this->include_blank = true;
185
		}
186
	}
187
188
	/**
189
	 * Set the direction property
190
	 *
191
	 * @since 2.03.11
192
	 *
193
	 * @param array $atts
194
	 */
195
	protected function init_direction( $atts ) {
196
		if ( isset( $atts['direction'] ) && $atts['direction'] === 'rtl' ) {
197
			$this->direction = 'rtl';
198
		}
199
	}
200
201
	/**
202
	 * Set the include_user_info property
203
	 *
204
	 * @since 2.03.11
205
	 *
206
	 * @param array $atts
207
	 */
208
	protected function init_include_user_info( $atts ) {
209
		if ( isset( $atts['user_info'] ) && $atts['user_info'] ) {
210
			$this->include_user_info = true;
211
		}
212
	}
213
214
	/**
215
	 * Set the table_generator property
216
	 *
217
	 * @since 2.03.11
218
	 *
219
	 * @param array $atts
220
	 */
221
	protected function init_table_generator( $atts ) {
222
		$this->table_generator = new FrmTableHTMLGenerator( 'entry', $atts );
223
	}
224
225
	/**
226
	 * Set the is_clickable property
227
	 *
228
	 * @since 2.03.11
229
	 *
230
	 * @param array $atts
231
	 */
232
	protected function init_is_clickable( $atts ) {
233
		if ( isset( $atts['clickable'] ) && $atts['clickable'] ) {
234
			$this->is_clickable = true;
235
		}
236
	}
237
238
	/**
239
	 * Package and return the formatted entry values
240
	 *
241
	 * @since 2.03.11
242
	 *
243
	 * @return array|string
244
	 */
245
	public function get_formatted_entry_values() {
246
		if ( $this->entry === null || $this->entry === false ) {
247
			return '';
248
		}
249
250
		if ( $this->format === 'json' ) {
251
			$content = json_encode( $this->prepare_array() );
252
253
		} else if ( $this->format === 'array' ) {
254
			$content = $this->prepare_array();
255
256
		} else if ( $this->format === 'table' ) {
257
			$content = $this->prepare_html_table();
258
259
		} else if ( $this->format === 'plain_text_block' ) {
260
			$content = $this->prepare_plain_text_block();
261
262
		} else {
263
			$content = '';
264
		}
265
266
		return $content;
267
	}
268
269
	/**
270
	 * Return the formatted HTML table with entry values
271
	 *
272
	 * @since 2.03.11
273
	 *
274
	 * @return string
275
	 */
276
	protected function prepare_html_table() {
277
		$content = $this->table_generator->generate_table_header();
278
279
		foreach ( $this->entry_values->get_field_values() as $field_id => $field_value ) {
280
			$this->add_field_value_to_content( $field_value, $content );
281
		}
282
283
		$this->add_user_info_to_html_table( $content );
284
285
		$content .= $this->table_generator->generate_table_footer();
286
287
		if ( $this->is_clickable ) {
288
			$content = make_clickable( $content );
289
		}
290
291
		return $content;
292
	}
293
294
	/**
295
	 * Return the formatted plain text content
296
	 *
297
	 * @since 2.03.11
298
	 *
299
	 * @return string
300
	 */
301
	protected function prepare_plain_text_block() {
302
		$content = '';
303
304
		foreach ( $this->entry_values->get_field_values() as $field_id => $field_value ) {
305
			$this->add_field_value_to_content( $field_value, $content );
306
		}
307
308
		$this->add_user_info_to_plain_text_content( $content );
309
310
		return $content;
311
	}
312
313
	/**
314
	 * Prepare the array output
315
	 *
316
	 * @since 2.03.11
317
	 *
318
	 * @return array
319
	 */
320
	protected function prepare_array() {
321
		$array_output = array();
322
323
		$this->push_field_values_to_array( $this->entry_values->get_field_values(), $array_output );
324
325
		return $array_output;
326
	}
327
328
	/**
329
	 * Push field values to array content
330
	 *
331
	 * @since 2.03.11
332
	 *
333
	 * @param array $field_values
334
	 * @param array $output
335
	 */
336
	protected function push_field_values_to_array( $field_values, &$output ) {
337
		foreach ( $field_values as $field_value ) {
338
			$this->push_single_field_to_array( $field_value, $output );
339
		}
340
	}
341
342
	/**
343
	 * Push a single field to the array content
344
	 *
345
	 * @since 2.03.11
346
	 *
347
	 * @param FrmFieldValue $field_value
348
	 * @param array $output
349
	 */
350
	protected function push_single_field_to_array( $field_value, &$output ) {
351
		if ( $this->include_field_in_content( $field_value ) ) {
352
353
			$displayed_value = $this->prepare_display_value_for_array( $field_value->get_displayed_value() );
354
			$output[ $field_value->get_field_key() ] = $displayed_value;
355
356
			if ( $displayed_value !== $field_value->get_saved_value() ) {
357
				$output[ $field_value->get_field_key() . '-value' ] = $field_value->get_saved_value();
358
			}
359
		}
360
	}
361
362
	/**
363
	 * Add a row of values to the plain text content
364
	 *
365
	 * @since 2.03.11
366
	 *
367
	 * @param string $label
368
	 * @param mixed $display_value
369
	 * @param string $content
370
	 */
371
	protected function add_plain_text_row( $label, $display_value, &$content ) {
372
		$display_value = $this->prepare_display_value_for_plain_text_content( $display_value );
373
374
		if ( 'rtl' == $this->direction ) {
375
			$content .= $display_value . ' :' . $label . "\r\n";
376
		} else {
377
			$content .= $label . ': ' . $display_value . "\r\n";
378
		}
379
	}
380
381
	/**
382
	 * Add a field value to the HTML table or plain text content
383
	 *
384
	 * @since 2.03.11
385
	 *
386
	 * @param FrmFieldValue $field_value
387
	 * @param string $content
388
	 */
389
	protected function add_field_value_to_content( $field_value, &$content ) {
390
		if ( ! $this->include_field_in_content( $field_value ) ) {
391
			return;
392
		}
393
394
		if ( $this->format === 'plain_text_block' ) {
395
			$this->add_plain_text_row( $field_value->get_field_label(), $field_value->get_displayed_value(), $content );
396
		} else if ( $this->format === 'table' ) {
397
			$value_args = $this->package_value_args( $field_value );
398
			$this->add_html_row( $value_args, $content );
399
		}
400
	}
401
402
	/**
403
	 * Package the value arguments for an HTML row
404
	 *
405
	 * @since 2.03.11
406
	 *
407
	 * @param FrmFieldValue $field_value
408
	 *
409
	 * @return array
410
	 */
411
	protected function package_value_args( $field_value ) {
412
		return array(
413
			'label'       => $field_value->get_field_label(),
414
			'value'       => $field_value->get_displayed_value(),
415
			'field_type'  => $field_value->get_field_type(),
416
		);
417
	}
418
419
	/**
420
	 * Add user info to an HTML table
421
	 *
422
	 * @since 2.03.11
423
	 *
424
	 * @param string $content
425
	 */
426
	protected function add_user_info_to_html_table( &$content ) {
427
		if ( $this->include_user_info ) {
428
429
			foreach ( $this->entry_values->get_user_info() as $user_info ) {
430
431
				$value_args = array(
432
					'label' => $user_info['label'],
433
					'value' => $user_info['value'],
434
					'field_type'  => 'none',
435
				);
436
437
				$this->add_html_row( $value_args, $content );
438
			}
439
		}
440
	}
441
442
	/**
443
	 * Add user info to plain text content
444
	 *
445
	 * @since 2.03.11
446
	 *
447
	 * @param string $content
448
	 */
449
	protected function add_user_info_to_plain_text_content( &$content ) {
450
		if ( $this->include_user_info ) {
451
452
			foreach ( $this->entry_values->get_user_info() as $user_info ) {
453
				$this->add_plain_text_row( $user_info['label'], $user_info['value'], $content );
454
			}
455
		}
456
	}
457
458
	/**
459
	 * Check if a field should be included in the content
460
	 *
461
	 * @since 2.03.11
462
	 *
463
	 * @param FrmFieldValue $field_value
464
	 *
465
	 * @return bool
466
	 */
467
	protected function include_field_in_content( $field_value ) {
468
		$include = true;
469
470
		if ( $this->is_extra_field( $field_value ) ) {
471
472
			$include = $this->is_extra_field_included( $field_value );
473
474
		} else if ( $field_value->get_displayed_value() === '' || empty( $field_value->get_displayed_value() ) ) {
475
476
			if ( ! $this->include_blank ) {
477
				$include = false;
478
			}
479
		}
480
481
		return $include;
482
	}
483
484
	/**
485
	 * Check if a field is normally a skipped type
486
	 *
487
	 * @since 2.03.11
488
	 *
489
	 * @param FrmFieldValue $field_value
490
	 *
491
	 * @return bool
492
	 */
493
	protected function is_extra_field( $field_value ) {
494
		return in_array( $field_value->get_field_type(), $this->skip_fields );
495
	}
496
497
	/**
498
	 * Check if an extra field is included
499
	 *
500
	 * @since 2.03.11
501
	 *
502
	 * @param FrmFieldValue $field_value
503
	 *
504
	 * @return bool
505
	 */
506
	protected function is_extra_field_included( $field_value ) {
507
		return in_array( $field_value->get_field_type(), $this->include_extras );
508
	}
509
510
	/**
511
	 * Add a row in an HTML table
512
	 *
513
	 * @since 2.03.11
514
	 *
515
	 * @param array $value_args
516
	 * 		$value_args = [
517
	 * 			'label'      => (string) The label. Required
518
	 *			'value'      => (mixed) The value to add. Required
519
	 *			'field_type' => (string) The field type. Blank string if not a field.
520
	 * 		]
521
	 * @param string $content
522
	 */
523
	protected function add_html_row( $value_args, &$content ) {
524
		$display_value = $this->prepare_display_value_for_html_table( $value_args['value'], $value_args['field_type'] );
525
526
		$content .= $this->table_generator->generate_two_cell_table_row( $value_args['label'], $display_value );
527
	}
528
529
	/**
530
	 * Prepare the displayed value for an array
531
	 *
532
	 * @since 2.03.11
533
	 *
534
	 * @param mixed $value
535
	 *
536
	 * @return mixed|string
537
	 */
538
	protected function prepare_display_value_for_array( $value ) {
539
		return $this->strip_html( $value );
540
	}
541
542
543
	/**
544
	 * Prepare a field's display value for an HTML table
545
	 *
546
	 * @since 2.03.11
547
	 *
548
	 * @param mixed $display_value
549
	 * @param string $field_type
550
	 *
551
	 * @return mixed|string
552
	 */
553
	protected function prepare_display_value_for_html_table( $display_value, $field_type = '' ) {
554
		$display_value = $this->flatten_array( $display_value );
555
		$display_value = str_replace( "\r\n", '<br/>', $display_value );
556
557
		return $display_value;
558
	}
559
560
	/**
561
	 * Prepare a field's display value for plain text content
562
	 *
563
	 * @since 2.03.11
564
	 *
565
	 * @param mixed $display_value
566
	 *
567
	 * @return string|int
568
	 */
569
	protected function prepare_display_value_for_plain_text_content( $display_value ) {
570
		$display_value = $this->flatten_array( $display_value );
571
		$display_value = $this->strip_html( $display_value );
572
573
		return $display_value;
574
	}
575
576
	/**
577
	 * Flatten an array
578
	 *
579
	 * @since 2.03.11
580
	 *
581
	 * @param array|string|int $value
582
	 *
583
	 * @return string|int
584
	 */
585
	protected function flatten_array( $value ) {
586
		if ( is_array( $value ) ) {
587
			$value = implode( ', ', $value );
588
		}
589
590
		return $value;
591
	}
592
593
	/**
594
	 * Strip HTML if from email value if plain text is selected
595
	 *
596
	 * @since 2.0.21
597
	 *
598
	 * @param mixed $value
599
	 *
600
	 * @return mixed
601
	 */
602
	protected function strip_html( $value ) {
603
604
		if ( $this->is_plain_text ) {
605
606
			if ( is_array( $value ) ) {
607
				foreach ( $value as $key => $single_value ) {
608
					$value[ $key ] = $this->strip_html( $single_value );
609
				}
610 View Code Duplication
			} else if ( $this->is_plain_text && ! is_array( $value ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
611
				if ( strpos( $value, '<img' ) !== false ) {
612
					$value = str_replace( array( '<img', 'src=', '/>', '"' ), '', $value );
613
					$value = trim( $value );
614
				}
615
				$value = strip_tags( $value );
616
			}
617
		}
618
619
		return $value;
620
	}
621
622
}