Bitrix-D7 22.6
 
Загрузка...
Поиск...
Не найдено
providerwithuserfieldstrait.php
1<?php
2
4
7use CUserTypeManager;
8
18{
24 abstract public function getUfEntityId(): string;
25
31 private function getUfDispatcher(): Dispatcher
32 {
33 return Dispatcher::instance();
34 }
35
41 private function getUfTypeManager(): CUserTypeManager
42 {
43 return UserFieldHelper::getInstance()->getManager();
44 }
45
53 public function getFields(): array
54 {
55 $fields = parent::getFields();
56 $fields += $this->getUfComponentFields();
57
58 return $fields;
59 }
60
66 protected function getUfComponentFields(): array
67 {
68 $fields = [];
69
70 $fields['ENABLE_USER_FIELD_CREATION'] = true;
71 $fields['ENABLE_USER_FIELD_MANDATORY_CONTROL'] = true;
72 $fields['USER_FIELD_ENTITY_ID'] = $this->getUfEntityId();
73 $fields['USER_FIELD_PREFIX'] = $this->getUfEntityId();
74 $fields['USER_FIELD_CREATE_SIGNATURE'] = $this->getUfDispatcher()->getCreateSignature([
75 'ENTITY_ID' => $this->getUfEntityId(),
76 ]);
77 $fields['USER_FIELD_CREATE_PAGE_URL'] = null;
78
79 return $fields;
80 }
81
89 protected function getUfEntityFields(): array
90 {
91 $result = [];
92
93 $userFieldsInfos = $this->getUfTypeManager()->GetUserFields(
94 $this->getUfEntityId(),
95 0,
96 LANGUAGE_ID
97 );
98 foreach ($userFieldsInfos as $userFieldInfo)
99 {
100 $fieldName = $userFieldInfo['FIELD_NAME'];
101 $result[] = [
102 'name' => $fieldName,
103 'title' => $userFieldInfo['EDIT_FORM_LABEL'] ?? $fieldName,
104 'type' => 'userField',
105 'data' => [
106 'fieldInfo' => [
107 'USER_TYPE_ID' => $userFieldInfo['USER_TYPE_ID'],
108 'ENTITY_ID' => $this->getUfEntityId(),
109 'ENTITY_VALUE_ID' => $this->getEntityId(),
110 'FIELD' => $fieldName,
111 'MULTIPLE' => $userFieldInfo['MULTIPLE'],
112 'MANDATORY' => $userFieldInfo['MANDATORY'],
113 'SETTINGS' => $userFieldInfo['SETTINGS'] ?? null,
114 ],
115 ],
116 ];
117 }
118
119 return $result;
120 }
121
129 protected function getUfEntityDataValue(array $field)
130 {
131 $fieldInfo = $field['data']['fieldInfo'];
132 $fieldName = $fieldInfo['FIELD'] ?? null;
133 if (!$fieldName)
134 {
135 $value = null;
136 }
137 else
138 {
139 $value = $this->getUfTypeManager()->GetUserFieldValue(
140 $this->getUfEntityId(),
141 $fieldName,
142 $this->getEntityId()
143 );
144 }
145
146 if (!empty($value))
147 {
148 $fieldInfo['VALUE'] = $value;
149 }
150
151 $userFieldDispatcher = \Bitrix\Main\UserField\Dispatcher::instance();
152 $signatire = $userFieldDispatcher->getSignature($fieldInfo);
153
154 if (empty($value))
155 {
156 return [
157 'IS_EMPTY' => true,
158 'SIGNATURE' => $signatire,
159 ];
160 }
161
162 return [
163 'IS_EMPTY' => false,
164 'VALUE' => $value,
165 'SIGNATURE' => $signatire,
166 ];
167 }
168
193 protected function fillUfEntityFields(array $fields): array
194 {
195 array_push($fields, ... $this->getUfEntityFields());
196
197 return $fields;
198 }
199
221 protected function fillUfEntityData(array $entityData): array
222 {
223 $ufFields = $this->getUfEntityFields();
224 foreach ($ufFields as $item)
225 {
226 $fieldName = $item['name'];
227 $entityData[$fieldName] = $this->getUfEntityDataValue($item);
228 }
229
230 return $entityData;
231 }
232}