|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @company MTE Telecom, Ltd. |
|
4
|
|
|
* @author Roman Malashin <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Nnx\DataGrid; |
|
8
|
|
|
|
|
9
|
|
|
use Nnx\DataGrid\Column\ColumnInterface; |
|
10
|
|
|
use Nnx\DataGrid\Mutator\MutatorInterface; |
|
11
|
|
|
use Traversable; |
|
12
|
|
|
use ArrayAccess; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class SimpleGrid |
|
16
|
|
|
* @package Nnx\DataGrid |
|
17
|
|
|
*/ |
|
18
|
|
|
class SimpleGrid extends AbstractGrid implements ColumHidebleProviderInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Коллекция колонок таблицы скрытых/показаннах пользователем |
|
22
|
|
|
* @var array | Traversable |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $userHiddenColums; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Флаг указывающий на изменеи списка колонок используется для проверки необходимости переустановки скрытия колонок пользователем |
|
|
|
|
|
|
28
|
|
|
* @var bool |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $columsChanged = false; |
|
31
|
|
|
/** |
|
32
|
|
|
* Данные в гриде |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $rowSet = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Возвращает массив строк таблицы |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getRowSet() |
|
42
|
|
|
{ |
|
43
|
|
|
if (count($this->rowSet) === 0) { |
|
44
|
|
|
$data = $this->getAdapter()->getData(); |
|
45
|
|
|
$this->buildRowSet($data); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $this->rowSet; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Добавление колонок в таблицу |
|
53
|
|
|
* @param array | Traversable $columns |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setColumns($columns) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->setColumsChanged(true); |
|
59
|
|
|
return parent::setColumns($columns); |
|
60
|
|
|
} |
|
61
|
|
|
/** |
|
62
|
|
|
* Добавление колонки в таблицу |
|
63
|
|
|
* @param ColumnInterface|array|ArrayAccess $column |
|
64
|
|
|
* @return $this |
|
65
|
|
|
* @throws Column\Exception\InvalidColumnException |
|
66
|
|
|
* @throws Exception\InvalidArgumentException |
|
67
|
|
|
* @throws Exception\RuntimeException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function add($column) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->setColumsChanged(true); |
|
72
|
|
|
return parent::add($column); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Создает из данных адаптера rowSet |
|
78
|
|
|
* @param array | ArrayAccess $data |
|
79
|
|
|
* @return $this |
|
80
|
|
|
* @throws Exception\RuntimeException |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function buildRowSet($data) |
|
83
|
|
|
{ |
|
84
|
|
|
if (!is_array($data) && $data instanceof Traversable) { |
|
85
|
|
|
throw new Exception\RuntimeException( |
|
86
|
|
|
sprintf('Данные должны быть массивом или %s', ArrayAccess::class) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
$columns = $this->getColumns(); |
|
90
|
|
|
|
|
91
|
|
|
foreach ($data as $item) { |
|
92
|
|
|
$mutators = $this->getMutators(); |
|
93
|
|
|
$item = $this->mutate($mutators, $item); |
|
|
|
|
|
|
94
|
|
|
/** @var ColumnInterface $column */ |
|
95
|
|
|
foreach ($columns as $column) { |
|
96
|
|
|
$columnName = $column->getName(); |
|
97
|
|
|
$mutators = $column->getMutators(); |
|
|
|
|
|
|
98
|
|
|
if (array_key_exists($columnName, $item)) { |
|
99
|
|
|
$item = $this->mutate($mutators, $item, $columnName); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
$this->rowSet[] = new Row($item); |
|
103
|
|
|
} |
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Метод непосредственно осуществляет мутацию данных |
|
109
|
|
|
* @param array $mutators |
|
110
|
|
|
* @param array | Row $row |
|
111
|
|
|
* @param string $name |
|
|
|
|
|
|
112
|
|
|
* @return mixed |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function mutate(array $mutators, $row, $name = null) |
|
115
|
|
|
{ |
|
116
|
|
|
/** @var MutatorInterface $mutator */ |
|
117
|
|
|
foreach ($mutators as $mutator) { |
|
118
|
|
|
if ($mutator instanceof MutatorInterface) { |
|
119
|
|
|
$mutator->setRowData($row); |
|
120
|
|
|
if ($mutator->validate()) { |
|
121
|
|
|
if ($name) { |
|
|
|
|
|
|
122
|
|
|
$row[$name] = $mutator->change($row[$name]); |
|
123
|
|
|
} else { |
|
124
|
|
|
$row = $mutator->change($row); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
return $row; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param array $rowSet |
|
134
|
|
|
* @return $this |
|
135
|
|
|
*/ |
|
136
|
|
|
public function setRowSet($rowSet) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->rowSet = $rowSet; |
|
139
|
|
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Функция инициализации колонок |
|
144
|
|
|
* @return void |
|
145
|
|
|
*/ |
|
146
|
|
|
public function init() |
|
147
|
|
|
{ |
|
148
|
|
|
} |
|
149
|
|
|
/** |
|
150
|
|
|
* Возвращает коллекцию колонок |
|
151
|
|
|
* @return array | Traversable |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getColumns() |
|
154
|
|
|
{ |
|
155
|
|
|
$colums = parent::getColumns(); |
|
156
|
|
|
if ($this->getUserHiddenColums() && $this->isColumsChanged()) { |
|
157
|
|
|
$newColums = []; |
|
|
|
|
|
|
158
|
|
|
$addedColums = []; |
|
159
|
|
|
foreach ($this->getUserHiddenColums() as $i => $userColum) { |
|
160
|
|
|
/** @var ColumnInterface $colum */ |
|
161
|
|
|
if (!empty($userColum['n']) && $colum = $this->get($userColum['n'])) { |
|
162
|
|
|
$addedColums[$userColum['n']] = true; |
|
163
|
|
|
$colum->setOrder($i); |
|
164
|
|
|
if (!empty($userColum['h'])) { |
|
165
|
|
|
$colum->setAttribute('hidden', (bool)$userColum['h']); |
|
166
|
|
|
} |
|
167
|
|
|
$newColums[] = $colum; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
$i++; |
|
|
|
|
|
|
171
|
|
|
/** @var ColumnInterface $colum */ |
|
172
|
|
|
foreach ($colums as $colum) { |
|
173
|
|
|
if (empty($addedColums[$colum->getName()])) { |
|
174
|
|
|
$colum->setOrder($i); |
|
175
|
|
|
$newColums[] = $colum; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
return $newColums; |
|
179
|
|
|
} |
|
180
|
|
|
return $colums; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* получить коллекцию колонок таблицы скрытых/показаннах пользователем |
|
186
|
|
|
* @return array|Traversable |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getUserHiddenColums() |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->userHiddenColums; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* установить коллекцию колонок таблицы скрытых/показаннах пользователем |
|
195
|
|
|
* @param array|Traversable $userHiddenColums |
|
196
|
|
|
* @return $this |
|
197
|
|
|
*/ |
|
198
|
|
|
public function setUserHiddenColums($userHiddenColums) |
|
199
|
|
|
{ |
|
200
|
|
|
$this->setColumsChanged(true); |
|
201
|
|
|
$this->userHiddenColums = $userHiddenColums; |
|
|
|
|
|
|
202
|
|
|
return $this; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @return boolean |
|
207
|
|
|
*/ |
|
208
|
|
|
public function isColumsChanged() |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->columsChanged; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param boolean $columsChanged |
|
215
|
|
|
* @return $this |
|
216
|
|
|
*/ |
|
217
|
|
|
public function setColumsChanged($columsChanged) |
|
218
|
|
|
{ |
|
219
|
|
|
$this->columsChanged = $columsChanged; |
|
220
|
|
|
return $this; |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
|
|
|
|
|
223
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.