Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
BaseProvider.php
1<?php
2
4
7use CFile;
8
14abstract class BaseProvider
15{
16 private array $rows;
17 private ?PageNavigation $pagination = null;
18
24 abstract public function getId(): string;
25
31 abstract public function getColumns(): array;
32
40 protected function getTemplateRow(): ?array
41 {
42 return null;
43 }
44
56 public function setRows(array $rows): void
57 {
58 $this->rows = $rows;
59 }
60
66 protected function getRows(): array
67 {
68 $result = $this->rows ?? [];
69
70 $templateRow = $this->getTemplateRow();
71 if (isset($templateRow))
72 {
73 $templateRowId = 'template_0';
74
75 $isHasTemplateRow = false;
76 foreach ($result as $row)
77 {
78 if ($row['id'] === $templateRowId)
79 {
80 $isHasTemplateRow = true;
81
82 break;
83 }
84 }
85
86 if (!$isHasTemplateRow)
87 {
88 $templateRow['id'] = $templateRowId;
89 array_unshift($result, $templateRow);
90 }
91 }
92
93 return $result;
94 }
95
103 public function setNavObject(PageNavigation $pagination): void
104 {
105 $this->pagination = $pagination;
106 }
107
113 protected function getNavObject(): ?PageNavigation
114 {
115 return $this->pagination;
116 }
117
125 public function getPageSizes(): array
126 {
128 }
129
135 protected function getDefaultPageSize(): int
136 {
137 $sizes = $this->getPageSizes();
138 $sizesCount = count($sizes);
139
140 if ($sizesCount > 0)
141 {
142 $middleIndex = ceil($sizesCount / 2) - 1;
143 if (isset($sizes[$middleIndex]))
144 {
145 return (int)$sizes[$middleIndex]['VALUE'];
146 }
147 }
148
149 return 0;
150 }
151
157 protected function getDefaultSort(): ?array
158 {
159 return null;
160 }
161
167 protected function getActionPanel(): ?array
168 {
169 $snippet = new Snippet();
170
171 $items = [];
172 $items[] = $snippet->getRemoveButton();
173 $items[] = $snippet->getEditButton();
174
175 if ($items)
176 {
177 return [
178 'GROUPS' => [
179 [
180 'ITEMS' => $items,
181 ],
182 ],
183 ];
184 }
185
186 return null;
187 }
188
194 protected function getAjaxId(): string
195 {
196 return $this->getId();
197 }
198
204 protected function getRowIdColumn(): string
205 {
206 return 'ID';
207 }
208
216 public function prepareRow(array $rawRow): array
217 {
218 $isEditable = $this->isEditable($rawRow);
219
220 return [
221 'id' => $rawRow[$this->getRowIdColumn()],
222 'data' => $rawRow,
223 'editable' => $isEditable,
224 'columns' => $this->getRowColumns($rawRow),
225 'actions' => $this->getRowActions($rawRow, $isEditable),
226 ];
227 }
228
236 protected function isEditable(array $row): bool
237 {
238 return true;
239 }
240
250 protected function getRowColumns(array $row): array
251 {
252 $result = [];
253
254 $columns = $this->getColumns();
255 foreach ($columns as $column)
256 {
257 $colName = $column['id'] ?? null;
258 if (!isset($colName))
259 {
260 continue;
261 }
262
263 $type = $column['type'] ?? null;
264 if ($type === 'list')
265 {
266 $value = $row[$colName] ?? null;
267
268 $items = $column['editable']['items'] ?? null;
269 if (is_array($items))
270 {
271 $value = $items[$value] ?? null;
272 }
273
274 $result[$colName] = $value;
275 }
276 elseif ($type === 'image')
277 {
278 $value = null;
279
280 $fileId = (int)($row[$colName] ?? null);
281 if ($fileId > 0)
282 {
283 $value = CFile::GetPath($fileId);
284 }
285
286 $result[$colName] = $value;
287 }
288 else
289 {
290 $result[$colName] = $row[$colName] ?? null;
291 }
292 }
293
294 return $result;
295 }
296
304 public function cleanFields(array $fields): array
305 {
306 $availableFields = array_column($this->getColumns(), 'id');
307
308 return array_filter(
309 $fields,
310 static fn($key) => isset($key) && in_array($key, $availableFields, true),
311 ARRAY_FILTER_USE_KEY
312 );
313 }
314
323 protected function getRowActions(array $row, bool $isEditable): array
324 {
325 return [];
326 }
327
335 public function toArray(): array
336 {
337 $pagination = $this->getNavObject();
338 $actionPanel = $this->getActionPanel();
339 $sort = $this->getDefaultSort();
340
341 return [
342 // general
343 'GRID_ID' => $this->getId(),
344 // rows
345 'ROWS' => $this->getRows(),
346 'COLUMNS' => $this->getColumns(),
347 'SORT' => $sort,
348 // navigation
349 'NAV_OBJECT' => $pagination,
350 'NAV_PARAM_NAME' => $pagination ? $pagination->getId() : null,
351 'CURRENT_PAGE' => $pagination ? $pagination->getCurrentPage() : null,
352 'TOTAL_ROWS_COUNT' => $pagination ? $pagination->getRecordCount() : 0,
353 'SHOW_PAGINATION' => $pagination ? $pagination->getPageCount() > 1 : false,
354 'SHOW_NAVIGATION_PANEL' => isset($pagination),
355 'SHOW_PAGESIZE' => isset($pagination),
356 'PAGE_SIZES' => $this->getPageSizes(),
357 'DEFAULT_PAGE_SIZE' => $this->getDefaultPageSize(),
358 // ajax
359 'AJAX_ID' => $this->getAjaxId(),
360 'AJAX_MODE' => 'Y',
361 'AJAX_OPTION_JUMP' => 'N',
362 'AJAX_OPTION_STYLE' => 'N',
363 'AJAX_OPTION_HISTORY' => 'N',
364 // actions
365 'ACTION_PANEL' => $actionPanel,
366 'SHOW_ACTION_PANEL' => isset($actionPanel),
367 // allows & shows
368 'ALLOW_ROWS_SORT' => isset($sort),
369 'SHOW_ROW_CHECKBOXES' => isset($actionPanel),
370 'SHOW_CHECK_ALL_CHECKBOXES' => isset($actionPanel),
371 'HANDLE_RESPONSE_ERRORS' => true,
372 ];
373 }
374}