-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathDependentData.php
More file actions
184 lines (151 loc) · 3.15 KB
/
Copy pathDependentData.php
File metadata and controls
184 lines (151 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* This file is part of the NasExt extensions of Nette Framework
*
* Copyright (c) 2013 Dusan Hudak (http://dusan-hudak.com)
*
* For the full copyright and license information, please view
* the file license.md that was distributed with this source code.
*/
namespace NasExt\Forms;
use Nette;
/**
* @property array $items
* @property string|int $value
* @property string $prompt
*
* @author Dusan Hudak
* @author Ales Wita
* @license MIT
*/
class DependentData
{
use Nette\SmartObject;
/** @var array */
private $items = [];
/** @var string|int */
private $value;
/** @var string */
private $prompt;
/**
* @param array $items
* @param string|int $value
* @param string $prompt
*/
public function __construct(array $items = [], $value = null, $prompt = null)
{
$this->items = $items;
$this->value = $value;
$this->prompt = $prompt;
}
/**
* @return array
*/
public function getItems()
{
return $this->items;
}
/**
* @param array $items
* @return self
*/
public function setItems(array $items)
{
$this->items = $items;
return $this;
}
/**
* @return string|int
*/
public function getValue()
{
return $this->value;
}
/**
* @param string|int $value
* @return self
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* @return string
*/
public function getPrompt()
{
return $this->prompt;
}
/**
* @param string $value
* @return self
*/
public function setPrompt($value)
{
$this->prompt = $value;
return $this;
}
/**
* @param array $disabledItems
* @return array
*/
public function getPreparedItems($disabledItems = null)
{
$items = [];
foreach ($this->items as $key => $item) {
$elements = [];
if (is_array($item)) {
$groupItems = [];
foreach ($item as $innerKey => $innerItem) {
$el = $this->getPreparedElement($innerKey, $innerItem, $disabledItems);
$this->addElementToItemsList($groupItems, $el);
}
$items[$key] = [
'key' => $key,
'value' => $groupItems,
];
} else {
$el = $this->getPreparedElement($key, $item, $disabledItems);
$this->addElementToItemsList($items, $el);
}
}
// make a List so the order of items is preserved when sent as JSON to client
return array_values($items);
}
/**
* @param string $key
* @param mixed $item
* @param array|null $disabledItems
* @return Nette\Utils\Html
*/
private function getPreparedElement($key, $item, $disabledItems = null)
{
if (!($item instanceof Nette\Utils\Html)) {
$el = Nette\Utils\Html::el('option')->value($key)->setText($item);
} else {
$el = $item;
}
// disable element
if (is_array($disabledItems) && array_key_exists($key, $disabledItems) && $disabledItems[$key] === true) {
$el->disabled(true);
}
return $el;
}
/**
* @param array &$items
* @param Nette\Utils\Html $el
*/
private function addElementToItemsList(&$items, $el)
{
$items[$el->getAttribute('value')] = [
'key' => $el->getValue(),
'value' => $el->getText(),
];
end($items);
$lKey = key($items);
foreach ($el->attrs as $attr => $val) {
$items[$lKey]['attributes'][$attr] = $val;
}
}
}