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

FrmEntryShortcodeFormatter::content()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @since 2.03.11
5
 */
6
class FrmEntryShortcodeFormatter {
7
8
	/**
9
	 * @var int
10
	 * @since 2.03.11
11
	 */
12
	protected $form_id = 0;
13
14
	/**
15
	 * @var array
16
	 * @since 2.03.11
17
	 */
18
	protected $skip_fields = array( 'captcha' );
19
20
	/**
21
	 * @var array
22
	 * @since 2.03.11
23
	 */
24
	protected $fields = array();
25
26
	/**
27
	 * @var string
28
	 * @since 2.03.11
29
	 */
30
	protected $format = 'text';
31
32
	/**
33
	 * @var FrmTableHTMLGenerator
34
	 * @since 2.03.11
35
	 */
36
	protected $table_generator = null;
37
	/**
38
	 * @var array
39
	 * @since 2.03.11
40
	 */
41
	protected $array_content = array();
42
43
	public function __construct( $form_id, $format ) {
44
		if ( ! $form_id ) {
45
			return;
46
		}
47
48
		$this->init_form_id( $form_id );
49
		$this->init_fields();
50
		$this->init_format( $format );
51
52
		if ( empty( $this->fields ) ) {
53
			return;
54
		}
55
56
		if ( $this->format == 'text' ) {
57
			$this->init_table_generator();
58
		}
59
	}
60
61
	/**
62
	 * Set the form_id property
63
	 *
64
	 * @since 2.03.11
65
	 * @param $form_id
66
	 */
67
	private function init_form_id( $form_id ) {
68
		$this->form_id = (int) $form_id;
69
	}
70
71
	/**
72
	 * Set the fields property
73
	 *
74
	 * @since 2.03.11
75
	 */
76
	private function init_fields() {
77
		$this->fields = FrmField::get_all_for_form( $this->form_id, '', 'exclude', 'exclude' );
78
	}
79
80
	/**
81
	 * Set the format property
82
	 *
83
	 * @since 2.03.11
84
	 *
85
	 * @param string|mixed $format
86
	 */
87
	private function init_format( $format ) {
88
		if ( is_string( $format ) && $format !== '' ) {
89
			$this->format = $format;
90
		}
91
	}
92
93
	/**
94
	 * Set the table_generator property
95
	 *
96
	 * @since 2.03.11
97
	 */
98
	protected function init_table_generator() {
99
		$this->table_generator = new FrmTableHTMLGenerator( 'shortcode' );
100
	}
101
102
	/**
103
	 * Return the default HTML for an entry
104
	 *
105
	 * @since 2.03.11
106
	 */
107
	public function content() {
108
		if ( $this->form_id === 0 ) {
109
			$content = '';
110
		} else if ( $this->format == 'array' ) {
111
			$content = $this->array();
112
		} else {
113
			$content = $this->text();
114
		}
115
116
		return $content;
117
	}
118
119
	/**
120
	 * Return the default HTML array
121
	 *
122
	 * @since 2.03.11
123
	 */
124
	private function array() {
125
		if ( ! $this->form_id || empty( $this->fields ) ) {
126
			return '';
127
		}
128
129
		foreach ( $this->fields as $field ) {
130
			$this->add_field_array( $field );
131
		}
132
133
		return $this->array_content;
134
	}
135
136
	/**
137
	 * Return the default HTML for an email message
138
	 *
139
	 * @since 2.03.11
140
	 */
141
	private function text() {
142
		if ( ! $this->form_id || empty( $this->fields ) ) {
143
			return '';
144
		}
145
146
		$content = $this->table_generator->generate_table_header();
147
148
		foreach ( $this->fields as $field ) {
149
			$content .= $this->generate_field_html( $field );
150
		}
151
152
		$content .= $this->table_generator->generate_table_footer();
153
154
		return $content;
155
	}
156
157
	/**
158
	 * Generate a field's HTML for the default HTML
159
	 *
160
	 * @since 2.03.11
161
	 *
162
	 * @param stdClass $field
163
	 *
164
	 * @return string
165
	 */
166
	protected function generate_field_html( $field ) {
167
		if ( in_array( $field->type, $this->skip_fields ) ) {
168
			return '';
169
		}
170
171
		$row = $this->generate_single_row( $field );
172
173
		return $row;
174
	}
175
176
	/**
177
	 * Generate a single table row
178
	 *
179
	 * @since 2.03.11
180
	 *
181
	 * @param stdClass $field
182
	 * @param null|string $value
183
	 *
184
	 * @return string
185
	 */
186
	protected function generate_single_row( $field, $value = null ) {
187
		return $this->table_generator->generate_two_cell_shortcode_row( $field, $value );
188
	}
189
190
	/**
191
	 * Generate a field's array for the default HTML array
192
	 *
193
	 * @since 2.03.11
194
	 *
195
	 * @param stdClass $field
196
	 */
197
	protected function add_field_array( $field ) {
198
		if ( in_array( $field->type, $this->skip_fields ) ) {
199
			return;
200
		}
201
202
		$this->add_single_field_array( $field, $field->id );
203
	}
204
205
	/**
206
	 * Generate a single field's array
207
	 *
208
	 * @since 2.03.11
209
	 *
210
	 * @param stdClass $field
211
	 * @param string $value
212
	 *
213
	 * @return array
214
	 */
215
	protected function add_single_field_array( $field, $value ) {
216
		$array = array(
217
			'label' => '[' . $field->id . ' show=field_label]',
218
			'val'   => '[' . $value . ']',
219
			'type'  => $field->type,
220
		);
221
222
		$this->array_content[ $field->id ] = apply_filters( 'frm_field_shortcodes_for_default_html_email', $array, $field );
223
224
		return $array;
225
	}
226
227
}