示例#1
0
 public static function GetList($arParams = array())
 {
     if (!is_array($arParams)) {
         $arParams = array();
     }
     $arID = array();
     if (isset($arParams['ID']) && is_array($arParams['ID']) && !empty($arParams['ID'])) {
         foreach ($arParams['ID'] as $key => $value) {
             $arID[] = intval($value);
         }
     } else {
         if (isset($arParams['ID']) && intval($arParams['ID']) > 0) {
             $arID[] = intval($arParams['ID']);
         }
     }
     $arParams['GET_OFFLINE'] = !empty($arID) && isset($arParams['GET_OFFLINE']) && $arParams['GET_OFFLINE'] == 'Y' ? 'Y' : 'N';
     global $USER;
     $userId = is_object($USER) ? intval($USER->GetID()) : 0;
     $bBusShowAll = !IsModuleInstalled('intranet') && COption::GetOptionInt('im', 'contact_list_show_all_bus');
     if (!$bBusShowAll && !isset($arParams['ID']) && $userId > 0 && !isset($arParams['SKIP_CHECK'])) {
         if (isset(self::$FRIENDS_USERS[$userId])) {
             $arID = self::$FRIENDS_USERS[$userId];
         } else {
             if (CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed()) {
                 $arID = array($userId);
                 $dbFriends = CSocNetUserRelations::GetList(array(), array("USER_ID" => $userId, "RELATION" => SONET_RELATIONS_FRIEND), false, false, array("ID", "FIRST_USER_ID", "SECOND_USER_ID"));
                 if ($dbFriends) {
                     while ($arFriends = $dbFriends->Fetch()) {
                         $arID[] = $userId == $arFriends["FIRST_USER_ID"] ? $arFriends["SECOND_USER_ID"] : $arFriends["FIRST_USER_ID"];
                     }
                 }
                 self::$FRIENDS_USERS[$userId] = $arID;
             }
         }
     }
     $arUsers = array();
     if (self::$ONLINE_USERS && $arParams['GET_OFFLINE'] == 'N') {
         $arUsers = self::$ONLINE_USERS;
     } else {
         if (!self::$ONLINE_USERS || $arParams['GET_OFFLINE'] == 'Y') {
             $enable = self::Enable();
             $arUsers = array();
             $query = new \Bitrix\Main\Entity\Query(\Bitrix\Main\UserTable::getEntity());
             $query->registerRuntimeField('', new \Bitrix\Main\Entity\ReferenceField('ref', 'Bitrix\\Im\\StatusTable', array('=this.ID' => 'ref.USER_ID')));
             $query->addSelect('ID')->addSelect('ref.COLOR', 'COLOR')->addSelect('PERSONAL_GENDER');
             if ($enable) {
                 $query->addSelect('ref.STATUS', 'STATUS')->addSelect('ref.IDLE', 'IDLE')->addSelect('ref.MOBILE_LAST_DATE', 'MOBILE_LAST_DATE');
             }
             if ($arParams['GET_OFFLINE'] == 'N') {
                 $query->addFilter('>LAST_ACTIVITY_DATE', new \Bitrix\Main\DB\SqlExpression(Bitrix\Main\Application::getConnection()->getSqlHelper()->addSecondsToDateTime('-180')));
             } else {
                 $sago = Bitrix\Main\Application::getConnection()->getSqlHelper()->addSecondsToDateTime('-180');
                 $query->registerRuntimeField('', new \Bitrix\Main\Entity\ExpressionField('IS_ONLINE_CUSTOM', 'CASE WHEN LAST_ACTIVITY_DATE > ' . $sago . ' THEN \'Y\' ELSE \'N\' END'));
                 $query->addSelect('IS_ONLINE_CUSTOM');
                 $query->addFilter('=ID', $arID);
             }
             $result = $query->exec();
             while ($arUser = $result->fetch()) {
                 $color = null;
                 if (isset($arUser['COLOR']) && strlen($arUser['COLOR']) > 0) {
                     $color = IM\Color::getColor($arUser['COLOR']);
                 }
                 if (!$color) {
                     $color = \CIMContactList::GetUserColor($arUser["ID"], $arUser['PERSONAL_GENDER'] == 'M' ? 'M' : 'F');
                 }
                 $arUsers[$arUser["ID"]] = array('id' => $arUser["ID"], 'status' => $enable && in_array($arUser['STATUS'], self::$AVAILABLE_STATUSES) ? $arUser['STATUS'] : 'online', 'color' => $color, 'idle' => $enable && is_object($arUser['IDLE']) ? $arUser['IDLE']->getTimestamp() : 0, 'mobileLastDate' => $enable && is_object($arUser['MOBILE_LAST_DATE']) ? $arUser['MOBILE_LAST_DATE']->getTimestamp() : 0);
                 if ($arParams['GET_OFFLINE'] == 'Y' && $arUser['IS_ONLINE_CUSTOM'] == 'N') {
                     $arUsers[$arUser["ID"]]['status'] = 'offline';
                     $arUsers[$arUser["ID"]]['idle'] = 0;
                     $arUsers[$arUser["ID"]]['mobileLastDate'] = 0;
                 }
             }
             if ($arParams['GET_OFFLINE'] == 'N') {
                 self::$ONLINE_USERS = $arUsers;
             }
         }
     }
     $arResult = array();
     if (empty($arID)) {
         $arResult = $arUsers;
     } else {
         foreach ($arID as $userId) {
             if (isset($arUsers[$userId])) {
                 $arResult[$userId] = $arUsers[$userId];
             }
         }
     }
     return array('users' => $arResult);
 }