1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
select.php
См. документацию.
1<?php
2
3namespace Bitrix\Bizproc\BaseType;
4
5use Bitrix\Main;
6use Bitrix\Main\Localization\Loc;
7use Bitrix\Bizproc\FieldType;
8
13class Select extends Base
14{
15
16 public const VIEW_TYPE_MENU = 'menu';
20 public static function getType()
21 {
22 return FieldType::SELECT;
23 }
24
30 protected static function formatValuePrintable(FieldType $fieldType, $value)
31 {
32 $options = static::getFieldOptions($fieldType);
33 if (is_scalar($value) && isset($options[$value]))
34 {
35 return (string) $options[$value];
36 }
37
38 return '';
39 }
40
47 public static function convertTo(FieldType $fieldType, $value, $toTypeClass)
48 {
50 $type = $toTypeClass::getType();
51 $options = static::getFieldOptions($fieldType);
52
53 $key = $originalValue = $value;
54 if (is_array($value))
55 {
56 foreach($value as $k => $v)
57 {
58 $key = $k;
59 $originalValue = $v;
60 }
61 }
62 elseif (isset($options[$key]))
63 {
64 $originalValue = $options[$value];
65 }
66
67 switch ($type)
68 {
69 case FieldType::BOOL:
70 $value = mb_strtolower((string)$key);
71 $value = in_array($value, array('y', 'yes', 'true', '1')) ? 'Y' : 'N';
72 break;
74 $value = str_replace(' ', '', str_replace(',', '.', $key));
75 $value = (float)$value;
76 break;
77 case FieldType::INT:
78 $value = str_replace(' ', '', $key);
79 $value = (int)$value;
80 break;
82 case FieldType::TEXT:
83 $value = (string) $originalValue;
84 break;
87 $value = (string) $key;
88 break;
89 case FieldType::USER:
90 $value = trim($key);
91 if (mb_strpos($value, 'user_') === false
92 && mb_strpos($value, 'group_') === false
93 && !preg_match('#^[0-9]+$#', $value)
94 )
95 {
96 $value = null;
97 }
98 break;
99 default:
100 $value = null;
101 }
102
103 return $value;
104 }
105
110 public static function getConversionMap()
111 {
112 return array(
113 array(
121 )
122 );
123 }
124
133 protected static function renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
134 {
135 if (
136 $renderMode === FieldType::RENDER_MODE_PUBLIC
137 && $fieldType->getProperty()['Settings']
138 && isset($fieldType->getProperty()['Settings']['ViewType'])
139 && $fieldType->getProperty()['Settings']['ViewType'] === self::VIEW_TYPE_MENU
140 )
141 {
142 $properties = $fieldType->getProperty();
143 if (is_null($value) && !is_null($properties['Default']))
144 {
145 $value = is_array($properties['Default']) ? $properties['Default'] : [$properties['Default']];
146 }
147 $config = [
148 'name' => $properties['Name'] ?? '',
149 'fieldName' => $field['Field'],
150 'options' => $properties['Options'] ?? [],
151 'multiple' => $properties['Multiple'] ?? false,
152 'selected' => is_array($value) ? $value : [$value],
153 ];
155
156 return <<<HTML
157 <div data-role="menu-selector" data-config="${config}"></div>
158 HTML;
159 }
160 $selectorValue = null;
161 $typeValue = [];
162 if (!is_array($value))
163 {
164 $value = (array)$value;
165 }
166
167 if (\CBPHelper::isAssociativeArray($value))
168 {
169 $value = array_keys($value);
170 }
171
172 foreach ($value as $v)
173 {
174 if ($allowSelection && \CBPActivity::isExpression($v))
175 {
176 $selectorValue = $v;
177 }
178 else
179 {
180 $typeValue[] = (string)$v;
181 }
182 }
183
184 // need to show at least one control
185 if (empty($typeValue))
186 {
187 $typeValue[] = null;
188 }
189
190 $className = static::generateControlClassName($fieldType, $field);
191 $selectorAttributes = '';
192
193 $isPublicControl = $renderMode & FieldType::RENDER_MODE_PUBLIC;
194
195 if ($allowSelection && $isPublicControl)
196 {
197 $selectorAttributes = sprintf(
198 'data-role="inline-selector-target" data-property="%s" ',
199 htmlspecialcharsbx(Main\Web\Json::encode($fieldType->getProperty()))
200 );
201 }
202
203 if ($fieldType->isMultiple())
204 {
205 $selectorAttributes .= 'size="5" multiple ';
206 }
207
208 $renderResult = sprintf(
209 '<select id="%s" class="%s" name="%s%s" %s>',
210 htmlspecialcharsbx(static::generateControlId($field)),
211 ($isPublicControl ? htmlspecialcharsbx($className) : ''),
212 htmlspecialcharsbx(static::generateControlName($field)),
213 $fieldType->isMultiple() ? '[]' : '',
214 $selectorAttributes
215 );
216
217 $settings = static::getFieldSettings($fieldType);
218
219 $showEmptyValue = isset($settings['ShowEmptyValue']) ? \CBPHelper::getBool($settings['ShowEmptyValue']) : null;
220 if (($showEmptyValue === null && !$fieldType->isMultiple()) || $showEmptyValue === true)
221 {
222 $renderResult .= '<option value="">['.Loc::getMessage('BPDT_SELECT_NOT_SET').']</option>';
223 }
224
225 $groups = $settings['Groups'] ?? null;
226
227 if(is_array($groups) && !empty($groups))
228 {
229 foreach($groups as $group)
230 {
231 if(!is_array($group))
232 {
233 continue;
234 }
235
236 $name = isset($group['name']) ? $group['name'] : '';
237
238 if($name !== '')
239 {
240 $renderResult .= '<optgroup label="'.htmlspecialcharsbx($name).'">';
241 }
242
243 $options = isset($group['items']) && is_array($group['items']) ? $group['items'] : array();
244 foreach($options as $k => $v)
245 {
246 $renderResult .= '<option value="';
247 $renderResult .= htmlspecialcharsbx($k);
248 $renderResult .= '"';
249
250 if(in_array((string)$k, $typeValue, true))
251 {
252 $renderResult .= ' selected';
253 }
254
255 $renderResult .= '>';
256 $renderResult .= htmlspecialcharsbx($v);
257 $renderResult .= '</option>';
258 }
259
260 if($name !== '')
261 {
262 $renderResult .= '</optgroup>';
263 }
264 }
265 }
266 else
267 {
268 $options = static::getFieldOptions($fieldType);
269 foreach ($options as $k => $v)
270 {
271 $renderResult .= '<option value="'.htmlspecialcharsbx($k).'"'.(in_array((string)$k, $typeValue) ? ' selected' : '').'>'.htmlspecialcharsbx(htmlspecialcharsback($v)).'</option>';
272 }
273 }
274
275 if ($allowSelection && $selectorValue && $isPublicControl)
276 {
277 $renderResult .= sprintf(
278 '<option value="%s" selected data-role="expression">%s</option>',
279 htmlspecialcharsbx($selectorValue),
280 htmlspecialcharsbx($selectorValue)
281 );
282 }
283
284 $renderResult .= '</select>';
285
286 if ($allowSelection && !$isPublicControl)
287 {
288 $renderResult .= static::renderControlSelector($field, $selectorValue, true, '', $fieldType);
289 }
290
291 return $renderResult;
292 }
293
298 public static function canRenderControl($renderMode)
299 {
300 return true;
301 }
302
311 public static function renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
312 {
313 if ($renderMode & FieldType::RENDER_MODE_PUBLIC)
314 {
315 $allowSelection = false;
316 }
317
318 return static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
319 }
320
329 public static function renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
330 {
331 if ($renderMode & FieldType::RENDER_MODE_PUBLIC)
332 {
333 $allowSelection = false;
334 }
335
336 return static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
337 }
338
345 public static function renderControlOptions(FieldType $fieldType, $callbackFunctionName, $value)
346 {
347 $options = static::getFieldOptions($fieldType);
348
349 $str = '';
350 foreach ($options as $k => $v)
351 {
352 if ((string)$k !== (string)$v)
353 $str .= '['.$k.']'.$v;
354 else
355 $str .= $v;
356
357 $str .= "\n";
358 }
359
360 $rnd = randString();
361 $renderResult = '<textarea id="WFSFormOptionsX'.$rnd.'" rows="5" cols="30">'.htmlspecialcharsbx($str).'</textarea><br />';
362 $renderResult .= Loc::getMessage('BPDT_SELECT_OPTIONS1').'<br />';
363 $renderResult .= Loc::getMessage('BPDT_SELECT_OPTIONS2').'<br />';
364 $renderResult .= '<script>
365 function WFSFormOptionsXFunction'.$rnd.'()
366 {
367 var result = {};
368 var i, id, val, str = document.getElementById("WFSFormOptionsX'.$rnd.'").value;
369
370 var arr = str.split(/[\r\n]+/);
371 var p, re = /\[([^\]]+)\].+/;
372 for (i in arr)
373 {
374 str = arr[i].replace(/^\s+|\s+$/g, \'\');
375 if (str.length > 0)
376 {
377 id = str.match(re);
378 if (id)
379 {
380 p = str.indexOf(\']\');
381 id = id[1];
382 val = str.substr(p + 1);
383 }
384 else
385 {
386 val = str;
387 id = val;
388 }
389 result[id] = val;
390 }
391 }
392
393 return result;
394 }
395 </script>';
396 $renderResult .= '<input type="button" onclick="'.htmlspecialcharsbx($callbackFunctionName)
397 .'(WFSFormOptionsXFunction'.$rnd.'())" value="'.Loc::getMessage('BPDT_SELECT_OPTIONS3').'">';
398
399 return $renderResult;
400 }
401
408 protected static function extractValue(FieldType $fieldType, array $field, array $request)
409 {
410 $value = parent::extractValue($fieldType, $field, $request);
411 $value =
412 !empty(static::getFieldOptions($fieldType))
413 ? self::validateValueSingle($value, $fieldType)
414 : null
415 ;
416
417 $errors = static::getErrors();
418 if (!empty($errors) && $value === null)
419 {
420 $lastErrorKey = array_key_last($errors);
421 if (!array_key_exists('parameter', $errors[$lastErrorKey]))
422 {
423 $errors[$lastErrorKey]['parameter'] = static::generateControlName($field);
424 }
425
426 static::cleanErrors();
427 static::addErrors($errors);
428 }
429
430 return $value;
431 }
432
439 public static function extractValueMultiple(FieldType $fieldType, array $field, array $request)
440 {
441 $name = $field['Field'];
442 $value = isset($request[$name]) ? $request[$name] : array();
443
444 if (!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
445 $value = array($value);
446 $value = array_unique($value);
448 return parent::extractValueMultiple($fieldType, $field, $request);
449 }
450
457 public static function formatValueMultiple(FieldType $fieldType, $value, $format = 'printable')
458 {
459 if (\CBPHelper::isAssociativeArray($value))
460 {
461 $value = array_keys($value);
462 }
463 return parent::formatValueMultiple($fieldType, $value, $format);
464 }
465
472 public static function formatValueSingle(FieldType $fieldType, $value, $format = 'printable')
473 {
474 if (\CBPHelper::isAssociativeArray($value))
475 {
476 $keys = array_keys($value);
477 $value = isset($keys[0]) ? $keys[0] : null;
478 }
479
480 if (is_array($value))
481 {
482 $value = current(array_values($value));
483 }
484
485 return parent::formatValueSingle($fieldType, $value, $format);
486 }
487
494 public static function convertValueMultiple(FieldType $fieldType, $value, $toTypeClass)
495 {
496 if (\CBPHelper::isAssociativeArray($value))
497 {
498 $value = array_keys($value);
499 }
500 return parent::convertValueMultiple($fieldType, $value, $toTypeClass);
501 }
502
503
508 protected static function getFieldOptions(FieldType $fieldType)
509 {
510 $options = $fieldType->getOptions();
511 return self::normalizeOptions($options);
512 }
513
519 protected static function getFieldSettings(FieldType $fieldType)
520 {
521 return $fieldType->getSettings();
522 }
523
528 protected static function normalizeOptions($options)
529 {
530 $normalized = [];
531 if (is_array($options))
532 {
533 foreach ($options as $key => $value)
534 {
535 if (is_array($value) && sizeof($value) == 2)
536 {
537 $v = array_values($value);
538 $key = $v[0];
539 $value = $v[1];
540 }
541 $normalized[$key] = $value;
542 }
543 }
544 elseif ($options !== '')
545 {
546 $normalized[$options] = $options;
547 }
548
549 return $normalized;
550 }
551
552 public static function externalizeValue(FieldType $fieldType, $context, $value)
553 {
554 $map = $fieldType->getSettings()['ExternalValues'] ?? null;
555 if ($map && isset($map[$value]))
556 {
557 return $map[$value];
558 }
559
560 return parent::externalizeValue($fieldType, $context, $value);
561 }
562
563 public static function mergeValue(FieldType $fieldType, array $baseValue, $appendValue): array
564 {
565 if (\CBPHelper::isAssociativeArray($baseValue))
566 {
567 $baseValue = array_keys($baseValue);
568 }
569 if (\CBPHelper::isAssociativeArray($appendValue))
570 {
571 $appendValue = array_keys($appendValue);
572 }
573
574 return parent::mergeValue($fieldType, $baseValue, $appendValue);
575 }
576
577 public static function validateValueSingle($value, FieldType $fieldType)
578 {
579 $options = static::getFieldOptions($fieldType);
580
581 if (\CBPActivity::isExpression($value) || empty($options))
582 {
583 return $value;
584 }
585
586 if ($value === '')
587 {
588 return null;
589 }
590
591 if (!(is_string($value) || is_int($value)))
592 {
593 return null;
594 }
595
596 if (!isset($options[$value]))
597 {
598 $key = array_search($value, $options, false);
599 if ($key === false)
600 {
601 static::addError([
602 'code' => 'ErrorValue',
603 'message' => Loc::getMessage('BPDT_SELECT_INVALID'),
604 ]);
605
606 return null;
607 }
608
609 return $key;
610 }
611
612 return $value;
613 }
614
615 public static function validateValueMultiple($value, FieldType $fieldType): array
616 {
617 $value = parent::validateValueMultiple($value, $fieldType);
618
619 return array_values(array_filter($value, static fn($v) => ($v !== null)));
620 }
621
622 public static function convertPropertyToView(FieldType $fieldType, int $viewMode, array $property): array
623 {
624 if ($viewMode === FieldType::RENDER_MODE_JN_MOBILE)
625 {
626 $options = static::getFieldOptions($fieldType);
627 $property['Options'] = array_map(
628 fn($value, $name) => ['value' => $value, 'name' => $name],
629 array_keys($options),
630 array_values($options),
631 );
632 }
633
634 return parent::convertPropertyToView($fieldType, $viewMode, $property);
635 }
636
637}
$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 formatValueMultiple(FieldType $fieldType, $value, $format='printable')
Определения base.php:123
static renderControlOptions(FieldType $fieldType, $callbackFunctionName, $value)
Определения base.php:634
static extractValueMultiple(FieldType $fieldType, array $field, array $request)
Определения base.php:693
static extractValue(FieldType $fieldType, array $field, array $request)
Определения base.php:645
static externalizeValue(FieldType $fieldType, $context, $value)
Определения base.php:805
static renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Определения base.php:477
static validateValueSingle($value, FieldType $fieldType)
Определения base.php:867
static convertTo(FieldType $fieldType, $value, $toTypeClass)
Определения base.php:209
static $errors
Определения base.php:273
static convertValueMultiple(FieldType $fieldType, $value, $toTypeClass)
Определения base.php:171
static convertPropertyToView(FieldType $fieldType, int $viewMode, array $property)
Определения base.php:887
static renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Определения base.php:505
static mergeValue(FieldType $fieldType, array $baseValue, $appendValue)
Определения base.php:245
static formatValueSingle(FieldType $fieldType, $value, $format='printable')
Определения base.php:141
static canRenderControl($renderMode)
Определения base.php:459
static validateValueMultiple($value, FieldType $fieldType)
Определения base.php:872
static renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Определения select.php:133
static getType()
Определения select.php:20
static formatValuePrintable(FieldType $fieldType, $value)
Определения select.php:30
const VIEW_TYPE_MENU
Определения select.php:16
static getConversionMap()
Определения select.php:110
const USER
Определения fieldtype.php:67
const BOOL
Определения fieldtype.php:17
const SELECT
Определения fieldtype.php:47
const STRING
Определения fieldtype.php:57
const INTERNALSELECT
Определения fieldtype.php:52
const TEXT
Определения fieldtype.php:62
const RENDER_MODE_JN_MOBILE
Определения fieldtype.php:97
const RENDER_MODE_PUBLIC
Определения fieldtype.php:92
getProperty()
Определения fieldtype.php:133
isMultiple()
Определения fieldtype.php:220
const INT
Определения fieldtype.php:42
const DOUBLE
Определения fieldtype.php:32
static isExpression($text)
Определения activity.php:1718
$options
Определения commerceml2.php:49
$str
Определения commerceml2.php:63
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$context
Определения csv_new_setup.php:223
$groups
Определения options.php:30
htmlspecialcharsback($str)
Определения tools.php:2693
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения tools.php:2701
randString($pass_len=10, $pass_chars=false)
Определения tools.php:2154
$name
Определения menu_edit.php:35
$map
Определения config.php:5
$value
Определения Param.php:39
Определения cookie.php:3
$settings
Определения product_settings.php:43
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$config
Определения quickway.php:69
if(empty($signedUserToken)) $key
Определения quickway.php:257
$k
Определения template_pdf.php:567