1
|
|
|
<?php |
2
|
|
|
namespace Ffcms\Core\Helper\HTML\Form; |
3
|
|
|
|
4
|
|
|
use Ffcms\Core\Helper\HTML\System\NativeGenerator; |
5
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
6
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
7
|
|
|
use Ffcms\Core\Exception\SyntaxException; |
8
|
|
|
|
9
|
|
|
class MultiSelectField extends NativeGenerator implements iField |
10
|
|
|
{ |
11
|
|
|
private $properties; |
12
|
|
|
private $name; |
13
|
|
|
private $value; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* MultiSelectField constructor. Pass params inside. |
17
|
|
|
* @param array $properties |
18
|
|
|
* @param string $name |
19
|
|
|
* @param string|null $value |
20
|
|
|
*/ |
21
|
|
|
public function __construct($properties, $name, $value = null) |
22
|
|
|
{ |
23
|
|
|
$this->properties = $properties; |
24
|
|
|
$this->name = $name; |
25
|
|
|
$this->value = $value; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Build <select @properties>@optionsDOM</select> container |
30
|
|
|
* {@inheritDoc} |
31
|
|
|
* @see \Ffcms\Core\Helper\HTML\Form\iField::make() |
32
|
|
|
*/ |
33
|
|
|
public function make() |
34
|
|
|
{ |
35
|
|
|
// check if options is defined |
36
|
|
|
$options = $this->properties['options']; |
37
|
|
|
$optionsKey = (bool)$this->properties['optionsKey']; |
38
|
|
View Code Duplication |
if (!Obj::isIterable($options)) { |
|
|
|
|
39
|
|
|
throw new SyntaxException('Options for field ' . self::nohtml($this->name) . ' is not iterable'); |
40
|
|
|
} |
41
|
|
|
unset($this->properties['options']); |
42
|
|
|
|
43
|
|
|
// set global field type |
44
|
|
|
$this->properties['type'] = 'select'; |
45
|
|
|
// add multiple element |
46
|
|
|
$this->properties['multiple'] = null; |
47
|
|
|
$this->properties['name'] .= '[]'; |
48
|
|
|
unset($this->properties['value'], $this->properties['options'], $this->properties['optionsKey']); |
49
|
|
|
|
50
|
|
|
// build options as HTML DOM element: <option @value>@text</option> |
51
|
|
|
$optionsDOM = null; |
52
|
|
|
foreach ($options as $val => $text) { |
53
|
|
|
// check if options key is value |
54
|
|
|
$optionProperty = null; |
55
|
|
|
if ($optionsKey === true) { |
56
|
|
|
$optionProperty['value'] = $val; |
57
|
|
|
// check if current element is active |
58
|
|
View Code Duplication |
if (Obj::isArray($this->value) && Arr::in((string)$val, $this->value)) { |
|
|
|
|
59
|
|
|
$optionProperty['selected'] = 'selected'; |
60
|
|
|
} |
61
|
|
View Code Duplication |
} else { |
|
|
|
|
62
|
|
|
if (Obj::isArray($this->value) && Arr::in((string)$text, $this->value)) { |
|
|
|
|
63
|
|
|
$optionProperty['selected'] = 'selected'; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
$optionsDOM .= self::buildContainerTag('option', $optionProperty, $text); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// build <select @properties>@optionsDOM</select> |
70
|
|
|
return self::buildContainerTag('select', $this->properties, $optionsDOM, true); |
71
|
|
|
} |
72
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.