Completed
Push — master ( e24d60...2190bd )
by Jamie
02:30
created

FrmEntryShortcodeFormatter::init_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @since 2.04
5
 */
6
class FrmEntryShortcodeFormatter {
7
8
	/**
9
	 * @var int
10
	 * @since 2.04
11
	 */
12
	protected $form_id = 0;
13
14
	/**
15
	 * @var array
16
	 * @since 2.04
17
	 */
18
	protected $skip_fields = array( 'captcha' );
19
20
	/**
21
	 * @var array
22
	 * @since 2.04
23
	 */
24
	protected $fields = array();
25
26
	/**
27
	 * @var bool
28
	 * @since 2.04.02
29
	 */
30
	protected $is_plain_text = false;
31
32
	/**
33
	 * @var string
34
	 * @since 2.04
35
	 */
36
	protected $format = 'text';
37
38
	/**
39
	 * @var FrmTableHTMLGenerator
40
	 * @since 2.04
41
	 */
42
	protected $table_generator = null;
43
44
	/**
45
	 * @var array
46
	 * @since 2.04
47
	 */
48
	protected $array_content = array();
49
50
	/**
51
	 * FrmEntryShortcodeFormatter constructor
52
	 *
53
	 * @param int|string $form_id
54
	 * @param array $atts
55
	 */
56
	public function __construct( $form_id, $atts ) {
57
		if ( ! $form_id ) {
58
			return;
59
		}
60
61
		$this->init_form_id( $form_id );
62
		$this->init_fields();
63
64
		if ( empty( $this->fields ) ) {
65
			return;
66
		}
67
68
		$this->init_plain_text( $atts );
69
		$this->init_format( $atts );
70
71
		if ( $this->is_table_format() ) {
72
			$this->init_table_generator();
73
		}
74
	}
75
76
	/**
77
	 * Initialize the form_id property
78
	 *
79
	 * @since 2.04
80
	 * @param $form_id
81
	 */
82
	protected function init_form_id( $form_id ) {
83
		$this->form_id = (int) $form_id;
84
	}
85
86
	/**
87
	 * Initialize the fields property
88
	 *
89
	 * @since 2.04
90
	 */
91
	protected function init_fields() {
92
		$this->fields = FrmField::get_all_for_form( $this->form_id, '', 'exclude', 'exclude' );
93
	}
94
95
	/**
96
	 * Initialize the is_plain_text property
97
	 *
98
	 * @since 2.04.02
99
	 *
100
	 * @param array $atts
101
	 */
102
	protected function init_plain_text( $atts ) {
103
		if ( isset( $atts['plain_text'] ) && $atts['plain_text'] ) {
104
			$this->is_plain_text = true;
105
		}
106
	}
107
108
	/**
109
	 * Initialize the format property
110
	 *
111
	 * @since 2.04
112
	 *
113
	 * @param array $atts
114
	 */
115 View Code Duplication
	protected function init_format( $atts ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
116
		if ( isset( $atts['format'] ) && is_string( $atts['format'] ) && $atts['format'] !== '' ) {
117
			$this->format = $atts['format'];
118
		} else {
119
			$this->format = 'text';
120
		}
121
	}
122
123
	/**
124
	 * Initialize the table_generator property
125
	 *
126
	 * @since 2.04
127
	 */
128
	protected function init_table_generator() {
129
		$this->table_generator = new FrmTableHTMLGenerator( 'shortcode' );
130
	}
131
132
	/**
133
	 * Return the default HTML for an entry
134
	 *
135
	 * @since 2.04
136
	 */
137
	public function content() {
138
		if ( ! $this->form_id || empty( $this->fields ) ) {
139
			return '';
140
		}
141
142
		if ( $this->format == 'array' ) {
143
			$content = $this->get_array();
144
		} else if ( $this->is_plain_text_format() ) {
145
			$content = $this->get_plain_text();
146
		} else {
147
			$content = $this->get_table();
148
		}
149
150
		return $content;
151
	}
152
153
	/**
154
	 * Return the default HTML array
155
	 *
156
	 * @since 2.04
157
	 */
158
	protected function get_array() {
159
		foreach ( $this->fields as $field ) {
160
			$this->add_field_array( $field );
161
		}
162
163
		return $this->array_content;
164
	}
165
166
	/**
167
	 * Return the default plain text for an email message
168
	 *
169
	 * @since 2.04
170
	 */
171
	protected function get_plain_text() {
172
		return $this->generate_content_for_all_fields();
173
	}
174
175
	/**
176
	 * Return the default HTML for an email message
177
	 *
178
	 * @since 2.04
179
	 */
180
	protected function get_table() {
181
		$content = $this->table_generator->generate_table_header();
182
		$content .= $this->generate_content_for_all_fields();
183
		$content .= $this->table_generator->generate_table_footer();
184
185
		return $content;
186
	}
187
188
	/**
189
	 * Generate the content for all fields
190
	 *
191
	 * @since 2.04.02
192
	 *
193
	 * @return string
194
	 */
195
	protected function generate_content_for_all_fields() {
196
		$content = '';
197
198
		foreach ( $this->fields as $field ) {
199
			$content .= $this->generate_field_content( $field );
200
		}
201
202
		return $content;
203
	}
204
205
	/**
206
	 * Generate a field's HTML or plain text shortcodes
207
	 *
208
	 * @since 2.04
209
	 *
210
	 * @param stdClass $field
211
	 *
212
	 * @return string
213
	 */
214
	protected function generate_field_content( $field ) {
215
		if ( in_array( $field->type, $this->skip_fields ) ) {
216
			return '';
217
		}
218
219
		$row = $this->generate_two_cell_shortcode_row( $field );
220
221
		return $row;
222
	}
223
224
	/**
225
	 * Generate a two cell row of shortcodes for an HTML or plain text table
226
	 *
227
	 * @since 2.04
228
	 *
229
	 * @param stdClass $field
230
	 * @param mixed $value
231
	 *
232
	 * @return string
233
	 */
234
	protected function generate_two_cell_shortcode_row( $field, $value = null ) {
235
		$row = '[if ' . $field->id . ']';
236
237
		$label = '[' . $field->id . ' show=field_label]';
238
239
		if ( $value === null ) {
240
			$value = '[' . $field->id . ']';
241
		}
242
243
		if ( $this->is_plain_text_format() ) {
244
			$row .= $label . ': ' . $value . "\r\n";
245
		} else {
246
			$row .= $this->table_generator->generate_two_cell_table_row( $label, $value );
247
		}
248
249
		$row .= '[/if ' . $field->id . ']';
250
251
		if ( $this->is_table_format() ) {
252
			$row .= "\r\n";
253
		}
254
255
		return $row;
256
	}
257
258
	/**
259
	 * Generate a field's array for the default HTML array
260
	 *
261
	 * @since 2.04
262
	 *
263
	 * @param stdClass $field
264
	 */
265
	protected function add_field_array( $field ) {
266
		if ( in_array( $field->type, $this->skip_fields ) ) {
267
			return;
268
		}
269
270
		$this->add_single_field_array( $field, $field->id );
271
	}
272
273
	/**
274
	 * Generate a single field's array
275
	 *
276
	 * @since 2.04
277
	 *
278
	 * @param stdClass $field
279
	 * @param string $value
280
	 *
281
	 * @return array
282
	 */
283
	protected function add_single_field_array( $field, $value ) {
284
		$array = array(
285
			'label' => '[' . $field->id . ' show=field_label]',
286
			'val'   => '[' . $value . ']',
287
			'type'  => $field->type,
288
		);
289
290
		$this->array_content[ $field->id ] = apply_filters( 'frm_field_shortcodes_for_default_html_email', $array, $field );
291
292
		return $array;
293
	}
294
295
	/**
296
	 * Check if the format is default plain text
297
	 *
298
	 * @since 2.04.02
299
	 *
300
	 * @return bool
301
	 */
302
	protected function is_plain_text_format() {
303
		return ( $this->format === 'text' && $this->is_plain_text === true );
304
	}
305
306
	/**
307
	 * Check if the format is default HTML
308
	 *
309
	 * @since 2.04.02
310
	 *
311
	 * @return bool
312
	 */
313
	protected function is_table_format() {
314
		return ( $this->format === 'text' && $this->is_plain_text === false );
315
	}
316
317
}