1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
user.php
См. документацию.
1<?php
2namespace Bitrix\Bizproc\BaseType;
3
4use Bitrix\HumanResources\Compatibility\Utils\DepartmentBackwardAccessCode;
5use Bitrix\HumanResources\Service\Container;
6use Bitrix\Main;
7use Bitrix\Bizproc\FieldType;
8use Bitrix\Bizproc\Automation;
9
14class User extends Base
15{
16
20 public static function getType()
21 {
22 return FieldType::USER;
23 }
24
29 public static function getFormats()
30 {
31 $formats = parent::getFormats();
32 $formats['friendly'] = array(
33 'callable' =>'formatValueFriendly',
34 'separator' => ', ',
35 );
36 return $formats;
37 }
38
46 public static function toSingleValue(FieldType $fieldType, $value)
47 {
48 if (is_array($value))
49 {
50 reset($value);
51 $value = current($value);
52 }
53
54 return $value;
55 }
56
62 protected static function formatValuePrintable(FieldType $fieldType, $value)
63 {
64 if (!is_array($value))
65 $value = array($value);
66
67 return \CBPHelper::usersArrayToString($value, null, $fieldType->getDocumentType());
68 }
69
75 protected static function formatValueFriendly(FieldType $fieldType, $value)
76 {
77 if (!is_array($value))
78 $value = array($value);
79
80 return \CBPHelper::usersArrayToString($value, null, $fieldType->getDocumentType(), false);
81 }
82
89 public static function convertTo(FieldType $fieldType, $value, $toTypeClass)
90 {
92 $type = $toTypeClass::getType();
93 switch ($type)
94 {
96 case FieldType::INT:
97 $value = (string)$value;
98 if (mb_strpos($value, 'user_') === 0)
99 $value = mb_substr($value, mb_strlen('user_'));
100 $value = (int)$value;
101 break;
103 case FieldType::TEXT:
104 case FieldType::USER:
105 $value = (string)$value;
106 break;
107 default:
108 $value = null;
109 }
110
111 return $value;
112 }
113
118 public static function getConversionMap()
119 {
120 return array(
121 array(
127 )
128 );
129 }
130
139 protected static function renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
140 {
141 if ($value !== null && !is_array($value))
142 {
143 if (self::isRawValue($value))
144 {
145 $errors = [];
146 $value = \CBPHelper::UsersStringToArray($value, $fieldType->getDocumentType(), $errors);
147 }
148 else
149 {
150 $value = [$value];
151 }
152 }
153
154 $isPublic = ($renderMode & FieldType::RENDER_MODE_PUBLIC);
155
156 $valueString = \CBPHelper::usersArrayToString($value, null, $fieldType->getDocumentType());
157
158 if ($allowSelection && !$isPublic)
159 {
160 return static::renderControlSelector($field, $valueString, 'combine', '', $fieldType);
161 }
162
163 if ($isPublic)
164 {
165 \CUtil::InitJSCore(['bp_user_selector']);
166 $name = static::generateControlName($field);
167 $controlId = static::generateControlId($field);
168
169 $settings = $fieldType->getSettings();
170
171 $config = [
172 'valueInputName' => $name,
173 'value' => $valueString,
174 'items' => $value ? static::getSelectedItems($value, $settings) : [],
175 'multiple' => $fieldType->isMultiple(),
176 'required' => $fieldType->isRequired(),
177 'canUseHumanResources' => static::canUseHumanResources(),
178 ];
179
180 if ($settings)
181 {
183 }
184
185 $groups =
186 \CBPRuntime::GetRuntime()
187 ->getDocumentService()
188 ->GetAllowableUserGroups($fieldType->getDocumentType(), true)
189 ;
190
191 if ($groups)
192 {
193 $config['groups'] = [];
194 foreach ($groups as $id => $groupName)
195 {
196 if (!$groupName || mb_strpos($id, 'group_') === 0)
197 {
198 continue;
199 }
200
201 $config['groups'][] = [
202 'id' => preg_match('/^[0-9]+$/', $id) ? 'G'.$id : $id,
203 'name' => $groupName
204 ];
205 }
206 }
207
208 $controlIdJs = \CUtil::JSEscape($controlId);
209 $controlIdHtml = htmlspecialcharsbx($controlId);
210 $configHtml = htmlspecialcharsbx(Main\Web\Json::encode($config));
211 $className = htmlspecialcharsbx(static::generateControlClassName($fieldType, $field));
212 $property = $fieldType->getProperty();
213 $property['Type'] = static::getType();
214 $propertyHtml = htmlspecialcharsbx(Main\Web\Json::encode($property));
215
216 return <<<HTML
217 <script>
218 BX.ready(function(){
219 const c = document.getElementById('{$controlIdJs}');
220 if (c)
221 {
222 BX.Bizproc.FieldType.initControl(c.parentNode, JSON.parse(c.dataset.property));
223 }
224 });
225 </script>
226 <div id="{$controlIdHtml}" data-role="user-selector" data-property="{$propertyHtml}" data-config="{$configHtml}" class="{$className}"></div>
227HTML;
228 }
229
230 $renderResult = parent::renderControl($fieldType, $field, $valueString, $allowSelection, $renderMode);
231 $renderResult .= static::renderControlSelector($field, null, false, '', $fieldType);
232
233 return $renderResult;
234 }
235
244 public static function renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
245 {
246 return static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
247 }
248
257 public static function renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
258 {
259 return static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
260 }
261
268 protected static function extractValue(FieldType $fieldType, array $field, array $request)
269 {
270 $value = parent::extractValue($fieldType, $field, $request);
271 $result = null;
272
273 if (is_string($value) && $value !== '')
274 {
275 $errors = [];
276 $result = \CBPHelper::usersStringToArray($value, $fieldType->getDocumentType(), $errors);
277 if (sizeof($errors) > 0)
278 {
279 static::addErrors($errors);
280 }
281 }
282
283 return $result;
284 }
285
292 public static function extractValueSingle(FieldType $fieldType, array $field, array $request)
293 {
294 static::cleanErrors();
295 $result = static::extractValue($fieldType, $field, $request);
296
297 return is_array($result)? array_shift($result) : $result;
298 }
299
306 public static function extractValueMultiple(FieldType $fieldType, array $field, array $request)
307 {
308 static::cleanErrors();
309 return static::extractValue($fieldType, $field, $request);
310 }
311
312 public static function externalizeValue(FieldType $fieldType, $context, $value)
313 {
314 $useExtraction = $fieldType->getSettings()['ExternalExtract'] ?? false;
315
317 {
318 $useExtraction = true;
319 }
320
321 if ($useExtraction && $value)
322 {
323 $docId = $fieldType->getDocumentId() ?: $fieldType->getDocumentType();
324
325 return \CBPHelper::ExtractUsers($value, $docId, true);
326 }
327
328 return parent::externalizeValue($fieldType, $context, $value);
329 }
330
331 public static function externalizeValueMultiple(FieldType $fieldType, $context, $value)
332 {
333 $useExtraction = $fieldType->getSettings()['ExternalExtract'] ?? false;
334
336 {
337 $useExtraction = true;
338 }
339
340 if ($useExtraction && $value)
341 {
342 $docId = $fieldType->getDocumentId() ?: $fieldType->getDocumentType();
343 return \CBPHelper::ExtractUsers($value, $docId);
344 }
345
346 return parent::externalizeValueMultiple($fieldType, $context, $value);
347 }
348
349 public static function internalizeValue(FieldType $fieldType, $context, $value)
350 {
351 if ($context === FieldType::VALUE_CONTEXT_JN_MOBILE && is_numeric($value))
352 {
353 $value = 'user_' . $value;
354 }
355
356 return parent::internalizeValue($fieldType, $context, $value);
357 }
358
359 private static function getSelectedItems(array $value, array $settings = []): ?array
360 {
361 if (!class_exists(\Bitrix\UI\EntitySelector\Dialog::class))
362 {
363 return null;
364 }
365
366 $mapCallback = static function ($value)
367 {
368 if ($value && str_starts_with($value, 'user_'))
369 {
370 return ['user', \CBPHelper::StripUserPrefix($value)];
371 }
372
373 if ($value && str_starts_with($value, 'group_d'))
374 {
375 return ['department', preg_replace('|\D+|', '', $value)];
376 }
377
378 if ($value && str_starts_with($value, 'group_hr'))
379 {
380 return ['structure-node', preg_replace('|\D+|', '', $value)];
381 }
382
383 return null;
384 };
385
386 $preselectedItems = array_filter(array_map($mapCallback, $value));
387
388 if (!$preselectedItems)
389 {
390 return [];
391 }
392
393 if (static::canUseHumanResources())
394 {
395 $nodeRepository = Container::getNodeRepository();
396 foreach ($preselectedItems as $key => $item)
397 {
398 if ($item[0] === 'department')
399 {
400 $node = $nodeRepository->getByAccessCode(DepartmentBackwardAccessCode::makeById((int)$item[1]));
401 if ($node)
402 {
403 unset($preselectedItems[$key]);
404 $preselectedItems[] = ['structure-node', $node->id];
405 }
406 }
407 }
408 }
409
410 $options = [];
411
412 if (!empty($settings['allowEmailUsers']))
413 {
414 $options['entities'] = [
415 [
416 'id' => 'user',
417 'options' => [
418 'emailUsers' => true,
419 'myEmailUsers' => true,
420 ]
421 ]
422 ];
423 }
424
425 return \Bitrix\UI\EntitySelector\Dialog::getPreselectedItems($preselectedItems, $options)->toArray();
426 }
427
428 public static function validateValueSingle($value, FieldType $fieldType)
429 {
430 $value = static::toSingleValue($fieldType, $value);
431
432 $value = trim($value);
433
434 $isUser = (mb_strpos($value, 'user_') !== false);
435 if ($isUser)
436 {
437 return $value;
438 }
439
440 $isGroup = (mb_strpos($value, 'group_') !== false);
441 if ($isGroup)
442 {
443 return $value;
444 }
445
446 $isNumber = preg_match('#^[0-9]+$#', $value);
447 if ($isNumber)
448 {
449 return $value;
450 }
451
452 $isDocumentGroup = Automation\Helper::isDocumentUserGroup($value, $fieldType->getDocumentType());
453 if ($isDocumentGroup)
454 {
455 return $value;
456 }
457
458 return null;
459 }
460
461 public static function validateValueMultiple($value, FieldType $fieldType): array
462 {
463 $value = parent::validateValueMultiple($value, $fieldType);
464 $value = array_filter($value, static fn($v) => ($v !== null));
465
466 return array_values(array_unique($value));
467 }
468
469 public static function convertPropertyToView(FieldType $fieldType, int $viewMode, array $property): array
470 {
471 if ($viewMode === FieldType::RENDER_MODE_JN_MOBILE && $fieldType->getValue())
472 {
473 $value = \CBPHelper::flatten($fieldType->getValue());
474 $value = array_values(array_filter($value, fn($v) => strpos($v, 'user_') !== false));
475
476 $property['Settings'] = ['entityList' => static::getSelectedItems($value)];
477 $property['Type'] = static::getType();
478 }
479
480 return parent::convertPropertyToView($fieldType, $viewMode, $property);
481 }
482
483 private static function isRawValue($value): bool
484 {
485 return (
486 is_string($value)
487 && !is_numeric($value)
488 && strpos($value, 'user_') === false
489 && strpos($value, 'group_') === false
490 );
491 }
492
493 private static function canUseHumanResources(): bool
494 {
495 return Main\Loader::includeModule('humanresources');
496 }
497}
$type
Определения options.php:106
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
static isDocumentUserGroup(string $value, array $documentType)
Определения helper.php:556
static $formats
Определения base.php:33
static convertTo(FieldType $fieldType, $value, $toTypeClass)
Определения base.php:209
static $errors
Определения base.php:273
static renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Определения user.php:139
static extractValueSingle(FieldType $fieldType, array $field, array $request)
Определения user.php:292
static extractValueMultiple(FieldType $fieldType, array $field, array $request)
Определения user.php:306
static getType()
Определения user.php:20
static getFormats()
Определения user.php:29
static extractValue(FieldType $fieldType, array $field, array $request)
Определения user.php:268
static externalizeValue(FieldType $fieldType, $context, $value)
Определения user.php:312
static renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Определения user.php:244
static formatValuePrintable(FieldType $fieldType, $value)
Определения user.php:62
static internalizeValue(FieldType $fieldType, $context, $value)
Определения user.php:349
static validateValueSingle($value, FieldType $fieldType)
Определения user.php:428
static toSingleValue(FieldType $fieldType, $value)
Определения user.php:46
static convertPropertyToView(FieldType $fieldType, int $viewMode, array $property)
Определения user.php:469
static renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Определения user.php:257
static getConversionMap()
Определения user.php:118
static formatValueFriendly(FieldType $fieldType, $value)
Определения user.php:75
static externalizeValueMultiple(FieldType $fieldType, $context, $value)
Определения user.php:331
static validateValueMultiple($value, FieldType $fieldType)
Определения user.php:461
const USER
Определения fieldtype.php:67
getDocumentId()
Определения fieldtype.php:202
const STRING
Определения fieldtype.php:57
const TEXT
Определения fieldtype.php:62
const RENDER_MODE_JN_MOBILE
Определения fieldtype.php:97
const RENDER_MODE_PUBLIC
Определения fieldtype.php:92
getProperty()
Определения fieldtype.php:133
isRequired()
Определения fieldtype.php:238
isMultiple()
Определения fieldtype.php:220
getDocumentType()
Определения fieldtype.php:184
const VALUE_CONTEXT_JN_MOBILE
Определения fieldtype.php:101
const INT
Определения fieldtype.php:42
getSettings()
Определения fieldtype.php:266
const DOUBLE
Определения fieldtype.php:32
static includeModule($moduleName)
Определения loader.php:67
$options
Определения commerceml2.php:49
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$context
Определения csv_new_setup.php:223
$groups
Определения options.php:30
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения tools.php:2701
$name
Определения menu_edit.php:35
Определения cookie.php:3
$settings
Определения product_settings.php:43
$config
Определения quickway.php:69
if(empty($signedUserToken)) $key
Определения quickway.php:257