1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
eventcategoryservice.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Integration\Im;
4
5use Bitrix\Calendar\Core\EventCategory\EventCategory;
6use Bitrix\Calendar\OpenEvents\Provider;
7use Bitrix\Im\Color;
8use Bitrix\Im\Model\ChatTable;
9use Bitrix\Im\V2\Chat;
10use Bitrix\Im\V2\Chat\ChatFactory;
11use Bitrix\Im\V2\Chat\CommentChat;
12use Bitrix\Im\V2\Message;
13use Bitrix\Main\Loader;
14use Bitrix\Main\NotSupportedException;
15
17{
26 public const OPEN_EVENT_CATEGORY_IM_ENTITY_TYPE = 'CALENDAR_EVENT_CATEGORY';
27
28 public function __construct()
29 {
30 if (!Loader::includeModule('im'))
31 {
32 throw new NotSupportedException('IM module is not installed');
33 }
34 }
35
40 {
41 $channelIdsResult = ChatTable::query()
42 ->setSelect(['ID'])
43 ->where('AUTHOR_ID', $userId)
44 ->whereIn('TYPE', [CHAT::IM_TYPE_CHANNEL, CHAT::IM_TYPE_OPEN_CHANNEL])
45 ->whereNull('ENTITY_TYPE')
46// ->where(
47// Query::filter()
48// ->logic(ConditionTree::LOGIC_OR)
49// ->whereNull('ENTITY_TYPE')
50// ->where('ENTITY_TYPE', self::OPEN_EVENT_CATEGORY_IM_ENTITY_TYPE)
51// )
52 ->fetchAll()
53 ;
54 $channelIds = array_map('intval', array_column($channelIdsResult, 'ID'));
55
56 $channels = [];
57 foreach ($channelIds as $channelId)
58 {
59 $channel = new Chat\ChannelChat($channelId);
60 $hasAccess = $this->hasAccess($userId, $channelId);
61 if (!$hasAccess)
62 {
63 continue;
64 }
65 $channels[$channelId] = [
66 'id' => $channelId,
67 'title' => $channel->getDisplayedTitle(),
68 'closed' => $channel->getType() === CHAT::IM_TYPE_CHANNEL,
69 'avatar' => $channel->getAvatar(),
70 'color' => $channel->getColor(),
71 ];
72 }
73
74 return $channels;
75 }
76
77 public function createChannel(EventCategory $eventCategory, array $userIds, array $departmentIds = []): int
78 {
79 if ($eventCategory->getClosed())
80 {
81 $channelType = Chat::IM_TYPE_CHANNEL;
82 $users = $userIds;
83 }
84 else
85 {
86 $channelType = Chat::IM_TYPE_OPEN_CHANNEL;
87 $users = [];
88 }
89
90 $departments = array_map(
91 static fn (int|string $departmentId) => ['department', $departmentId],
92 $departmentIds,
93 );
94
95 $params = [
96 'TYPE' => $channelType,
97 'AUTHOR_ID' => $eventCategory->getCreatorId(), // get from context otherwise
98 'OWNER_ID' => $eventCategory->getCreatorId(), // get from context otherwise
99 'USERS' => $users,
100 'MEMBER_ENTITIES' => $departments,
101 'TITLE' => (new Provider\CategoryProvider())->prepareCategoryName($eventCategory->getName()),
102 'ENTITY_TYPE' => self::OPEN_EVENT_CATEGORY_IM_ENTITY_TYPE,
103 'SEND_GREETING_MESSAGES' => 'Y',
104 ];
105
106 $addChannelResult = ChatFactory::getInstance()->addChat($params);
107
108 $chat = $addChannelResult->getResult()['CHAT'];
109
110 if ($eventCategory->getDescription())
111 {
112 $chat->setDescription($eventCategory->getDescription());
113 $chat->save();
114 }
115
116 if ($addChannelResult->isSuccess())
117 {
118 return $chat->getChatId();
119 }
120
121 throw new \RuntimeException('channel not created');
122 }
123
124 public function updateChannel(EventCategory $eventCategory): void
125 {
126 $channel = ChatFactory::getInstance()->getChatById($eventCategory->getChannelId());
127 if (!in_array($channel->getType(), [Chat::IM_TYPE_CHANNEL, Chat::IM_TYPE_OPEN_CHANNEL], true))
128 {
129 return;
130 }
131
132 $categoryProvider = new Provider\CategoryProvider();
133
134 $channel->setTitle($categoryProvider->prepareCategoryName($eventCategory->getName()));
135 $channel->setDescription($categoryProvider->prepareCategoryDescription($eventCategory->getDescription()));
136
137 $channel->save();
138 }
139
143 public function setMuteChannel(int $userId, int $channelId, bool $newMuteState): void
144 {
145 $channel = ChatFactory::getInstance()->getChatById($channelId);
146 $isMuted = $this->isChannelMuted($userId, $channelId);
147 if ($isMuted === $newMuteState)
148 {
149 return;
150 }
151
152 $channelType = $channel->getType();
153 if (!in_array($channelType, [Chat::IM_TYPE_OPEN_CHANNEL, Chat::IM_TYPE_CHANNEL], true))
154 {
155 throw new \RuntimeException('unknown channel type');
156 }
157
158 $CIMChat = new \CIMChat($userId);
159 $CIMChat->muteNotify($channelId, $newMuteState);
160 }
161
162 public function getChannelInfo(int $channelId): array
163 {
164 $channel = ChatFactory::getInstance()->getChatById($channelId);
165
166 return [
167 'id' => $channel->getId(),
168 'title' => $channel->getTitle(),
169 'avatar' => $channel->getAvatar(),
170 'color' => Color::getColor($channel->getColor()),
171 ];
172 }
173
174 public function isChannelMuted(int $userId, int $channelId): ?bool
175 {
176 return ChatFactory::getInstance()
177 ->getChatById($channelId)
178 ?->getRelations(['FILTER' => ['USER_ID' => $userId]])
179 ->getByUserId($userId, $channelId)
180 ?->getNotifyBlock()
181 ;
182 }
183
184 public function getThreadCommentsCount(int $threadId): ?int
185 {
186 $threadMessage = new Message($threadId);
187
188 if ($threadMessage->getMessageId() === null)
189 {
190 return null;
191 }
192
193 if (!$threadMessage->hasAccess())
194 {
195 return null;
196 }
197
198 $commentChatResult = CommentChat::get($threadMessage);
199 if ($commentChatResult->getErrors())
200 {
201 return null;
202 }
203
205 $commentChat = $commentChatResult->getResult();
206
207 return $commentChat->getMessageCount() > 0 ? $commentChat->getMessageCount() - 1 : 0;
208 }
209
210 public function includeUserToChannel(int $userId, int $channelId): bool
211 {
212 $channel = ChatFactory::getInstance()->getChatById($channelId);
213// $channel->addUsers([$userId]);
214// $channel->save();
215 $CIMChat = new \CIMChat(0);
216 $addResult = $CIMChat->AddUser($channelId, [$userId], false, false);
217
218 return $addResult;
219 }
220
221 public function hasAccess(int $userId, int $channelId): bool
222 {
223 $channel = ChatFactory::getInstance()->getChatById($channelId);
224
225 return $channel->hasAccess($userId);
226 }
227
228 public function getChannelUsers(int $channelId): array
229 {
230 return ChatFactory::getInstance()
231 ->getChatById($channelId)
232 ?->getRelations()
233 ->getUserIds()
234 ;
235 }
236
237 public function isChannelPrivate(int $channelId): bool
238 {
239 return ChatFactory::getInstance()
240 ->getChatById($channelId)
241 ->getType() === Chat::IM_TYPE_CHANNEL
242 ;
243 }
244
245 public function isManagerOfChannel(int $userId, int $channelId): bool
246 {
247 $channel = ChatFactory::getInstance()->getChatById($channelId);
248
249 return $channel->getAuthorId() === $userId;
250 }
251
252 public function connectChannelToCategory(int $channelId): void
253 {
254 $chat = ChatFactory::getInstance()->getChatById($channelId);
255
256 if ($chat->getEntityType())
257 {
258 throw new \Exception('can not connect channel with not empty ENTITY_TYPE to category');
259 }
260
261 $chat->setEntityType(self::OPEN_EVENT_CATEGORY_IM_ENTITY_TYPE);
262 $chat->save();
263 }
264}
if(empty( $fields)) foreach($fields as $field) $channelId
Определения push.php:23
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
createChannel(EventCategory $eventCategory, array $userIds, array $departmentIds=[])
Определения eventcategoryservice.php:77
hasAccess(int $userId, int $channelId)
Определения eventcategoryservice.php:221
isChannelMuted(int $userId, int $channelId)
Определения eventcategoryservice.php:174
includeUserToChannel(int $userId, int $channelId)
Определения eventcategoryservice.php:210
isManagerOfChannel(int $userId, int $channelId)
Определения eventcategoryservice.php:245
setMuteChannel(int $userId, int $channelId, bool $newMuteState)
Определения eventcategoryservice.php:143
updateChannel(EventCategory $eventCategory)
Определения eventcategoryservice.php:124
static get(Message $message, bool $createIfNotExists=true)
Определения CommentChat.php:33
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
Определения Uuid.php:3
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799