Passed
Push — release-3.2.0 ( 0c5bd3...24bcfb )
by Daniel
11:19 queued 06:31
created

cfg_fields::fullscreen_allowed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
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 */
15
	protected $translator;
16
17
	/**
18
	 * Constructor
19
	 *
20
	 * @param \phpbb\language\language	$translator		Language object
21
	 */
22
	public function __construct(\phpbb\language\language $translator)
23
	{
24
		$this->translator = $translator;
25
	}
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
	public function build_select(array $option_ary, $selected_item, $key, $size = 1, $multi_select = false, $data_toggle_key = '')
39
	{
40
		$selected_item = $this->ensure_array($selected_item);
41
42
		$options = $this->get_select_options($option_ary, $selected_item, $data_toggle_key);
43
		$data_toggle = ($data_toggle_key) ? ' data-togglable-settings="true"' : '';
44
45
		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
	public function build_multi_select(array $option_ary, $selected_items, $field)
57
	{
58
		$selected_items = $this->ensure_array($selected_items);
59
60
		$html = '<select id="' . $field . '" name="config[' . $field . '][]" multiple="multiple">';
61
		foreach ($option_ary as $value => $title)
62
		{
63
			$selected = $this->get_selected_option($value, $selected_items);
64
			$html .= '<option value="' . $value . '"' . $selected . '>' . $this->translator->lang($title) . '</option>';
65
		}
66
		$html .= '</select>';
67
68
		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
	public function build_checkbox(array $option_ary, $selected_items, $field)
91
	{
92
		$index = 0;
93
		$column_class = 'col ';
94
		$selected_items = $this->ensure_array($selected_items);
95
		$option_ary = $this->ensure_multi_array($option_ary, $column_class);
96
97
		$html = '';
98
		foreach ($option_ary as $col => $row)
99
		{
100
			$html .= $this->get_checkbox_column($row, $selected_items, $field, $column_class, $col, $index);
101
		}
102
103
		return ($column_class) ? '<div class="grid-noBottom">' . $html . '</div>' : $html;
104
	}
105
106
	/**
107
	 * Build radio buttons other than yes_no/enable_disable in blocks config
108
	 *
109
	 * @param array $option_ary
110
	 * @param mixed $selected_item
111
	 * @param string $key
112
	 * @return string
113
	 */
114
	public function build_radio(array $option_ary, $selected_item, $key)
115
	{
116
		$selected_item = (is_array($selected_item)) ? $selected_item : array($selected_item);
117
118
		$html = '';
119
		foreach ($option_ary as $value => $title)
120
		{
121
			$selected = $this->get_selected_option($value, $selected_item, 'checked');
122
			$html .= '<label><input type="radio" name="config[' . $key . ']" value="' . $value . '"' . $selected . ' class="radio" /> ' . $this->translator->lang($title) . '</label><br />';
123
		}
124
125
		return $html;
126
	}
127
128
	/**
129
	 * Used to add a code editor to blocks config
130
	 *
131
	 * @param string $key
132
	 * @param string $value
133
	 * @param string $explain
134
	 * @param array $data_props
135
	 * @param string $label
136
	 * @return string
137
	 */
138
	public function build_code_editor($key, $value, $explain, array $data_props = array(), $label = '')
139
	{
140
		$html = '';
141
		$id = $key . '-editor';
142
		$class = $id . '-button';
143
		$attributes = $this->get_code_editor_attributes($data_props);
144
145
		$html .= ($label) ? '<label for="' . $key . '"><strong>' . $this->translator->lang($label) . '</strong></label>' : '';
146
		$html .= ($explain) ? '<span>' . $explain . '</span>' : '';
147
		$html .= '<textarea id="' . $id . '" class="code-editor" name="config[' . $key . ']"' . $attributes . '>' . $value . '</textarea>';
148
		$html .= '<div class="align-right">';
149
		$html .= '<button class="' . $class . ' CodeMirror-button" data-action="undo" title="' . $this->translator->lang('UNDO') . '"><i class="fa fa-undo" aria-hidden="true"></i></button>';
150
		$html .= '<button class="' . $class . ' CodeMirror-button" data-action="redo" title="' . $this->translator->lang('REDO') . '"><i class="fa fa-repeat" aria-hidden="true"></i></button>';
151
		$html .= '<button class="' . $class . ' CodeMirror-button" data-action="clear" title="' . $this->translator->lang('CLEAR') . '"><i class="fa fa-ban" aria-hidden="true"></i></button>';
152
		$html .= $this->fullscreen_allowed($data_props) ? '<button class="' . $class . ' CodeMirror-button" data-action="fullscreen" title="' . $this->translator->lang('FULLSCREEN') . '"><i class="fa fa-window-restore" aria-hidden="true"></i></button>' : '';
153
		$html .= '</div>';
154
155
		return $html;
156
	}
157
158
	/**
159
	 * @param array $data_props
160
	 * @return bool
161
	 */
162
	protected function fullscreen_allowed(array $data_props = array())
163
	{
164
		return (!isset($data_props['allow-full-screen']) || $data_props['allow-full-screen']);
165
	}
166
167
	/**
168
	 * @param array $data_props
169
	 * @return string
170
	 */
171
	protected function get_code_editor_attributes(array $data_props = array())
172
	{
173
		$attributes = '';
174
		foreach ($data_props as $prop => $value)
175
		{
176
			$value = (gettype($value) === 'boolean') ? (int) $value : $value;
177
			$attributes .= " data-{$prop}=\"{$value}\"";
178
		}
179
		return $attributes;
180
	}
181
182
	/**
183
	 * Force array
184
	 *
185
	 * @param mixed $items
186
	 * @return array
187
	 */
188
	protected function ensure_array($items)
189
	{
190
		return is_array($items) ? $items : explode(',', $items);
191
	}
192
193
	/**
194
	 * Force multi dimensional array
195
	 *
196
	 * @param mixed $options
197
	 * @param string $css_class
198
	 * @return array
199
	 */
200
	protected function ensure_multi_array($options, &$css_class)
201
	{
202
		$test = current($this->ensure_array($options));
203
		if (!is_array($test))
204
		{
205
			$css_class = '';
206
			$options = array($options);
207
		}
208
209
		return array_map('array_filter', $options);
210
	}
211
212
	/**
213
	 * @param string $needle
214
	 * @param array $haystack
215
	 * @param string $type selected|checked
216
	 * @return string
217
	 */
218
	protected function get_selected_option($needle, array $haystack, $type = 'selected')
219
	{
220
		return (in_array($needle, $haystack)) ? ' ' . $type . '="' . $type . '"' : '';
221
	}
222
223
	/**
224
	 * @param array $row
225
	 * @param array $selected_items
226
	 * @param string $field
227
	 * @param string $column_class
228
	 * @param int $column_count
229
	 * @param int $index
230
	 * @return string
231
	 */
232
	protected function get_checkbox_column(array $row, array $selected_items, $field, $column_class, $column_count, &$index)
233
	{
234
		$column = "<div class=\"{$column_class}{$field}-checkbox\" id=\"{$field}-col-{$column_count}\">";
235
		foreach ($row as $value => $title)
236
		{
237
			$title = $this->translator->lang($title);
238
			$selected = $this->get_selected_option($value, $selected_items, 'checked');
239
			$column .= '<label><input type="checkbox" name="config[' . $field . '][' . $index . ']" value="' . $value . '"' . $selected . ' class="checkbox" /> ' . $title . '</label><br />';
240
			$index++;
241
		}
242
		$column .= '</div>';
243
244
		return $column;
245
	}
246
247
	/**
248
	 * @param array $option_ary
249
	 * @param array $selected_items
250
	 * @param string $togglable_key
251
	 * @return string
252
	 */
253
	protected function get_select_options(array $option_ary, array $selected_items, $togglable_key)
254
	{
255
		$options = '';
256
		foreach ($option_ary as $value => $title)
257
		{
258
			$selected = $this->get_selected_option($value, $selected_items);
259
			$togglable_option = ($togglable_key) ? ' data-toggle-setting="#' . $togglable_key . '-' . $value . '"' : '';
260
			$options .= '<option value="' . $value . '"' . $selected . $togglable_option . '>' . $this->translator->lang($title) . '</option>';
261
		}
262
		return $options;
263
	}
264
}
265