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 = false; |
|
|
|
|
37
|
|
|
$optionsKey = (bool)$this->properties['optionsKey']; |
38
|
|
|
if (isset($this->properties['options']) && Obj::isArray($this->properties['options'])) { |
39
|
|
|
$options = $this->properties['options']; |
40
|
|
|
} else { |
41
|
|
|
throw new SyntaxException('Options for field ' . self::nohtml($this->name) . ' is not defined'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// set global field type |
45
|
|
|
$this->properties['type'] = 'select'; |
46
|
|
|
// add multiple element |
47
|
|
|
$this->properties['multiple'] = null; |
48
|
|
|
$this->properties['name'] .= '[]'; |
49
|
|
|
unset($this->properties['value'], $this->properties['options'], $this->properties['optionsKey']); |
50
|
|
|
|
51
|
|
|
// build options as HTML DOM element: <option @value>@text</option> |
52
|
|
|
$optionsDOM = null; |
53
|
|
|
foreach ($options as $val => $text) { |
54
|
|
|
// check if options key is value |
55
|
|
|
$optionProperty = null; |
56
|
|
|
if ($optionsKey === true) { |
57
|
|
|
$optionProperty['value'] = $val; |
58
|
|
|
// check if current element is active |
59
|
|
View Code Duplication |
if (Obj::isArray($this->value) && Arr::in((string)$val, $this->value)) { |
|
|
|
|
60
|
|
|
$optionProperty['selected'] = 'selected'; |
61
|
|
|
} |
62
|
|
View Code Duplication |
} else { |
|
|
|
|
63
|
|
|
if (Obj::isArray($this->value) && Arr::in((string)$text, $this->value)) { |
|
|
|
|
64
|
|
|
$optionProperty['selected'] = 'selected'; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
$optionsDOM .= self::buildContainerTag('option', $optionProperty, $text); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// build <select @properties>@optionsDOM</select> |
71
|
|
|
return self::buildContainerTag('select', $this->properties, $optionsDOM, true); |
72
|
|
|
} |
73
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.