Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
2
4
8
13class Base
14{
19 public static function getName(): string
20 {
21 return static::getType();
22 }
23
27 public static function getType()
28 {
29 return FieldType::STRING;
30 }
31
33 protected static $formats = [
34 'printable' => [
35 'callable' => 'formatValuePrintable',
36 'separator' => ', ',
37 ],
38 ];
39
44 protected static function getFormatCallable($format)
45 {
46 $format = mb_strtolower($format);
47 $formats = static::getFormats();
48 if (isset($formats[$format]['callable']))
49 {
50 $callable = $formats[$format]['callable'];
51 if (is_string($callable))
52 {
53 $callable = [get_called_class(), $callable];
54 }
55
56 return $callable;
57 }
58
59 return null;
60 }
61
66 protected static function getFormatSeparator($format)
67 {
68 $format = mb_strtolower($format);
69 $separator = ', '; //default - coma
70 $formats = static::getFormats();
71 if (isset($formats[$format]['separator']))
72 {
73 $separator = $formats[$format]['separator'];
74 }
75
76 return $separator;
77 }
78
85 public static function addFormat($name, array $options)
86 {
87 $name = mb_strtolower($name);
88 if (empty($options['callable']))
89 {
90 throw new Main\ArgumentException('Callable property in format options is not set.');
91 }
92
93 static::$formats[$name] = $options;
94 }
95
100 public static function getFormats()
101 {
102 return static::$formats;
103 }
104
112 public static function toSingleValue(FieldType $fieldType, $value)
113 {
114 return $value;
115 }
116
123 public static function formatValueMultiple(FieldType $fieldType, $value, $format = 'printable')
124 {
125 $value = (array)$value;
126
127 foreach ($value as $k => $v)
128 {
129 $value[$k] = static::formatValueSingle($fieldType, $v, $format);
130 }
131
132 return implode(static::getFormatSeparator($format), $value);
133 }
134
141 public static function formatValueSingle(FieldType $fieldType, $value, $format = 'printable')
142 {
143 $callable = static::getFormatCallable($format);
144 $value = static::toSingleValue($fieldType, $value);
145
146 if (is_callable($callable))
147 {
148 return call_user_func($callable, $fieldType, $value);
149 }
150
151 //return original value if format not found
152 return $value;
153 }
154
160 protected static function formatValuePrintable(FieldType $fieldType, $value)
161 {
162 return static::convertValueSingle(clone $fieldType, $value, StringType::class);
163 }
164
171 public static function convertValueMultiple(FieldType $fieldType, $value, $toTypeClass)
172 {
173 $value = (array)$value;
174 foreach ($value as $k => $v)
175 {
176 $value[$k] = static::convertValueSingle($fieldType, $v, $toTypeClass);
177 }
178
179 return $value;
180 }
181
189 public static function convertValueSingle(FieldType $fieldType, $value, $toTypeClass)
190 {
191 $value = static::toSingleValue($fieldType, $value);
193 $result = static::convertTo($fieldType, $value, $toTypeClass);
194 if ($result === null)
195 $result = $toTypeClass::convertFrom($fieldType, $value, get_called_class());
196
197 if ($result !== null)
198 $fieldType->setTypeClass($toTypeClass);
199
200 return $result !== null ? $result : $value;
201 }
202
209 public static function convertTo(FieldType $fieldType, $value, $toTypeClass)
210 {
211 return null;
212 }
213
220 public static function convertFrom(FieldType $fieldType, $value, $fromTypeClass)
221 {
222 return null;
223 }
224
229 public static function getConversionMap()
230 {
231 return [
232 //to
233 [],
234 //from
235 [],
236 ];
237 }
238
245 public static function mergeValue(FieldType $fieldType, array $baseValue, $appendValue): array
246 {
247 if (\CBPHelper::isEmptyValue($baseValue))
248 {
249 return (array)$appendValue;
250 }
251
252 if (!is_array($appendValue))
253 {
254 $baseValue[] = $appendValue;
255
256 return $baseValue;
257 }
258
259 $isSimple = !\CBPHelper::isAssociativeArray($baseValue) && !\CBPHelper::isAssociativeArray($appendValue);
260 $result = $isSimple ? array_merge($baseValue, $appendValue) : $baseValue + $appendValue;
261
262 if ($isSimple)
263 {
264 $result = array_values(array_unique($result));
265 }
266
267 return $result;
268 }
269
273 protected static $errors = [];
274
278 public static function addError($error)
279 {
280 static::$errors[] = $error;
281 }
282
287 public static function addErrors(array $errors)
288 {
289 static::$errors = array_merge(static::$errors, $errors);
290 }
291
295 public static function getErrors()
296 {
297 return static::$errors;
298 }
299
303 protected static function cleanErrors()
304 {
305 static::$errors = [];
306 }
307
312 protected static function generateControlId(array $field)
313 {
314 $id = 'id_' . $field['Field'];
315 $index = isset($field['Index']) ? $field['Index'] : null;
316 if ($index !== null)
317 {
318 $id .= '__n' . $index . '_';
319 }
320
321 return $id;
322 }
323
328 protected static function generateControlName(array $field)
329 {
330 $name = $field['Field'];
331 $index = isset($field['Index']) ? $field['Index'] : null;
332 if ($index !== null)
333 {
334 //new multiple name style
335 $name .= '[]';
336 }
337
338 return $name;
339 }
340
341 protected static function generateControlClassName(FieldType $fieldType, array $field)
342 {
343 $prefix = 'bizproc-type-control';
344 $classes = [$prefix];
345 $classes[] = $prefix . '-' . static::getType();
346
347 if ($fieldType->isMultiple())
348 {
349 $classes[] = $prefix . '-multiple';
350 }
351 if ($fieldType->isRequired())
352 {
353 $classes[] = $prefix . '-required';
354 }
355
356 return implode(' ', $classes);
357 }
358
364 protected static function wrapCloneableControls(array $controls, $wrapperId)
365 {
366 $wrapperId = (string)$wrapperId;
367 $renderResult = '<table width="100%" border="0" cellpadding="2" cellspacing="2" id="BizprocCloneable_'
368 . htmlspecialcharsbx($wrapperId) . '">';
369
370 foreach ($controls as $control)
371 {
372 $renderResult .= '<tr><td>' . $control . '</td></tr>';
373 }
374 $renderResult .= '</table>';
375 $renderResult .= sprintf(
376 '<input type="button" value="%s" onclick="BX.Bizproc.cloneTypeControl(\'BizprocCloneable_%s\')"/><br />',
377 Loc::getMessage('BPDT_BASE_ADD'),
378 htmlspecialcharsbx($wrapperId)
379 );
380
381 return $renderResult;
382 }
383
390 protected static function renderPublicMultipleWrapper(FieldType $fieldType, array $field, array $controls)
391 {
392 $messageAdd = Loc::getMessage('BPDT_BASE_ADD');
393
394 $name = Main\Text\HtmlFilter::encode(\CUtil::jsEscape(static::generateControlName($field)));
395 $property = Main\Text\HtmlFilter::encode(Main\Web\Json::encode($fieldType->getProperty()));
396
397 $renderResult = implode('', $controls) . <<<HTML
398 <div>
399 <a onclick="BX.Bizproc.FieldType.cloneControl({$property}, '{$name}', this.parentNode); return false;"
400 class="bizproc-type-control-clone-btn">
401 {$messageAdd}
402 </a>
403 </div>
404HTML;
405
406 return $renderResult;
407 }
408
418 protected static function renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
419 {
420 $name = static::generateControlName($field);
421 $controlId = static::generateControlId($field);
422 $className = static::generateControlClassName($fieldType, $field);
423
424 if ($renderMode&FieldType::RENDER_MODE_PUBLIC)
425 {
426 $selectorAttributes = '';
427 if ($allowSelection)
428 {
429 $selectorAttributes = sprintf(
430 'data-role="inline-selector-target" data-property="%s" ',
431 htmlspecialcharsbx(Main\Web\Json::encode($fieldType->getProperty()))
432 );
433 }
434
435 return sprintf(
436 '<input type="text" class="%s" name="%s" value="%s" placeholder="%s" %s/>',
437 htmlspecialcharsbx($className),
438 htmlspecialcharsbx($name),
439 htmlspecialcharsbx((string)$value),
440 htmlspecialcharsbx($fieldType->getDescription()),
441 $selectorAttributes
442 );
443 }
444
445 // example: control rendering
446 return sprintf(
447 '<input type="text" class="%s" size="40" id="%s" name="%s" value="%s"/>',
448 htmlspecialcharsbx($className),
449 htmlspecialcharsbx($controlId),
450 htmlspecialcharsbx($name),
451 htmlspecialcharsbx((string)$value)
452 );
453 }
454
459 public static function canRenderControl($renderMode)
460 {
461 if ($renderMode&FieldType::RENDER_MODE_MOBILE)
462 {
463 return false;
464 }
465
466 return true;
467 }
468
477 public static function renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
478 {
479 $value = static::toSingleValue($fieldType, $value);
480 $selectorValue = null;
481 if ($allowSelection && \CBPActivity::isExpression($value))
482 {
483 $selectorValue = $value;
484 $value = null;
485 }
486
487 $renderResult = static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
488
489 if ($allowSelection)
490 {
491 $renderResult .= static::renderControlSelector($field, $selectorValue, true, '', $fieldType);
492 }
493
494 return $renderResult;
495 }
496
505 public static function renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
506 {
507 $selectorValue = null;
508 $typeValue = [];
509 if (!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
510 {
511 $value = [$value];
512 }
513
514 foreach ($value as $v)
515 {
516 if (\CBPActivity::isExpression($v))
517 {
518 $selectorValue = $v;
519 }
520 else
521 {
522 $typeValue[] = $v;
523 }
524 }
525 // need to show at least one control
526 if (empty($typeValue))
527 {
528 $typeValue[] = null;
529 }
530
531 $controls = [];
532
533 foreach ($typeValue as $k => $v)
534 {
535 $singleField = $field;
536 $singleField['Index'] = $k;
537 $controls[] = static::renderControl(
538 $fieldType,
539 $singleField,
540 $v,
541 $allowSelection,
542 $renderMode
543 );
544 }
545
546 if ($renderMode&FieldType::RENDER_MODE_PUBLIC)
547 {
548 $renderResult = static::renderPublicMultipleWrapper($fieldType, $field, $controls);
549 }
550 else
551 {
552 $renderResult = static::wrapCloneableControls($controls, static::generateControlName($field));
553 }
554
555 if ($allowSelection)
556 {
557 $renderResult .= static::renderControlSelector($field, $selectorValue, true, '', $fieldType);
558 }
559
560 return $renderResult;
561 }
562
571 protected static function renderControlSelector(array $field, $value = null, $showInput = false, $selectorMode = '', FieldType $fieldType = null)
572 {
573 $html = '';
574 $controlId = static::generateControlId($field);
575 $name = static::generateControlName($field);
576
577 if ($showInput)
578 {
579 if ($showInput !== 'combine')
580 {
581 $controlId = $controlId . '_text';
582 $name = static::generateControlName($field) . '_text';
583 }
584
585 $cols = 70;
586 $rows = max((static::getType() === FieldType::TEXT ? 5 : 1), min(5, ceil(mb_strlen((string)$value)) / $cols));
587 $html = '<table cellpadding="0" cellspacing="0" border="0" width="100%" style="margin: 2px 0"><tr><td valign="top"><textarea ';
588 $html .= 'rows="' . $rows . '" ';
589 $html .= 'cols="' . $cols . '" ';
590 $html .= 'name="' . htmlspecialcharsbx($name) . '" ';
591 $html .= 'id="' . htmlspecialcharsbx($controlId) . '" ';
592 $html .= 'style="width: 100%"';
593 $html .= '>' . htmlspecialcharsbx((string)$value);
594 $html .= '</textarea></td>';
595 $html .= '<td valign="top" style="padding-left:4px" width="30">';
596 }
597 $html .= static::renderControlSelectorButton($controlId, $fieldType, $selectorMode);
598
599 if ($showInput)
600 {
601 $html .= '</td></tr></table>';
602 }
603
604 return $html;
605 }
606
607 protected static function renderControlSelectorButton($controlId, FieldType $fieldType, $selectorMode = '')
608 {
609 $baseType = $fieldType ? $fieldType->getBaseType() : null;
610 $selectorProps = Main\Web\Json::encode([
611 'controlId' => $controlId,
612 'baseType' => $baseType,
613 'type' => $fieldType ? $fieldType->getType() : null,
614 'documentType' => $fieldType ? $fieldType->getDocumentType() : null,
615 'documentId' => $fieldType ? $fieldType->getDocumentId() : null,
616 ]);
617
618 return sprintf(
619 '<input type="button" value="..." onclick="BPAShowSelector(\'%s\', \'%s\', %s, null, %s);" data-role="bp-selector-button" data-bp-selector-props="%s">',
620 \CUtil::jsEscape(htmlspecialcharsbx($controlId)),
621 \CUtil::jsEscape(htmlspecialcharsbx($baseType)),
622 $selectorMode ? '\'' . \CUtil::jsEscape(htmlspecialcharsbx($selectorMode)) . '\'' : 'null',
623 htmlspecialcharsbx(Main\Web\Json::encode($fieldType ? $fieldType->getDocumentType() : null)),
624 htmlspecialcharsbx($selectorProps)
625 );
626 }
627
634 public static function renderControlOptions(FieldType $fieldType, $callbackFunctionName, $value)
635 {
636 return '';
637 }
638
645 protected static function extractValue(FieldType $fieldType, array $field, array $request)
646 {
647 $name = $field['Field'];
648 $value = isset($request[$name]) ? $request[$name] : null;
649 $fieldIndex = isset($field['Index']) ? $field['Index'] : null;
650 if (is_array($value) && !\CBPHelper::isAssociativeArray($value))
651 {
652 if ($fieldIndex !== null)
653 {
654 $value = isset($value[$fieldIndex]) ? $value[$fieldIndex] : null;
655 }
656 else
657 {
658 reset($value);
659 $value = current($value);
660 }
661 }
662
663 return $value;
664 }
665
672 public static function extractValueSingle(FieldType $fieldType, array $field, array $request)
673 {
674 static::cleanErrors();
675 $result = static::extractValue($fieldType, $field, $request);
676 if ($result === null || $result === '')
677 {
678 $nameText = $field['Field'] . '_text';
679 $text = isset($request[$nameText]) ? $request[$nameText] : null;
680 if (\CBPActivity::isExpression($text))
681 $result = $text;
682 }
683
684 return $result;
685 }
686
693 public static function extractValueMultiple(FieldType $fieldType, array $field, array $request)
694 {
695 static::cleanErrors();
696
697 $name = $field['Field'];
698 $value = isset($request[$name]) ? $request[$name] : [];
699
700 if (!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
701 {
702 $value = [$value];
703 }
704
705 foreach ($value as $k => $v)
706 {
707 $field['Index'] = $k;
708 $result = static::extractValue($fieldType, $field, $request);
709 if ($result === null || $result === '')
710 {
711 unset($value[$k]);
712 }
713 else
714 {
715 $value[$k] = $result;
716 }
717 }
718
719 //append selector value
720 $nameText = $field['Field'] . '_text';
721 $text = isset($request[$nameText]) ? $request[$nameText] : null;
722 if (\CBPActivity::isExpression($text))
723 {
724 $value[] = $text;
725 }
726
727 return array_values($value);
728 }
729
735 public static function clearValueSingle(FieldType $fieldType, $value)
736 {
737 //Method fires when workflow was complete
738 }
739
745 public static function clearValueMultiple(FieldType $fieldType, $value)
746 {
747 if (!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
748 {
749 $value = [$value];
750 }
751
752 foreach ($value as $v)
753 {
754 static::clearValueSingle($fieldType, $v);
755 }
756 }
757
764 public static function internalizeValue(FieldType $fieldType, $context, $value)
765 {
766 return $value;
767 }
768
775 public static function internalizeValueSingle(FieldType $fieldType, $context, $value)
776 {
777 return static::internalizeValue($fieldType, $context, $value);
778 }
779
786 public static function internalizeValueMultiple(FieldType $fieldType, $context, $value)
787 {
788 if (is_array($value))
789 {
790 foreach ($value as $k => $v)
791 {
792 $value[$k] = static::internalizeValue($fieldType, $context, $v);
793 }
794 }
795
796 return $value;
797 }
798
805 public static function externalizeValue(FieldType $fieldType, $context, $value)
806 {
807 if (is_object($value) && method_exists($value, '__toString'))
808 {
809 return (string)$value;
810 }
811
812 return $value;
813 }
814
821 public static function externalizeValueSingle(FieldType $fieldType, $context, $value)
822 {
823 return static::externalizeValue($fieldType, $context, $value);
824 }
825
832 public static function externalizeValueMultiple(FieldType $fieldType, $context, $value)
833 {
834 if (!is_array($value) || \CBPHelper::isAssociativeArray($value))
835 {
836 $value = [$value];
837 }
838
839 foreach ($value as $k => $v)
840 {
841 $value[$k] = static::externalizeValue($fieldType, $context, $v);
842 }
843
844 return $value;
845 }
846
852 public static function compareValues($valueA, $valueB)
853 {
854 if ($valueA > $valueB)
855 {
856 return 1;
857 }
858
859 if ($valueA < $valueB)
860 {
861 return -1;
862 }
863
864 return 0;
865 }
866
867 public static function validateValueSingle($value, FieldType $fieldType)
868 {
869 return static::toSingleValue($fieldType, $value);
870 }
871
872 public static function validateValueMultiple($value, FieldType $fieldType): array
873 {
874 if (!is_array($value))
875 {
876 $value = [$value];
877 }
878
879 foreach ($value as $k => $v)
880 {
881 $value[$k] = static::validateValueSingle($v, $fieldType);
882 }
883
884 return $value;
885 }
886
887 public static function convertPropertyToView(FieldType $fieldType, int $viewMode, array $property): array
888 {
889 return $property;
890 }
891}
static formatValueMultiple(FieldType $fieldType, $value, $format='printable')
Definition base.php:123
static generateControlClassName(FieldType $fieldType, array $field)
Definition base.php:341
static externalizeValueSingle(FieldType $fieldType, $context, $value)
Definition base.php:821
static renderControlOptions(FieldType $fieldType, $callbackFunctionName, $value)
Definition base.php:634
static renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition base.php:418
static extractValueSingle(FieldType $fieldType, array $field, array $request)
Definition base.php:672
static extractValueMultiple(FieldType $fieldType, array $field, array $request)
Definition base.php:693
static renderControlSelector(array $field, $value=null, $showInput=false, $selectorMode='', FieldType $fieldType=null)
Definition base.php:571
static internalizeValueSingle(FieldType $fieldType, $context, $value)
Definition base.php:775
static generateControlName(array $field)
Definition base.php:328
static extractValue(FieldType $fieldType, array $field, array $request)
Definition base.php:645
static externalizeValue(FieldType $fieldType, $context, $value)
Definition base.php:805
static clearValueSingle(FieldType $fieldType, $value)
Definition base.php:735
static renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition base.php:477
static formatValuePrintable(FieldType $fieldType, $value)
Definition base.php:160
static clearValueMultiple(FieldType $fieldType, $value)
Definition base.php:745
static getFormatSeparator($format)
Definition base.php:66
static addError($error)
Definition base.php:278
static internalizeValueMultiple(FieldType $fieldType, $context, $value)
Definition base.php:786
static internalizeValue(FieldType $fieldType, $context, $value)
Definition base.php:764
static validateValueSingle($value, FieldType $fieldType)
Definition base.php:867
static toSingleValue(FieldType $fieldType, $value)
Definition base.php:112
static convertTo(FieldType $fieldType, $value, $toTypeClass)
Definition base.php:209
static convertValueMultiple(FieldType $fieldType, $value, $toTypeClass)
Definition base.php:171
static addErrors(array $errors)
Definition base.php:287
static wrapCloneableControls(array $controls, $wrapperId)
Definition base.php:364
static getFormatCallable($format)
Definition base.php:44
static convertPropertyToView(FieldType $fieldType, int $viewMode, array $property)
Definition base.php:887
static renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition base.php:505
static mergeValue(FieldType $fieldType, array $baseValue, $appendValue)
Definition base.php:245
static generateControlId(array $field)
Definition base.php:312
static convertFrom(FieldType $fieldType, $value, $fromTypeClass)
Definition base.php:220
static formatValueSingle(FieldType $fieldType, $value, $format='printable')
Definition base.php:141
static compareValues($valueA, $valueB)
Definition base.php:852
static externalizeValueMultiple(FieldType $fieldType, $context, $value)
Definition base.php:832
static canRenderControl($renderMode)
Definition base.php:459
static renderControlSelectorButton($controlId, FieldType $fieldType, $selectorMode='')
Definition base.php:607
static renderPublicMultipleWrapper(FieldType $fieldType, array $field, array $controls)
Definition base.php:390
static addFormat($name, array $options)
Definition base.php:85
static validateValueMultiple($value, FieldType $fieldType)
Definition base.php:872
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29