Completed
Push — develop ( 8abf2d...b28862 )
by Daniel
09:52
created

cfg_fields   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 0
dl 0
loc 181
ccs 60
cts 60
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A build_select() 0 12 3
A __construct() 0 4 1
A build_multi_select() 0 14 2
A build_checkbox() 0 16 2
A build_radio() 0 13 3
A ensure_array() 0 4 2
A ensure_multi_array() 0 10 2
A get_selected_option() 0 4 2
A get_checkbox_column() 0 14 2
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2015 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services\blocks;
11
12
abstract class cfg_fields
13
{
14
	/** @var \phpbb\language\language */
15
	protected $translator;
16
17
	/**
18
	 * Constructor
19
	 *
20
	 * @param \phpbb\language\language	$translator		Language object
21
	 */
22 55
	public function __construct(\phpbb\language\language $translator)
23
	{
24 55
		$this->translator = $translator;
25 55
	}
26
27
	/**
28
	 * Used to add a select drop down in blocks config
29
	 *
30
	 * @param array $option_ary
31
	 * @param $selected_items
32
	 * @param $key
33
	 * @return string
34
	 */
35 3
	public function build_select($option_ary, $selected_item, $key)
36
	{
37 3
		$html = '<select id="' . $key . '" name="config[' . $key . ']">';
38 3
		foreach ($option_ary as $value => $title)
39
		{
40 2
			$selected = ($value == $selected_item) ? ' selected="selected"' : '';
41 2
			$html .= '<option value="' . $value . '"' . $selected . '>' . $this->translator->lang($title) . '</option>';
42 3
		}
43 3
		$html .= '</select>';
44
45 3
		return $html;
46
	}
47
48
	/**
49
	 * Used to add multi-select drop down in blocks config
50
	 *
51
	 * @param array $option_ary
52
	 * @param mixed $selected_items
53
	 * @param string $field
54
	 * @return string
55
	 */
56 3
	public function build_multi_select(array $option_ary, $selected_items, $field)
57
	{
58 3
		$selected_items = $this->ensure_array($selected_items);
59
60 3
		$html = '<select id="' . $field . '" name="config[' . $field . '][]" multiple="multiple">';
61 3
		foreach ($option_ary as $value => $title)
62
		{
63 2
			$selected = $this->get_selected_option($value, $selected_items);
64 2
			$html .= '<option value="' . $value . '"' . $selected . '>' . $this->translator->lang($title) . '</option>';
65 3
		}
66 3
		$html .= '</select>';
67
68 3
		return $html;
69
	}
70
71
	/**
72
	 * Used to build multi-column checkboxes for blocks config
73
	 *
74
	 * if multi-dimensional array, we break the checkboxes into columns ex.
75
	 * array(
76
	 * 		'news' => array(
77
	 * 			'field1' => 'Label 1',
78
	 * 			'field2' => 'Label 2',
79
	 * 		),
80
	 * 		'articles' => array(
81
	 * 			'field1' => 'Label 1',
82
	 * 			'field2' => 'Label 2',
83
	 * 		),
84
	 * )
85
	 * @param array $option_ary
86
	 * @param mixed $selected_items
87
	 * @param string $field
88
	 * @return string
89
	 */
90 4
	public function build_checkbox(array $option_ary, $selected_items, $field)
91
	{
92 4
		$column_class = 'grid__col grid__col--1-of-2 ';
93 4
		$index = 0;
94 4
		$html = '';
95
96 4
		$selected_items = $this->ensure_array($selected_items);
97 4
		$option_ary = $this->ensure_multi_array($option_ary, $column_class);
98
99 4
		foreach ($option_ary as $col => $row)
100
		{
101 4
			$html .= $this->get_checkbox_column($row, $selected_items, $field, $column_class, $col, $index);
102 4
		}
103
104 4
		return $html;
105
	}
106
107
	/**
108
	 * Build radio buttons other than yes_no/enable_disable in blocks config
109
	 *
110
	 * @param array $option_ary
111
	 * @param mixed $selected_item
112
	 * @param string $key
113
	 * @return string
114
	 */
115 3
	public function build_radio(array $option_ary, $selected_item, $key)
116
	{
117 3
		$selected_item = (is_array($selected_item)) ? $selected_item : array($selected_item);
118
119 3
		$html = '';
120 3
		foreach ($option_ary as $value => $title)
121
		{
122 2
			$selected = $this->get_selected_option($value, $selected_item, 'checked');
123 2
			$html .= '<label><input type="radio" name="config[' . $key . ']" value="' . $value . '"' . $selected . ' class="radio" /> ' . $this->translator->lang($title) . '</label><br />';
124 3
		}
125
126 3
		return $html;
127
	}
128
129
	/**
130
	 * Force array
131
	 *
132
	 * @param mixed $items
133
	 * @return array
134
	 */
135 14
	protected function ensure_array($items)
136
	{
137 14
		return is_array($items) ? $items : explode(',', $items);
138
	}
139
140
	/**
141
	 * Force multi dimensional array
142
	 *
143
	 * @param mixed $options
144
	 * @param string $css_class
145
	 * @return array
146
	 */
147 4
	protected function ensure_multi_array($options, &$css_class)
148
	{
149 4
		$test = current($this->ensure_array($options));
150 4
		if (!is_array($test))
151 4
		{
152 3
			$css_class = '';
153 3
			$options = array($options);
154 3
		}
155 4
		return array_map('array_filter', $options);
156
	}
157
158
	/**
159
	 * @param string $needle
160
	 * @param array $haystack
161
	 * @param string $type selected|checked
162
	 * @return string
163
	 */
164 7
	protected function get_selected_option($needle, array $haystack, $type = 'selected')
165
	{
166 7
		return (in_array($needle, $haystack)) ? ' ' . $type . '="' . $type . '"' : '';
167
	}
168
169
	/**
170
	 * @param array $row
171
	 * @param array $selected_items
172
	 * @param string $field
173
	 * @param string $column_class
174
	 * @param int $column_count
175
	 * @param int $index
176
	 * @return string
177
	 */
178 4
	protected function get_checkbox_column(array $row, array $selected_items, $field, $column_class, $column_count, &$index)
179
	{
180 4
		$column = '<div class="' . $column_class . $field . '-checkbox" id="' . $field . '-col-' . $column_count . '">';
181 4
		foreach ($row as $value => $title)
182
		{
183 3
			$title = $this->translator->lang($title);
184 3
			$selected = $this->get_selected_option($value, $selected_items, 'checked');
185 3
			$column .= '<label><input type="checkbox" name="config[' . $field . '][' . $index . ']" value="' . $value . '"' . $selected . ' class="checkbox" /> ' . $title . '</label><br />';
186 3
			$index++;
187 4
		}
188 4
		$column .= '</div>';
189
190 4
		return $column;
191
	}
192
}
193