1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
userfieldconfig.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\Controller;
4
5use Bitrix\Intranet\ActionFilter;
6use Bitrix\Main\Engine\Action;
7use Bitrix\Main\Engine\Controller;
8use Bitrix\Main\Error;
9use Bitrix\Main\Loader;
10use Bitrix\Main\Localization\Loc;
11use Bitrix\Main\ObjectNotFoundException;
12use Bitrix\Main\UI\PageNavigation;
13use Bitrix\Main\UserField\UserFieldAccess;
14use Bitrix\Main\UserFieldTable;
15use Bitrix\Main\Engine\Response\Converter;
16use Bitrix\Main\Engine\Response\DataType\Page;
17
19{
20 protected function getDefaultPreFilters()
21 {
22 $defaultPreFilters = parent::getDefaultPreFilters();
23
24 if (Loader::includeModule('intranet'))
25 {
26 $defaultPreFilters[] = new ActionFilter\IntranetUser();
27 }
28
29 return $defaultPreFilters;
30 }
31
32 protected function getAccess(string $moduleId, ?\CRestServer $restServer = null): ?UserFieldAccess
33 {
34 try
35 {
37 }
39 {
40 if ($restServer)
41 {
42 $this->addError(new Error($e->getMessage()));
43
44 return null;
45 }
46
47 throw $e;
48 }
49
50 if ($restServer)
51 {
52 $scopes = $restServer->getAuthScope();
53 if (!in_array($moduleId, $scopes, true))
54 {
55 $this->addError(
56 new Error(
57 'The current method required more scopes. (' . $moduleId . ')',
58 \Bitrix\Rest\Engine\ActionFilter\Scope::ERROR_INSUFFICIENT_SCOPE
59 )
60 );
61
62 return null;
63 }
64 }
65
66 return $access;
67 }
68
70 {
72
73 return null;
74 }
75
76 protected function getEmptyModuleIdError(): Error
77 {
78 return new Error(Loc::getMessage('MAIN_USER_FIELD_CONTROLLER_EMPTY_MODULE_ID_ERROR'));
79 }
80
81 protected function getReadAccessDeniedError(): Error
82 {
83 return new Error(Loc::getMessage('MAIN_USER_FIELD_CONTROLLER_ACCESS_VIEW_ERROR'));
84 }
85
86 protected function getAddAccessDeniedError(): Error
87 {
88 return new Error(loc::getMessage('MAIN_USER_FIELD_CONTROLLER_ACCESS_CREATE_ERROR'));
89 }
90
91 protected function getUpdateAccessDeniedError(): Error
92 {
93 return new Error(loc::getMessage('MAIN_USER_FIELD_CONTROLLER_ACCESS_MODIFY_ERROR'));
94 }
95
96 protected function getDeleteAccessDeniedError(): Error
97 {
98 return new Error(loc::getMessage('MAIN_USER_FIELD_CONTROLLER_ACCESS_DELETE_ERROR'));
99 }
100
101 protected function getCommonError(): Error
102 {
103 return new Error(Loc::getMessage('MAIN_USER_FIELD_CONTROLLER_ERROR'));
104 }
105
106 public function getTypesAction(string $moduleId, \CRestServer $restServer = null): ?array
107 {
108 $access = $this->getAccess($moduleId, $restServer);
109 if (!$access)
110 {
111 return null;
112 }
113
114 $result = [];
115
116 $restrictedTypes = array_flip($access->getRestrictedTypes());
117
118 global $USER_FIELD_MANAGER;
119 $types = $USER_FIELD_MANAGER->GetUserType();
120
121 if (empty($restrictedTypes))
122 {
123 return $types;
124 }
125
126 foreach ($types as $id => $type)
127 {
128 if (!isset($restrictedTypes[$id]))
129 {
130 $result[$id] = [
131 'userTypeId' => $type['USER_TYPE_ID'],
132 'description' => $type['DESCRIPTION'],
133 ];
134 }
135 }
136
137 return [
138 'types' => $result,
139 ];
140 }
141
142 protected function prepareFields(array $fields): array
143 {
144 $allowedKeys = [
145 'editFormLabel' => true,
146 'multiple' => true,
147 'userTypeId' => true,
148 'fieldName' => true,
149 'enum' => true,
150 'entityId' => true,
151 'xmlId' => true,
152 'sort' => true,
153 'mandatory' => true,
154 'showFilter' => true,
155 'isSearchable' => true,
156 'settings' => true,
157 'editInList' => true,
158 ];
159
160 $fields = array_intersect_key($fields, $allowedKeys);
161
162 $converter = new Converter(Converter::TO_UPPER | Converter::KEYS | Converter::TO_SNAKE);
163
164 if ($fields['showFilter'] === 'Y')
165 {
166 $fields['showFilter'] = 'E';
167 }
168
169 return $converter->process($fields);
170 }
171
172 public function preparePublicData(array $field): array
173 {
174 foreach (UserFieldTable::getLabelFields() as $labelName)
175 {
176 if (isset($field[$labelName]) && !is_array($field[$labelName]))
177 {
178 $field[$labelName] = [
179 Loc::getCurrentLang() => $field[$labelName],
180 ];
181 }
182 }
183 $settings = $field['SETTINGS'];
184
186 $field = (
187 new Converter(
188 Converter::KEYS
189 | Converter::TO_CAMEL
190 | Converter::LC_FIRST
191 | Converter::RECURSIVE
192 )
193 )->process($field);
194
195 $field['settings'] = $settings;
196
197 return $field;
198 }
199
200 protected function fillErrorsFromApplication(): void
201 {
202 global $APPLICATION;
203
204 $exception = $APPLICATION->GetException();
205 if (($exception instanceof \CAdminException) && is_array($exception->messages))
206 {
207 foreach ($exception->messages as $message)
208 {
209 if (isset($message['text']))
210 {
211 $message = $message['text'];
212 }
213 $this->addError(new Error($message));
214 }
215 }
216
217 $APPLICATION->ResetException();
218 }
219
220 protected function prepareEnums(array $newEnums, array $currentEnums): array
221 {
222 $deletedEnum = [];
223 $storedEnum = [];
224 $updatedEnum = [];
225
226 foreach ($currentEnums as $enumItem)
227 {
228 $storedEnum[$enumItem['ID']] = $enumItem;
229 $deletedEnum[$enumItem['ID']] = true;
230 }
231
232 $countAdded = 0;
233 foreach ($newEnums as $enumItem)
234 {
235 if (is_array($enumItem))
236 {
237 if (!empty($enumItem['id']))
238 {
239 if (empty($enumItem['xmlId']))
240 {
241 $enumItem['xmlId'] = $storedEnum[$enumItem['id']]['XML_ID'];
242 }
243 if (empty($enumItem['def']))
244 {
245 $enumItem['def'] = $storedEnum[$enumItem['id']]['DEF'];
246 }
247
248 unset($deletedEnum[$enumItem['id']]);
249 }
250 $itemKey = ($enumItem['id'] > 0 ? $enumItem['id'] : 'n' . ($countAdded++));
251
252 $itemDescription = [
253 'VALUE' => $enumItem['value'],
254 'DEF' => $enumItem['def'] === 'Y' ? 'Y' : 'N',
255 'SORT' => $enumItem['sort'],
256 ];
257
258 if (!empty($enumItem['xmlId']))
259 {
260 $itemDescription['XML_ID'] = $enumItem['xmlId'];
261 }
262
263 $enumItem['sort'] = (int)$enumItem['sort'];
264 if ($enumItem['sort'] > 0)
265 {
266 $itemDescription['SORT'] = $enumItem['sort'];
267 }
268
269 $updatedEnum[$itemKey] = $itemDescription;
270 }
271 }
272
273 foreach ($deletedEnum as $deletedId => $t)
274 {
275 $updatedEnum[$deletedId] = [
276 'ID' => $deletedId,
277 'DEL' => 'Y',
278 ];
279 }
280
281 return $updatedEnum;
282 }
283
284 protected function updateEnums(int $id, array $enums, array $currentEnums = []): void
285 {
286 $updatedEnum = $this->prepareEnums($enums, $currentEnums);
287
288 $enumValuesManager = new \CUserFieldEnum();
289 $setEnumResult = $enumValuesManager->setEnumValues($id, $updatedEnum);
290
291 if (!$setEnumResult)
292 {
294 }
295 }
296
297 public function getAction(string $moduleId, int $id, \CRestServer $restServer = null): ?array
298 {
299 $access = $this->getAccess($moduleId, $restServer);
300 if (!$access)
301 {
302 return null;
303 }
304 if ($this->getErrors())
305 {
306 return null;
307 }
308 if (!$access->canRead($id))
309 {
310 $this->addError($this->getReadAccessDeniedError());
311 return null;
312 }
313
314 $field = UserFieldTable::getFieldData($id);
315 if (is_array($field))
316 {
317 return [
318 'field' => $this->preparePublicData($field),
319 ];
320 }
321
322 return null;
323 }
324
325 public function listAction(
326 string $moduleId,
327 array $select = ['*'],
328 array $order = [],
329 array $filter = [],
330 PageNavigation $pageNavigation = null,
331 \CRestServer $restServer = null
332 ): ?Page
333 {
334 $access = $this->getAccess($moduleId, $restServer);
335 if (!$access)
336 {
337 return null;
338 }
339
340 $converter = new Converter(Converter::TO_UPPER | Converter::KEYS | Converter::TO_SNAKE);
342 $filter = $converter->process($filter);
344 $order = $converter->process($order);
346 $select = $converter->process($select);
347
348 if (!$access->canReadWithFilter($filter))
349 {
350 $this->addError($this->getReadAccessDeniedError());
351 return null;
352 }
354 $filter = $access->prepareFilter($filter);
355
356 $runtime = [];
357 if (!empty($select['LANGUAGE']) && is_string($select['LANGUAGE']))
358 {
359 $runtime[] = UserFieldTable::getLabelsReference('LABELS', $select['LANGUAGE']);
360 unset($select['LANGUAGE']);
361
363 }
364
365 $fields = [];
367 'select' => $select,
368 'filter' => $filter,
369 'order' => $order,
370 'offset' => $pageNavigation ? $pageNavigation->getOffset() : null,
371 'limit' => $pageNavigation ? $pageNavigation->getLimit() : null,
372 'runtime' => $runtime,
373 ]);
374 while ($field = $list->fetch())
375 {
376 $fields[] = $this->preparePublicData($field);
377 }
378
379 return new Page('fields', $fields, static function () use ($filter) {
381 });
382 }
383
384 public function addAction(string $moduleId, array $field, \CRestServer $restServer = null): ?array
385 {
386 $access = $this->getAccess($moduleId, $restServer);
387 if (!$access)
388 {
389 return null;
390 }
391 $field = $this->prepareFields($field);
392 if (!$access->canAdd($field))
393 {
394 $this->addError($this->getAddAccessDeniedError());
395 return null;
396 }
397
398 $fieldName = $field['FIELD_NAME'] ?? '';
399 $entityId = $field['ENTITY_ID'] ?? '';
400 $prefix = 'UF_' . $entityId . '_';
401 if (!str_starts_with($fieldName, $prefix))
402 {
403 $this->addError(new Error(Loc::getMessage('MAIN_USER_FIELD_CONTROLLER_FIELD_NAME_ERROR')));
404 return null;
405 }
406
407 $userTypeEntity = new \CUserTypeEntity();
408 $id = $userTypeEntity->Add($field);
409 if ($id > 0)
410 {
411 if ($field['USER_TYPE_ID'] === 'enumeration')
412 {
413 $this->updateEnums($id, (array)$field['ENUM']);
414 }
415
416 return [
417 'field' => $this->preparePublicData(UserFieldTable::getFieldData($id)),
418 ];
419 }
420
422 if (!$this->getErrors())
423 {
424 $this->addError($this->getCommonError());
425 }
426
427 return null;
428 }
429
430 public function updateAction(string $moduleId, int $id, array $field, \CRestServer $restServer = null): ?array
431 {
432 $access = $this->getAccess($moduleId, $restServer);
433 if (!$access)
434 {
435 return null;
436 }
437 if (!$access->canUpdate($id))
438 {
439 $this->addError($this->getUpdateAccessDeniedError());
440 return null;
441 }
442
443 $field = $this->prepareFields($field);
444 $userTypeEntity = new \CUserTypeEntity();
445 $isUpdated = $userTypeEntity->Update($id, $field);
446 if ($isUpdated)
447 {
448 if ($field['USER_TYPE_ID'] === 'enumeration')
449 {
450 $this->updateEnums($id, (array)$field['ENUM'], (array)UserFieldTable::getFieldData($id)['ENUM']);
451 }
452
453 return [
454 'field' => $this->preparePublicData(UserFieldTable::getFieldData($id)),
455 ];
456 }
457
459 if (!$this->getErrors())
460 {
461 $this->addError($this->getCommonError());
462 }
463
464 return null;
465 }
466
467 public function deleteAction(string $moduleId, int $id, \CRestServer $restServer = null): void
468 {
469 $access = $this->getAccess($moduleId, $restServer);
470 if (!$access)
471 {
472 return;
473 }
474 if (!$access->canDelete($id))
475 {
476 $this->addError($this->getDeleteAccessDeniedError());
477 return;
478 }
479
480 $userTypeEntity = new \CUserTypeEntity();
481 $userTypeEntity->Delete($id);
482 }
483}
$type
Определения options.php:106
global $APPLICATION
Определения include.php:80
getAccess(string $moduleId, ?\CRestServer $restServer=null)
Определения userfieldconfig.php:32
prepareEnums(array $newEnums, array $currentEnums)
Определения userfieldconfig.php:220
processAfterAction(Action $action, $result)
Определения userfieldconfig.php:69
listAction(string $moduleId, array $select=[' *'], array $order=[], array $filter=[], PageNavigation $pageNavigation=null, \CRestServer $restServer=null)
Определения userfieldconfig.php:325
getTypesAction(string $moduleId, \CRestServer $restServer=null)
Определения userfieldconfig.php:106
prepareFields(array $fields)
Определения userfieldconfig.php:142
getAction(string $moduleId, int $id, \CRestServer $restServer=null)
Определения userfieldconfig.php:297
updateEnums(int $id, array $enums, array $currentEnums=[])
Определения userfieldconfig.php:284
updateAction(string $moduleId, int $id, array $field, \CRestServer $restServer=null)
Определения userfieldconfig.php:430
preparePublicData(array $field)
Определения userfieldconfig.php:172
addAction(string $moduleId, array $field, \CRestServer $restServer=null)
Определения userfieldconfig.php:384
deleteAction(string $moduleId, int $id, \CRestServer $restServer=null)
Определения userfieldconfig.php:467
addError(Error $error)
Определения controller.php:1070
Определения error.php:15
static includeModule($moduleName)
Определения loader.php:67
static getList(array $parameters=array())
Определения datamanager.php:431
static getCount($filter=array(), array $cache=array())
Определения datamanager.php:516
static getInstance(string $moduleId, int $userId=null)
Определения userfieldaccess.php:28
static getLabelsSelect(string $referenceName=null)
Определения userfield.php:138
static getLabelsReference(string $referenceName=null, string $languageId=null)
Определения userfield.php:103
static getLabelFields()
Определения userfield.php:126
static getFieldData(int $id)
Определения userfield.php:154
Определения rest.php:24
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
global $USER_FIELD_MANAGER
Определения attempt.php:6
$result
Определения get_property_values.php:14
$moduleId
$select
Определения iblock_catalog_list.php:194
$filter
Определения iblock_catalog_list.php:54
if(!is_array($deviceNotifyCodes)) $access
Определения options.php:174
Определения action.php:3
Определения aliases.php:54
Определения handlers.php:8
$order
Определения payment.php:8
$entityId
Определения payment.php:4
$message
Определения payment.php:8
$settings
Определения product_settings.php:43
$action
Определения file_dialog.php:21
$fields
Определения yandex_run.php:501