Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
CommentChat.php
1<?php
2
3namespace Bitrix\Im\V2\Chat;
4
9
14{
15 protected ?Chat $parentChat;
16
17 protected function getDefaultType(): string
18 {
19 return self::IM_TYPE_COMMENT;
20 }
21
22 public function setParentChatId(int $parentId): self
23 {
24 if (!$parentId)
25 {
26 return $this;
27 }
28
29 $parentChat = ChatFactory::getInstance()->getChat($parentId);
30
31 if ($parentChat->getType() !== self::IM_TYPE_CHAT)
32 {
33 return $this;
34 }
35
36 $this->parentChat = $parentChat;
37
38 return parent::setParentChatId($parentId);
39 }
40
41 public function setParentMessageId(int $messageId): self
42 {
43 if ($messageId)
44 {
45 $message = new Message($messageId);
46 if ($message->getChatId() && $message->getChatId() == $this->getParentChatId())
47 {
48 return parent::setParentMessageId($messageId);
49 }
50 }
51
52 return $this;
53 }
54
55 public function getParent(): ?Chat
56 {
57 return $this->parentChat;
58 }
59
60 public function add(array $params, ?Context $context = null): Result
61 {
62 $result = new Result;
63
64 $paramsResult = $this->prepareParams($params);
65 if ($paramsResult->isSuccess())
66 {
67 $params = $paramsResult->getResult();
68 }
69 else
70 {
71 return $result->addErrors($paramsResult->getErrors());
72 }
73
74 $chat = new static($params);
75 $chat->setParentChatId($params['PARENT_ID']);
76 if (!$chat->getParent())
77 {
78 return $result->addError(new ChatError(ChatError::WRONG_PARENT_CHAT));
79 }
80 $chat
81 ->setExtranet($chat->getParent()->getExtranet())
82 ->setManageUsersAdd($chat->getParent()->getManageUsersAdd())
83 ->setManageUsersDelete($chat->getParent()->getManageUsersDelete())
84 ->setManageUI($chat->getParent()->getManageUI())
85 ->setParentMessageId($params['PARENT_MID'])
86 ;
87 if (!$chat->getParentMessageId())
88 {
89 return $result->addError(new ChatError(ChatError::WRONG_PARENT_MESSAGE));
90 }
91 $chat->save();
92
93 if (!$chat->getChatId())
94 {
95 return $result->addError(new ChatError(ChatError::CREATION_ERROR));
96 }
97
98 $result->setResult([
99 'CHAT_ID' => $chat->getChatId(),
100 'CHAT' => $chat,
101 ]);
102
103 return $result;
104 }
105
106 protected function prepareParams(array $params = []): Result
107 {
108 $result = new Result();
109
110 if (!isset($params['PARENT_ID']) || !(int)$params['PARENT_ID'])
111 {
112 return $result->addError(new ChatError(ChatError::WRONG_PARENT_CHAT));
113 }
114
115 if (!isset($params['PARENT_MID']) || !(int)$params['PARENT_MID'])
116 {
117 return $result->addError(new ChatError(ChatError::WRONG_PARENT_MESSAGE));
118 }
119
120 return parent::prepareParams($params);
121 }
122
123 public function hasAccess($user = null): bool
124 {
125 $parent = $this->getParent();
126 if ($parent)
127 {
128 return $parent->hasAccess($user);
129 }
130
131 return false;
132 }
133
134 protected function addIndex(): Chat
135 {
136 return $this;
137 }
138
139 protected function updateIndex(): Chat
140 {
141 return $this;
142 }
143}
static getType($chatData)
Definition chat.php:41
add(array $params, ?Context $context=null)
prepareParams(array $params=[])
setParentMessageId(int $messageId)