Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
deleteelementitem.php
1<?php
2
4
11use CIBlockElement;
12use CMain;
13use CUtil;
14
15final class DeleteElementItem extends BaseItem
16{
17 private IblockRightsChecker $rights;
18
19 public static function getId(): ?string
20 {
21 return 'delete_element';
22 }
23
24 public function __construct(IblockRightsChecker $rights)
25 {
26 $this->rights = $rights;
27 }
28
29 protected function getText(): string
30 {
31 return Loc::getMessage('IBLOCK_GRID_ROW_ACTIONS_DELETE_ELEMENT_NAME');
32 }
33
34 public function getControl(array $rawFields): ?array
35 {
36 $id = (int)($rawFields['ID'] ?? 0);
37 if ($id <= 0)
38 {
39 return null;
40 }
41
42 $actionId = self::getId();
43 $data = CUtil::PhpToJSObject([
44 'id' => $id,
45 ]);
46 $confirmMessage = \CUtil::JSEscape(
47 Loc::getMessage('IBLOCK_GRID_ROW_ACTIONS_DELETE_ELEMENT_CONFIRM_MESSAGE')
48 );
49
50 $confirmButtonMessage = \CUtil::JSEscape(
51 Loc::getMessage('IBLOCK_GRID_ROW_ACTIONS_DELETE_ELEMENT_CONFIRM_BUTTON')
52 );
53 $backButtonMessage = \CUtil::JSEscape(
54 Loc::getMessage('IBLOCK_GRID_ROW_ACTIONS_DELETE_ELEMENT_BACK_BUTTON')
55 );
56
57 $this->onclick = "IblockGridInstance.sendSmallPopupWithConfirm('{$actionId}', {$data}, '{$confirmMessage}', '{$confirmButtonMessage}', '{$backButtonMessage}')";
58
59 return parent::getControl($rawFields);
60 }
61
62 public function processRequest(HttpRequest $request): ?Result
63 {
64 global $APPLICATION;
65
70 $id = $request->getPost('id');
71 if (empty($id) || !is_numeric($id))
72 {
73 return null;
74 }
75 $id = (int)$id;
76
77 if (!$this->rights->canDeleteElement($id))
78 {
79 throw new AccessDeniedException('Cannot delete element');
80 }
81
82 $result = new Result();
83
84 $updateResult = CIBlockElement::Delete($id);
85 if (!$updateResult)
86 {
87 $message = (string)$APPLICATION->GetException() ?: 'Cannot delete element';
88 $result->addError(new Error($message));
89 }
90
91 return $result;
92 }
93}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
processRequest(HttpRequest $request)