Passed
Push — release-3.2.0 ( 24bcfb...4d8cca )
by Daniel
04:25
created

multi_select   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A prep_field() 0 5 1
A build_multi_select() 0 13 2
A get_name() 0 3 1
A __construct() 0 3 1
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2019 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\config\fields;
11
12
use blitze\sitemaker\services\blocks\config\cfg_utils;
13
14
/**
15
 * @package sitemaker
16
 */
17
class multi_select implements cfg_field_interface
18
{
19
	/** @var \phpbb\language\language */
20
	protected $translator;
21
22
	/**
23
	 * Constructor
24
	 *
25
	 * @param \phpbb\language\language	$translator		Language object
26
	 */
27
	public function __construct(\phpbb\language\language $translator)
28
	{
29
		$this->translator = $translator;
30
	}
31
32
	/**
33
	 * @inheritdoc
34
	 */
35
	public function get_name()
36
	{
37
		return 'multi_select';
38
	}
39
40
	/**
41
	 * {@inheritdoc}
42
	 */
43
	public function prep_field(array &$vars, array &$type, $field, array $db_settings)
44
	{
45
		$vars['method'] ='build_multi_select';
46
		$vars['params'][] = $field;
47
		$type[0] = 'custom';
48
	}
49
50
	/**
51
	 * Used to add multi-select drop down in blocks config
52
	 *
53
	 * @param array $option_ary
54
	 * @param mixed $selected_items
55
	 * @param string $field
56
	 * @return string
57
	 */
58
	public function build_multi_select(array $option_ary, $selected_items, $field)
59
	{
60
		$selected_items = cfg_utils::ensure_array($selected_items);
61
62
		$html = '<select id="' . $field . '" name="config[' . $field . '][]" multiple="multiple">';
63
		foreach ($option_ary as $value => $title)
64
		{
65
			$selected = cfg_utils::get_selected_option($value, $selected_items);
66
			$html .= '<option value="' . $value . '"' . $selected . '>' . $this->translator->lang($title) . '</option>';
67
		}
68
		$html .= '</select>';
69
70
		return $html;
71
	}
72
}
73