sUserId = $sUserId; $this->sUserFindId = $sUserFindId; $aUserPaths = $this->findUserAssociation(); $this->getUniqueRequiredUsers($aUserPaths); $this->loadRequiredUsers(); return $this->buildAssociationList($aUserPaths); } /** * returns all relation paths from start to end user * * @return array */ private function findUserAssociation() { $iFounds = 0; $aUserPaths = array(); for ($i = 0; $i <= $this->iMaxDepth; $i ++) { $sSelect = $this->getQuery($i, $this->iMaxPaths - $iFounds); $recordSet = parent::execute($sSelect); if (is_array($recordSet) && count($recordSet) > 0) { foreach ($recordSet as $oUser) $aUserPaths[] = $oUser; } } return $aUserPaths; } /** * creates select query * * @param $iCurrentDepth integer * @param $iMaxDepth integer * * @return string */ private function getQuery($iCurrentDepth, $iMaxDepth) { $sSelect = "SELECT l0.user_id AS USER1, l0.user_id_2 AS USER2"; $sSelectFrom = "FROM buddy_list l0 "; $sSelectWhere = "WHERE l0.user_id = '{$this->sUserId}'"; for ($i = 0, $j = 1; $i < $iCurrentDepth; $i ++, $j ++) { $sSelect .=",l$j.user_id_2 AS USER" . ($i + 3) . ""; $sSelectFrom .=" JOIN buddy_list l$j ON l$i.user_id_2 = l$j.user_id"; for ($k = 0; $k < $j; $k ++) $sSelectWhere .= " AND l$j.user_id_2 != l$k.user_id AND l$j.status = 'a' AND l$k.status_2 = 'a'"; } $sSelectWhere .= " AND l$i.user_id_2= '{$this->sUserFindId}' AND l$i.status_2 = 'a' LIMIT $iMaxDepth"; return "$sSelect $sSelectFrom $sSelectWhere"; } /** * builds array of user relation unique id's * * @param $aUserPaths array */ private function getUniqueRequiredUsers($aUserPaths) { foreach ($aUserPaths as $aPath) { foreach ($aPath as $sUserField => $sUserId) $this->aUserUniqueIds[$sUserId] = $sUserId; } $this->removeNotRequiredLoadIds(); } /** * remove start and end user */ private function removeNotRequiredLoadIds() { return; unset($this->aUserUniqueIds[$this->sUserId]); unset($this->aUserUniqueIds[$this->sUserFindId]); } /** * generate user object list of all required users * * @return array */ private function loadRequiredUsers() { $oUserList = newCore("user"); $oUserList->load(0); $oUserList->sWhere = $this->buildWhereForUserList(); $oUserList->createList(count($this->aUserUniqueIds)); return $this->regenerateUserList($oUserList->list); } /** * where part of select query to get all required users * * @return string */ private function buildWhereForUserList() { $sWhere = " WHERE (id = '"; $sWhere .= implode("' OR id = '", $this->aUserUniqueIds); $sWhere .= "')"; return $sWhere; } /** * builds user association list * * @param $aUserPaths array * * @return array */ private function buildAssociationList($aUserPaths) { foreach ($aUserPaths as $sPathKey => $oPath) foreach ($oPath as $sUserKey => $sUserId) { if($this->aUserList[$sUserId]->user__id->value) $aUserPaths[$sPathKey]->$sUserKey = $this->aUserList[$sUserId]; else unset($aUserPaths[$sPathKey]->$sUserKey); } return $aUserPaths; } /** * creates merged user list for view class * * @param $aUserList array * @return array */ private function regenerateUserList($aUserList) { if(!is_array($aUserList)) return; foreach ($aUserList as $oUser) $this->aUserList[$oUser->user__id->value] = $oUser; return $aNewUserList; } }