Completed
Push — master ( 7b2bed...f1df0d )
by Stephanie
02:58
created

FrmEntryFormatter::add_html_row()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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