Completed
Push — master ( feb49b...2e7061 )
by Stephanie
06:17 queued 03:03
created

FrmTableHTMLGenerator::init_table_style()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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