1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
chatfactory.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Socialnetwork\Integration\Im;
6
7use Bitrix\Im\V2\Chat;
8use Bitrix\Main\Error;
9use Bitrix\Main\Loader;
10use Bitrix\Main\Localization\Loc;
11use Bitrix\Main\Result;
12use Bitrix\Socialnetwork\Item\Workgroup;
13use Bitrix\Socialnetwork\Item\Workgroup\Type;
14use CSite;
15
17{
18 private const CHAT_ENTITY_TYPE = 'SONET_GROUP';
19
20 private Workgroup $group;
21
22 public static function createChat(Workgroup $group): Result
23 {
24 $result = new Result();
25
26 if (!Loader::includeModule('im'))
27 {
28 $result->addError(new Error('IM is not installed'));
29
30 return $result;
31 }
32
33 $factory = new static($group);
34
35 $fields = match ($factory->group->getType())
36 {
37 Type::Project, Type::Scrum => $factory->getProjectChatFields(),
38 Type::Collab => $factory->getCollabChatFields(),
39 default => $factory->getGroupChatFields(),
40 };
41
42 $chatResult = Chat\ChatFactory::getInstance()->addChat($fields);
43
44 $result->addErrors($chatResult->getErrors());
45
46 return $result;
47 }
48
49 public static function getChatTitle(string $groupTitle, ?Type $groupType): string
50 {
51 return match ($groupType)
52 {
53 Type::Project, Type::Scrum => Loc::getMessage('SOCIALNETWORK_WORKGROUP_CHAT_FACTORY_TITLE_PROJECT', [
54 '#GROUP_NAME#' => $groupTitle,
55 ], static::getLanguageId()),
56
57 Type::Collab => $groupTitle,
58
59 default => Loc::getMessage('SOCIALNETWORK_WORKGROUP_CHAT_FACTORY_TITLE', [
60 '#GROUP_NAME#' => $groupTitle,
61 ], static::getLanguageId()),
62 };
63 }
64
65 private function __construct(Workgroup $group)
66 {
67 $this->group = $group;
68 }
69
70 private static function getLanguageId(): string
71 {
72 $currentSite = CSite::getById(SITE_ID)->fetch();
73
74 return (string)($currentSite ? $currentSite['LANGUAGE_ID'] : LANGUAGE_ID);
75 }
76
77 private function getCollabChatFields(): array
78 {
79 return array_merge($this->getCommonFields(), self::getUniqueCollabChatFields());
80 }
81
82 public static function getUniqueFieldsByType(Type $type): array
83 {
84 return match ($type) {
85 Type::Collab => self::getUniqueCollabChatFields(),
86 default => self::getUniqueGroupChatFields(),
87 };
88 }
89
90 public static function getUniqueGroupChatFields(): array
91 {
92 $fields = [];
93
94 $fields['TYPE'] = Chat::IM_TYPE_CHAT;
95 $fields['SKIP_ADD_MESSAGE'] = 'Y';
96
97 return $fields;
98 }
99
100 public static function getUniqueCollabChatFields(): array
101 {
102 $fields = [];
103
104 $fields['TYPE'] = Chat::IM_TYPE_COLLAB;
105 $fields['SKIP_ADD_MESSAGE'] = 'N';
106 $fields['MANAGE_USERS_DELETE'] = Chat::MANAGE_RIGHTS_OWNER;
107 $fields['MANAGE_UI'] = Chat::MANAGE_RIGHTS_OWNER;
108
109 return $fields;
110 }
111
112 private function getGroupChatFields(): array
113 {
114 $fields = $this->getCommonFields();
115
116 $fields['TYPE'] = IM_MESSAGE_CHAT;
117
118 return $fields;
119 }
120
121 private function getProjectChatFields(): array
122 {
123 $fields = $this->getCommonFields();
124
125 $fields['TYPE'] = IM_MESSAGE_CHAT;
126
127 return $fields;
128 }
129
130 private function getCommonFields(): array
131 {
132 $fields = [
133 'TITLE' => static::getChatTitle($this->group->getName(), $this->group->getType()),
134 'DESCRIPTION' => $this->group->getDescription(),
135 'ENTITY_TYPE' => self::CHAT_ENTITY_TYPE,
136 'ENTITY_ID' => $this->group->getId(),
137 'SKIP_ADD_MESSAGE' => 'Y',
138 'AUTHOR_ID' => $this->group->getOwnerId(),
139 'USERS' => $this->getMembers(),
140 'USER_ID' => 0,
141 ];
142
143 if ($this->group->getImageId() > 0)
144 {
145 $fields['AVATAR_ID'] = $this->group->getImageId();
146 $fields['AVATAR'] = $this->group->getImageId();
147 }
148
149 return $fields;
150 }
151
152 private function getMembers(): array
153 {
154 $members = $this->group->getUserMemberIds();
155 if (empty($members))
156 {
157 $members[] = $this->group->getOwnerId();
158 }
159
160 return $members;
161 }
162}
$type
Определения options.php:106
Определения result.php:20
Определения error.php:15
static createChat(Workgroup $group)
Определения chatfactory.php:22
static getUniqueFieldsByType(Type $type)
Определения chatfactory.php:82
static getChatTitle(string $groupTitle, ?Type $groupType)
Определения chatfactory.php:49
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
const IM_MESSAGE_CHAT
Определения include.php:23
Определения collection.php:2
const SITE_ID
Определения sonet_set_content_view.php:12
$fields
Определения yandex_run.php:501