48 $userId = (int)$userId;
49 if ($userId <= 0 && is_object($USER) && $USER->GetID() > 0)
51 $userId = (int)$USER->GetID();
54 if (!isset(self::$instance[$userId]))
56 self::$instance[$userId] =
new self($userId);
59 return self::$instance[$userId];
271 if ($type == self::PHONE_ANY)
273 if (isset($fields[self::PHONE_MOBILE]))
277 else if (isset($fields[self::PHONE_PERSONAL]))
281 else if (isset($fields[self::PHONE_WORK]))
286 else if (isset($fields[$type]))
288 $result = $fields[$type];
476 $hrPhotoOption = $options[
'HR_PHOTO'] ??
null;
477 $livechatOption = $options[
'LIVECHAT'] ??
null;
478 $jsonOption = $options[
'JSON'] ??
null;
479 $skipOnlineOption = $options[
'SKIP_ONLINE'] ??
null;
482 'ID' => $this->
getId(),
485 'FIRST_NAME' => $this->
getName(
false),
494 'BOT' => $this->
isBot(),
498 'IDLE' => $skipOnlineOption ===
'Y' ?
false: $this->
getIdle(),
500 'MOBILE_LAST_DATE' => $skipOnlineOption ===
'Y' ?
false: $this->
getMobileLastDate(),
514 $lineId = \Bitrix\ImOpenLines\Queue::getActualLineId([
'LINE_ID' => $livechatOption,
'USER_CODE' => $options[
'USER_CODE']]);
516 $imolUserData = \Bitrix\ImOpenLines\Queue::getUserData($lineId, $this->
getId());
519 $result = array_merge($result, $imolUserData);
520 $result[
'AVATAR_HR'] = $result[
'AVATAR'];
527 foreach ($result as $key => $value)
531 $result[$key] = date(
'c', $value->getTimestamp());
533 else if (is_string($value) && is_string($key) && in_array($key, [
'AVATAR',
'AVATAR_HR']) && is_string($value) && $value && mb_strpos($value,
'http') !== 0)
535 $result[$key] = \Bitrix\Im\Common::getPublicDomain().$value;
538 $result = array_change_key_case($result, CASE_LOWER);
548 public static function getArrayWithOnline(array $users, array $options = [
'JSON' =>
'Y',
'SKIP_ONLINE' =>
'Y']): array
552 foreach ($users as $user)
554 $result[$user->getId()] = $user->getArray($options);
557 $ids = array_keys($result);
564 $statuses = StatusTable::query()
565 ->setSelect([
'USER_ID',
'IDLE',
'MOBILE_LAST_DATE',
'DESKTOP_LAST_DATE',
'LAST_ACTIVITY_DATE' =>
'USER.LAST_ACTIVITY_DATE'])
566 ->whereIn(
'USER_ID', $ids)
570 foreach ($statuses as $status)
572 $id = (int)$status[
'USER_ID'];
573 $result[$id][
'last_activity_date'] = $status[
'LAST_ACTIVITY_DATE'] ? $status[
'LAST_ACTIVITY_DATE']->format(
'c') :
false;
574 $result[$id][
'desktop_last_date'] = $status[
'DESKTOP_LAST_DATE'] ? $status[
'DESKTOP_LAST_DATE']->format(
'c') :
false;
575 $result[$id][
'mobile_last_date'] = $status[
'MOBILE_LAST_DATE'] ? $status[
'MOBILE_LAST_DATE']->format(
'c') :
false;
576 $result[$id][
'idle'] = $status[
'IDLE'] ? $status[
'IDLE']->format(
'c') :
false;
579 return array_values($result);
612 if (!$ar = parse_url($avatarUrl))
617 if (!preg_match(
'#\.(png|jpg|jpeg|gif|webp)$#i', $ar[
'path'], $matches))
622 $hash = md5($hash. $avatarUrl);
624 $orm = \Bitrix\Im\Model\ExternalAvatarTable::getList([
625 'select' => [
'*',
'FILE_EXISTS' =>
'FILE.ID'],
626 'filter' => [
'=LINK_MD5' => $hash]
628 if ($cache = $orm->fetch())
630 if ((
int)$cache[
'FILE_EXISTS'] > 0)
632 return (
int)$cache[
'AVATAR_ID'];
636 \Bitrix\Im\Model\ExternalAvatarTable::delete((
int)$cache[
'ID']);
642 $tempPath = \CFile::GetTempName(
'', $hash.
'.'.$matches[1]);
644 $http = new \Bitrix\Main\Web\HttpClient();
647 ->setStreamTimeout(10);
649 if (!defined(
'BOT_CLIENT_URL'))
651 $http->setPrivateIp(
false);
653 if ($http->download($avatarUrl, $tempPath))
655 $recordFile = \CFile::MakeFileArray($tempPath);
667 if (!\CFile::IsImage($recordFile[
'name'], $recordFile[
'type']))
672 if (is_array($recordFile) && $recordFile[
'size'] && $recordFile[
'size'] > 0 && $recordFile[
'size'] < 1000000)
674 $recordFile = array_merge($recordFile, [
'MODULE_ID' =>
'imbot']);
683 $recordFile = \CFile::SaveFile($recordFile,
'botcontroller',
true);
686 if ((
int)$recordFile > 0)
688 \Bitrix\Im\Model\ExternalAvatarTable::add([
690 'AVATAR_ID' => (
int)$recordFile
738 $params = is_array($params)? $params: Array();
740 if (!isset($params[
'CURRENT_USER']) && is_object(
$GLOBALS[
'USER']))
742 $params[
'CURRENT_USER'] =
$GLOBALS[
'USER']->GetID();
745 $params[
'CURRENT_USER'] = intval($params[
'CURRENT_USER']);
747 $userId = $params[
'CURRENT_USER'];
753 $enableLimit =
false;
754 if (isset($params[
'OFFSET']))
756 $filterLimit = intval($params[
'LIMIT']);
759 $filterOffset = intval($params[
'OFFSET']);
765 $filterLimit =
false;
766 $filterOffset =
false;
770 if (is_null($ormParams))
775 $filter = $ormParams[
'filter'];
776 $filter[
'ACTIVE'] =
'Y';
778 $intranetInstalled = \Bitrix\Main\Loader::includeModule(
'intranet');
779 $voximplantInstalled = \Bitrix\Main\Loader::includeModule(
'voximplant');
782 "ID",
"LAST_NAME",
"NAME",
"LOGIN",
"PERSONAL_PHOTO",
"SECOND_NAME",
"PERSONAL_BIRTHDAY",
"WORK_POSITION",
"PERSONAL_GENDER",
"EXTERNAL_AUTH_ID",
"WORK_PHONE",
"PERSONAL_PHONE",
"PERSONAL_MOBILE",
"TIME_ZONE_OFFSET",
"ACTIVE",
"LAST_ACTIVITY_DATE",
783 "COLOR" =>
"ST.COLOR",
"STATUS" =>
"ST.STATUS",
"IDLE" =>
"ST.IDLE",
"MOBILE_LAST_DATE" =>
"ST.MOBILE_LAST_DATE",
785 if($intranetInstalled)
787 $select[] =
'UF_PHONE_INNER';
788 $select[] =
'UF_DEPARTMENT';
790 if ($voximplantInstalled)
792 $select[] =
'UF_VI_PHONE';
801 '\Bitrix\Im\Model\StatusTable',
803 "=ref.USER_ID" =>
"this.ID",
805 array(
"join_type"=>
"LEFT")
811 $ormParams[
'offset'] = $filterOffset;
812 $ormParams[
'limit'] = $filterLimit;
815 $orm = \Bitrix\Main\UserTable::getList($ormParams);
816 $bots = \Bitrix\Im\Bot::getListCache();
819 while ($user = $orm->fetch())
821 if (\CIMContactList::IsExtranet($user))
827 if (isset($user[
'COLOR']) && $user[
'COLOR'] <>
'')
829 $color = \Bitrix\Im\Color::getColor($user[
'COLOR']);
833 $color = \CIMContactList::GetUserColor($user[
"ID"], $user[
'PERSONAL_GENDER'] ==
'M'?
'M':
'F');
836 $users[$user[
"ID"]] = Array(
837 'ID' => (
int)$user[
"ID"],
840 'LAST_NAME' => $user[
'LAST_NAME'],
841 'WORK_POSITION' => $user[
'WORK_POSITION'],
843 'AVATAR' => \CIMChat::GetAvatarImage($user[
"PERSONAL_PHOTO"], 200,
false),
844 'GENDER' => $user[
'PERSONAL_GENDER'] ==
'F'?
'F':
'M',
845 'BIRTHDAY' => $user[
'PERSONAL_BIRTHDAY'] instanceof \Bitrix\Main\Type\Date? $user[
'PERSONAL_BIRTHDAY']->format(
'd-m'):
false,
846 'EXTRANET' => \CIMContactList::IsExtranet($user),
847 'NETWORK' => $user[
'EXTERNAL_AUTH_ID'] == \CIMContactList::NETWORK_AUTH_ID || $user[
'EXTERNAL_AUTH_ID'] == \Bitrix\Im\Bot::EXTERNAL_AUTH_ID && $bots[$user[
"ID"]][
'TYPE'] == \Bitrix\Im\Bot::TYPE_NETWORK,
848 'BOT' => $user[
'EXTERNAL_AUTH_ID'] == \Bitrix\Im\Bot::EXTERNAL_AUTH_ID,
849 'CONNECTOR' => $user[
'EXTERNAL_AUTH_ID'] ==
"imconnector",
850 'EXTERNAL_AUTH_ID' => $user[
'EXTERNAL_AUTH_ID']? $user[
'EXTERNAL_AUTH_ID']:
'default',
851 'STATUS' => $user[
'STATUS'],
852 'IDLE' => $user[
'IDLE'] instanceof \Bitrix\Main\Type\DateTime? $user[
'IDLE']:
false,
853 'LAST_ACTIVITY_DATE' => $user[
'MOBILE_LAST_DATE'] instanceof \Bitrix\Main\Type\DateTime? $user[
'MOBILE_LAST_DATE']:
false,
854 'MOBILE_LAST_DATE' => $user[
'LAST_ACTIVITY_DATE'] instanceof \Bitrix\Main\Type\DateTime? $user[
'LAST_ACTIVITY_DATE']:
false,
855 'DEPARTMENTS' => is_array($user[
'UF_DEPARTMENT']) && !empty($user[
'UF_DEPARTMENT'])? $user[
'UF_DEPARTMENT']: [],
856 'ABSENT' => \CIMContactList::formatAbsentResult($user[
"ID"]),
858 if ($params[
'HR_PHOTO'])
860 $users[$user[
"ID"]][
'AVATAR_HR'] = $users[$user[
"ID"]][
'avatar'];
863 if ($voximplantInstalled)
865 $user[
"WORK_PHONE"] = \CVoxImplantPhone::Normalize($user[
"WORK_PHONE"]);
866 if ($user[
"WORK_PHONE"])
868 $users[$user[
"ID"]][
'PHONES'][
'WORK_PHONE'] = $user[
'WORK_PHONE'];
870 $user[
"PERSONAL_MOBILE"] = \CVoxImplantPhone::Normalize($user[
"PERSONAL_MOBILE"]);
871 if ($user[
"PERSONAL_MOBILE"])
873 $users[$user[
"ID"]][
'PHONES'][
'PERSONAL_MOBILE'] = $user[
'PERSONAL_MOBILE'];
875 $user[
"PERSONAL_PHONE"] = \CVoxImplantPhone::Normalize($user[
"PERSONAL_PHONE"]);
876 if ($user[
"PERSONAL_PHONE"])
878 $users[$user[
"ID"]][
'PHONES'][
'PERSONAL_PHONE'] = $user[
'PERSONAL_PHONE'];
880 $user[
"UF_PHONE_INNER"] = preg_replace(
"/[^0-9\#\*]/i",
"", $user[
"UF_PHONE_INNER"]);
881 if ($user[
"UF_PHONE_INNER"])
883 $users[$user[
"ID"]][
'PHONES'][
'INNER_PHONE'] = $user[
"UF_PHONE_INNER"];
888 $users[$user[
"ID"]][
'PHONES'][
'WORK_PHONE'] = $user[
'WORK_PHONE'];
889 $users[$user[
"ID"]][
'PHONES'][
'PERSONAL_MOBILE'] = $user[
'PERSONAL_MOBILE'];
890 $users[$user[
"ID"]][
'PHONES'][
'PERSONAL_PHONE'] = $user[
'PERSONAL_PHONE'];
891 $users[$user[
"ID"]][
'PHONES'][
'INNER_PHONE'] = $user[
"UF_PHONE_INNER"];
897 foreach ($users as $key => $userData)
899 foreach ($userData as $field => $value)
903 $users[$key][$field] = date(
'c', $value->getTimestamp());
905 else if (is_string($value) && $value && is_string($field) && in_array($field, Array(
'AVATAR',
'AVATAR_HR')) && mb_strpos($value,
'http') !== 0)
907 $users[$key][$field] = \Bitrix\Im\Common::getPublicDomain().$value;
909 else if (is_array($value))
911 $users[$key][$field] = array_change_key_case($value, CASE_LOWER);
914 $users[$key] = array_change_key_case($users[$key], CASE_LOWER);;
923 if (isset($params[
'FILTER'][
'SEARCH']))
925 $filter = \Bitrix\Main\UserUtils::getUserSearchFilter(Array(
'FIND' => $params[
'FILTER'][
'SEARCH']));
936 $filter[
'=ACTIVE'] =
'Y';
937 $filter[
'=CONFIRM_CODE'] =
false;
938 $filter[
'!=EXTERNAL_AUTH_ID'] = \Bitrix\Im\Model\UserTable::filterExternalUserTypes([\
Bitrix\Im\Bot::EXTERNAL_AUTH_ID]);
944 $groups = \Bitrix\Im\Integration\Socialnetwork\Extranet::getGroup(Array(), $params[
'CURRENT_USER']);
945 if (is_array($groups))
947 foreach ($groups as $group)
949 foreach ($group[
'USERS'] as $userId)
951 $filterByUsers[$userId] = $userId;
954 $filterByUsers[$params[
'CURRENT_USER']] = $params[
'CURRENT_USER'];
959 $params[
'FILTER'][
'BUSINESS'] ==
'Y'
960 && \
Bitrix\Main\Loader::includeModule(
'bitrix24')
961 && !\CBitrix24BusinessTools::isLicenseUnlimited()
964 $businessUsers = \CBitrix24BusinessTools::getUnlimUsers();
968 $extranetBusinessResult = [];
969 foreach ($filterByUsers as $userId)
971 if (in_array($userId, $businessUsers))
973 $extranetBusinessResult[$userId] = $userId;
976 $filterByUsers = $extranetBusinessResult;
980 foreach ($businessUsers as $userId)
982 $filterByUsers[$userId] = $userId;
989 $filter[
'=ID'] = array_keys($filterByUsers);
992 return [
'filter' => $filter];
995 public static function getBusiness($userId =
null, $options = array())
997 $userId = \Bitrix\Im\Common::getUserId($userId);
1003 $pagination = isset($options[
'LIST'])?
true:
false;
1005 $limit = isset($options[
'LIST'][
'LIMIT'])? intval($options[
'LIST'][
'LIMIT']): 50;
1006 $offset = isset($options[
'LIST'][
'OFFSET'])? intval($options[
'LIST'][
'OFFSET']): 0;
1010 $businessUsersAvailable =
false;
1011 if (\
Bitrix\Main\Loader::includeModule(
'bitrix24') && !\CBitrix24BusinessTools::isLicenseUnlimited())
1013 $businessUsers = \CBitrix24BusinessTools::getUnlimUsers();
1017 $extranetBusinessResult = [];
1018 $groups = \Bitrix\Im\Integration\Socialnetwork\Extranet::getGroup(Array(), $userId);
1019 if (is_array($groups))
1021 foreach ($groups as $group)
1023 foreach ($group[
'USERS'] as $uid)
1025 if (in_array($uid, $businessUsers))
1027 $extranetUserList[$uid] = $uid;
1032 $list = $extranetBusinessResult;
1036 foreach ($businessUsers as $userId)
1038 $list[$userId] = $userId;
1042 $businessUsersAvailable =
true;
1045 $count = count($list);
1047 $list = array_slice($list, $offset, $limit);
1049 if ($options[
'USER_DATA'] ==
'Y')
1053 $getOptions = Array();
1054 if ($options[
'JSON'] ==
'Y')
1056 $getOptions[
'JSON'] =
'Y';
1059 foreach ($list as $userId)
1061 $result[] = \Bitrix\Im\User::getInstance($userId)->getArray($getOptions);
1066 $result = array_values($list);
1071 $result = Array(
'TOTAL' => $count,
'RESULT' => $result,
'AVAILABLE' => $businessUsersAvailable);
1073 if ($options[
'JSON'] ==
'Y')
1075 $result = array_change_key_case($result, CASE_LOWER);
1080 if (!$businessUsersAvailable)
1089 public static function getMessages($userId =
null, $options = Array())
1091 $userId = \Bitrix\Im\Common::getUserId($userId);
1099 '=AUTHOR_ID' => $userId
1102 if (isset($options[
'FIRST_ID']))
1106 if (intval($options[
'FIRST_ID']) > 0)
1108 $filter[
'>ID'] = $options[
'FIRST_ID'];
1113 $order = Array(
'ID' =>
'DESC');
1115 if (isset($options[
'LAST_ID']) && intval($options[
'LAST_ID']) > 0)
1117 $filter[
'<ID'] = intval($options[
'LAST_ID']);
1121 if (isset($options[
'LIMIT']))
1123 $options[
'LIMIT'] = intval($options[
'LIMIT']);
1124 $limit = $options[
'LIMIT'] >= 500? 500: $options[
'LIMIT'];
1131 $skipMessage = isset($options[
'SKIP_MESSAGE']) && $options[
'SKIP_MESSAGE'] ==
'Y';
1134 'ID',
'CHAT_ID',
'DATE_CREATE',
1135 'CHAT_TITLE' =>
'CHAT.TITLE'
1139 $select[] =
'MESSAGE';
1142 $orm = \Bitrix\Im\Model\MessageTable::getList(array(
1143 'filter' => $filter,
1144 'select' => $select,
1149 $messages = Array();
1150 $messagesChat = Array();
1151 while($message = $orm->fetch())
1153 $messages[$message[
'ID']] = Array(
1154 'ID' => (
int)$message[
'ID'],
1155 'DATE' => $message[
'DATE_CREATE'],
1156 'TEXT' => (
string)$message[
'MESSAGE'],
1161 unset($messages[$message[
'ID']][
'TEXT']);
1164 $messagesChat[$message[
'ID']] = Array(
1165 'ID' => (
int)$message[
'ID'],
1166 'CHAT_ID' => (
int)$message[
'CHAT_ID']
1170 $params = \CIMMessageParam::Get(array_keys($messages));
1173 foreach ($params as $messageId => $param)
1175 $messages[$messageId][
'params'] = empty($param)?
null: $param;
1177 if (isset($param[
'FILE_ID']))
1179 foreach ($param[
'FILE_ID'] as $fileId)
1181 $fileIds[$messagesChat[$messageId][
'CHAT_ID']][$fileId] = $fileId;
1186 $messages = \CIMMessageLink::prepareShow($messages, $params);
1189 foreach ($fileIds as $chatId => $fileId)
1191 if ($result = \CIMDisk::GetFiles($chatId, $fileId))
1193 $files = array_merge($files, $result);
1198 'MESSAGES' => $messages,
1202 if ($options[
'JSON'])
1204 foreach ($result[
'MESSAGES'] as $key => $value)
1208 $result[
'MESSAGES'][$key][
'DATE'] = date(
'c', $value[
'DATE']->getTimestamp());
1211 $result[
'MESSAGES'][$key] = array_change_key_case($result[
'MESSAGES'][$key], CASE_LOWER);
1213 $result[
'MESSAGES'] = array_values($result[
'MESSAGES']);
1215 foreach ($result[
'FILES'] as $key => $value)
1219 $result[
'FILES'][$key][
'date'] = date(
'c', $value[
'date']->getTimestamp());
1222 foreach ([
'urlPreview',
'urlShow',
'urlDownload'] as $field)
1224 $url = $result[
'FILES'][$key][$field];
1225 if (is_string($url) && $url && mb_strpos($url,
'http') !== 0)
1227 $result[
'FILES'][$key][$field] = \Bitrix\Im\Common::getPublicDomain().$url;
1232 $result = array_change_key_case($result, CASE_LOWER);