Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
user.php
1<?php
3
7
12class User extends Base
13{
14
18 public static function getType()
19 {
20 return FieldType::USER;
21 }
22
27 public static function getFormats()
28 {
29 $formats = parent::getFormats();
30 $formats['friendly'] = array(
31 'callable' =>'formatValueFriendly',
32 'separator' => ', ',
33 );
34 return $formats;
35 }
36
44 public static function toSingleValue(FieldType $fieldType, $value)
45 {
46 if (is_array($value))
47 {
48 reset($value);
49 $value = current($value);
50 }
51
52 return $value;
53 }
54
60 protected static function formatValuePrintable(FieldType $fieldType, $value)
61 {
62 if (!is_array($value))
63 $value = array($value);
64
65 return \CBPHelper::usersArrayToString($value, null, $fieldType->getDocumentType());
66 }
67
73 protected static function formatValueFriendly(FieldType $fieldType, $value)
74 {
75 if (!is_array($value))
76 $value = array($value);
77
78 return \CBPHelper::usersArrayToString($value, null, $fieldType->getDocumentType(), false);
79 }
80
87 public static function convertTo(FieldType $fieldType, $value, $toTypeClass)
88 {
90 $type = $toTypeClass::getType();
91 switch ($type)
92 {
94 case FieldType::INT:
95 $value = (string)$value;
96 if (mb_strpos($value, 'user_') === 0)
97 $value = mb_substr($value, mb_strlen('user_'));
98 $value = (int)$value;
99 break;
101 case FieldType::TEXT:
102 case FieldType::USER:
103 $value = (string)$value;
104 break;
105 default:
106 $value = null;
107 }
108
109 return $value;
110 }
111
116 public static function getConversionMap()
117 {
118 return array(
119 array(
125 )
126 );
127 }
128
137 protected static function renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
138 {
139 if ($value !== null && !is_array($value))
140 {
141 if (self::isRawValue($value))
142 {
143 $errors = [];
144 $value = \CBPHelper::UsersStringToArray($value, $fieldType->getDocumentType(), $errors);
145 }
146 else
147 {
148 $value = [$value];
149 }
150 }
151
152 $isPublic = ($renderMode & FieldType::RENDER_MODE_PUBLIC);
153
154 $valueString = \CBPHelper::usersArrayToString($value, null, $fieldType->getDocumentType());
155
156 if ($allowSelection && !$isPublic)
157 {
158 return static::renderControlSelector($field, $valueString, 'combine', '', $fieldType);
159 }
160
161 if ($isPublic)
162 {
163 \CUtil::InitJSCore(['bp_user_selector']);
164 $name = static::generateControlName($field);
165 $controlId = static::generateControlId($field);
166
167 $settings = $fieldType->getSettings();
168
169 $config = [
170 'valueInputName' => $name,
171 'value' => $valueString,
172 'items' => $value ? static::getSelectedItems($value, $settings) : [],
173 'multiple' => $fieldType->isMultiple(),
174 'required' => $fieldType->isRequired(),
175 ];
176
177 if ($settings)
178 {
179 $config += $settings;
180 }
181
182 $groups = \CBPRuntime::GetRuntime()
183 ->GetService('DocumentService')
184 ->GetAllowableUserGroups($fieldType->getDocumentType(), true);
185
186 if ($groups)
187 {
188 $config['groups'] = [];
189 foreach ($groups as $id => $groupName)
190 {
191 if (!$groupName || mb_strpos($id, 'group_') === 0)
192 {
193 continue;
194 }
195
196 $config['groups'][] = [
197 'id' => preg_match('/^[0-9]+$/', $id) ? 'G'.$id : $id,
198 'name' => $groupName
199 ];
200 }
201 }
202
203 $controlIdJs = \CUtil::JSEscape($controlId);
204 $controlIdHtml = htmlspecialcharsbx($controlId);
205 $configHtml = htmlspecialcharsbx(Main\Web\Json::encode($config));
206 $className = htmlspecialcharsbx(static::generateControlClassName($fieldType, $field));
207 $property = $fieldType->getProperty();
208 $property['Type'] = static::getType();
209 $propertyHtml = htmlspecialcharsbx(Main\Web\Json::encode($property));
210
211 return <<<HTML
212 <script>
213 BX.ready(function(){
214 var c = document.getElementById('{$controlIdJs}');
215 if (c)
216 {
217 BX.Bizproc.FieldType.initControl(c.parentNode, JSON.parse(c.dataset.property));
218 }
219 });
220 </script>
221 <div id="{$controlIdHtml}" data-role="user-selector" data-property="{$propertyHtml}" data-config="{$configHtml}" class="{$className}"></div>
222HTML;
223 }
224
225 $renderResult = parent::renderControl($fieldType, $field, $valueString, $allowSelection, $renderMode);
226 $renderResult .= static::renderControlSelector($field, null, false, '', $fieldType);
227
228 return $renderResult;
229 }
230
239 public static function renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
240 {
241 return static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
242 }
243
252 public static function renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
253 {
254 return static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
255 }
256
263 protected static function extractValue(FieldType $fieldType, array $field, array $request)
264 {
265 $value = parent::extractValue($fieldType, $field, $request);
266 $result = null;
267
268 if (is_string($value) && $value !== '')
269 {
270 $errors = [];
271 $result = \CBPHelper::usersStringToArray($value, $fieldType->getDocumentType(), $errors);
272 if (sizeof($errors) > 0)
273 {
274 static::addErrors($errors);
275 }
276 }
277
278 return $result;
279 }
280
287 public static function extractValueSingle(FieldType $fieldType, array $field, array $request)
288 {
289 static::cleanErrors();
290 $result = static::extractValue($fieldType, $field, $request);
291
292 return is_array($result)? array_shift($result) : $result;
293 }
294
301 public static function extractValueMultiple(FieldType $fieldType, array $field, array $request)
302 {
303 static::cleanErrors();
304 return static::extractValue($fieldType, $field, $request);
305 }
306
307 public static function externalizeValue(FieldType $fieldType, $context, $value)
308 {
309 $useExtraction = $fieldType->getSettings()['ExternalExtract'] ?? false;
310
311 if ($context === FieldType::VALUE_CONTEXT_JN_MOBILE)
312 {
313 $useExtraction = true;
314 }
315
316 if ($useExtraction && $value)
317 {
318 $docId = $fieldType->getDocumentId() ?: $fieldType->getDocumentType();
319
320 return \CBPHelper::ExtractUsers($value, $docId, true);
321 }
322
323 return parent::externalizeValue($fieldType, $context, $value);
324 }
325
326 public static function externalizeValueMultiple(FieldType $fieldType, $context, $value)
327 {
328 $useExtraction = $fieldType->getSettings()['ExternalExtract'] ?? false;
329
330 if ($context === FieldType::VALUE_CONTEXT_JN_MOBILE)
331 {
332 $useExtraction = true;
333 }
334
335 if ($useExtraction && $value)
336 {
337 $docId = $fieldType->getDocumentId() ?: $fieldType->getDocumentType();
338 return \CBPHelper::ExtractUsers($value, $docId);
339 }
340
341 return parent::externalizeValueMultiple($fieldType, $context, $value);
342 }
343
344 public static function internalizeValue(FieldType $fieldType, $context, $value)
345 {
346 if ($context === FieldType::VALUE_CONTEXT_JN_MOBILE && is_numeric($value))
347 {
348 $value = 'user_' . $value;
349 }
350
351 return parent::internalizeValue($fieldType, $context, $value);
352 }
353
354 private static function getSelectedItems(array $value, array $settings = []): ?array
355 {
356 if (!class_exists(\Bitrix\UI\EntitySelector\Dialog::class))
357 {
358 return null;
359 }
360
361 $mapCallback = function ($value)
362 {
363 if ($value && strpos($value, 'user_') === 0)
364 {
365 return ['user', \CBPHelper::StripUserPrefix($value)];
366 }
367 if ($value && strpos($value, 'group_d') === 0)
368 {
369 return ['department', preg_replace('|[^0-9]+|', '', $value)];
370 }
371
372 return null;
373 };
374
375 $preselectedItems = array_filter(array_map($mapCallback, $value));
376
377 if (!$preselectedItems)
378 {
379 return [];
380 }
381
382 $options = [];
383
384 if (!empty($settings['allowEmailUsers']))
385 {
386 $options['entities'] = [
387 [
388 'id' => 'user',
389 'options' => [
390 'emailUsers' => true,
391 'myEmailUsers' => true,
392 ]
393 ]
394 ];
395 }
396
397 return \Bitrix\UI\EntitySelector\Dialog::getSelectedItems($preselectedItems, $options)->toArray();
398 }
399
400 public static function validateValueSingle($value, FieldType $fieldType)
401 {
402 $value = static::toSingleValue($fieldType, $value);
403
404 $value = trim($value);
405
406 $isUser = (mb_strpos($value, 'user_') !== false);
407 if ($isUser)
408 {
409 return $value;
410 }
411
412 $isGroup = (mb_strpos($value, 'group_') !== false);
413 if ($isGroup)
414 {
415 return $value;
416 }
417
418 $isNumber = preg_match('#^[0-9]+$#', $value);
419 if ($isNumber)
420 {
421 return $value;
422 }
423
424 $isDocumentGroup = Automation\Helper::isDocumentUserGroup($value, $fieldType->getDocumentType());
425 if ($isDocumentGroup)
426 {
427 return $value;
428 }
429
430 return null;
431 }
432
433 public static function validateValueMultiple($value, FieldType $fieldType): array
434 {
435 $value = parent::validateValueMultiple($value, $fieldType);
436 $value = array_filter($value, static fn($v) => ($v !== null));
437
438 return array_values(array_unique($value));
439 }
440
441 public static function convertPropertyToView(FieldType $fieldType, int $viewMode, array $property): array
442 {
443 if ($viewMode === FieldType::RENDER_MODE_JN_MOBILE && $fieldType->getValue())
444 {
445 $value = \CBPHelper::flatten($fieldType->getValue());
446 $value = array_values(array_filter($value, fn($v) => strpos($v, 'user_') !== false));
447
448 $property['Settings'] = ['entityList' => static::getSelectedItems($value)];
449 $property['Type'] = static::getType();
450 }
451
452 return parent::convertPropertyToView($fieldType, $viewMode, $property);
453 }
454
455 private static function isRawValue($value): bool
456 {
457 return (
458 is_string($value)
459 && !is_numeric($value)
460 && strpos($value, 'user_') === false
461 && strpos($value, 'group_') === false
462 );
463 }
464}
static convertTo(FieldType $fieldType, $value, $toTypeClass)
Definition base.php:209
static renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition user.php:137
static extractValueSingle(FieldType $fieldType, array $field, array $request)
Definition user.php:287
static extractValueMultiple(FieldType $fieldType, array $field, array $request)
Definition user.php:301
static extractValue(FieldType $fieldType, array $field, array $request)
Definition user.php:263
static externalizeValue(FieldType $fieldType, $context, $value)
Definition user.php:307
static renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition user.php:239
static formatValuePrintable(FieldType $fieldType, $value)
Definition user.php:60
static internalizeValue(FieldType $fieldType, $context, $value)
Definition user.php:344
static validateValueSingle($value, FieldType $fieldType)
Definition user.php:400
static toSingleValue(FieldType $fieldType, $value)
Definition user.php:44
static convertPropertyToView(FieldType $fieldType, int $viewMode, array $property)
Definition user.php:441
static renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition user.php:252
static formatValueFriendly(FieldType $fieldType, $value)
Definition user.php:73
static externalizeValueMultiple(FieldType $fieldType, $context, $value)
Definition user.php:326
static validateValueMultiple($value, FieldType $fieldType)
Definition user.php:433