|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Form\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Form\Elements\AbstractElement; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait HasElementsTrait |
|
9
|
|
|
* @package Nip\Form\Traits |
|
10
|
|
|
*/ |
|
11
|
|
|
trait HasElementsTrait |
|
12
|
|
|
{ |
|
13
|
|
|
protected $_elements = []; |
|
14
|
|
|
protected $_elementsLabel; |
|
15
|
|
|
protected $_elementsOrder = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param AbstractElement $element |
|
19
|
|
|
* @return $this |
|
20
|
|
|
*/ |
|
21
|
|
|
public function addElement(AbstractElement $element) |
|
22
|
6 |
|
{ |
|
23
|
|
|
$name = $element->getUniqueId(); |
|
24
|
6 |
|
$this->_elements[$name] = $element; |
|
25
|
6 |
|
$this->_elementsLabel[$element->getLabel()] = $name; |
|
26
|
6 |
|
$this->_elementsOrder[] = $name; |
|
27
|
6 |
|
|
|
28
|
|
|
return $this; |
|
29
|
6 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param $name |
|
33
|
|
|
* @return $this |
|
34
|
|
|
*/ |
|
35
|
|
|
public function removeElement($name) |
|
36
|
|
|
{ |
|
37
|
|
|
unset($this->_elements[$name]); |
|
38
|
|
|
|
|
39
|
|
|
$key = array_search($name, $this->_elementsOrder); |
|
40
|
|
|
if ($key) { |
|
41
|
|
|
unset($this->_elementsOrder[$key]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param $name |
|
49
|
|
|
* @return AbstractElement |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getElement($name) |
|
52
|
4 |
|
{ |
|
53
|
|
|
if (array_key_exists($name, $this->_elements)) { |
|
54
|
4 |
|
return $this->_elements[$name]; |
|
55
|
4 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
return null; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return AbstractElement[] |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getElements() |
|
64
|
2 |
|
{ |
|
65
|
|
|
$return = []; |
|
66
|
2 |
|
foreach ($this->_elementsOrder as $current) { |
|
67
|
2 |
|
$return[$current] = $this->_elements[$current]; |
|
68
|
2 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $return; |
|
71
|
2 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $name |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
|
|
public function hasElement($name) |
|
78
|
|
|
{ |
|
79
|
|
|
return array_key_exists($name, $this->_elements); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param $label |
|
84
|
|
|
* @return AbstractElement |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getElementByLabel($label) |
|
87
|
|
|
{ |
|
88
|
|
|
if (array_key_exists($label, $this->_elementsLabel)) { |
|
89
|
|
|
return $this->_elements[$this->_elementsLabel[$label]]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return null; |
|
93
|
|
|
} |
|
94
|
|
|
/** |
|
95
|
|
|
* @param $element |
|
96
|
|
|
* @param $neighbour |
|
97
|
|
|
* @param string $type |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setElementOrder($element, $neighbour, $type = 'bellow') |
|
101
|
|
|
{ |
|
102
|
|
|
if (in_array($element, $this->_elementsOrder) && in_array($neighbour, $this->_elementsOrder)) { |
|
103
|
|
|
$newOrder = []; |
|
104
|
|
|
foreach ($this->_elementsOrder as $current) { |
|
105
|
|
|
if ($current == $element) { |
|
106
|
|
|
} elseif ($current == $neighbour) { |
|
107
|
|
|
if ($type == 'above') { |
|
108
|
|
|
$newOrder[] = $element; |
|
109
|
|
|
$newOrder[] = $neighbour; |
|
110
|
|
|
} else { |
|
111
|
|
|
$newOrder[] = $neighbour; |
|
112
|
|
|
$newOrder[] = $element; |
|
113
|
|
|
} |
|
114
|
|
|
} else { |
|
115
|
|
|
$newOrder[] = $current; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
$this->_elementsOrder = $newOrder; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
/** |
|
124
|
|
|
* @param bool $params |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
public function findElements($params = false) |
|
128
|
|
|
{ |
|
129
|
|
|
$elements = []; |
|
130
|
|
|
foreach ($this->getElements() as $element) { |
|
131
|
|
|
if (isset($params['type'])) { |
|
132
|
|
|
if ($element->getType() != $params['type']) { |
|
133
|
|
|
continue; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
if (isset($params['attribs']) && is_array($params['attribs'])) { |
|
137
|
|
|
foreach ($params['attribs'] as $name => $value) { |
|
138
|
|
|
if ($element->getAttrib($name) != $value) { |
|
139
|
|
|
continue(2); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
$elements[$element->getUniqueId()] = $element; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $elements; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|