Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
providerwithuserfieldstrait.php
1<?php
2
4
7use CUserTypeManager;
8
17trait ProviderWithUserFieldsTrait
18{
19 protected $userFieldInfos = null;
20
26 abstract public function getUfEntityId(): string;
27
33 abstract public function getUfPrefix(): string;
34
40 private function getUfDispatcher(): Dispatcher
41 {
42 return Dispatcher::instance();
43 }
44
50 private function getUfTypeManager(): CUserTypeManager
51 {
52 return UserFieldHelper::getInstance()->getManager();
53 }
54
62 public function getFields(): array
63 {
64 $fields = parent::getFields();
65 $fields += $this->getUfComponentFields();
66
67 return $fields;
68 }
69
75 protected function getUfComponentFields(): array
76 {
77 $fields = [];
78
79 $fields['ENABLE_USER_FIELD_CREATION'] = true;
80 $fields['ENABLE_USER_FIELD_MANDATORY_CONTROL'] = true;
81 $fields['USER_FIELD_ENTITY_ID'] = $this->getUfEntityId();
82 $fields['USER_FIELD_PREFIX'] = $this->getUfPrefix();
83 $fields['USER_FIELD_CREATE_SIGNATURE'] = $this->getUfDispatcher()->getCreateSignature([
84 'ENTITY_ID' => $this->getUfEntityId(),
85 ]);
86 $fields['USER_FIELD_CREATE_PAGE_URL'] = null;
87
88 return $fields;
89 }
90
98 protected function getUfEntityFields(): array
99 {
100 if (!is_null($this->userFieldInfos))
101 {
102 return $this->userFieldInfos;
103 }
104
105 $result = [];
106
107 $userFieldsInfos = $this->getUfTypeManager()->GetUserFields(
108 $this->getUfEntityId(),
109 0,
110 LANGUAGE_ID
111 );
112 foreach ($userFieldsInfos as $userFieldInfo)
113 {
114 $fieldName = $userFieldInfo['FIELD_NAME'];
115 $fieldInfo = [
116 'USER_TYPE_ID' => $userFieldInfo['USER_TYPE_ID'],
117 'ENTITY_ID' => $this->getUfEntityId(),
118 'ENTITY_VALUE_ID' => $this->getEntityId() ?? 0,
119 'FIELD' => $fieldName,
120 'MULTIPLE' => $userFieldInfo['MULTIPLE'],
121 'MANDATORY' => $userFieldInfo['MANDATORY'],
122 'SETTINGS' => $userFieldInfo['SETTINGS'] ?? null,
123 ];
124
125 // required for the enum fields to work on mobile
126 if ($fieldInfo['USER_TYPE_ID'] === 'enumeration')
127 {
128 $fieldInfo['ENUM'] = [];
129 $enumDbResult = \CUserFieldEnum::GetList(
130 [],
131 [
132 'USER_FIELD_ID' => $userFieldInfo['ID'],
133 ]
134 );
135 while ($enum = $enumDbResult->Fetch())
136 {
137 $fieldInfo['ENUM'][] = [
138 'ID' => $enum['ID'],
139 'VALUE' => $enum['VALUE'],
140 ];
141 }
142 }
143
144 $result[$fieldName] = [
145 'name' => $fieldName,
146 'title' => $userFieldInfo['EDIT_FORM_LABEL'] ?? $fieldName,
147 'type' => 'userField',
148 'data' => ['fieldInfo' => $fieldInfo],
149 'editable' => $userFieldInfo['EDIT_IN_LIST'] === 'Y',
150 'required' => isset($userFieldInfo['MANDATORY']) && $userFieldInfo['MANDATORY'] === 'Y',
151 ];
152 }
153
154 $this->userFieldInfos = $result;
155 return $result;
156 }
157
182 protected function fillUfEntityFields(array $fields): array
183 {
184 array_push($fields, ...array_values($this->getUfEntityFields()));
185
186 return $fields;
187 }
188
210 protected function fillUfEntityData(array $entityData): array
211 {
212 return array_merge($entityData, $this->getUfEntityData());
213 }
214
222 protected function getUfEntityData(): array
223 {
224 $userFields =
225 $this
226 ->getUfTypeManager()
227 ->GetUserFields(
228 $this->getUfEntityId(),
229 $this->getEntityId() ?? 0,
230 LANGUAGE_ID
231 )
232 ;
233 $userFieldInfos = $this->getUfEntityFields();
234
235 $userFieldValues = [];
236 foreach($userFields as $fieldName => $userField)
237 {
238 $fieldValue = $userField['VALUE'] ?? '';
239 $fieldData = $userFieldInfos[$fieldName] ?? null;
240
241 if (!is_array($fieldData))
242 {
243 continue;
244 }
245
246 $isEmptyField = true;
247 $fieldParams = $fieldData['data']['fieldInfo'];
248 if (
249 (is_string($fieldValue) && $fieldValue !== '')
250 || (is_array($fieldValue) && !empty($fieldValue))
251 )
252 {
253 $fieldParams['VALUE'] = $fieldValue;
254 $isEmptyField = false;
255 }
256
257 $fieldSignature = $this->getUfDispatcher()->getSignature($fieldParams);
258 if ($isEmptyField)
259 {
260 $userFieldValues[$fieldName] = array(
261 'SIGNATURE' => $fieldSignature,
262 'IS_EMPTY' => true
263 );
264 }
265 else
266 {
267 $userFieldValues[$fieldName] = array(
268 'VALUE' => $fieldValue,
269 'SIGNATURE' => $fieldSignature,
270 'IS_EMPTY' => false
271 );
272 }
273 }
274
275 return $userFieldValues;
276 }
277}