Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
gridcomponent.php
1
<?php
2
3
namespace
Bitrix\Iblock\Component\Grid
;
4
5
use
Bitrix\Iblock\Integration\UI\Grid\General\BaseProvider
;
6
use
Bitrix\Main\AccessDeniedException
;
7
use
Bitrix\Main\Context
;
8
use
Bitrix\Main\Filter\Filter
;
9
use
Bitrix\Main\Grid\MessageType
;
10
use
Bitrix\Main\Grid\Options
;
11
use
Bitrix\Main\HttpRequest
;
12
use
Bitrix\Main\Localization\Loc
;
13
use
Bitrix\Main\Result
;
14
use
Bitrix\Main\UI\PageNavigation
;
15
use
Bitrix\Main\Web\Json
;
16
use
Bitrix\Main\Web\PostDecodeFilter
;
17
use CBitrixComponent;
18
use CMain;
19
use Iterator;
20
use Throwable;
21
25
abstract
class
GridComponent
extends
CBitrixComponent
26
{
27
private
array $rowsFilter;
28
private
Options
$gridOptions;
29
30
protected
PageNavigation
$pagination
;
31
39
abstract
protected
function
getRawRows
(array $params);
40
46
abstract
protected
function
getFilter
(): ?
Filter
;
47
53
abstract
protected
function
getTotalCount
(): int;
54
60
abstract
protected
function
getGridProvider
():
BaseProvider
;
61
67
protected
function
getGridOptions
():
Options
68
{
69
$this->gridOptions ??=
new
Options
($this->
getGridProvider
()->getId());
70
71
return
$this->gridOptions;
72
}
73
81
protected
function
init
(): void
82
{
83
}
84
90
protected
function
initRows
(): void
91
{
92
$params = [
93
'select'
=> $this->
getRowsSelect
(),
94
'filter'
=> $this->
getRowsFilter
(),
95
'order'
=> $this->
getRowsSorting
(),
96
];
97
98
$pagination = $this->
getRowsPagination
();
99
if
($pagination)
100
{
101
$params[
'limit'
] =
$pagination
->
getLimit
();
102
$params[
'offset'
] =
$pagination
->
getOffset
();
103
}
104
105
$rows = [];
106
foreach
($this->
getRawRows
($params) as $row)
107
{
108
$rows[] = $this->
prepareRow
($row);
109
}
110
111
$this->
getGridProvider
()->setRows($rows);
112
113
if
(
$pagination
)
114
{
115
$this->
getGridProvider
()->setNavObject($pagination);
116
}
117
}
118
126
protected
function
prepareRow
(array $row): array
127
{
128
return
$this->
getGridProvider
()->prepareRow($row);
129
}
130
136
protected
function
initResult
(): void
137
{
138
$this->arResult[
'GRID'
] = $this->
getGridProvider
()->toArray();
139
}
140
146
protected
function
initToolbar
(): void
147
{
148
}
149
153
public
function
executeComponent
()
154
{
155
try
156
{
157
$this->
init
();
158
159
if
(!$this->
checkReadPermissions
())
160
{
161
throw
new
AccessDeniedException
();
162
}
163
164
$this->
processActionsGrid
();
165
166
$this->
initRows
();
167
$this->
initResult
();
168
$this->
initToolbar
();
169
170
$this->includeComponentTemplate();
171
}
172
catch
(
AccessDeniedException
$e)
173
{
174
$this->arResult[
'ERROR'
] =
Loc::getMessage
(
'IBLOCK_GRID_COMPONENT_ERROR_ACCESS_DENIED'
);
175
$this->includeComponentTemplate(
'error'
);
176
}
177
}
178
184
protected
function
checkReadPermissions
(): bool
185
{
186
return
true
;
187
}
188
194
final
protected
function
getRowsFilter
(): array
195
{
196
if
(isset($this->rowsFilter))
197
{
198
return
$this->rowsFilter;
199
}
200
201
$filterObject = $this->
getFilter
();
202
if
($filterObject)
203
{
204
$this->rowsFilter = $filterObject->getValue();
205
}
206
else
207
{
208
$this->rowsFilter = [];
209
}
210
211
$additionalFilter = $this->
getAdditionalRowsFilter
();
212
if
(isset($additionalFilter))
213
{
214
$this->rowsFilter[] = $additionalFilter;
215
}
216
217
return
$this->rowsFilter;
218
}
219
220
protected
function
getAdditionalRowsFilter
(): ?array
221
{
222
return
null
;
223
}
224
230
protected
function
getRowsSelect
(): array
231
{
232
$columns = array_column($this->
getGridProvider
()->getColumns(),
'id'
);
233
234
return
array_filter($columns);
235
}
236
242
protected
function
getRowsSorting
(): array
243
{
244
return
$this->
getGridOptions
()->getSorting()[
'sort'
];
245
}
246
252
protected
function
getRowsPagination
(): ?
PageNavigation
253
{
254
if
(!isset($this->pagination))
255
{
256
$this->pagination =
new
PageNavigation
(
'page'
);
257
$this->pagination->setPageSizes(
258
$this->
getPageSizes
()
259
);
260
$this->pagination->setRecordCount(
261
$this->
getTotalCount
()
262
);
263
264
$this->initFromGrid($this->pagination);
265
$this->pagination->initFromUri();
266
}
267
268
return
$this->pagination
;
269
}
270
276
protected
function
getPageSizes
(): array
277
{
278
$gridSizes = $this->
getGridProvider
()->getPageSizes();
279
$gridSizes = array_column($gridSizes,
'VALUE'
);
280
281
\Bitrix\Main\Type\Collection::normalizeArrayValuesByInt($gridSizes);
282
283
return
$gridSizes;
284
}
285
293
private
function
initFromGrid(
PageNavigation
$pagination
): void
294
{
295
$params = $this->
getGridOptions
()->GetNavParams();
296
$pagination->
setPageSize
((
int
)$params[
'nPageSize'
]);
297
}
298
304
protected
function
processActionsGrid
(): void
305
{
306
$request =
Context::getCurrent
()->getRequest();
307
$request->addFilter(
new
PostDecodeFilter
);
308
309
$actionName =
'action_button_'
. $this->
getGridProvider
()->getId();
310
$action = $request->getPost($actionName);
311
312
if
(isset($action))
313
{
314
try
315
{
316
$result = $this->
processActionGrid
($action, $request);
317
if
(isset($result) && !$result->isSuccess())
318
{
319
$this->sendErrorsResponse(
320
$result->getErrorMessages()
321
);
322
}
323
}
324
catch
(
AccessDeniedException
$e)
325
{
326
$this->sendErrorsResponse([
327
Loc::getMessage
(
'IBLOCK_GRID_COMPONENT_ERROR_ACCESS_DENIED'
)
328
]);
329
}
330
catch
(Throwable $e)
331
{
332
$this->sendErrorsResponse([
333
$e->getMessage()
334
]);
335
}
336
}
337
}
338
346
protected
function
sendErrorsResponse(array $messages): void
347
{
348
global $APPLICATION;
349
354
$APPLICATION->RestartBuffer();
355
356
foreach
($messages as &$message)
357
{
358
if
(is_array($message) && isset($message[
'TEXT'
]))
359
{
360
$message = [
361
'TYPE'
=> $message[
'TYPE'
] ??
MessageType::ERROR
,
362
'TEXT'
=> $message[
'TEXT'
],
363
];
364
}
365
else
366
{
367
$message = [
368
'TYPE'
=>
MessageType::ERROR
,
369
'TEXT'
=> (string)$message,
370
];
371
}
372
}
373
unset($message);
374
375
CMain::FinalActions(
376
Json::encode([
377
'messages'
=> $messages,
378
])
379
);
380
}
381
390
protected
function
processActionGrid
(
string
$actionName,
HttpRequest
$request): ?
Result
391
{
392
return
null
;
393
}
394
}
Bitrix\Iblock\Component\Grid\GridComponent
Definition
gridcomponent.php:26
Bitrix\Iblock\Component\Grid\GridComponent\getPageSizes
getPageSizes()
Definition
gridcomponent.php:276
Bitrix\Iblock\Component\Grid\GridComponent\initToolbar
initToolbar()
Definition
gridcomponent.php:146
Bitrix\Iblock\Component\Grid\GridComponent\getFilter
getFilter()
Bitrix\Iblock\Component\Grid\GridComponent\getRowsSelect
getRowsSelect()
Definition
gridcomponent.php:230
Bitrix\Iblock\Component\Grid\GridComponent\getAdditionalRowsFilter
getAdditionalRowsFilter()
Definition
gridcomponent.php:220
Bitrix\Iblock\Component\Grid\GridComponent\init
init()
Definition
gridcomponent.php:81
Bitrix\Iblock\Component\Grid\GridComponent\getTotalCount
getTotalCount()
Bitrix\Iblock\Component\Grid\GridComponent\getGridOptions
getGridOptions()
Definition
gridcomponent.php:67
Bitrix\Iblock\Component\Grid\GridComponent\getGridProvider
getGridProvider()
Bitrix\Iblock\Component\Grid\GridComponent\getRowsPagination
getRowsPagination()
Definition
gridcomponent.php:252
Bitrix\Iblock\Component\Grid\GridComponent\prepareRow
prepareRow(array $row)
Definition
gridcomponent.php:126
Bitrix\Iblock\Component\Grid\GridComponent\getRowsSorting
getRowsSorting()
Definition
gridcomponent.php:242
Bitrix\Iblock\Component\Grid\GridComponent\$pagination
PageNavigation $pagination
Definition
gridcomponent.php:30
Bitrix\Iblock\Component\Grid\GridComponent\processActionsGrid
processActionsGrid()
Definition
gridcomponent.php:304
Bitrix\Iblock\Component\Grid\GridComponent\initRows
initRows()
Definition
gridcomponent.php:90
Bitrix\Iblock\Component\Grid\GridComponent\processActionGrid
processActionGrid(string $actionName, HttpRequest $request)
Definition
gridcomponent.php:390
Bitrix\Iblock\Component\Grid\GridComponent\getRowsFilter
getRowsFilter()
Definition
gridcomponent.php:194
Bitrix\Iblock\Component\Grid\GridComponent\executeComponent
executeComponent()
Definition
gridcomponent.php:153
Bitrix\Iblock\Component\Grid\GridComponent\checkReadPermissions
checkReadPermissions()
Definition
gridcomponent.php:184
Bitrix\Iblock\Component\Grid\GridComponent\getRawRows
getRawRows(array $params)
Bitrix\Iblock\Component\Grid\GridComponent\initResult
initResult()
Definition
gridcomponent.php:136
Bitrix\Iblock\Integration\UI\Grid\General\BaseProvider
Definition
BaseProvider.php:15
Bitrix\Main\AccessDeniedException
Definition
exception.php:214
Bitrix\Main\Context\getCurrent
static getCurrent()
Definition
context.php:241
Bitrix\Main\Filter\Filter
Definition
filter.php:5
Bitrix\Main\Grid\MessageType
Definition
messagetype.php:11
Bitrix\Main\Grid\MessageType\ERROR
const ERROR
Definition
messagetype.php:13
Bitrix\Main\Grid\Options
Definition
options.php:13
Bitrix\Main\HttpRequest
Definition
httprequest.php:20
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\ORM\Data\Result
Definition
result.php:16
Bitrix\Main\UI\PageNavigation
Definition
pagenavigation.php:27
Bitrix\Main\UI\PageNavigation\getLimit
getLimit()
Definition
pagenavigation.php:247
Bitrix\Main\UI\PageNavigation\setPageSize
setPageSize($n)
Definition
pagenavigation.php:134
Bitrix\Main\UI\PageNavigation\getOffset
getOffset()
Definition
pagenavigation.php:234
Bitrix\Main\Web\Json
Definition
json.php:11
Bitrix\Main\Web\PostDecodeFilter
Definition
postdecodefilter.php:10
Bitrix\Iblock\Component\Grid
Definition
gridcomponent.php:3
Bitrix\Main\Context
Definition
culture.php:9
modules
iblock
lib
component
grid
gridcomponent.php
Создано системой
1.10.0