1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Loading in extra data

Discussion in 'XenForo Development Discussions' started by Jerry, Sep 5, 2011.

  1. Jerry

    Jerry Well-Known Member

    Hey all, I've got a task to deal with ........ I'm integrating a XF board with a Facebook game and visa versa.

    First step is that I need to get some data from the game database so I can use it in the message_user_info template (show the level & awards, give links to game profiles for admins etc).

    So I need to get xf_user_external_auth.provider_key (the facebook ID) and then use that to get the player data from the game database.

    Though where to load it in and how to grab the extra data ?

    I'm all ears :)
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Here is a quick hack to join xf_user_external_auth for posts:

    library/XenForo/Model/Post.php

    Add the red code:

    Code:
    	public function preparePostJoinOptions(array $fetchOptions)
    	{
    		$selectFields = '';
    		$joinTables = '';
    
    		$db = $this->_getDb();
    
    		if (!empty($fetchOptions['join']))
    		{
    			if ($fetchOptions['join'] & self::FETCH_THREAD || $fetchOptions['join'] & self::FETCH_FORUM)
    			{
    				$selectFields .= ',
    					thread.*, thread.user_id AS thread_user_id, thread.username AS thread_username,
    					post.user_id, post.username, post.post_date'; // overwrite thread.post_date with post.post_date
    				$joinTables .= '
    					INNER JOIN xf_thread AS thread ON
    						(thread.thread_id = post.thread_id)';
    			}
    
    			if ($fetchOptions['join'] & self::FETCH_FORUM)
    			{
    				$selectFields .= ',
    					node.title AS node_title';
    				$joinTables .= '
    					INNER JOIN xf_node AS node ON
    						(node.node_id = thread.node_id)';
    			}
    
    			if ($fetchOptions['join'] & self::FETCH_USER)
    			{
    				$selectFields .= ',
    					user.*, IF(user.username IS NULL, post.username, user.username) AS username';
    				$joinTables .= '
    					LEFT JOIN xf_user AS user ON
    						(user.user_id = post.user_id)';
    
    				/* JOIN xf_user_external_auth */
    				$selectFields .= ',
    					user_external_auth.*';
    				$joinTables .= '
    					LEFT JOIN xf_user_external_auth AS user_external_auth ON
    						(user_external_auth.user_id = post.user_id)';
    				/* JOIN xf_user_external_auth */
    			}
    
    			if ($fetchOptions['join'] & self::FETCH_USER_PROFILE)
    			{
    				$selectFields .= ',
    					user_profile.*';
    				$joinTables .= '
    					LEFT JOIN xf_user_profile AS user_profile ON
    						(user_profile.user_id = post.user_id)';
    			}
    
    			if ($fetchOptions['join'] & self::FETCH_USER_OPTIONS)
    			{
    				$selectFields .= ',
    					user_option.*';
    				$joinTables .= '
    					LEFT JOIN xf_user_option AS user_option ON
    						(user_option.user_id = post.user_id)';
    			}
    
    			if ($fetchOptions['join'] & self::FETCH_DELETION_LOG)
    			{
    				$selectFields .= ',
    					deletion_log.delete_date, deletion_log.delete_reason,
    					deletion_log.delete_user_id, deletion_log.delete_username';
    				$joinTables .= '
    					LEFT JOIN xf_deletion_log AS deletion_log ON
    						(deletion_log.content_type = \'post\' AND deletion_log.content_id = post.post_id)';
    			}
    		}
    
    		if (!empty($fetchOptions['permissionCombinationId']))
    		{
    			$selectFields .= ',
    				permission.cache_value AS node_permission_cache';
    			$joinTables .= '
    				LEFT JOIN xf_permission_cache_content AS permission
    					ON (permission.permission_combination_id = ' . $db->quote($fetchOptions['permissionCombinationId']) . '
    						AND permission.content_type = \'node\'
    						AND permission.content_id = thread.node_id)';
    		}
    
    		if (isset($fetchOptions['likeUserId']))
    		{
    			if (empty($fetchOptions['likeUserId']))
    			{
    				$selectFields .= ',
    					0 AS like_date';
    			}
    			else
    			{
    				$selectFields .= ',
    					liked_content.like_date';
    				$joinTables .= '
    					LEFT JOIN xf_liked_content AS liked_content
    						ON (liked_content.content_type = \'post\'
    							AND liked_content.content_id = post.post_id
    							AND liked_content.like_user_id = ' .$db->quote($fetchOptions['likeUserId']) . ')';
    			}
    		}
    
    		return array(
    			'selectFields' => $selectFields,
    			'joinTables'   => $joinTables
    		);
    	}
    
    Now this variable will work in the message_user_info template:

    Code:
    {$user.provider_key}
    
    You can edit this file to use the provider_key to query your game database:

    library/XenForo/ControllerPublic/Thread.php

    Add the red code:

    Code:
    		$inlineModOptions = array();
    		$maxPostDate = 0;
    		$firstUnreadPostId = 0;
    
    		$deletedPosts = 0;
    		$moderatedPosts = 0;
    
    		$pagePosition = 0;
    
    		$permissions = $visitor->getNodePermissions($thread['node_id']);
    		foreach ($posts AS &$post)
    		{
    			/* GET PLAYER DATA */
    			if ($post['provider_key'])
    			{
    				// QUERY THE GAME DATABASE
    			}
    			/* GET PLAYER DATA */
    
    			$post['position_on_page'] = ++$pagePosition;
    
    			$postModOptions = $postModel->addInlineModOptionToPost(
    				$post, $thread, $forum, $permissions
    			);
    			$inlineModOptions += $postModOptions;
    
    			$post = $postModel->preparePost($post, $thread, $forum, $permissions);
    
    			if ($post['post_date'] > $maxPostDate)
    			{
    				$maxPostDate = $post['post_date'];
    			}
    
    You probably don't want to query inside of a foreach, but you get the idea. ;)
     

Share This Page