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

Trying to merge gender with user's online status

Luxus

Well-known member
#1
Hello,

I am using this Online Status Indicator add-on and wanted to merge it with user's gender to save space and http requests.

In the message_user_online template I have this code:
PHP:
<xen:if is="{$user.gender} == 'male'"><img src="styles/default/xenforo/icons/{$userStatus.text}Male.png" class="user{$userStatus.class}">
<xen:elseif is="{$user.gender} == 'female'" /><img src="styles/default/xenforo/icons/{$userStatus.text}Female.png" class="user{$userStatus.class}">
<xen:else /><img src="styles/default/xenforo/icons/{$userStatus.text}.png" class="user{$userStatus.class}">
</xen:if>
But it doesn't trigger the gender conditions :( It always displays the latter, non-gender online/offline icons. What do I need to add and/or edit in the code to make this work?
 

James

Well-known member
#2
Quick question: are you sure the gender array key is available in that particular template?

If so, make sure you're putting the gender exactly as XenForo does (Male instead of male... maybe)
 

Luxus

Well-known member
#3
Quick question: are you sure the gender array key is available in that particular template?
I'm not sure. The message_user_online template is a custom template that comes with the Online Indicator add-on. Since it didn't work, what should I do to make it work?

If so, make sure you're putting the gender exactly as XenForo does (Male instead of male... maybe)
It's male and female ;)
 

James

Well-known member
#4
I'm not sure. The message_user_online template is a custom template that comes with the Online Indicator add-on. Since it didn't work, what should I do to make it work?

It's male and female ;)
type {xen:helper dump, $user} and see if you can actually access the array.
 

Luxus

Well-known member
#5
type {xen:helper dump, $user} and see if you can actually access the array.
I know I'm late. Missed totally this one, lol.

Anyway I put {xen:helper dump, $user} in the above template and I got a <pre>NULL</pre> in the html output. What does this mean?
 

James

Well-known member
#6
It means the $user variable isn't available in your template, so you can't merge them.

Put: {xen:helper dump, $userStatus} and post the outcome here.
 

Luxus

Well-known member
#7
Here you go
HTML:
array(2) {
  ["text"] => object(XenForo_Phrase)#89 (4) {
    ["_phraseName":protected] => string(6) "online"
    ["_params":protected] => array(0) {
    }
    ["_insertParamsEscaped":protected] => bool(true)
    ["_phraseNameOnInvalid":protected] => bool(true)
  }
  ["class"] => string(10) "UserOnline"
}
 

Luxus

Well-known member
#9
That's too bad. The add-on is only 2 php files. Perhaps someone does know what to edit there to make it work?

1) AspPost.php
PHP:
<?php
 
class Asp_OnlineStatus_Model_AspPost extends XFCP_Asp_OnlineStatus_Model_AspPost
{
    public function preparePostJoinOptions(array $fetchOptions)
    {
        $array = parent::preparePostJoinOptions($fetchOptions);
 
        if(!empty($fetchOptions['join']))
        {
            if($fetchOptions['join'] & self::FETCH_USER)
            {
                $array['selectFields'] .= ', session.view_date';
                $array['joinTables'] .= 'LEFT OUTER JOIN xf_session_activity AS session ON post.user_id = session.user_id';
            }
        }
 
        return $array;
    }
}
2) Plugin.php
PHP:
<?php
 
class Asp_OnlineStatus_Plugin_Plugin
{
 
    protected static $_session_timeout;
    protected static $_online_status_template;
    protected static $_online_location;
 
    public static function extendModel($class, array &$extend)
    {
        if($class == 'XenForo_Model_Post')
        {
            $extend[] = 'Asp_OnlineStatus_Model_AspPost';
        }
    }
 
    public static function messageUserInfo_TemplateHook($name, &$contents, $params, XenForo_Template_Abstract $template)
    {
        if(in_array($name, array('message_user_info_avatar', 'message_user_info_name', 'message_user_info_extra')) && isset($params['user']['message']) && !isset($params['user']['conversation_id']))
        {
           
           
            $visitor = XenForo_Visitor::getInstance();
           
            //if the user is online, and their online status is visible, show them regardless
            if(($params['user']['view_date'] > self::_getSessionTimeout()) && $params['user']['visible'])
            {
                $userStatus['text'] = new XenForo_Phrase('online');
                $userStatus['class'] = 'UserOnline';
            }
            //if the user is online, their online status is invisible, but the current user is an admin, or they're the current user, we'll call them online-invisible
            else if(($params['user']['view_date'] > self::_getSessionTimeout()) && ($params['user']['visible'] == 0) && ($visitor['is_admin'] || $visitor['user_id'] == $params['user']['user_id']))
            {
                $userStatus['text'] = new XenForo_Phrase('online_invisible');
                $userStatus['class'] = 'UserOnlineInvisible';
            }
            //if the user is an admin, their online status is invisible, but the current user is a moderator, we'll call them online_invisible
            else if($params['user']['view_date'] > self::_getSessionTimeout() && $params['user']['visible'] == 0 && $params['user']['is_admin'] && $visitor['is_moderator'])
            {
                $userStatus['text'] = new XenForo_Phrase('online_invisible');
                $userStatus['class'] = 'UserOnlineInvisible';
            }
            //otherwise, we'll call them offline
            else
            {
                $userStatus['text'] = new XenForo_Phrase('offline');
                $userStatus['class'] = 'UserOffline';
            }
 
            self::_getOnlineStatusTemplate()->setParam('userStatus', $userStatus);
            foreach(array('message_user_info_avatar', 'message_user_info_name', 'message_user_info_extra') AS $location => $hook_name)
            {
                /*
                * This logic is kind of obtuse
                * The location of the the online indicator tag is stored as a number between 1 to 6
                * I'm using the $location variable to determine where we should be displaying the online status based on that.
                * ($location + 1) * 2 is one of 2, 4, or 6.
                * So, if it's 1 or 2, and the template hook name is "message_user_info_avatar", we execute the code...and so on.
                * But, I don't care about the actual number, just if it's even or odd.
                * The unfortunate part is that we don't have a great way of extending it. I could drop it into the middle of the first two blocks
                * and multiply by 3, but the third block would need special logic
                * So, this is what it is for now.
                */
                if($name == $hook_name && (self::_getOnlineLocation() == ((($location + 1) * 2) -1) || self::_getOnlineLocation() == (($location + 1) * 2)))
                {
                    if(self::_getOnlineLocation() %2 == 0)
                    {
                        $contents .= self::_getOnlineStatusTemplate();
                    }
                    else
                    {
                        $contents = self::_getOnlineStatusTemplate() . $contents;
                    }
                }
            }
        }
    }
 
    protected static function _getOnlineStatusTemplate()
    {
        if(!isset(self::$_online_status_template))
        {
            self::$_online_status_template = new XenForo_Template_Public('message_user_online');
        }
 
        return self::$_online_status_template;
    }
 
    protected static function _getSessionTimeout()
    {
        if(!isset(self::$_session_timeout))
        {
            self::$_session_timeout = XenForo_Model::create('XenForo_Model_Session')->getOnlineStatusTimeout();
        }
 
        return self::$_session_timeout;
    }
 
    protected static function _getOnlineLocation()
    {
        if(!isset(self::$_online_location))
        {
            self::$_online_location = XenForo_Application::get('options')->displayOnlineLocation;
        }
 
        return self::$_online_location;
    }
}