Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
columns.php
1<?php
2
4
13class Columns implements \IteratorAggregate, \Countable
14{
18 private array $providers;
19 private array $columns;
20 private array $providersColumns;
21
25 public function __construct(DataProvider ...$providers)
26 {
27 $this->providers = [];
28 foreach ($providers as $provider)
29 {
30 $this->providers[] = $provider;
31 }
32 }
33
39 final public function getIterator(): \ArrayIterator
40 {
41 return new \ArrayIterator($this->getColumns());
42 }
43
49 final public function count(): int
50 {
51 return count($this->getColumns());
52 }
53
61 final public function getColumn(string $id): ?Column
62 {
63 return $this->getColumns()[$id] ?? null;
64 }
65
80 final public function getColumns(): array
81 {
82 if (!isset($this->columns))
83 {
84 $this->columns = [];
85
86 foreach ($this->getProvidersColumns() as $providerColumns)
87 {
91 foreach ($providerColumns as $column)
92 {
93 $this->columns[$column->getId()] = $column;
94 }
95 }
96 }
97
98 return $this->columns;
99 }
100
106 final protected function getProvidersColumns(): array
107 {
108 if (!isset($this->providersColumns))
109 {
110 $this->providersColumns = [];
111
112 foreach ($this->providers as $provider)
113 {
114 $providerColumns = $provider->prepareColumns();
115 if (empty($providerColumns))
116 {
117 continue;
118 }
119
120 $providerClass = get_class($provider);
121 $this->providersColumns[$providerClass] = $providerColumns;
122 }
123 }
124
125 return $this->providersColumns;
126 }
127
177 public function getSelect(?array $columnIds = null, ?array $providers = null): array
178 {
179 $result = [];
180
181 // filter by providers
182 if (isset($providers))
183 {
184 $columns = [];
185 foreach ($providers as $providerClass)
186 {
187 $providerColumns = $this->getProvidersColumns()[$providerClass] ?? null;
188 if (!empty($providerColumns))
189 {
190 array_push($columns, ...$providerColumns);
191 }
192 }
193
194 if (empty($columns))
195 {
196 return [];
197 }
198 }
199 else
200 {
201 $columns = $this->getColumns();
202 }
203
204 // filter by columns
205 if (isset($columnIds))
206 {
207 $columnIds = array_fill_keys($columnIds, true);
208 foreach ($columns as $column)
209 {
210 if (
211 $column->isNecessary()
212 || isset($columnIds[$column->getId()])
213 )
214 {
215 array_push($result, ...$column->getSelect());
216 }
217 }
218 }
219 else
220 {
221 foreach ($columns as $column)
222 {
223 array_push($result, ...$column->getSelect());
224 }
225 }
226
227 return $result;
228 }
229
237 public function prepareEditableColumnsValues(array $values): array
238 {
239 $result = [];
240
241 foreach ($this->getColumns() as $column)
242 {
243 if (!$column->isEditable())
244 {
245 continue;
246 }
247
248 $id = $column->getId();
249 if (array_key_exists($id, $values))
250 {
251 $result[$id] = $values[$id];
252 }
253 }
254
255 return $result;
256 }
257}
prepareEditableColumnsValues(array $values)
Definition columns.php:237
__construct(DataProvider ... $providers)
Definition columns.php:25
getSelect(?array $columnIds=null, ?array $providers=null)
Definition columns.php:177