Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
fieldtype.php
1<?php
2namespace Bitrix\Bizproc;
3
6
12{
13
17 public const BOOL = 'bool';
18
22 public const DATE = 'date';
23
27 public const DATETIME = 'datetime';
28
32 public const DOUBLE = 'double';
33
37 public const FILE = 'file';
38
42 public const INT = 'int';
43
47 public const SELECT = 'select';
48
52 public const INTERNALSELECT = 'internalselect';
53
57 public const STRING = 'string';
58
62 public const TEXT = 'text';
63
67 public const USER = 'user';
68
72 public const TIME = 'time';
73
77 public const RENDER_MODE_DESIGNER = 1;
78
82 public const RENDER_MODE_ADMIN = 2;
83
87 public const RENDER_MODE_MOBILE = 4;
88
92 public const RENDER_MODE_PUBLIC = 8;
93
97 public const RENDER_MODE_JN_MOBILE = 16;
98
99 public const VALUE_CONTEXT_DOCUMENT = 'Document';
100 public const VALUE_CONTEXT_REST = 'rest';
101 public const VALUE_CONTEXT_JN_MOBILE = 'jn_mobile';
102
104 protected $typeClass;
105
107 protected $property;
108
110 protected $documentType;
111
113 protected $documentId;
114
121 public function __construct(array $property, array $documentType, $typeClass, array $documentId = null)
122 {
123 $this->property = static::normalizeProperty($property);
124 $this->documentType = $documentType;
125
126 $this->typeClass = $typeClass;
127 $this->documentId = $documentId;
128 }
129
133 public function getProperty()
134 {
135 return $this->property;
136 }
137
141 public function getType()
142 {
143 return isset($this->property['Type']) ? $this->property['Type'] : null;
144 }
145
149 public function getBaseType()
150 {
151 $class = $this->typeClass;
152 return $class::getType();
153 }
154
158 public function getTypeClass()
159 {
160 return $this->typeClass;
161 }
162
169 public function setTypeClass($typeClass)
170 {
171 if (is_subclass_of($typeClass, '\Bitrix\Bizproc\BaseType\Base'))
172 {
173 $this->typeClass = $typeClass;
174 }
175 else
176 throw new Main\ArgumentException('Incorrect type class.');
177
178 return $this;
179 }
180
184 public function getDocumentType()
185 {
186 return $this->documentType;
187 }
188
193 public function setDocumentType(array $documentType)
194 {
195 $this->documentType = $documentType;
196 return $this;
197 }
198
202 public function getDocumentId()
203 {
204 return $this->documentId;
205 }
206
211 public function setDocumentId(array $documentId)
212 {
213 $this->documentId = $documentId;
214 return $this;
215 }
216
220 public function isMultiple()
221 {
222 return !empty($this->property['Multiple']);
223 }
224
229 public function setMultiple($value)
230 {
231 $this->property['Multiple'] = (bool)$value;
232 return $this;
233 }
234
238 public function isRequired()
239 {
240 return !empty($this->property['Required']);
241 }
242
246 public function getOptions()
247 {
248 return isset($this->property['Options']) ? $this->property['Options'] : null;
249 }
250
255 public function setOptions($options)
256 {
257 $this->property['Options'] = $options;
258 return $this;
259 }
260
266 public function getSettings()
267 {
268 return isset($this->property['Settings']) && is_array($this->property['Settings'])
269 ? $this->property['Settings'] : array();
270 }
271
278 public function setSettings(array $settings)
279 {
280 $this->property['Settings'] = $settings;
281 return $this;
282 }
283
287 public function getName()
288 {
289 return $this->property['Name'];
290 }
291
295 public function getDescription()
296 {
297 return $this->property['Description'];
298 }
299
305 public function formatValue($value, $format = 'printable')
306 {
308
309 if ($this->isMultiple())
310 {
311 return $typeClass::formatValueMultiple($this, $value, $format);
312 }
313 else
314 {
315 return $typeClass::formatValueSingle($this, $value, $format);
316 }
317 }
318
324 public function convertValue($value, $toTypeClass)
325 {
327
328 if ($this->isMultiple())
329 {
330 return $typeClass::convertValueMultiple($this, $value, $toTypeClass);
331 }
332 else
333 {
334 return $typeClass::convertValueSingle($this, $value, $toTypeClass);
335 }
336 }
337
343 public function mergeValue($baseValue, $appendValue): array
344 {
346 $baseValue = (array) $baseValue;
347
348 if ($this->isMultiple() && !\CBPHelper::isEmptyValue($appendValue))
349 {
350 return $typeClass::mergeValue($this, $baseValue, $appendValue);
351 }
352
353 return $baseValue;
354 }
355
360 public function canRenderControl($renderMode)
361 {
363 return $typeClass::canRenderControl($renderMode);
364 }
365
373 public function renderControl(array $field, $value, $allowSelection, $renderMode)
374 {
376
377 if ($this->isMultiple())
378 {
379 return $typeClass::renderControlMultiple($this, $field, $value, $allowSelection, $renderMode);
380 }
381 else
382 {
383 return $typeClass::renderControlSingle($this, $field, $value, $allowSelection, $renderMode);
384 }
385 }
386
392 public function renderControlOptions($callbackFunctionName, $value)
393 {
395 return $typeClass::renderControlOptions($this, $callbackFunctionName, $value);
396 }
397
404 public function extractValue(array $field, array $request, array &$errors = null)
405 {
407
408 if ($this->isMultiple())
409 {
410 $result = $typeClass::extractValueMultiple($this, $field, $request);
411 }
412 else
413 {
414 $result = $typeClass::extractValueSingle($this, $field, $request);
415 }
416 $errors = $typeClass::getErrors();
417
418 return $result;
419 }
420
425 public function clearValue($value)
426 {
428
429 if ($this->isMultiple())
430 {
431 $typeClass::clearValueMultiple($this, $value);
432 }
433 else
434 {
435 $typeClass::clearValueSingle($this, $value);
436 }
437 }
438
444 public function internalizeValue($context, $value)
445 {
447
448 if ($this->isMultiple())
449 {
450 return $typeClass::internalizeValueMultiple($this, $context, $value);
451 }
452 else
453 {
454 return $typeClass::internalizeValueSingle($this, $context, $value);
455 }
456 }
457
463 public function externalizeValue($context, $value)
464 {
466
467 if ($this->isMultiple())
468 {
469 return $typeClass::externalizeValueMultiple($this, $context, $value);
470 }
471 else
472 {
473 return $typeClass::externalizeValueSingle($this, $context, $value);
474 }
475 }
476
481 public static function getBaseTypesMap()
482 {
483 return array(
484 static::BOOL => BaseType\BoolType::class,
485 static::DATE => BaseType\Date::class,
486 static::DATETIME => BaseType\Datetime::class,
487 static::DOUBLE => BaseType\Double::class,
488 static::FILE => BaseType\File::class,
489 static::INT => BaseType\IntType::class,
490 static::SELECT => BaseType\Select::class,
491 static::STRING => BaseType\StringType::class,
492 static::TEXT => BaseType\Text::class,
493 static::USER => BaseType\User::class,
494 static::INTERNALSELECT => BaseType\InternalSelect::class,
495 static::TIME => BaseType\Time::class,
496 );
497 }
498
499 public static function isBaseType(string $type): bool
500 {
501 return array_key_exists($type, static::getBaseTypesMap());
502 }
503
504 public static function convertUfType(string $type): ?string
505 {
506 $bpType = null;
507 switch ($type)
508 {
509 case 'string':
510 case 'datetime':
511 case 'date':
512 case 'double':
513 case 'file':
514 $bpType = $type;
515 break;
516 case 'integer':
517 $bpType = 'int';
518 break;
519 case 'boolean':
520 $bpType = 'bool';
521 break;
522 case 'employee':
523 $bpType = 'user';
524 break;
525 case 'enumeration':
526 $bpType = 'select';
527 break;
528 case 'money':
529 case 'url':
530 case 'address':
531 case 'resourcebooking':
532 case 'crm_status':
533 case 'iblock_section':
534 case 'iblock_element':
535 case 'crm':
536 $bpType = "UF:{$type}";
537 break;
538 }
539
540 return $bpType;
541 }
542
548 public static function normalizeProperty($property)
549 {
550 $normalized = [
551 'Id' => null,
552 'Type' => null,
553
554 'Name' => null,
555 'Description' => null,
556
557 'Multiple' => false,
558 'Required' => false,
559
560 'Options' => null,
561 'Settings' => null,
562 'Default' => null,
563 ];
564
565 if (!is_array($property))
566 {
567 $normalized['Type'] = (string) $property;
568
569 return $normalized;
570 }
571
572 foreach ($property as $key => $val)
573 {
574 switch(mb_strtoupper($key))
575 {
576 case 'TYPE':
577 case '0':
578 $normalized['Type'] = (string)$val;
579 break;
580 case 'MULTIPLE':
581 case '1':
582 $normalized['Multiple'] = \CBPHelper::getBool($val);
583 break;
584 case 'REQUIRED':
585 case '2':
586 $normalized['Required'] = \CBPHelper::getBool($val);
587 break;
588 case 'OPTIONS':
589 case '3':
590 $normalized['Options'] = is_array($val)? $val : (string)$val;
591 break;
592 case 'SETTINGS':
593 $normalized['Settings'] = is_array($val) ? $val : null;
594 break;
595 case 'ID':
596 $normalized['Id'] = (string)$val;
597 break;
598 case 'NAME':
599 case 'TITLE':
600 $normalized['Name'] = (string)$val;
601 break;
602 case 'DESCRIPTION':
603 $normalized['Description'] = (string)$val;
604 break;
605 case 'DEFAULT':
606 $normalized['Default'] = $val;
607 break;
608 }
609 }
610
611 return $normalized;
612 }
613
618 public function setValue($value)
619 {
621
622 $this->property['Default'] =
623 ($this->isMultiple())
624 ? $typeClass::validateValueMultiple($value, $this)
625 : $typeClass::validateValueSingle($value, $this)
626 ;
627 }
628
632 public function getValue()
633 {
634 return $this->property['Default'];
635 }
636
637 public function convertPropertyToView(int $viewMode): array
638 {
640
641 return $typeClass::convertPropertyToView($this, $viewMode, $this->getProperty());
642 }
643}
renderControl(array $field, $value, $allowSelection, $renderMode)
canRenderControl($renderMode)
static convertUfType(string $type)
setDocumentType(array $documentType)
externalizeValue($context, $value)
setDocumentId(array $documentId)
extractValue(array $field, array $request, array &$errors=null)
__construct(array $property, array $documentType, $typeClass, array $documentId=null)
internalizeValue($context, $value)
convertValue($value, $toTypeClass)
setSettings(array $settings)
formatValue($value, $format='printable')
static normalizeProperty($property)
static isBaseType(string $type)
mergeValue($baseValue, $appendValue)
renderControlOptions($callbackFunctionName, $value)
convertPropertyToView(int $viewMode)