|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ffcms\Core\Helper\HTML\Form; |
|
3
|
|
|
|
|
4
|
|
|
use Ffcms\Core\App; |
|
5
|
|
|
use Ffcms\Core\Helper\HTML\System\NativeGenerator; |
|
6
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
|
7
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
|
8
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
9
|
|
|
use Ffcms\Core\Exception\SyntaxException; |
|
10
|
|
|
|
|
11
|
|
|
class MultiCheckboxField extends NativeGenerator implements iField |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
private $properties; |
|
15
|
|
|
|
|
16
|
|
|
private $name; |
|
17
|
|
|
|
|
18
|
|
|
private $value; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* MultiCheckboxField constructor. |
|
22
|
|
|
* Pass params inside. |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $properties |
|
25
|
|
|
* @param string $name |
|
26
|
|
|
* @param string|null $value |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($properties, $name, $value = null) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->properties = $properties; |
|
31
|
|
|
$this->name = $name; |
|
32
|
|
|
$this->value = $value; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Build <input type="checkbox" checked {$properties} /> response |
|
37
|
|
|
* @todo: fix bug when option is unseted |
|
38
|
|
|
* {@inheritDoc} |
|
39
|
|
|
* @see \Ffcms\Core\Helper\HTML\Form\iField::make() |
|
40
|
|
|
*/ |
|
41
|
|
|
public function make() |
|
42
|
|
|
{ |
|
43
|
|
|
// check if options is defined |
|
44
|
|
|
$options = false; |
|
|
|
|
|
|
45
|
|
|
if (isset($this->properties['options']) && Obj::isArray($this->properties['options'])) { |
|
46
|
|
|
$options = $this->properties['options']; |
|
47
|
|
|
} else { |
|
48
|
|
|
throw new SyntaxException('Options for field ' . self::nohtml($this->name) . ' is not defined'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// set field type |
|
52
|
|
|
$this->properties['type'] = 'checkbox'; |
|
53
|
|
|
// set this field as array html dom object |
|
54
|
|
|
$this->properties['name'] .= '[]'; |
|
55
|
|
|
unset($this->properties['value'], $this->properties['id']); |
|
56
|
|
|
|
|
57
|
|
|
$build = null; |
|
58
|
|
|
foreach ($options as $opt) { |
|
59
|
|
|
// check if this is active element |
|
60
|
|
|
if (Obj::isArray($this->value) && Arr::in($opt, $this->value)) { |
|
|
|
|
|
|
61
|
|
|
$this->properties['checked'] = null; |
|
62
|
|
|
} else { |
|
63
|
|
|
unset($this->properties['checked']); // remove checked if it setted before |
|
64
|
|
|
} |
|
65
|
|
|
$this->properties['value'] = $opt; |
|
66
|
|
|
// apply structured checkboxes style for each item |
|
67
|
|
|
$build .= App::$View->render('native/form/multi_checkboxes_list', [ |
|
68
|
|
|
'item' => self::buildSingleTag('input', $this->properties) . self::nohtml($opt) |
|
69
|
|
|
]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $build; |
|
73
|
|
|
} |
|
74
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.