1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
user.php
См. документацию.
1<?php
2namespace Bitrix\Im\Integration\Intranet;
3
4use Bitrix\Im\V2\Chat\GeneralChat;
5use Bitrix\Im\V2\Entity\User\UserCollaber;
6use Bitrix\Im\V2\Recent\Initializer;
7use Bitrix\Main\Config\Option;
8use Bitrix\Main\Entity\ExpressionField;
9use Bitrix\Main\Localization\Loc;
10use Bitrix\Main\Type\DateTime;
11use Bitrix\Main\UserTable;
12
13class User
14{
15 static $isEmployee = [];
16
18
19 public static function canInvite(): bool
20 {
22 {
23 return false;
24 }
25
26 return \Bitrix\Intranet\Invitation::canListDelete();
27 }
28 public static function onInviteLinkCopied(\Bitrix\Main\Event $event): bool
29 {
31 {
32 return false;
33 }
34
35 $userId = (int)$event->getParameter('userId');
36
37 return self::sendMessageToGeneralChat($userId, [
38 'MESSAGE' => Loc::getMessage('IM_INT_USR_LINK_COPIED', [
39 '#USER_NAME#' => self::getUserBlock($userId)
40 ]),
41 'SYSTEM' => 'Y'
42 ]);
43 }
44
45 public static function onUserInvited(\Bitrix\Main\Event $event): bool
46 {
48 {
49 return false;
50 }
51
52 $originatorId = (int)$event->getParameter('originatorId');
53 $users = (array)$event->getParameter('userId');
54
55 if (!self::isEmployee($originatorId))
56 {
57 return false;
58 }
59
60 $users = self::filterEmployee($users);
61 if (empty($users))
62 {
63 return false;
64 }
65
66 $userForSend = [];
67 $result = \Bitrix\Intranet\UserTable::getList([
68 'filter' => [
69 '=ID' => $users
70 ],
71 'select' => ['ID', 'EMAIL']
72
73 ]);
74 while ($row = $result->fetch())
75 {
76 $userForSend[] = [
77 'ID' => $row['ID'],
78 'INVITED' => [
79 'originator_id' => $originatorId,
80 'can_resend' => !empty($row['EMAIL'])
81 ]
82 ];
83 }
84
85 if (empty($userForSend))
86 {
87 return false;
88 }
89
90 self::sendInviteEvent($userForSend);
91
92 $userForSend = array_map(function($user) {
93 return self::getUserBlock($user['ID']);
94 }, $userForSend);
95
96 return self::sendMessageToGeneralChat($originatorId, [
97 'MESSAGE' => Loc::getMessage('IM_INT_USR_INVITE_USERS', [
98 '#USER_NAME#' => self::getUserBlock($originatorId),
99 '#USERS#' => implode(', ', $userForSend)
100 ]),
101 'SYSTEM' => 'Y',
102 'PUSH' => 'N'
103 ]);
104 }
105
106 public static function onUserAdded(\Bitrix\Main\Event $event): bool
107 {
109 {
110 return false;
111 }
112
113 $originatorId = (int)$event->getParameter('originatorId');
114 $users = (array)$event->getParameter('userId');
115
116 if (!self::isEmployee($originatorId))
117 {
118 return false;
119 }
120
121 $users = self::filterEmployee($users);
122 if (empty($users))
123 {
124 return false;
125 }
126
127 $userForSend = [];
128 $result = \Bitrix\Intranet\UserTable::getList([
129 'filter' => [
130 '=ID' => $users
131 ],
132 'select' => ['ID', 'EMAIL']
133
134 ]);
135 while ($row = $result->fetch())
136 {
137 $userForSend[] = [
138 'ID' => $row['ID'],
139 'INVITED' => [
140 'originator_id' => $originatorId,
141 'can_resend' => !empty($row['EMAIL'])
142 ]
143 ];
144 }
145
146 self::sendInviteEvent($userForSend);
147
148 $users = array_map(function($userId) {
149 return self::getUserBlock($userId);
150 }, $users);
151
152 return self::sendMessageToGeneralChat($originatorId, [
153 'MESSAGE' => Loc::getMessage('IM_INT_USR_REGISTER_USERS', [
154 '#USER_NAME#' => self::getUserBlock($originatorId),
155 '#USERS#' => implode(', ', $users)
156 ]),
157 'SYSTEM' => 'Y',
158 'PUSH' => 'N'
159 ]);
160 }
161
162 public static function onUserAdminRights(\Bitrix\Main\Event $event): bool
163 {
165 {
166 return false;
167 }
168
169 if (!\COption::GetOptionString("im", "general_chat_message_admin_rights", true))
170 {
171 return false;
172 }
173
174 $originatorId = (int)$event->getParameter('originatorId');
175 $users = (array)$event->getParameter('userId');
176 $type = (string)$event->getParameter('type');
177
178 $users = array_map(function($userId) {
179 return self::getUserBlock($userId);
180 }, $users);
181
182 $originatorGender = 'M';
183 if ($originatorId > 0)
184 {
185 $dbUser = \CUser::GetList('', '', ['ID_EQUAL_EXACT' => $originatorId], array('FIELDS' => ['PERSONAL_GENDER']));
186 if ($user = $dbUser->Fetch())
187 {
188 $originatorGender = $user["PERSONAL_GENDER"] == 'F'? 'F': 'M';
189 }
190 }
191
192 $messId = (
193 $type === 'setAdminRigths'
194 ? 'IM_INT_USR_SET_ADMIN_RIGTHS_'.$originatorGender
195 : 'IM_INT_USR_REMOVE_ADMIN_RIGTHS_'.$originatorGender
196 );
197
198 return self::sendMessageToGeneralChat($originatorId, [
199 'MESSAGE' => Loc::getMessage($messId, [
200 '#USER_NAME#' => self::getUserBlock($originatorId),
201 '#USERS#' => implode(', ', $users)
202 ]),
203 'SYSTEM' => 'Y'
204 ]);
205
206 }
207
208 public static function onInviteSend(array $params): bool
209 {
211 {
212 return false;
213 }
214
215 $userId = (int)$params['ID'];
216
217 if (!self::isEmployee($userId))
218 {
219 return false;
220 }
221
222 \CIMContactList::SetRecent(['ENTITY_ID' => $userId]);
223
224 return true;
225 }
226
227 public static function onInviteAccepted(array $params): bool
228 {
230 {
231 return true;
232 }
233
234 $userData = $params['user_fields'];
235
236 if (in_array($userData['EXTERNAL_AUTH_ID'], \Bitrix\Main\UserTable::getExternalUserTypes()))
237 {
238 return true;
239 }
240
241 if ($userData['LAST_LOGIN'])
242 {
243 return true;
244 }
245
246 $userId = (int)$userData['ID'];
247 if ($userData['LAST_ACTIVITY_DATE'])
248 {
249 return true;
250 }
251
253
254 if (!self::isEmployee($userId) && !($user instanceof UserCollaber))
255 {
256 return false;
257 }
258
259 \CUser::SetLastActivityDate($userId);
260 $user->unsetOnlineData();
261 Initializer::onAfterUserAcceptInvite($userId);
262
263 if ($user instanceof UserCollaber)
264 {
265 return true;
266 }
267
268 self::addUserToGeneralChat($userData);
269 \CIMContactList::SetRecent(Array('ENTITY_ID' => $userId));
270
271 if (self::isCountOfUsersExceededForPersonalNotify())
272 {
273 if (!\CIMChat::GetGeneralChatAutoMessageStatus(\CIMChat::GENERAL_MESSAGE_TYPE_JOIN))
274 {
275 return false;
276 }
277
278 return self::sendMessageToGeneralChat($userId, [
279 "MESSAGE" => Loc::getMessage('IM_INT_USR_JOIN_GENERAL_2'),
280 "PARAMS" => [
281 "CODE" => 'USER_JOIN_GENERAL',
282 ]
283 ]);
284 }
285
286 self::sendInviteEvent([[
287 'ID' => $userId,
288 'INVITED' => false
289 ]]);
290
292 'select' => ['ID'],
293 'filter' => [
294 '=ACTIVE' => 'Y',
295 '=IS_REAL_USER' => 'Y',
296 '!=UF_DEPARTMENT' => false
297 ]
298 ]);
299 while($row = $orm->fetch())
300 {
301 if ($row['ID'] == $userId)
302 {
303 continue;
304 }
305
306 $viewCommonUsers = (bool)\CIMSettings::GetSetting(\CIMSettings::SETTINGS, 'viewCommonUsers', $row['ID']);
307 if (!$viewCommonUsers)
308 {
309 continue;
310 }
311
313 "TO_USER_ID" => $row['ID'],
314 "FROM_USER_ID" => $userId,
315 "MESSAGE" => Loc::getMessage('IM_INT_USR_JOIN_2'),
316 "SYSTEM" => 'Y',
317 "RECENT_SKIP_AUTHOR" => 'Y',
318 "MESSAGE_OUT" => IM_MAIL_SKIP,
319 "PARAMS" => [
320 "CODE" => 'USER_JOIN',
321 ],
322 ]);
323 }
324
325 return true;
326 }
327
328 private static function addUserToGeneralChat(array $userData): void
329 {
330 $userId = (int)$userData['ID'];
331 if (
332 $userData['ACTIVE'] !== 'Y'
333 || in_array($userData['EXTERNAL_AUTH_ID'], \Bitrix\Main\UserTable::getExternalUserTypes(), true)
334 || !self::isEmployee($userId)
335 )
336 {
337 return;
338 }
339
340 $generalChatId = GeneralChat::getGeneralChatId();
341 if (empty($generalChatId))
342 {
343 return;
344 }
345
346 $chatService = new \CIMChat(0);
347 $chatService->AddUser($generalChatId, [$userId], false, true);
348 }
349
350 private static function sendInviteEvent(array $users): bool
351 {
352 if (!\Bitrix\Main\Loader::includeModule('pull'))
353 {
354 return false;
355 }
356
357 if (!\Bitrix\Main\ModuleManager::isModuleInstalled('intranet'))
358 {
359 return false;
360 }
361
362 $onlineUsers = \Bitrix\Im\Helper::getOnlineIntranetUsers();
363 foreach ($users as $user)
364 {
365 \Bitrix\Pull\Event::add($onlineUsers, [
366 'module_id' => 'im',
367 'command' => 'userInvite',
368 'expiry' => 3600,
369 'params' => [
370 'userId' => $user['ID'],
371 'invited' => $user['INVITED'],
372 'user' => \Bitrix\Im\User::getInstance($user['ID'])->getFields(),
373 'date' => new DateTime(),
374 ],
375 'extra' => \Bitrix\Im\Common::getPullExtra()
376 ]);
377 }
378
379 return true;
380 }
381
382 private static function sendMessageToGeneralChat(int $fromUserId, array $params): bool
383 {
384 $chatId = \CIMChat::GetGeneralChatId();
385 if (!$chatId)
386 return false;
387
388 $params = array_merge($params, [
389 "TO_CHAT_ID" => $chatId,
390 "FROM_USER_ID" => $fromUserId,
391 "MESSAGE_OUT" => IM_MAIL_SKIP,
392 "SKIP_USER_CHECK" => 'Y',
393 ]);
394
395 $result = \CIMChat::AddMessage($params);
396
397 return $result !== false;
398 }
399
400 private static function getUserBlock(int $userId): string
401 {
402 return '[USER='.$userId.'][/USER]';
403 }
404
405 private static function isEmployee(int $userId): bool
406 {
407 if (isset(self::$isEmployee[$userId]))
408 {
409 return self::$isEmployee[$userId];
410 }
411
412 if (!\Bitrix\Main\Loader::includeModule('humanresources'))
413 {
414 return false;
415 }
416
417 try
418 {
419 $isEmployee = \Bitrix\HumanResources\Service\Container::getUserService()->isEmployee($userId);
420 }
421 catch (\Exception $exception)
422 {
423 $isEmployee = false;
424 }
425
426 self::$isEmployee[$userId] = $isEmployee;
427
428 return self::$isEmployee[$userId];
429 }
430
431 private static function filterEmployee(array $userIds): array
432 {
433 if (!\Bitrix\Main\Loader::includeModule('humanresources'))
434 {
435 return [];
436 }
437
438 try
439 {
440 return \Bitrix\HumanResources\Service\Container::getUserService()->filterEmployees($userIds);
441 }
442 catch (\Exception $exception)
443 {
444 return [];
445 }
446 }
447
448 public static function getBirthdayForToday()
449 {
451 {
452 return [];
453 }
454
455 $option = Option::get('im', 'contact_list_birthday');
456 if ($option === 'none' || \Bitrix\Im\User::getInstance()->isExtranet())
457 {
458 return [];
459 }
460
461 global $USER;
462
463 $today = (new DateTime())->format('m-d');
464 if ($option === 'department')
465 {
466 $cacheId = 'birthday_'.$today.'_'.$USER->GetID();
467 }
468 else
469 {
470 $cacheId = 'birthday_'.$today;
471 }
472
473 $cache = \Bitrix\Main\Data\Cache::createInstance();
474 if($cache->initCache(86400, $cacheId, '/bx/im/birthday/'))
475 {
476 return $cache->getVars();
477 }
478
479 $user = \CUser::getById($USER->GetId())->Fetch();
480
481 $filter = [
482 '=ACTIVE' => 'Y',
483 '=BIRTHDAY_DATE' => $today,
484 '=IS_REAL_USER' => true,
485 ];
486 if ($option === 'department')
487 {
488 $filter['=UF_DEPARTMENT'] = $user['UF_DEPARTMENT'];
489 }
490 else
491 {
492 $filter['!=UF_DEPARTMENT'] = false;
493 }
494
496 $helper = $connection->getSqlHelper();
497
498 $result = [];
499 $users = UserTable::getList([
500 'filter' => $filter,
501 'select' => ['ID'],
502 'runtime' => [
503 new ExpressionField('BIRTHDAY_DATE', str_replace('PERSONAL_BIRTHDAY', '%s', str_replace('%', '%%', $helper->formatDate('MM-DD', 'PERSONAL_BIRTHDAY'))), 'PERSONAL_BIRTHDAY')
504 ],
505 'limit' => 100,
506 ])->fetchAll();
507
508 foreach ($users as $user)
509 {
510 $result[] = \Bitrix\Im\User::getInstance($user['ID'])->getArray(['SKIP_ONLINE' => 'Y', 'JSON' => 'Y']);
511 }
512
513 $cache->forceRewriting(true);
514 $cache->startDataCache();
515 $cache->endDataCache($result);
516
517 return $result;
518 }
519
520 private static function isCountOfUsersExceededForPersonalNotify(): bool
521 {
522 $count = UserTable::query()
523 ->setSelect(['ID'])
524 ->where('ACTIVE', true)
525 ->where('REAL_USER', 'expr', true)
526 ->whereNotNull('LAST_LOGIN')
527 ->setLimit(self::INVITE_MAX_USER_NOTIFY + 1)
528 ->fetchCollection()
529 ->count()
530 ;
531
532 return $count > self::INVITE_MAX_USER_NOTIFY;
533 }
534
535 public static function registerEventHandler()
536 {
538 $eventManager->registerEventHandlerCompatible('main', 'OnAfterUserAuthorize', 'im', self::class, 'onInviteAccepted');
539 $eventManager->registerEventHandlerCompatible('intranet', 'OnRegisterUser', 'im', self::class, 'onInviteSend');
540 $eventManager->registerEventHandler('intranet', 'OnCopyRegisterUrl', 'im', self::class, 'onInviteLinkCopied');
541 $eventManager->registerEventHandler('intranet', 'onUserInvited', 'im', self::class, 'onUserInvited');
542 $eventManager->registerEventHandler('intranet', 'onUserAdded', 'im', self::class, 'onUserAdded');
543 $eventManager->registerEventHandler('intranet', 'onUserAdminRights', 'im', self::class, 'onUserAdminRights');
544 }
545
546 public static function unRegisterEventHandler()
547 {
549 $eventManager->unRegisterEventHandler('main', 'OnAfterUserAuthorize', 'im', self::class, 'onInviteAccepted');
550 $eventManager->unRegisterEventHandler('intranet', 'OnRegisterUser', 'im', self::class, 'onInviteSend');
551 $eventManager->unRegisterEventHandler('intranet', 'OnCopyRegisterUrl', 'im', self::class, 'onInviteLinkCopied');
552 $eventManager->unRegisterEventHandler('intranet', 'onUserInvited', 'im', self::class, 'onUserInvited');
553 $eventManager->unRegisterEventHandler('intranet', 'onUserAdded', 'im', self::class, 'onUserAdded');
554 $eventManager->unRegisterEventHandler('intranet', 'onUserAdminRights', 'im', self::class, 'onUserAdminRights');
555 }
556}
$connection
Определения actionsdefinitions.php:38
$count
Определения admin_tab.php:4
$type
Определения options.php:106
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getPullExtra()
Определения common.php:127
static onInviteSend(array $params)
Определения user.php:208
const INVITE_MAX_USER_NOTIFY
Определения user.php:17
static unRegisterEventHandler()
Определения user.php:546
static $isEmployee
Определения user.php:15
static canInvite()
Определения user.php:19
static onUserAdded(\Bitrix\Main\Event $event)
Определения user.php:106
static onInviteLinkCopied(\Bitrix\Main\Event $event)
Определения user.php:28
static getBirthdayForToday()
Определения user.php:448
static onUserInvited(\Bitrix\Main\Event $event)
Определения user.php:45
static onInviteAccepted(array $params)
Определения user.php:227
static registerEventHandler()
Определения user.php:535
static onUserAdminRights(\Bitrix\Main\Event $event)
Определения user.php:162
static getInstance($userId=null)
Определения user.php:45
static getGeneralChatId()
Определения GeneralChat.php:123
static getInstance(?int $id)
Определения User.php:72
static getConnection($name="")
Определения application.php:638
static getInstance()
Определения eventmanager.php:31
static isModuleInstalled($moduleName)
Определения modulemanager.php:125
static getList(array $parameters=array())
Определения datamanager.php:431
static Add($arFields)
Определения im_message.php:28
static GetSetting($type, $value, $userId=false)
Определения im_settings.php:223
const SETTINGS
Определения im_settings.php:12
</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
$filter
Определения iblock_catalog_list.php:54
const IM_MAIL_SKIP
Определения include.php:52
global $USER
Определения csv_new_run.php:40
int $chatId
Определения Param.php:36
$user
Определения mysql_to_pgsql.php:33
$event
Определения prolog_after.php:141
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$option
Определения options.php:1711
$eventManager
Определения include.php:412