2namespace Bitrix\Im\Integration\Intranet;
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;
26 return \Bitrix\Intranet\Invitation::canListDelete();
37 return self::sendMessageToGeneralChat(
$userId, [
38 'MESSAGE' => Loc::getMessage(
'IM_INT_USR_LINK_COPIED', [
39 '#USER_NAME#' => self::getUserBlock(
$userId)
52 $originatorId = (int)
$event->getParameter(
'originatorId');
55 if (!self::isEmployee($originatorId))
60 $users = self::filterEmployee($users);
67 $result = \Bitrix\Intranet\UserTable::getList([
71 'select' => [
'ID',
'EMAIL']
79 'originator_id' => $originatorId,
80 'can_resend' => !empty($row[
'EMAIL'])
85 if (empty($userForSend))
90 self::sendInviteEvent($userForSend);
92 $userForSend = array_map(
function(
$user) {
93 return self::getUserBlock(
$user[
'ID']);
96 return self::sendMessageToGeneralChat($originatorId, [
97 'MESSAGE' => Loc::getMessage(
'IM_INT_USR_INVITE_USERS', [
98 '#USER_NAME#' => self::getUserBlock($originatorId),
99 '#USERS#' => implode(
', ', $userForSend)
113 $originatorId = (int)
$event->getParameter(
'originatorId');
116 if (!self::isEmployee($originatorId))
121 $users = self::filterEmployee($users);
128 $result = \Bitrix\Intranet\UserTable::getList([
132 'select' => [
'ID',
'EMAIL']
135 while ($row =
$result->fetch())
140 'originator_id' => $originatorId,
141 'can_resend' => !empty($row[
'EMAIL'])
146 self::sendInviteEvent($userForSend);
148 $users = array_map(
function(
$userId) {
149 return self::getUserBlock(
$userId);
152 return self::sendMessageToGeneralChat($originatorId, [
153 'MESSAGE' => Loc::getMessage(
'IM_INT_USR_REGISTER_USERS', [
154 '#USER_NAME#' => self::getUserBlock($originatorId),
155 '#USERS#' => implode(
', ', $users)
169 if (!\COption::GetOptionString(
"im",
"general_chat_message_admin_rights",
true))
174 $originatorId = (int)
$event->getParameter(
'originatorId');
178 $users = array_map(
function(
$userId) {
179 return self::getUserBlock(
$userId);
182 $originatorGender =
'M';
183 if ($originatorId > 0)
185 $dbUser = \CUser::GetList(
'',
'', [
'ID_EQUAL_EXACT' => $originatorId],
array(
'FIELDS' => [
'PERSONAL_GENDER']));
186 if (
$user = $dbUser->Fetch())
188 $originatorGender =
$user[
"PERSONAL_GENDER"] ==
'F'?
'F':
'M';
193 $type ===
'setAdminRigths'
194 ?
'IM_INT_USR_SET_ADMIN_RIGTHS_'.$originatorGender
195 :
'IM_INT_USR_REMOVE_ADMIN_RIGTHS_'.$originatorGender
198 return self::sendMessageToGeneralChat($originatorId, [
199 'MESSAGE' => Loc::getMessage($messId, [
200 '#USER_NAME#' => self::getUserBlock($originatorId),
201 '#USERS#' => implode(
', ', $users)
217 if (!self::isEmployee(
$userId))
222 \CIMContactList::SetRecent([
'ENTITY_ID' =>
$userId]);
234 $userData =
$params[
'user_fields'];
236 if (in_array($userData[
'EXTERNAL_AUTH_ID'], \
Bitrix\
Main\UserTable::getExternalUserTypes()))
241 if ($userData[
'LAST_LOGIN'])
246 $userId = (int)$userData[
'ID'];
247 if ($userData[
'LAST_ACTIVITY_DATE'])
259 \CUser::SetLastActivityDate(
$userId);
260 $user->unsetOnlineData();
261 Initializer::onAfterUserAcceptInvite(
$userId);
268 self::addUserToGeneralChat($userData);
269 \CIMContactList::SetRecent(Array(
'ENTITY_ID' =>
$userId));
271 if (self::isCountOfUsersExceededForPersonalNotify())
273 if (!\CIMChat::GetGeneralChatAutoMessageStatus(\CIMChat::GENERAL_MESSAGE_TYPE_JOIN))
278 return self::sendMessageToGeneralChat(
$userId, [
279 "MESSAGE" => Loc::getMessage(
'IM_INT_USR_JOIN_GENERAL_2'),
281 "CODE" =>
'USER_JOIN_GENERAL',
286 self::sendInviteEvent([[
295 '=IS_REAL_USER' =>
'Y',
296 '!=UF_DEPARTMENT' =>
false
299 while($row = $orm->fetch())
307 if (!$viewCommonUsers)
313 "TO_USER_ID" => $row[
'ID'],
315 "MESSAGE" => Loc::getMessage(
'IM_INT_USR_JOIN_2'),
317 "RECENT_SKIP_AUTHOR" =>
'Y',
320 "CODE" =>
'USER_JOIN',
328 private static function addUserToGeneralChat(
array $userData): void
330 $userId = (int)$userData[
'ID'];
332 $userData[
'ACTIVE'] !==
'Y'
333 || in_array($userData[
'EXTERNAL_AUTH_ID'], \
Bitrix\
Main\UserTable::getExternalUserTypes(),
true)
341 if (empty($generalChatId))
346 $chatService = new \CIMChat(0);
347 $chatService->AddUser($generalChatId, [
$userId],
false,
true);
350 private static function sendInviteEvent(
array $users): bool
352 if (!\
Bitrix\
Main\Loader::includeModule(
'pull'))
362 $onlineUsers = \Bitrix\Im\Helper::getOnlineIntranetUsers();
363 foreach ($users as
$user)
365 \Bitrix\Pull\Event::add($onlineUsers, [
367 'command' =>
'userInvite',
370 'userId' =>
$user[
'ID'],
371 'invited' =>
$user[
'INVITED'],
373 'date' =>
new DateTime(),
382 private static function sendMessageToGeneralChat(
int $fromUserId,
array $params): bool
384 $chatId = \CIMChat::GetGeneralChatId();
389 "TO_CHAT_ID" => $chatId,
390 "FROM_USER_ID" => $fromUserId,
392 "SKIP_USER_CHECK" =>
'Y',
400 private static function getUserBlock(
int $userId): string
402 return '[USER='.$userId.
'][/USER]';
405 private static function isEmployee(
int $userId): bool
407 if (isset(self::$isEmployee[
$userId]))
409 return self::$isEmployee[
$userId];
412 if (!\Bitrix\Main\Loader::includeModule(
'humanresources'))
419 $isEmployee = \Bitrix\HumanResources\Service\Container::getUserService()->isEmployee(
$userId);
421 catch (\Exception $exception)
428 return self::$isEmployee[
$userId];
431 private static function filterEmployee(
array $userIds):
array
433 if (!\Bitrix\Main\Loader::includeModule(
'humanresources'))
440 return \Bitrix\HumanResources\Service\Container::getUserService()->filterEmployees($userIds);
442 catch (\Exception $exception)
455 $option = Option::get(
'im',
'contact_list_birthday');
463 $today = (
new DateTime())->format(
'm-d');
466 $cacheId =
'birthday_'.$today.
'_'.
$USER->GetID();
470 $cacheId =
'birthday_'.$today;
473 $cache = \Bitrix\Main\Data\Cache::createInstance();
474 if($cache->initCache(86400, $cacheId,
'/bx/im/birthday/'))
476 return $cache->getVars();
479 $user = \CUser::getById(
$USER->GetId())->Fetch();
483 '=BIRTHDAY_DATE' => $today,
484 '=IS_REAL_USER' =>
true,
492 $filter[
'!=UF_DEPARTMENT'] =
false;
499 $users = UserTable::getList([
503 new ExpressionField(
'BIRTHDAY_DATE', str_replace(
'PERSONAL_BIRTHDAY',
'%s', str_replace(
'%',
'%%', $helper->formatDate(
'MM-DD',
'PERSONAL_BIRTHDAY'))),
'PERSONAL_BIRTHDAY')
508 foreach ($users as
$user)
513 $cache->forceRewriting(
true);
514 $cache->startDataCache();
520 private static function isCountOfUsersExceededForPersonalNotify(): bool
522 $count = UserTable::query()
524 ->where(
'ACTIVE',
true)
525 ->where(
'REAL_USER',
'expr',
true)
526 ->whereNotNull(
'LAST_LOGIN')
527 ->setLimit(self::INVITE_MAX_USER_NOTIFY + 1)
532 return $count > self::INVITE_MAX_USER_NOTIFY;
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');
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');
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
static onInviteSend(array $params)
const INVITE_MAX_USER_NOTIFY
static unRegisterEventHandler()
static onUserAdded(\Bitrix\Main\Event $event)
static onInviteLinkCopied(\Bitrix\Main\Event $event)
static getBirthdayForToday()
static onUserInvited(\Bitrix\Main\Event $event)
static onInviteAccepted(array $params)
static registerEventHandler()
static onUserAdminRights(\Bitrix\Main\Event $event)
static getInstance($userId=null)
static getGeneralChatId()
static getInstance(?int $id)
static getConnection($name="")
static isModuleInstalled($moduleName)
static getList(array $parameters=array())
static GetSetting($type, $value, $userId=false)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']