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

FrmTableHTMLGenerator   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 302
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A init_style_settings() 0 16 4
A init_use_inline_style() 0 5 3
A init_direction() 0 5 3
A init_table_style() 0 8 2
A init_td_style() 0 10 3
A table_row_background_color() 0 3 2
A tr_style() 0 12 3
A switch_odd() 0 5 2
A generate_table_header() 0 3 1
A generate_table_footer() 0 3 1
A generate_two_cell_table_row() 0 20 2
A generate_single_cell_table_row() 0 9 1
A generate_two_cell_shortcode_row() 0 15 2
A generate_single_cell_shortcode_row() 0 7 1
1
<?php
2
3
/**
4
 * @since 2.03.11
5
 */
6
class FrmTableHTMLGenerator {
7
8
	/**
9
	 * @var string
10
	 * @since 2.03.11
11
	 */
12
	private $type = '';
13
14
	/**
15
	 * @var array
16
	 * @since 2.03.11
17
	 */
18
	private $style_settings = array();
19
20
	/**
21
	 * @var bool
22
	 * @since 2.03.11
23
	 */
24
	private $use_inline_style = true;
25
26
	/**
27
	 * @var string
28
	 * @since 2.03.11
29
	 */
30
	private $direction = 'ltr';
31
32
	/**
33
	 * @var bool
34
	 * @since 2.03.11
35
	 */
36
	private $odd = true;
37
38
	/**
39
	 * @var string
40
	 * @since 2.03.11
41
	 */
42
	private $table_style = '';
43
44
	/**
45
	 * @var string
46
	 * @since 2.03.11
47
	 */
48
	private $td_style = '';
49
50
51
	/**
52
	 * FrmTableHTMLGenerator constructor.
53
	 *
54
	 * @param string $type
55
	 * @param array $atts
56
	 */
57
	public function __construct( $type, $atts = array() ) {
58
59
		$this->type = (string) $type;
60
		$this->init_style_settings( $atts );
61
		$this->init_use_inline_style( $atts );
62
		$this->init_direction( $atts );
63
		$this->init_table_style();
64
		$this->init_td_style();
65
66
	}
67
68
	/**
69
	 * Set the style_settings property
70
	 *
71
	 * @since 2.03.11
72
	 *
73
	 * @param array $atts
74
	 */
75
	private function init_style_settings( $atts ) {
76
		$this->style_settings = apply_filters( 'frm_show_entry_styles', array(
77
			'border_color' => 'dddddd',
78
			'bg_color'     => 'f7f7f7',
79
			'text_color'   => '444444',
80
			'font_size'    => '12px',
81
			'border_width' => '1px',
82
			'alt_bg_color' => 'ffffff',
83
		) );
84
85
		foreach ( $this->style_settings as $key => $setting ) {
86
			if ( isset( $atts[ $key ] ) && $atts[ $key ] !== '' ) {
87
				$this->style_settings[ $key ] = str_replace( '#', '', $atts[ $key ] );
88
			}
89
		}
90
	}
91
92
	/**
93
	 * Set the use_inline_style property
94
	 *
95
	 * @since 2.03.11
96
	 *
97
	 * @param array $atts
98
	 */
99
	private function init_use_inline_style( $atts ) {
100
		if ( isset( $atts['inline_style'] ) && ! $atts['inline_style'] ) {
101
			$this->use_inline_style = false;
102
		}
103
	}
104
105
	/**
106
	 * Set the direction property
107
	 *
108
	 * @since 2.03.11
109
	 *
110
	 * @param array $atts
111
	 */
112
	private function init_direction( $atts ) {
113
		if ( isset( $atts['direction'] ) && $atts['direction'] === 'rtl' ) {
114
			$this->direction = 'rtl';
115
		}
116
	}
117
118
	/**
119
	 * Set the table_style property
120
	 *
121
	 * @since 2.03.11
122
	 */
123
	private function init_table_style() {
124
		if ( $this->use_inline_style === true ) {
125
126
			$this->table_style = ' style="' . esc_attr( 'font-size:' . $this->style_settings[ 'font_size' ] . ';line-height:135%;' );
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...
127
			$this->table_style .= esc_attr( 'border-bottom:' . $this->style_settings[ 'border_width' ] . ' solid #' . $this->style_settings[ 'border_color' ] . ';' ) . '"';
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...
128
129
		}
130
	}
131
132
	/**
133
	 * Set the td_style property
134
	 *
135
	 * @since 2.03.11
136
	 */
137
	private function init_td_style() {
138
		if ( $this->use_inline_style === true ) {
139
140
			$td_style_attributes = 'text-align:' . ( $this->direction == 'rtl' ? 'right' : 'left' ) . ';';
141
			$td_style_attributes .= 'color:#' . $this->style_settings[ 'text_color' ] . ';padding:7px 9px;vertical-align:top;';
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...
142
			$td_style_attributes .= 'border-top:' . $this->style_settings[ 'border_width' ] . ' solid #' . $this->style_settings[ 'border_color' ] . ';';
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...
143
144
			$this->td_style = ' style="' . $td_style_attributes . '"';
145
		}
146
	}
147
148
	/**
149
	 * Get the table row background color
150
	 *
151
	 * @since 2.03.11
152
	 *
153
	 * @return string
154
	 */
155
	private function table_row_background_color() {
156
		return ( $this->odd ? $this->style_settings['bg_color'] : $this->style_settings['alt_bg_color'] );
157
	}
158
159
	/**
160
	 * Get the table row style
161
	 *
162
	 * @since 2.03.11
163
	 *
164
	 * @return string
165
	 */
166
	private function tr_style() {
167
168
		if ( $this->type === 'shortcode' ) {
169
			$tr_style = ' style="[frm-alt-color]"';
170
		} else if ( $this->use_inline_style ) {
171
			$tr_style = ' style="background-color:#' . $this->table_row_background_color() . ';"';
172
		} else {
173
			$tr_style = '';
174
		}
175
176
		return $tr_style;
177
	}
178
179
	/**
180
	 * Switch the odd property from true to false or false to true
181
	 *
182
	 * @since 2.03.11
183
	 */
184
	private function switch_odd() {
185
		if ( $this->type !== 'shortcode' ) {
186
			$this->odd = ! $this->odd;
187
		}
188
	}
189
190
	/**
191
	 * Generate a table header
192
	 *
193
	 * @since 2.03.11
194
	 *
195
	 * @return string
196
	 */
197
	public function generate_table_header() {
198
		return '<table cellspacing="0"' . $this->table_style . '><tbody>' . "\r\n";
199
	}
200
201
	/**
202
	 * Generate a table footer
203
	 *
204
	 * @since 2.03.11
205
	 *
206
	 * @return string
207
	 */
208
	public function generate_table_footer() {
209
		return '</tbody></table>';
210
	}
211
212
	/**
213
	 * Generate a two cell row for an HTML table
214
	 *
215
	 * @since 2.03.11
216
	 *
217
	 * @param string $label
218
	 * @param string $value
219
	 *
220
	 * @return string
221
	 */
222
	public function generate_two_cell_table_row( $label, $value ) {
223
		$row = '<tr' . $this->tr_style() . '>';
224
225
		if ( 'rtl' == $this->direction ) {
226
			$first = $value;
227
			$second = $label;
228
		} else {
229
			$first = $label;
230
			$second = $value;
231
		}
232
233
		$row .= '<td' . $this->td_style . '>' . $first . '</td>';
234
		$row .= '<td' . $this->td_style . '>' . $second . '</td>';
235
236
		$row .= '</tr>' . "\r\n";
237
238
		$this->switch_odd();
239
240
		return $row;
241
	}
242
243
	/**
244
	 * Generate a single cell row for an HTML table
245
	 *
246
	 * @since 2.03.11
247
	 *
248
	 * @param string $value
249
	 *
250
	 * @return string
251
	 */
252
	public function generate_single_cell_table_row( $value ) {
253
		$row = '<tr' . $this->tr_style() . '>';
254
		$row .= '<td colspan="2"' . $this->td_style . '>' . $value . '</td>';
255
		$row .= '</tr>' . "\r\n";
256
257
		$this->switch_odd();
258
259
		return $row;
260
	}
261
262
263
	/**
264
	 * Generate a two cell row of shortcodes for an HTML table
265
	 *
266
	 * @since 2.03.11
267
	 *
268
	 * @param stdClass $field
269
	 * @param mixed $value
270
	 *
271
	 * @return string
272
	 */
273
	public function generate_two_cell_shortcode_row( $field, $value = null ) {
274
		$row = '[if ' . $field->id . ']';
275
276
		$label = '[' . $field->id . ' show=field_label]';
277
278
		if ( $value === null ) {
279
			$value = '[' . $field->id . ']';
280
		}
281
282
		$row .= $this->generate_two_cell_table_row( $label, $value );
283
284
		$row .= '[/if ' . $field->id . ']' . "\r\n";
285
286
		return $row;
287
	}
288
289
	/**
290
	 * Generate a sinle cell row of shortcodes for an HTML table
291
	 *
292
	 * @since 2.03.11
293
	 *
294
	 * @param stdClass $field
295
	 * @param mixed $value
296
	 *
297
	 * @return string
298
	 */
299
	public function generate_single_cell_shortcode_row( $field, $value ) {
300
		$row = '[if ' . $field->id . ']';
301
		$row .= $this->generate_single_cell_table_row( $value );
302
		$row .= '[/if ' . $field->id . ']' . "\r\n";
303
304
		return $row;
305
	}
306
307
}