Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
workgroup.php
1<?php
9
11use Bitrix\Main\Entity\Query;
17
18Loc::loadMessages(__FILE__);
19
21{
22 const CHAT_ENTITY_TYPE = "SONET_GROUP";
23 private static $staticCache = array();
24
25 public static function getUseChat()
26 {
27 return (Option::get('socialnetwork', 'use_workgroup_chat', "Y") == "Y");
28 }
29
30 public static function getChatData($params)
31 {
32 $result = array();
33
34 if (
35 !array($params)
36 || !isset($params['group_id'])
37 || !self::getUseChat()
38 || !Loader::includeModule('im')
39 )
40 {
41 return $result;
42 }
43
44 if (!is_array($params['group_id']))
45 {
46 $params['group_id'] = array($params['group_id']);
47 }
48
49 $cacheKey = serialize($params);
50 if (isset(self::$staticCache[$cacheKey]))
51 {
52 return self::$staticCache[$cacheKey];
53 }
54
55 $params['group_id'] = array_values(array_unique(array_filter(array_map(
56 function($groupId) {
57 return (is_array($groupId) || intval($groupId) <= 0 ? false : intval($groupId));
58 },
59 $params['group_id']
60 ))));
61
62 if (
63 !isset($params['skipAvailabilityCheck'])
64 || !$params['skipAvailabilityCheck']
65 )
66 {
67 foreach($params['group_id'] as $key => $value)
68 {
69 if (!self::getGroupChatAvailable($value))
70 {
71 unset($params['group_id'][$key]);
72 }
73 }
74 }
75
76 $res = ChatTable::getList(array(
77 'select' => Array('ID', 'ENTITY_ID'),
78 'filter' => array(
79 '=ENTITY_TYPE' => self::CHAT_ENTITY_TYPE,
80 '@ENTITY_ID' => $params['group_id']
81 )
82 ));
83 while ($chat = $res->fetch())
84 {
85 $result[$chat['ENTITY_ID']] = $chat['ID'];
86 }
87
88 self::$staticCache[$cacheKey] = $result;
89
90 return $result;
91 }
92
93 public static function getGroupChatAvailable($groupId = false)
94 {
95 $result = false;
96
97 if (intval($groupId) <= 0)
98 {
99 return $result;
100 }
101
102 $activeFeatures = \CSocNetFeatures::getActiveFeatures(SONET_ENTITY_GROUP, $groupId);
103 if (
104 is_array($activeFeatures)
105 && in_array('chat', $activeFeatures)
106 )
107 {
108 $result = true;
109 }
110
111 return $result;
112 }
113
114 public static function createChat($params)
115 {
116 $result = false;
117
118 if (
119 !array($params)
120 || !isset($params['group_id'])
121 || intval($params['group_id']) <= 0
122 || !self::getUseChat()
123 || !Loader::includeModule('im')
124 )
125 {
126 return $result;
127 }
128
129 $groupItem = Item\Workgroup::getById($params['group_id']);
130 if (!$groupItem)
131 {
132 return $result;
133 }
134
135 $groupFields = $groupItem->getFields();
136 $project = $groupItem->isProject();
137
138 $userIdList = array();
139
140 $res = UserToGroupTable::getList(array(
141 'filter' => array(
142 'GROUP_ID' => $params['group_id'],
144 ),
145 'select' => array('USER_ID')
146 ));
147
148 while($relation = $res->fetch())
149 {
150 $userIdList[] = intval($relation['USER_ID']);
151 }
152
153 if (empty($userIdList))
154 {
155 $userIdList = array($groupFields['OWNER_ID']);
156 }
157
158 $chatFields = array(
159 'TITLE' => self::buildChatName($groupFields['NAME'], array(
160 'project' => $project
161 )),
162 'TYPE' => IM_MESSAGE_CHAT,
163 'ENTITY_TYPE' => self::CHAT_ENTITY_TYPE,
164 'ENTITY_ID' => intval($params['group_id']),
165 'SKIP_ADD_MESSAGE' => 'Y',
166 'AUTHOR_ID' => $groupFields['OWNER_ID'],
167 'USERS' => $userIdList
168 );
169
170 $groupItem = Item\Workgroup::getById($params['group_id'], false);
171 if ($groupItem)
172 {
173 $groupFields = $groupItem->getFields();
174 if (!empty($groupFields['IMAGE_ID']))
175 {
176 $chatFields['AVATAR_ID'] = $groupFields['IMAGE_ID'];
177 }
178 }
179
180 $chat = new \CIMChat(0);
181 $result = $chat->add($chatFields);
182
183 if ($result)
184 {
185 self::$staticCache = array();
186 }
187
188 return $result;
189 }
190
191 public static function buildChatName($groupName, $params = array())
192 {
193 $project = (
194 is_array($params)
195 && isset($params['project'])
196 && $params['project']
197 );
198 $currentSite = \CSite::getById(SITE_ID);
199 $siteLanguageId = (
200 ($siteFields = $currentSite->fetch())
201 ? $siteFields['LANGUAGE_ID']
202 : LANGUAGE_ID
203 );
204
205 return Loc::getMessage(($project ? "SOCIALNETWORK_WORKGROUP_CHAT_TITLE_PROJECT" : "SOCIALNETWORK_WORKGROUP_CHAT_TITLE"), array(
206 "#GROUP_NAME#" => $groupName
207 ), $siteLanguageId);
208 }
209
210 public static function setChatManagers($params)
211 {
212 $result = false;
213
214 if (
215 !array($params)
216 || !isset($params['group_id'])
217 || intval($params['group_id']) <= 0
218 || !isset($params['user_id'])
219 || !self::getUseChat()
220 || !Loader::includeModule('im')
221 )
222 {
223 return $result;
224 }
225
226 $userIdList = (is_array($params['user_id']) ? $params['user_id'] : array($params['user_id']));
227 $groupId = intval($params['group_id']);
228 $setFlag = (isset($params['set']) && $params['set']);
229
230 $chatData = self::getChatData(array(
231 'group_id' => $groupId
232 ));
233
234 if (
235 empty($chatData)
236 || empty($chatData[$groupId])
237 || intval($chatData[$groupId]) <= 0
238 )
239 {
240 return $result;
241 }
242
243 $chatId = $chatData[$groupId];
244
245 $chat = new \CIMChat();
246
247 $managersInfo = array();
248 foreach($userIdList as $userId)
249 {
250 $managersInfo[$userId] = $setFlag;
251 }
252
253 return $chat->setManagers($chatId, $managersInfo, false);
254 }
255
256 public static function unlinkChat($params)
257 {
258 $result = false;
259
260 if (
261 !array($params)
262 || !isset($params['group_id'])
263 || intval($params['group_id']) <= 0
264 || !self::getUseChat()
265 || !Loader::includeModule('im')
266 )
267 {
268 return $result;
269 }
270
271 $groupItem = Item\Workgroup::getById($params['group_id']);
272 if (!$groupItem)
273 {
274 return $result;
275 }
276
277 $groupFields = $groupItem->getFields();
278
279 $chatMessageFields = array(
280 "MESSAGE" => str_replace('#GROUP_NAME#', $groupFields['NAME'], Loc::getMessage($groupItem->isProject() ? "SOCIALNETWORK_WORKGROUP_CHAT_UNLINKED_PROJECT" : "SOCIALNETWORK_WORKGROUP_CHAT_UNLINKED")),
281 "SYSTEM" => "Y"
282 );
283
284 $res = ChatTable::getList(array(
285 'select' => Array('ID'),
286 'filter' => array(
287 '=ENTITY_TYPE' => self::CHAT_ENTITY_TYPE,
288 '=ENTITY_ID' => $params['group_id']
289 )
290 ));
291 while ($chat = $res->fetch())
292 {
293 if (ChatTable::update($chat['ID'], array(
294 'ENTITY_TYPE' => false,
295 'ENTITY_ID' => false
296 ))->isSuccess())
297 {
298 return \CIMChat::addMessage(array_merge(
299 $chatMessageFields, array(
300 "TO_CHAT_ID" => $chat['ID']
301 )
302 ));
303 }
304 }
305
306 $result = true;
307
308 return $result;
309 }
310
320 public static function getNumberOfMembers(int $groupId): int
321 {
322 $query = new Query(UserToGroupTable::getEntity());
323
324 $records = $query
325 ->setSelect([
326 'USER_ID',
327 ])
328 ->where('GROUP_ID', $groupId)
329 ->countTotal(true)
330 ->exec()
331 ;
332
333 return $records->getCount();
334 }
335}
336?>
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static buildChatName($groupName, $params=array())