Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
select.php
1<?php
2
4
8
13class Select extends Base
14{
18 public static function getType()
19 {
20 return FieldType::SELECT;
21 }
22
28 protected static function formatValuePrintable(FieldType $fieldType, $value)
29 {
30 $options = static::getFieldOptions($fieldType);
31 if (isset($options[$value]))
32 return (string) $options[$value];
33 return '';
34 }
35
42 public static function convertTo(FieldType $fieldType, $value, $toTypeClass)
43 {
45 $type = $toTypeClass::getType();
46 $options = static::getFieldOptions($fieldType);
47
48 $key = $originalValue = $value;
49 if (is_array($value))
50 {
51 foreach($value as $k => $v)
52 {
53 $key = $k;
54 $originalValue = $v;
55 }
56 }
57 elseif (isset($options[$key]))
58 {
59 $originalValue = $options[$value];
60 }
61
62 switch ($type)
63 {
64 case FieldType::BOOL:
65 $value = mb_strtolower((string)$key);
66 $value = in_array($value, array('y', 'yes', 'true', '1')) ? 'Y' : 'N';
67 break;
69 $value = str_replace(' ', '', str_replace(',', '.', $key));
70 $value = (float)$value;
71 break;
72 case FieldType::INT:
73 $value = str_replace(' ', '', $key);
74 $value = (int)$value;
75 break;
77 case FieldType::TEXT:
78 $value = (string) $originalValue;
79 break;
82 $value = (string) $key;
83 break;
84 case FieldType::USER:
85 $value = trim($key);
86 if (mb_strpos($value, 'user_') === false
87 && mb_strpos($value, 'group_') === false
88 && !preg_match('#^[0-9]+$#', $value)
89 )
90 {
91 $value = null;
92 }
93 break;
94 default:
95 $value = null;
96 }
97
98 return $value;
99 }
100
105 public static function getConversionMap()
106 {
107 return array(
108 array(
116 )
117 );
118 }
119
128 protected static function renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
129 {
130 $selectorValue = null;
131 $typeValue = [];
132 if (!is_array($value))
133 {
134 $value = (array)$value;
135 }
136
137 if (\CBPHelper::isAssociativeArray($value))
138 {
139 $value = array_keys($value);
140 }
141
142 foreach ($value as $v)
143 {
144 if ($allowSelection && \CBPActivity::isExpression($v))
145 {
146 $selectorValue = $v;
147 }
148 else
149 {
150 $typeValue[] = (string)$v;
151 }
152 }
153
154 // need to show at least one control
155 if (empty($typeValue))
156 {
157 $typeValue[] = null;
158 }
159
160 $className = static::generateControlClassName($fieldType, $field);
161 $selectorAttributes = '';
162
163 $isPublicControl = $renderMode & FieldType::RENDER_MODE_PUBLIC;
164
165 if ($allowSelection && $isPublicControl)
166 {
167 $selectorAttributes = sprintf(
168 'data-role="inline-selector-target" data-property="%s" ',
169 htmlspecialcharsbx(Main\Web\Json::encode($fieldType->getProperty()))
170 );
171 }
172
173 if ($fieldType->isMultiple())
174 {
175 $selectorAttributes .= 'size="5" multiple ';
176 }
177
178 $renderResult = sprintf(
179 '<select id="%s" class="%s" name="%s%s" %s>',
180 htmlspecialcharsbx(static::generateControlId($field)),
181 ($isPublicControl ? htmlspecialcharsbx($className) : ''),
182 htmlspecialcharsbx(static::generateControlName($field)),
183 $fieldType->isMultiple() ? '[]' : '',
184 $selectorAttributes
185 );
186
187 $settings = static::getFieldSettings($fieldType);
188
189 $showEmptyValue = isset($settings['ShowEmptyValue']) ? \CBPHelper::getBool($settings['ShowEmptyValue']) : null;
190 if (($showEmptyValue === null && !$fieldType->isMultiple()) || $showEmptyValue === true)
191 {
192 $renderResult .= '<option value="">['.Loc::getMessage('BPDT_SELECT_NOT_SET').']</option>';
193 }
194
195 $groups = $settings['Groups'] ?? null;
196
197 if(is_array($groups) && !empty($groups))
198 {
199 foreach($groups as $group)
200 {
201 if(!is_array($group))
202 {
203 continue;
204 }
205
206 $name = isset($group['name']) ? $group['name'] : '';
207
208 if($name !== '')
209 {
210 $renderResult .= '<optgroup label="'.htmlspecialcharsbx($name).'">';
211 }
212
213 $options = isset($group['items']) && is_array($group['items']) ? $group['items'] : array();
214 foreach($options as $k => $v)
215 {
216 $renderResult .= '<option value="';
217 $renderResult .= htmlspecialcharsbx($k);
218 $renderResult .= '"';
219
220 if(in_array((string)$k, $typeValue, true))
221 {
222 $renderResult .= ' selected';
223 }
224
225 $renderResult .= '>';
226 $renderResult .= htmlspecialcharsbx($v);
227 $renderResult .= '</option>';
228 }
229
230 if($name !== '')
231 {
232 $renderResult .= '</optgroup>';
233 }
234 }
235 }
236 else
237 {
238 $options = static::getFieldOptions($fieldType);
239 foreach ($options as $k => $v)
240 {
241 $renderResult .= '<option value="'.htmlspecialcharsbx($k).'"'.(in_array((string)$k, $typeValue) ? ' selected' : '').'>'.htmlspecialcharsbx(htmlspecialcharsback($v)).'</option>';
242 }
243 }
244
245 if ($allowSelection && $selectorValue && $isPublicControl)
246 {
247 $renderResult .= sprintf(
248 '<option value="%s" selected data-role="expression">%s</option>',
249 htmlspecialcharsbx($selectorValue),
250 htmlspecialcharsbx($selectorValue)
251 );
252 }
253
254 $renderResult .= '</select>';
255
256 if ($allowSelection && !$isPublicControl)
257 {
258 $renderResult .= static::renderControlSelector($field, $selectorValue, true, '', $fieldType);
259 }
260
261 return $renderResult;
262 }
263
268 public static function canRenderControl($renderMode)
269 {
270 return true;
271 }
272
281 public static function renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
282 {
283 if ($renderMode & FieldType::RENDER_MODE_PUBLIC)
284 {
285 $allowSelection = false;
286 }
287
288 return static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
289 }
290
299 public static function renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
300 {
301 if ($renderMode & FieldType::RENDER_MODE_PUBLIC)
302 {
303 $allowSelection = false;
304 }
305
306 return static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
307 }
308
315 public static function renderControlOptions(FieldType $fieldType, $callbackFunctionName, $value)
316 {
317 $options = static::getFieldOptions($fieldType);
318
319 $str = '';
320 foreach ($options as $k => $v)
321 {
322 if ((string)$k !== (string)$v)
323 $str .= '['.$k.']'.$v;
324 else
325 $str .= $v;
326
327 $str .= "\n";
328 }
329
330 $rnd = randString();
331 $renderResult = '<textarea id="WFSFormOptionsX'.$rnd.'" rows="5" cols="30">'.htmlspecialcharsbx($str).'</textarea><br />';
332 $renderResult .= Loc::getMessage('BPDT_SELECT_OPTIONS1').'<br />';
333 $renderResult .= Loc::getMessage('BPDT_SELECT_OPTIONS2').'<br />';
334 $renderResult .= '<script type="text/javascript">
335 function WFSFormOptionsXFunction'.$rnd.'()
336 {
337 var result = {};
338 var i, id, val, str = document.getElementById("WFSFormOptionsX'.$rnd.'").value;
339
340 var arr = str.split(/[\r\n]+/);
341 var p, re = /\[([^\]]+)\].+/;
342 for (i in arr)
343 {
344 str = arr[i].replace(/^\s+|\s+$/g, \'\');
345 if (str.length > 0)
346 {
347 id = str.match(re);
348 if (id)
349 {
350 p = str.indexOf(\']\');
351 id = id[1];
352 val = str.substr(p + 1);
353 }
354 else
355 {
356 val = str;
357 id = val;
358 }
359 result[id] = val;
360 }
361 }
362
363 return result;
364 }
365 </script>';
366 $renderResult .= '<input type="button" onclick="'.htmlspecialcharsbx($callbackFunctionName)
367 .'(WFSFormOptionsXFunction'.$rnd.'())" value="'.Loc::getMessage('BPDT_SELECT_OPTIONS3').'">';
368
369 return $renderResult;
370 }
371
378 protected static function extractValue(FieldType $fieldType, array $field, array $request)
379 {
380 $value = parent::extractValue($fieldType, $field, $request);
381 $value =
382 !empty(static::getFieldOptions($fieldType))
383 ? self::validateValueSingle($value, $fieldType)
384 : null
385 ;
386
387 $errors = static::getErrors();
388 if (!empty($errors) && $value === null)
389 {
390 $lastErrorKey = array_key_last($errors);
391 if (!array_key_exists('parameter', $errors[$lastErrorKey]))
392 {
393 $errors[$lastErrorKey]['parameter'] = static::generateControlName($field);
394 }
395
396 static::cleanErrors();
397 static::addErrors($errors);
398 }
399
400 return $value;
401 }
402
409 public static function extractValueMultiple(FieldType $fieldType, array $field, array $request)
410 {
411 $name = $field['Field'];
412 $value = isset($request[$name]) ? $request[$name] : array();
413
414 if (!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
415 $value = array($value);
416 $value = array_unique($value);
417 $request[$name] = $value;
418 return parent::extractValueMultiple($fieldType, $field, $request);
419 }
420
427 public static function formatValueMultiple(FieldType $fieldType, $value, $format = 'printable')
428 {
429 if (\CBPHelper::isAssociativeArray($value))
430 {
431 $value = array_keys($value);
432 }
433 return parent::formatValueMultiple($fieldType, $value, $format);
434 }
435
442 public static function formatValueSingle(FieldType $fieldType, $value, $format = 'printable')
443 {
444 if (\CBPHelper::isAssociativeArray($value))
445 {
446 $keys = array_keys($value);
447 $value = isset($keys[0]) ? $keys[0] : null;
448 }
449
450 if (is_array($value))
451 {
452 $value = current(array_values($value));
453 }
454
455 return parent::formatValueSingle($fieldType, $value, $format);
456 }
457
464 public static function convertValueMultiple(FieldType $fieldType, $value, $toTypeClass)
465 {
466 if (\CBPHelper::isAssociativeArray($value))
467 {
468 $value = array_keys($value);
469 }
470 return parent::convertValueMultiple($fieldType, $value, $toTypeClass);
471 }
472
473
478 protected static function getFieldOptions(FieldType $fieldType)
479 {
480 $options = $fieldType->getOptions();
481 return self::normalizeOptions($options);
482 }
483
489 protected static function getFieldSettings(FieldType $fieldType)
490 {
491 return $fieldType->getSettings();
492 }
493
498 protected static function normalizeOptions($options)
499 {
500 $normalized = [];
501 if (is_array($options))
502 {
503 foreach ($options as $key => $value)
504 {
505 if (is_array($value) && sizeof($value) == 2)
506 {
507 $v = array_values($value);
508 $key = $v[0];
509 $value = $v[1];
510 }
511 $normalized[$key] = $value;
512 }
513 }
514 elseif ($options !== '')
515 {
516 $normalized[$options] = $options;
517 }
518
519 return $normalized;
520 }
521
522 public static function externalizeValue(FieldType $fieldType, $context, $value)
523 {
524 $map = $fieldType->getSettings()['ExternalValues'] ?? null;
525 if ($map && isset($map[$value]))
526 {
527 return $map[$value];
528 }
529
530 return parent::externalizeValue($fieldType, $context, $value);
531 }
532
533 public static function mergeValue(FieldType $fieldType, array $baseValue, $appendValue): array
534 {
535 if (\CBPHelper::isAssociativeArray($baseValue))
536 {
537 $baseValue = array_keys($baseValue);
538 }
539 if (\CBPHelper::isAssociativeArray($appendValue))
540 {
541 $appendValue = array_keys($appendValue);
542 }
543
544 return parent::mergeValue($fieldType, $baseValue, $appendValue);
545 }
546
547 public static function validateValueSingle($value, FieldType $fieldType)
548 {
549 $options = static::getFieldOptions($fieldType);
550
551 if (\CBPActivity::isExpression($value) || empty($options))
552 {
553 return $value;
554 }
555
556 if ($value === '')
557 {
558 return null;
559 }
560
561 if (!(is_string($value) || is_int($value)))
562 {
563 return null;
564 }
565
566 if (!isset($options[$value]))
567 {
568 $key = array_search($value, $options, false);
569 if ($key === false)
570 {
571 static::addError([
572 'code' => 'ErrorValue',
573 'message' => Loc::getMessage('BPDT_SELECT_INVALID'),
574 ]);
575
576 return null;
577 }
578
579 return $key;
580 }
581
582 return $value;
583 }
584
585 public static function validateValueMultiple($value, FieldType $fieldType): array
586 {
587 $value = parent::validateValueMultiple($value, $fieldType);
588
589 return array_values(array_filter($value, static fn($v) => ($v !== null)));
590 }
591
592 public static function convertPropertyToView(FieldType $fieldType, int $viewMode, array $property): array
593 {
594 if ($viewMode === FieldType::RENDER_MODE_JN_MOBILE)
595 {
596 $options = static::getFieldOptions($fieldType);
597 $property['Options'] = array_map(
598 fn($value, $name) => ['value' => $value, 'name' => $name],
599 array_keys($options),
600 array_values($options),
601 );
602 }
603
604 return parent::convertPropertyToView($fieldType, $viewMode, $property);
605 }
606
607}
static convertTo(FieldType $fieldType, $value, $toTypeClass)
Definition base.php:209
static formatValueMultiple(FieldType $fieldType, $value, $format='printable')
Definition select.php:427
static renderControlOptions(FieldType $fieldType, $callbackFunctionName, $value)
Definition select.php:315
static renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition select.php:128
static extractValueMultiple(FieldType $fieldType, array $field, array $request)
Definition select.php:409
static extractValue(FieldType $fieldType, array $field, array $request)
Definition select.php:378
static externalizeValue(FieldType $fieldType, $context, $value)
Definition select.php:522
static renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition select.php:281
static formatValuePrintable(FieldType $fieldType, $value)
Definition select.php:28
static validateValueSingle($value, FieldType $fieldType)
Definition select.php:547
static getFieldOptions(FieldType $fieldType)
Definition select.php:478
static convertValueMultiple(FieldType $fieldType, $value, $toTypeClass)
Definition select.php:464
static normalizeOptions($options)
Definition select.php:498
static convertPropertyToView(FieldType $fieldType, int $viewMode, array $property)
Definition select.php:592
static renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition select.php:299
static mergeValue(FieldType $fieldType, array $baseValue, $appendValue)
Definition select.php:533
static formatValueSingle(FieldType $fieldType, $value, $format='printable')
Definition select.php:442
static canRenderControl($renderMode)
Definition select.php:268
static getFieldSettings(FieldType $fieldType)
Definition select.php:489
static validateValueMultiple($value, FieldType $fieldType)
Definition select.php:585
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29