98 self::IS_DELIVERED => [
110 self::CAN_ANSWER => [
118 self::LARGE_FONT => [
123 'className' => \Bitrix\Im\V2\Message\Param\AttachArray::class,
124 'classItem' => \Bitrix\Im\V2\Message\Param\Attach::class,
128 'className' => \Bitrix\Im\V2\Message\Param\Menu::class,
132 'className' => \Bitrix\Im\V2\Message\Param\Keyboard::class,
135 self::KEYBOARD_UID => [
143 self::SENDING_TS => [
152 'className' => \Bitrix\Im\V2\Message\Param\UserAvatar::class,
156 'className' => \Bitrix\Im\V2\Message\Param\UserName::class,
171 self::COMPONENT_ID => [
175 self::COMPONENT_PARAMS => [
176 'className' => Message\Param\ComponentParams::class,
179 self::STYLE_CLASS => [
191 self::CHAT_MESSAGE => [
207 self::KEYBOARD_ACTION => [
213 self::LINK_ACTIVE => [
219 self::CHAT_LAST_DATE => [
223 'className' => \Bitrix\Im\V2\Message\Param\TextDate::class,
226 self::IS_ROBOT_MESSAGE => [
229 self::FORWARD_ID => [
233 self::FORWARD_USER_ID => [
237 self::FORWARD_CONTEXT_ID => [
250 self::CRM_FORM_FILLED => [
254 self::CRM_FORM_ID => [
258 self::CRM_FORM_SEC => [
277 public static function getType(
string $paramName): array
281 if (isset(self::$typeMap[$paramName]))
283 $type = self::$typeMap[$paramName];
284 if (!isset($type[
'className']))
288 $type[
'className'] = ParamArray::class;
292 $type[
'className'] = Param\DateTime::class;
308 $type[
'isHidden'] ??=
false;
319 public static function addType(
string $paramName, array $description): void
321 self::$typeMap[$paramName] = $description;
330 if (!self::$typeLoaded)
332 $event =
new Event(
'im', self::EVENT_MESSAGE_PARAM_TYPE_INIT);
334 $resultList = $event->getResults();
336 foreach ($resultList as $eventResult)
338 if ($eventResult->getType() === EventResult::SUCCESS)
340 $settings = $eventResult->getParameters();
341 if (is_array($settings))
343 foreach ($settings as $paramName => $description)
346 is_array($description)
348 !empty($description[
'type'])
349 || !empty($description[
'className'])
361 self::$typeLoaded =
true;
383 if (is_array($source))
387 elseif ($source instanceof ORM\Objectify\
Collection)
389 $result = $this->initByEntitiesCollection($source);
400 if ($result->isSuccess())
402 foreach ($this as $param)
404 if ($param->getMessageId())
442 ->setSelect([
'ID',
'MESSAGE_ID',
'PARAM_NAME',
'PARAM_VALUE',
'PARAM_JSON'])
447 return $this->initByEntitiesCollection($collection);
457 protected function initByEntitiesCollection(ORM\Objectify\
Collection $entitiesCollection):
Result
460 foreach ($entitiesCollection as $entity)
462 $paramName = $entity->getParamName();
463 if (!parent::offsetExists($paramName))
468 $item = $this[$paramName];
472 if (isset($type[
'classItem']))
474 $classItem = $type[
'classItem'];
475 $item->add(
new $classItem($entity));
479 $item->add(
new Param($entity));
484 $item->load($entity);
499 foreach ($items as $entityId => $entity)
501 if (is_array($entity) && isset($entity[
'PARAM_NAME']))
503 $paramName = $entity[
'PARAM_NAME'];
504 if (!parent::offsetExists($paramName))
509 $item = $this[$paramName];
513 if (isset($type[
'classItem']))
515 $classItem = $type[
'classItem'];
516 $item->add(
new $classItem($entity));
520 $item->add(
new Param($entity));
525 $item->load($entity);
530 if (!parent::offsetExists($entityId))
535 $this[$entityId]->setValue($entity);
548 $paramName = $entity->getParamName();
549 if (!parent::offsetExists($paramName))
554 $item = $this[$paramName];
558 if (isset($type[
'classItem']))
560 $classItem = $type[
'classItem'];
561 $item->add(
new $classItem($entity));
565 $item->add(
new Param($entity));
570 $item->load($entity);
584 public function save():
Result
590 $dataEntityCollection =
new $entityCollectionClass;
594 $itemKeyToUnset = [];
595 foreach ($this as $item)
597 if ($item->isDeleted())
599 $itemKeyToUnset[] = $item->getName();
602 if (!$item->hasValue())
607 if ($item instanceof Param)
609 $prepareResult = $item->prepareFields();
610 if ($prepareResult->isSuccess())
612 if ($item->isChanged())
614 $dataEntityCollection->add($item->getDataEntity());
615 $item->markChanged(
false);
620 $result->addErrors($prepareResult->getErrors());
623 elseif ($item instanceof ParamArray)
625 $subItemKeyToUnset = [];
626 foreach ($item as $key => $subItem)
628 if ($subItem->isDeleted())
630 if ($subItem->getPrimaryId())
632 $dropIds[] = $subItem->getPrimaryId();
633 $subItemKeyToUnset[] = $key;
638 $prepareResult = $subItem->prepareFields();
639 if ($prepareResult->isSuccess())
641 if ($subItem->isChanged())
643 $dataEntityCollection->add($subItem->getDataEntity());
644 $subItem->markChanged(
false);
649 $result->addErrors($prepareResult->getErrors());
653 $item->unsetByKeys($subItemKeyToUnset);
658 foreach ($this->droppedItems as $item)
660 if ($item instanceof Param)
662 if ($item->getPrimaryId())
664 $dropIds[] = $item->getPrimaryId();
667 elseif ($item instanceof ParamArray)
669 foreach ($item as $subItem)
671 if ($subItem->getPrimaryId())
673 $dropIds[] = $subItem->getPrimaryId();
679 $saveResult = $dataEntityCollection->save(
true);
680 if ($saveResult->isSuccess())
682 if (!empty($dropIds))
684 MessageParamTable::deleteBatch([
'=ID' => $dropIds]);
687 $this->droppedItems = [];
691 $result->addErrors($saveResult->getErrors());
703 public function delete(
bool $deleteWithTs =
false):
Result
708 foreach ($this as $key => $item)
710 $keysToUnset[$key] = $key;
723 MessageParamTable::deleteBatch($filter);
726 $this->droppedItems = [];
746 if ($type[
'className'])
748 $paramClass = $type[
'className'];
755 return (
new $paramClass)
756 ->setName($paramName)
765 public function isSet(
string $paramName): bool
767 return isset($this[$paramName])
768 && $this[$paramName]->hasValue()
769 && !$this[$paramName]->isDeleted()
780 !parent::offsetExists($offset)
789 return parent::offsetExists($offset);
798 if (!isset($this[$paramName]))
803 return $this[$paramName];
811 public function set(
string $paramName, $parameter): self
815 $this[$paramName] = $parameter;
820 $this[$paramName]->load($parameter);
831 public function isValid():
Result
836 foreach ($this as $param)
838 $validParamResult = $param->isValid();
839 if (!$validParamResult->isSuccess())
841 $result->addErrors($validParamResult->getErrors());
854 $this[$parameter->
getName()] = $parameter;
876 $entryClass = \get_class($entry);
877 throw new ArgumentTypeException(
"Entry is instance of {$entryClass}, but collection support MessageParameter");
885 parent::offsetSet($offset, $entry);
892 public function remove(
string $paramName =
''): self
894 if (empty($paramName))
897 foreach ($this as $paramName => $param)
899 $keysToUnset[$paramName] = $paramName;
906 unset($this[$paramName]);
918 if (parent::offsetExists($offset))
920 $this[$offset]->markDrop();
921 $this->droppedItems[] = $this[$offset];
924 parent::offsetUnset($offset);
934 foreach ($this as $param)
936 $param->setMessageId($this->messageId);
955 foreach ($this as $paramName => $param)
957 if ($param->hasValue() && !$param->isHidden())
959 $result[$paramName] = $param->toRestFormat();
972 foreach ($this as $paramName => $param)
974 if ($param->hasValue() && !$param->isHidden())
976 $result[$paramName] = $param->toPullFormat();
980 if (!isset($extraParams))
985 foreach ($extraParams as $extraParam)
987 if (!isset($result[$extraParam]))
989 $result[$extraParam] =
Params::create($extraParam)->toPullFormat();
999 public function fill(array $values): self
1001 foreach ($values as $paramName => $value)
1003 $this->
get($paramName)->setValue($value);
1011 foreach ($this as $key => $param)
1013 $this[$key] = clone $param;