Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
BaseUserFieldProvider.php
1<?php
2
4
12
13abstract class BaseUserFieldProvider extends BaseProvider
14{
15 private array $userField;
16 protected ?array $list = null;
17
18 public function __construct(array $options = [])
19 {
20 parent::__construct();
21
22 $fieldInfo = $options['fieldInfo'] ?? [];
23
24 if (
25 empty($fieldInfo)
26 || !is_array($fieldInfo)
27 || empty($fieldInfo['ENTITY_ID'])
28 || empty($fieldInfo['FIELD'])
29 )
30 {
31 throw new ArgumentException('Option "fieldInfo" is must be a valid array.');
32 }
33
34 if (!$this->validateSignature($fieldInfo))
35 {
36 throw new ArgumentException('Invalid signature.');
37 }
38
39 $this->userField = $this->loadUserField($fieldInfo);
40 if (empty($this->userField))
41 {
42 throw new ArgumentException('User field not found.');
43 }
44 }
45
46 private function validateSignature(array $fieldInfo): bool
47 {
48 $signature = $fieldInfo['SIGNATURE'] ?? '';
49 $signatureManager = new SignatureManager();
50
51 return SignatureHelper::validateSignature($signatureManager, $fieldInfo, $signature);
52 }
53
54 private function loadUserField(array $fieldInfo): array
55 {
56 $userFieldResult = \CUserTypeEntity::GetList([], [
57 'ENTITY_ID' => $fieldInfo['ENTITY_ID'],
58 'FIELD_NAME' => $fieldInfo['FIELD'],
59 ]);
60
61 return $userFieldResult->Fetch();
62 }
63
64 protected function getUserField(): array
65 {
66 return $this->userField;
67 }
68
69 abstract protected function getEntityId(): string;
70
71 public function isAvailable(): bool
72 {
73 global $USER;
74
75 if (!$USER->isAuthorized())
76 {
77 return false;
78 }
79
80 return true;
81 }
82
83 private function getEnumList(): array
84 {
85 $result = [];
86
87 $this->fillEnumList();
88
89 foreach ($this->list as $id => $name)
90 {
91 if (!empty($id))
92 {
93 $result[] = [
94 'ID' => $id,
95 'NAME' => $name,
96 ];
97 }
98 }
99
100 return $result;
101 }
102
103 protected function fillEnumList(): void
104 {
105 if ($this->list === null)
106 {
107 $userField = $this->getUserField();
108 $this->getEnumTypeClass()::getEnumList($userField);
109
110 $this->list = $userField['USER_TYPE']['FIELDS'];
111 }
112 }
113
117 abstract protected function getEnumTypeClass(): string;
118
119 public function getItems(array $ids): array
120 {
121 $result = [];
122
123 foreach ($this->getEnumList() as $userFieldItem)
124 {
125 $result[] = $this->makeItem($userFieldItem);
126 }
127
128 return $result;
129 }
130
131 public function getSelectedItems(array $ids): array
132 {
133 return $this->getItems($ids);
134 }
135
136 public function fillDialog(Dialog $dialog): void
137 {
138 if ($dialog->getItemCollection()->count() > 0)
139 {
140 foreach ($dialog->getItemCollection() as $item)
141 {
142 $dialog->addRecentItem($item);
143 }
144 }
145
146 foreach ($this->getEnumList() as $item)
147 {
148 $dialog->addRecentItem(
149 $this->makeItem($item)
150 );
151 }
152 }
153
154 protected function makeItem(array $item): Item
155 {
156 return new Item([
157 'id' => $item['ID'],
158 'entityId' => $this->getEntityId(),
159 'title' => $item['NAME'],
160 ]);
161 }
162}
static validateSignature(SignatureManager $signatureManager, array $fieldParam, $signature)