Completed
Push — master ( 4a8b55...d86878 )
by Daniel
11:13
created

cfg_fields::build_radio()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4286
cc 3
eloc 8
nc 4
nop 3
crap 3
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
use phpbb\user;
13
14
abstract class cfg_fields
15
{
16
	/** @var user */
17
	protected $user;
18
19
	/**
20
	 * Constructor
21
	 *
22
	 * @param user	$user	User object
23
	 */
24 48
	public function __construct(user $user)
25
	{
26 48
		$this->user = $user;
27 48
	}
28
29
	/**
30
	 * Used to add multi-select drop down in blocks config
31
	 *
32
	 * @param array $option_ary
33
	 * @param $selected_items
34
	 * @param $key
35
	 * @return string
36
	 */
37 3
	public function build_multi_select(array $option_ary, $selected_items, $key)
38
	{
39 3
		$selected_items = $this->_ensure_array($selected_items);
40 3
		$html = '<select id="' . $key . '" name="config[' . $key . '][]" multiple="multiple">';
41 3
		foreach ($option_ary as $value => $title)
42
		{
43 2
			$title = $this->user->lang($title);
44 2
			$selected = $this->_get_selected_option($value, $selected_items);
45 2
			$html .= '<option value="' . $value . '"' . $selected . '>' . $title . '</option>';
46 3
		}
47 3
		$html .= '</select>';
48
49 3
		return $html;
50
	}
51
52
	/**
53
	 * Used to build multi-column checkboxes for blocks config
54
	 *
55
	 * if multi-dimensional array, we break the checkboxes into columns ex.
56
	 * array(
57
	 * 		'news' => array(
58
	 * 			'field1' => 'Label 1',
59
	 * 			'field2' => 'Label 2',
60
	 * 		),
61
	 * 		'articles' => array(
62
	 * 			'field1' => 'Label 1',
63
	 * 			'field2' => 'Label 2',
64
	 * 		),
65
	 * )
66
	 * @param array $option_ary
67
	 * @param $selected_items
68
	 * @param $field
69
	 * @return string
70
	 */
71 4
	public function build_checkbox(array $option_ary, $selected_items, $field)
72
	{
73 4
		$column_class = 'grid__col grid__col--1-of-2 ';
74 4
		$html = '';
75
76 4
		$selected_items = $this->_ensure_array($selected_items);
77 4
		$option_ary = $this->_ensure_multi_array($option_ary, $column_class);
78
79 4
		foreach ($option_ary as $col => $row)
80
		{
81 4
			$html .= $this->_get_checkbox_column($row, $selected_items, $field, $col, $column_class);
82 4
		}
83
84 4
		return $html;
85
	}
86
87
	/**
88
	 * Build radio buttons other than yes_no/enable_disable in blocks config
89
	 *
90
	 * @param array $option_ary
91
	 * @param $selected_items
92
	 * @param $key
93
	 * @return string
94
	 */
95 3
	public function build_radio(array $option_ary, $selected_item, $key)
96
	{
97 3
		$selected_item = (is_array($selected_item)) ? $selected_item : array($selected_item);
98
99 3
		$html = '';
100 3
		foreach ($option_ary as $value => $title)
101
		{
102 2
			$title = $this->user->lang($title);
103 2
			$selected = $this->_get_selected_option($value, $selected_item, 'checked');
104 2
			$html .= '<label><input type="radio" name="config[' . $key . ']" value="' . $value . '"' . $selected . ' class="radio" /> ' . $title . '</label><br />';
105 3
		}
106
107 3
		return $html;
108
	}
109
110
	/**
111
	 * build hidden field for blocks config
112
	 *
113
	 * @param $value
114
	 * @param $key
115
	 * @return string
116
	 */
117 2
	public function build_hidden($value, $key)
118
	{
119 2
		return '<input type="hidden" name="config[' . $key . ']" value="' . $value . '" />';
120
	}
121
122
	/**
123
	 * @param $selected_items
124
	 * @return array
125
	 */
126 13
	protected function _ensure_array($selected_items)
127
	{
128 13
		return array_filter(is_array($selected_items) ? $selected_items : explode(',', $selected_items));
129
	}
130
131
	/**
132
	 * @param $options
133
	 * @param $css_class
134
	 * @return array
135
	 */
136 4
	protected function _ensure_multi_array($options, &$css_class)
137
	{
138 4
		$test = current($this->_ensure_array($options));
139 4
		if (!is_array($test))
140 4
		{
141 3
			$css_class = '';
142 3
			$options = array($options);
143 3
		}
144 4
		return $options;
145
	}
146
147
	/**
148
	 * @param string $needle
149
	 * @param array $haystack
150
	 * @param string $type selected|checked
151
	 * @return string
152
	 */
153 7
	protected function _get_selected_option($needle, array $haystack, $type = 'selected')
154
	{
155 7
		return (in_array($needle, $haystack)) ? ' ' . $type . '="' . $type . '"' : '';
156
	}
157
158
	/**
159
	 * @param array $row
160
	 * @param array $selected_items
161
	 * @param string $field
162
	 * @param integer $column_count
163
	 * @param string $column_class
164
	 * @return string
165
	 */
166 4
	protected function _get_checkbox_column(array $row, array $selected_items, $field, $column_count, $column_class)
167
	{
168 4
		$column = '<div class="' . $column_class . $field . '-checkbox" id="' . $field . '-col-' . $column_count . '">';
169 4
		foreach ($row as $value => $title)
170
		{
171 3
			$title = $this->user->lang($title);
172 3
			$selected = $this->_get_selected_option($value, $selected_items, 'checked');
173 3
			$column .= '<label><input type="checkbox" name="config[' . $field . '][]" value="' . $value . '"' . $selected . ' accesskey="' . $field . '" class="checkbox" /> ' . $title . '</label><br />';
174 4
		}
175 4
		$column .= '</div>';
176
177 4
		return $column;
178
	}
179
}
180