Passed
Branch develop (786e4f)
by Daniel
07:32
created

cfg_fields::get_select_options()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 4
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
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 */
0 ignored issues
show
Bug introduced by
The type phpbb\language\language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
	protected $translator;
16
17
	/**
18
	 * Constructor
19
	 *
20
	 * @param \phpbb\language\language	$translator		Language object
21
	 */
22 58
	public function __construct(\phpbb\language\language $translator)
23
	{
24 58
		$this->translator = $translator;
25 58
	}
26
27
	/**
28
	 * Used to add a select drop down in blocks config
29
	 *
30
	 * @param array $option_ary
31
	 * @param string $selected_item
32
	 * @param string $key
33
	 * @param int $size
34
	 * @param bool $multi_select
35
	 * @param string $data_toggle_key
36
	 * @return string
37
	 */
38 6
	public function build_select(array $option_ary, $selected_item, $key, $size = 1, $multi_select = false, $data_toggle_key = '')
39
	{
40 6
		$selected_item = $this->ensure_array($selected_item);
41
42 6
		$options = $this->get_select_options($option_ary, $selected_item, $key, $data_toggle_key);
43 6
		$data_toggle = ($data_toggle_key) ? ' data-togglable-settings="true"' : '';
44
45 6
		return '<select id="' . $key . '" name="config[' . $key . ']' . (($multi_select) ? '[]" multiple="multiple"' : '"') . (($size > 1) ? ' size="' . $size . '"' : '') . $data_toggle . '>' . $options . '</select>';
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 19
	protected function ensure_array($items)
136
	{
137 19
		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
156 4
		return array_map('array_filter', $options);
157
	}
158
159
	/**
160
	 * @param string $needle
161
	 * @param array $haystack
162
	 * @param string $type selected|checked
163
	 * @return string
164
	 */
165 12
	protected function get_selected_option($needle, array $haystack, $type = 'selected')
166
	{
167 12
		return (in_array($needle, $haystack)) ? ' ' . $type . '="' . $type . '"' : '';
168
	}
169
170
	/**
171
	 * @param array $row
172
	 * @param array $selected_items
173
	 * @param string $field
174
	 * @param string $column_class
175
	 * @param int $column_count
176
	 * @param int $index
177
	 * @return string
178
	 */
179 4
	protected function get_checkbox_column(array $row, array $selected_items, $field, $column_class, $column_count, &$index)
180
	{
181 4
		$column = '<div class="' . $column_class . $field . '-checkbox" id="' . $field . '-col-' . $column_count . '">';
182 4
		foreach ($row as $value => $title)
183
		{
184 3
			$title = $this->translator->lang($title);
185 3
			$selected = $this->get_selected_option($value, $selected_items, 'checked');
186 3
			$column .= '<label><input type="checkbox" name="config[' . $field . '][' . $index . ']" value="' . $value . '"' . $selected . ' class="checkbox" /> ' . $title . '</label><br />';
187 3
			$index++;
188 4
		}
189 4
		$column .= '</div>';
190
191 4
		return $column;
192
	}
193
194
	/**
195
	 * @param array $option_ary
196
	 * @param array $selected_items
197
	 * @param string $key
198
	 * @param string $togglable_key
199
	 * @return string
200
	 */
201 6
	protected function get_select_options(array $option_ary, array $selected_items, $key, $togglable_key)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

201
	protected function get_select_options(array $option_ary, array $selected_items, /** @scrutinizer ignore-unused */ $key, $togglable_key)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
202
	{
203 6
		$options = '';
204 6
		foreach ($option_ary as $value => $title)
205
		{
206 5
			$selected = $this->get_selected_option($value, $selected_items);
207 5
			$togglable_option = ($togglable_key) ? ' data-toggle-setting="#' . $togglable_key . '-' . $value . '"' : '';
208 5
			$options .= '<option value="' . $value . '"' . $selected . $togglable_option . '>' . $this->translator->lang($title) . '</option>';
209 6
		}
210 6
		return $options;
211
	}
212
}
213