Completed
Push — master ( adf79c...3fa9d1 )
by Jamie
03:51 queued 01:11
created

FrmTableHTMLGenerator::init_style_settings()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 4
nop 1
dl 0
loc 21
rs 8.7624
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
	/**
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.04
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
88
				if ( $this->is_color_setting( $key ) ) {
89
					$this->style_settings[ $key ] = $this->get_color_markup( $atts[ $key ] );
90
				} else {
91
					$this->style_settings[ $key ] = $atts[ $key ];
92
				}
93
			}
94
		}
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
137
	/**
138
	 * Set the td_style property
139
	 *
140
	 * @since 2.04
141
	 */
142
	private function init_td_style() {
143
		if ( $this->use_inline_style === true ) {
144
145
			$td_style_attributes = 'text-align:' . ( $this->direction == 'rtl' ? 'right' : 'left' ) . ';';
146
			$td_style_attributes .= 'color:' . $this->style_settings['text_color'] . ';padding:7px 9px;vertical-align:top;';
147
			$td_style_attributes .= 'border-top:' . $this->style_settings['border_width'] . ' solid ' . $this->style_settings['border_color'] . ';';
148
149
			$this->td_style = ' style="' . $td_style_attributes . '"';
150
		}
151
	}
152
153
	/**
154
	 * Determine if setting is for a color, e.g. text color, background color, or border color
155
	 *
156
	 * @param string $setting_key name of setting
157
	 *
158
	 * @since 2.04.02
159
	 *
160
	 * @return boolean
161
	 */
162
	private function is_color_setting ( $setting_key ) {
163
		return strpos( $setting_key, 'color' ) !== false;
164
	}
165
166
	/**
167
	 * Get color markup from color setting value
168
	 *
169
	 * @param string $color_markup value of a color setting, with format #FFFFF, FFFFFF, or white.
170
	 *
171
	 * @since 2.04.02
172
	 *
173
	 * @return string
174
	 */
175
	private function get_color_markup( $color_markup ) {
176
		$color_markup = trim( $color_markup );
177
178
		//check if each character in string is valid hex digit
179
		if ( ctype_xdigit( $color_markup ) ) {
180
			$color_markup = '#' . $color_markup;
181
		}
182
183
		return $color_markup;
184
	}
185
186
	/**
187
	 * Get the table row background color
188
	 *
189
	 * @since 2.04
190
	 *
191
	 * @return string
192
	 */
193
	private function table_row_background_color() {
194
		return ( $this->odd ? $this->style_settings['bg_color'] : $this->style_settings['alt_bg_color'] );
195
	}
196
197
	/**
198
	 * Get the table row style
199
	 *
200
	 * @since 2.04
201
	 *
202
	 * @return string
203
	 */
204
	private function tr_style() {
205
206
		if ( $this->type === 'shortcode' ) {
207
			$tr_style = ' style="[frm-alt-color]"';
208
		} else if ( $this->use_inline_style ) {
209
			$tr_style = ' style="background-color:' . $this->table_row_background_color() . ';"';
210
		} else {
211
			$tr_style = '';
212
		}
213
214
		return $tr_style;
215
	}
216
217
	/**
218
	 * Switch the odd property from true to false or false to true
219
	 *
220
	 * @since 2.04
221
	 */
222
	private function switch_odd() {
223
		if ( $this->type !== 'shortcode' ) {
224
			$this->odd = ! $this->odd;
225
		}
226
	}
227
228
	/**
229
	 * Generate a table header
230
	 *
231
	 * @since 2.04
232
	 *
233
	 * @return string
234
	 */
235
	public function generate_table_header() {
236
		return '<table cellspacing="0"' . $this->table_style . '><tbody>' . "\r\n";
237
	}
238
239
	/**
240
	 * Generate a table footer
241
	 *
242
	 * @since 2.04
243
	 *
244
	 * @return string
245
	 */
246
	public function generate_table_footer() {
247
		return '</tbody></table>';
248
	}
249
250
	/**
251
	 * Generate a two cell row for an HTML table
252
	 *
253
	 * @since 2.04
254
	 *
255
	 * @param string $label
256
	 * @param string $value
257
	 *
258
	 * @return string
259
	 */
260
	public function generate_two_cell_table_row( $label, $value ) {
261
		$row = '<tr' . $this->tr_style() . '>';
262
263
		if ( 'rtl' == $this->direction ) {
264
			$first = $value;
265
			$second = $label;
266
		} else {
267
			$first = $label;
268
			$second = $value;
269
		}
270
271
		$row .= '<td' . $this->td_style . '>' . $first . '</td>';
272
		$row .= '<td' . $this->td_style . '>' . $second . '</td>';
273
274
		$row .= '</tr>' . "\r\n";
275
276
		$this->switch_odd();
277
278
		return $row;
279
	}
280
281
	/**
282
	 * Generate a single cell row for an HTML table
283
	 *
284
	 * @since 2.04
285
	 *
286
	 * @param string $value
287
	 *
288
	 * @return string
289
	 */
290
	public function generate_single_cell_table_row( $value ) {
291
		$row = '<tr' . $this->tr_style() . '>';
292
		$row .= '<td colspan="2"' . $this->td_style . '>' . $value . '</td>';
293
		$row .= '</tr>' . "\r\n";
294
295
		$this->switch_odd();
296
297
		return $row;
298
	}
299
}