library/Garp/Social/Facebook.php
public function mapFriends(array $config) {
        $config = $config instanceof Garp_Util_Configuration ? $config : new Garp_Util_Configuration($config);
        $config->obligate('bindingModel')
            ->obligate('user_id')
            ->setDefault('accessToken', $this->getAccessToken())
        ;

        if (!$config['accessToken']) {
            // Find the auth record
            $authModel = new Model_AuthFacebook();
            $authRow   = $authModel->fetchRow($authModel->select()->where('user_id = ?', $config['user_id']));
            if (!$authRow || !$authRow->access_token) {
                return false;
            }

            // Use the stored access token to create a user session. Me() in the FQL ahead will contain the user's Facebook ID.
            // Note that the access token is available for a very limited time. Chances are it's not valid anymore.
            $accessToken = $authRow->access_token;
        }
        try {
            $this->_client->setAccessToken($config['accessToken']);

            // Find the friends' Facebook UIDs
            $friends = $this->_client->api(array(
                'method' => 'fql.query',
                'query'  => 'SELECT uid2 FROM friend WHERE uid1 = me()'
            ));

            // Find local user records
            $userModel = new Model_User();
            $userTable = $userModel->getName();
            $authFbModel = new Model_AuthFacebook();
            $authFbTable = $authFbModel->getName();

            $fbIds = '';
            $friendCount = count($friends);
            foreach ($friends as $i => $friend) {
                $fbIds .= $userModel->getAdapter()->quote($friend['uid2']);
                if ($i < ($friendCount-1)) {
                    $fbIds .= ',';
                }
            }
            $friendQuery = $userModel->select()
                ->setIntegrityCheck(false)
                ->from($userTable, array('id'))
                ->join($authFbTable, $authFbTable.'.user_id = '.$userTable.'.id', array())
                ->where('facebook_uid IN ('.$fbIds.')')
                ->order($userTable.'.id')
            ;
            $localUsers = $userModel->fetchAll($friendQuery);
            $localUserCount = count($localUsers);

            // Insert new friendships into binding model
            $bindingModel = new $config['bindingModel'];
            $insertSql = 'INSERT IGNORE INTO '.$bindingModel->getName().' (user1_id, user2_id) VALUES ';
            foreach ($localUsers as $i => $localUser) {
                $insertSql .= '('.$localUser->id.','.$config['user_id'].'),';
                $insertSql .= '('.$config['user_id'].','.$localUser->id.')';
                if ($i < ($localUserCount-1)) {
                    $insertSql .= ',';
                }
            }
            $result = $bindingModel->getAdapter()->query($insertSql);
            // Clear cache manually, since the table isn't updated thru conventional paths.
            Garp_Cache_Manager::purge($bindingModel);
            return !!$result;
        } catch (Exception $e) {
            return false;
        }
    }
public function mapFriends(array $config) {
        $config = $config instanceof Garp_Util_Configuration ? $config : new Garp_Util_Configuration($config);
        $config->obligate('bindingModel')
            ->obligate('user_id')
            ->setDefault('accessToken', $this->getAccessToken())
        ;

        if (!$config['accessToken']) {
            // Find the auth record
            $authModel = new Model_AuthFacebook();
            $authRow   = $authModel->fetchRow($authModel->select()->where('user_id = ?', $config['user_id']));
            if (!$authRow || !$authRow->access_token) {
                return false;
            }

            // Use the stored access token to create a user session. Me() in the FQL ahead will contain the user's Facebook ID.
            // Note that the access token is available for a very limited time. Chances are it's not valid anymore.
            $accessToken = $authRow->access_token;
        }
        try {
            $this->_client->setAccessToken($config['accessToken']);

            // Find the friends' Facebook UIDs
            $friends = $this->_client->api(array(
                'method' => 'fql.query',
                'query'  => 'SELECT uid2 FROM friend WHERE uid1 = me()'
            ));

            // Find local user records
            $userModel = new Model_User();
            $userTable = $userModel->getName();
            $authFbModel = new Model_AuthFacebook();
            $authFbTable = $authFbModel->getName();

            $fbIds = '';
            $friendCount = count($friends);
            foreach ($friends as $i => $friend) {
                $fbIds .= $userModel->getAdapter()->quote($friend['uid2']);
                if ($i < ($friendCount-1)) {
                    $fbIds .= ',';
                }
            }
            $friendQuery = $userModel->select()
                ->setIntegrityCheck(false)
                ->from($userTable, array('id'))
                ->join($authFbTable, $authFbTable.'.user_id = '.$userTable.'.id', array())
                ->where('facebook_uid IN ('.$fbIds.')')
                ->order($userTable.'.id')
            ;
            $localUsers = $userModel->fetchAll($friendQuery);
            $localUserCount = count($localUsers);

            // Insert new friendships into binding model
            $bindingModel = new $config['bindingModel'];
            $insertSql = 'INSERT IGNORE INTO '.$bindingModel->getName().' (user1_id, user2_id) VALUES ';
            foreach ($localUsers as $i => $localUser) {
                $insertSql .= '('.$localUser->id.','.$config['user_id'].'),';
                $insertSql .= '('.$config['user_id'].','.$localUser->id.')';
                if ($i < ($localUserCount-1)) {
                    $insertSql .= ',';
                }
            }
            $result = $bindingModel->getAdapter()->query($insertSql);
            // Clear cache manually, since the table isn't updated thru conventional paths.
            Garp_Cache_Manager::purge($bindingModel);
            return !!$result;
        } catch (Exception $e) {
            return false;
        }
    }
public function mapFriends ( array $ config ) { $ config = $ config instanceof Garp_Util_Configuration ? $ config : new Garp_Util_Configuration ( $ config ) ; $ config -> obligate ( 'bindingModel' ) -> obligate ( 'user_id' ) -> setDefault ( 'accessToken' , $ this -> getAccessToken ( ) ) ; if ( ! $ config [ 'accessToken' ] ) { // Find the auth record $ authModel = new Model_AuthFacebook ( ) ; $ authRow = $ authModel -> fetchRow ( $ authModel -> select ( ) -> where ( 'user_id = ?' , $ config [ 'user_id' ] ) ) ; if ( ! $ authRow || ! $ authRow -> access_token ) { return false ; } // Use the stored access token to create a user session. Me() in the FQL ahead will contain the user's Facebook ID. // Note that the access token is available for a very limited time. Chances are it's not valid anymore. $ accessToken = $ authRow -> access_token ; } try { $ this -> _client -> setAccessToken ( $ config [ 'accessToken' ] ) ; // Find the friends' Facebook UIDs $ friends = $ this -> _client -> api ( array ( 'method' => 'fql.query' , 'query' => 'SELECT uid2 FROM friend WHERE uid1 = me()' ) ) ; // Find local user records $ userModel = new Model_User ( ) ; $ userTable = $ userModel -> getName ( ) ; $ authFbModel = new Model_AuthFacebook ( ) ; $ authFbTable = $ authFbModel -> getName ( ) ; $ fbIds = '' ; $ friendCount = count ( $ friends ) ; foreach ( $ friends as $ i => $ friend ) { $ fbIds .= $ userModel -> getAdapter ( ) -> quote ( $ friend [ 'uid2' ] ) ; if ( $ i < ( $ friendCount - 1 ) ) { $ fbIds .= ',' ; } } $ friendQuery = $ userModel -> select ( ) -> setIntegrityCheck ( false ) -> from ( $ userTable , array ( 'id' ) ) -> join ( $ authFbTable , $ authFbTable . '.user_id = ' . $ userTable . '.id' , array ( ) ) -> where ( 'facebook_uid IN (' . $ fbIds . ')' ) -> order ( $ userTable . '.id' ) ; $ localUsers = $ userModel -> fetchAll ( $ friendQuery ) ; $ localUserCount = count ( $ localUsers ) ; // Insert new friendships into binding model $ bindingModel = new $ config [ 'bindingModel' ] ; $ insertSql = 'INSERT IGNORE INTO ' . $ bindingModel -> getName ( ) . ' (user1_id, user2_id) VALUES ' ; foreach ( $ localUsers as $ i => $ localUser ) { $ insertSql .= '(' . $ localUser -> id . ',' . $ config [ 'user_id' ] . '),' ; $ insertSql .= '(' . $ config [ 'user_id' ] . ',' . $ localUser -> id . ')' ; if ( $ i < ( $ localUserCount - 1 ) ) { $ insertSql .= ',' ; } } $ result = $ bindingModel -> getAdapter ( ) -> query ( $ insertSql ) ; // Clear cache manually, since the table isn't updated thru conventional paths. Garp_Cache_Manager :: purge ( $ bindingModel ) ; return ! ! $ result ; } catch ( Exception $ e ) { return false ; } }
Find friends of logged in user and map to local friends table.
@param Array $config
@return Bool Success
Find friends of logged in user and map to local friends table .