Completed
Push — master ( 24ebcc...799b0c )
by Daniel
10:31
created

cfg_fields::_get_group_options()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.3332

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
ccs 8
cts 12
cp 0.6667
rs 9.4286
cc 3
eloc 11
nc 3
nop 1
crap 3.3332
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 44
	public function __construct(user $user)
25
	{
26 44
		$this->user = $user;
27 44
	}
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 hidden field for blocks config
89
	 *
90
	 * @param $value
91
	 * @param $key
92
	 * @return string
93
	 */
94 2
	public function build_hidden($value, $key)
95
	{
96 2
		return '<input type="hidden" name="config[' . $key . ']" value="' . $value . '" />';
97
	}
98
99
	/**
100
	 * @param $selected_items
101
	 * @return array
102
	 */
103 12
	protected function _ensure_array($selected_items)
104
	{
105 12
		return array_filter(is_array($selected_items) ? $selected_items : explode(',', $selected_items));
106
	}
107
108
	/**
109
	 * @param $options
110
	 * @param $css_class
111
	 * @return array
112
	 */
113 4
	protected function _ensure_multi_array($options, &$css_class)
114
	{
115 4
		$test = current($this->_ensure_array($options));
116 4
		if (!is_array($test))
117 4
		{
118 3
			$css_class = '';
119 3
			$options = array($options);
120 3
		}
121 4
		return $options;
122
	}
123
124
	/**
125
	 * @param string $needle
126
	 * @param array $haystack
127
	 * @param string $type selected|checked
128
	 * @return string
129
	 */
130 10
	protected function _get_selected_option($needle, array $haystack, $type = 'selected')
131
	{
132 10
		return (in_array($needle, $haystack)) ? ' ' . $type . '="' . $type . '"' : '';
133
	}
134
135
	/**
136
	 * @param array $row
137
	 * @param array $selected_items
138
	 * @param string $field
139
	 * @param integer $column_count
140
	 * @param string $column_class
141
	 * @return string
142
	 */
143 4
	protected function _get_checkbox_column(array $row, array $selected_items, $field, $column_count, $column_class)
144
	{
145 4
		$column = '<div class="' . $column_class . $field . '-checkbox" id="' . $field . '-col-' . $column_count . '">';
146 4
		foreach ($row as $value => $title)
147
		{
148 3
			$title = $this->user->lang($title);
149 3
			$selected = $this->_get_selected_option($value, $selected_items, 'checked');
150 3
			$column .= '<label><input type="checkbox" name="config[' . $field . '][]" value="' . $value . '"' . $selected . ' accesskey="' . $field . '" class="checkbox" /> ' . $title . '</label><br />';
151 4
		}
152 4
		$column .= '</div>';
153
154 4
		return $column;
155
	}
156
}
157