Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
userfieldconfig.php
1<?php
2
4
5use Bitrix\Intranet\ActionFilter;
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 {
36 $access = UserFieldAccess::getInstance($moduleId);
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
69 protected function processAfterAction(Action $action, $result)
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 ];
158
159 $fields = array_intersect_key($fields, $allowedKeys);
160
161 $converter = new Converter(Converter::TO_UPPER | Converter::KEYS | Converter::TO_SNAKE);
162
163 if ($fields['showFilter'] === 'Y')
164 {
165 $fields['showFilter'] = 'E';
166 }
167
168 return $converter->process($fields);
169 }
170
171 public function preparePublicData(array $field): array
172 {
173 foreach (UserFieldTable::getLabelFields() as $labelName)
174 {
175 if (isset($field[$labelName]) && !is_array($field[$labelName]))
176 {
177 $field[$labelName] = [
178 Loc::getCurrentLang() => $field[$labelName],
179 ];
180 }
181 }
182 $settings = $field['SETTINGS'];
183
185 $field = (
186 new Converter(
187 Converter::KEYS
188 | Converter::TO_CAMEL
189 | Converter::LC_FIRST
190 | Converter::RECURSIVE
191 )
192 )->process($field);
193
194 $field['settings'] = $settings;
195
196 return $field;
197 }
198
199 protected function fillErrorsFromApplication(): void
200 {
201 global $APPLICATION;
202
203 $exception = $APPLICATION->GetException();
204 if (($exception instanceof \CAdminException) && is_array($exception->messages))
205 {
206 foreach ($exception->messages as $message)
207 {
208 if (isset($message['text']))
209 {
210 $message = $message['text'];
211 }
212 $this->addError(new Error($message));
213 }
214 }
215
216 $APPLICATION->ResetException();
217 }
218
219 protected function prepareEnums(array $newEnums, array $currentEnums): array
220 {
221 $deletedEnum = [];
222 $storedEnum = [];
223 $updatedEnum = [];
224
225 foreach ($currentEnums as $enumItem)
226 {
227 $storedEnum[$enumItem['ID']] = $enumItem;
228 $deletedEnum[$enumItem['ID']] = true;
229 }
230
231 $countAdded = 0;
232 foreach ($newEnums as $enumItem)
233 {
234 if (is_array($enumItem))
235 {
236 if (!empty($enumItem['id']))
237 {
238 if (empty($enumItem['xmlId']))
239 {
240 $enumItem['xmlId'] = $storedEnum[$enumItem['id']]['XML_ID'];
241 }
242 if (empty($enumItem['def']))
243 {
244 $enumItem['def'] = $storedEnum[$enumItem['id']]['DEF'];
245 }
246
247 unset($deletedEnum[$enumItem['id']]);
248 }
249 $itemKey = ($enumItem['id'] > 0 ? $enumItem['id'] : 'n' . ($countAdded++));
250
251 $itemDescription = [
252 'VALUE' => $enumItem['value'],
253 'DEF' => $enumItem['def'] === 'Y' ? 'Y' : 'N',
254 'SORT' => $enumItem['sort'],
255 ];
256
257 if (!empty($enumItem['xmlId']))
258 {
259 $itemDescription['XML_ID'] = $enumItem['xmlId'];
260 }
261
262 $enumItem['sort'] = (int)$enumItem['sort'];
263 if ($enumItem['sort'] > 0)
264 {
265 $itemDescription['SORT'] = $enumItem['sort'];
266 }
267
268 $updatedEnum[$itemKey] = $itemDescription;
269 }
270 }
271
272 foreach ($deletedEnum as $deletedId => $t)
273 {
274 $updatedEnum[$deletedId] = [
275 'ID' => $deletedId,
276 'DEL' => 'Y',
277 ];
278 }
279
280 return $updatedEnum;
281 }
282
283 protected function updateEnums(int $id, array $enums, array $currentEnums = []): void
284 {
285 $updatedEnum = $this->prepareEnums($enums, $currentEnums);
286
287 $enumValuesManager = new \CUserFieldEnum();
288 $setEnumResult = $enumValuesManager->setEnumValues($id, $updatedEnum);
289
290 if (!$setEnumResult)
291 {
293 }
294 }
295
296 public function getAction(string $moduleId, int $id, \CRestServer $restServer = null): ?array
297 {
298 $access = $this->getAccess($moduleId, $restServer);
299 if (!$access)
300 {
301 return null;
302 }
303 if ($this->getErrors())
304 {
305 return null;
306 }
307 if (!$access->canRead($id))
308 {
309 $this->addError($this->getReadAccessDeniedError());
310 return null;
311 }
312
313 $field = UserFieldTable::getFieldData($id);
314 if (is_array($field))
315 {
316 return [
317 'field' => $this->preparePublicData($field),
318 ];
319 }
320
321 return null;
322 }
323
324 public function listAction(
325 string $moduleId,
326 array $select = ['*'],
327 array $order = [],
328 array $filter = [],
329 PageNavigation $pageNavigation = null,
330 \CRestServer $restServer = null
331 ): ?Page
332 {
333 $access = $this->getAccess($moduleId, $restServer);
334 if (!$access)
335 {
336 return null;
337 }
338
339 $converter = new Converter(Converter::TO_UPPER | Converter::KEYS | Converter::TO_SNAKE);
341 $filter = $converter->process($filter);
343 $order = $converter->process($order);
345 $select = $converter->process($select);
346
347 if (!$access->canReadWithFilter($filter))
348 {
349 $this->addError($this->getReadAccessDeniedError());
350 return null;
351 }
353 $filter = $access->prepareFilter($filter);
354
355 $runtime = [];
356 if (!empty($select['LANGUAGE']) && is_string($select['LANGUAGE']))
357 {
358 $runtime[] = UserFieldTable::getLabelsReference('LABELS', $select['LANGUAGE']);
359 unset($select['LANGUAGE']);
360
361 $select = array_merge($select, UserFieldTable::getLabelsSelect());
362 }
363
364 $fields = [];
366 'select' => $select,
367 'filter' => $filter,
368 'order' => $order,
369 'offset' => $pageNavigation ? $pageNavigation->getOffset() : null,
370 'limit' => $pageNavigation ? $pageNavigation->getLimit() : null,
371 'runtime' => $runtime,
372 ]);
373 while ($field = $list->fetch())
374 {
375 $fields[] = $this->preparePublicData($field);
376 }
377
378 return new Page('fields', $fields, static function () use ($filter) {
379 return UserFieldTable::getCount($filter);
380 });
381 }
382
383 public function addAction(string $moduleId, array $field, \CRestServer $restServer = null): ?array
384 {
385 $access = $this->getAccess($moduleId, $restServer);
386 if (!$access)
387 {
388 return null;
389 }
390 $field = $this->prepareFields($field);
391 if (!$access->canAdd($field))
392 {
393 $this->addError($this->getAddAccessDeniedError());
394 return null;
395 }
396
397 $fieldName = $field['FIELD_NAME'] ?? '';
398 $entityId = $field['ENTITY_ID'] ?? '';
399 $prefix = 'UF_' . $entityId . '_';
400 if (strpos($fieldName, $prefix) !== 0)
401 {
402 $this->addError(new Error(Loc::getMessage('MAIN_USER_FIELD_CONTROLLER_FIELD_NAME_ERROR')));
403 return null;
404 }
405
406 $userTypeEntity = new \CUserTypeEntity();
407 $id = $userTypeEntity->Add($field);
408 if ($id > 0)
409 {
410 if ($field['USER_TYPE_ID'] === 'enumeration')
411 {
412 $this->updateEnums($id, (array)$field['ENUM']);
413 }
414
415 return [
416 'field' => $this->preparePublicData(UserFieldTable::getFieldData($id)),
417 ];
418 }
419
421 if (!$this->getErrors())
422 {
423 $this->addError($this->getCommonError());
424 }
425
426 return null;
427 }
428
429 public function updateAction(string $moduleId, int $id, array $field, \CRestServer $restServer = null): ?array
430 {
431 $access = $this->getAccess($moduleId, $restServer);
432 if (!$access)
433 {
434 return null;
435 }
436 if (!$access->canUpdate($id))
437 {
438 $this->addError($this->getUpdateAccessDeniedError());
439 return null;
440 }
441
442 $field = $this->prepareFields($field);
443 $userTypeEntity = new \CUserTypeEntity();
444 $isUpdated = $userTypeEntity->Update($id, $field);
445 if ($isUpdated)
446 {
447 if ($field['USER_TYPE_ID'] === 'enumeration')
448 {
449 $this->updateEnums($id, (array)$field['ENUM'], (array)UserFieldTable::getFieldData($id)['ENUM']);
450 }
451
452 return [
453 'field' => $this->preparePublicData(UserFieldTable::getFieldData($id)),
454 ];
455 }
456
458 if (!$this->getErrors())
459 {
460 $this->addError($this->getCommonError());
461 }
462
463 return null;
464 }
465
466 public function deleteAction(string $moduleId, int $id, \CRestServer $restServer = null): void
467 {
468 $access = $this->getAccess($moduleId, $restServer);
469 if (!$access)
470 {
471 return;
472 }
473 if (!$access->canDelete($id))
474 {
475 $this->addError($this->getDeleteAccessDeniedError());
476 return;
477 }
478
479 $userTypeEntity = new \CUserTypeEntity();
480 $userTypeEntity->Delete($id);
481 }
482}
getAccess(string $moduleId, ?\CRestServer $restServer=null)
prepareEnums(array $newEnums, array $currentEnums)
processAfterAction(Action $action, $result)
listAction(string $moduleId, array $select=[' *'], array $order=[], array $filter=[], PageNavigation $pageNavigation=null, \CRestServer $restServer=null)
getTypesAction(string $moduleId, \CRestServer $restServer=null)
getAction(string $moduleId, int $id, \CRestServer $restServer=null)
updateEnums(int $id, array $enums, array $currentEnums=[])
updateAction(string $moduleId, int $id, array $field, \CRestServer $restServer=null)
addAction(string $moduleId, array $field, \CRestServer $restServer=null)
deleteAction(string $moduleId, int $id, \CRestServer $restServer=null)
static includeModule($moduleName)
Definition loader.php:69
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())
static getCount($filter=array(), array $cache=array())
static getInstance(string $moduleId, int $userId=null)
static getLabelsSelect(string $referenceName=null)
static getLabelsReference(string $referenceName=null, string $languageId=null)
static getFieldData(int $id)