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

Arguments of ismemberof function?

Insy

Well-known member
#2
PHP:
public static function helperIsMemberOf(array $user, $userGroupId, $multipleIds = null)
{
if (!is_null($multipleIds))
{
// check multiple groups
$userGroupId = array_slice(func_get_args(), 1);
}
 
return self::_getModelFromCache('XenForo_Model_User')->isMemberOfUserGroup($user, $userGroupId);
}
 

Jake Bunce

XenForo moderator
Staff member
#4
$thread likely contains the thread record as well as the user record. Only the user part of the array is relevant here.

It seems like I can't extract the userID which I know is there by looking and the code and how it's used. This userID I'm going to use in the function ismemberof.
ismemberof requires the full user record, not just the user_id. Make sure $thread contains the full user record.
 
#5
$thread likely contains the thread record as well as the user record. Only the user part of the array is relevant here.



ismemberof requires the full user record, not just the user_id. Make sure $thread contains the full user record.
What would be the correct usage of $thread in the ismemberof function? I want to point to the userID, but I'm not sure really how.
 

Insy

Well-known member
#6
Here is how this code works:

First, this query will retrieve the latest topics according to many conditions. The result will be an array ($recent).
PHP:
if (in_array($widget['options']['type'], array('recent', 'all'))) {
$recent = $threadModel->getThreads(
$conditions
, array_merge($fetchOptions, array(
'order' => 'last_post_date',
'orderDirection' => 'desc',
'join' => 0,
'last_post_join' => XenForo_Model_Thread::FETCH_AVATAR,
))
);
For each topic, the user id and user name are defined in such a way that the helper isMemberOf can identify the user.
Here, the values ​​are defined by reference, no need to write something like $recent[$id] = $thread ;)
PHP:
foreach ($recent as &$thread) {
$thread['user_id'] = $thread['last_post_user_id'];
$thread['username'] = $thread['last_post_username'];
}
} else {
$recent = array();
}
In the template wf_widget_threads, the $recent array is iterated. Each $thread is also an array.
The two values defined above (user_id and username) will be sought by the isMemberOf helper into $thread.
HTML:
<xen:foreach loop="$recent" value="$thread">
              <li>
              <xen:avatar user="$thread" size="s" img="true" />
 
<xen:include template="wf_widget_threads_thread_title">
<xen:set var="$_showPrefix">{$widget.options.display.show_prefix}</xen:set>
<xen:set var="$_threadLink">{xen:if '{$visitor.user_id} > 0', {xen:link threads/unread, $thread}, {xen:link posts, {xen:array 'post_id={$thread.last_post_id}'}}}</xen:set>
</xen:include>
 
              <div class="userTitle">{xen:phrase wf_x_replied, 'user=<a href="{xen:link 'members', $thread}">{$thread.username}</a>'} <xen:datetime time="{$thread.last_post_date}" /></div>
              </li>
          </xen:foreach>
To summarize, the isMemberOf helper needs to have as first parameter an array with two keys: user_id (int) and username (string).
 
#7
Thanks for that great summary! (y)

Does that mean that I only have to submit the $thread array in the isMemberOf helper without specifying user or username attribute of the array? :unsure:
 
#9
When I look at the FAQ the example of a condition looks like this:

How can I show content to a specific user group?
PHP:
<xen:if is="{xen:helper ismemberof, $visitor, x}">
This content will show to members of user group x
</xen:if>
That's what I have used in my condition. Username is nowhere to be found as far as I can see. I'm probably the one where who's a little bit stupid at the moment not understanding what you are so clearly trying to tell me :)
 

Insy

Well-known member
#10
That's what I have used in my condition. Username is nowhere to be found as far as I can see. I'm probably the one where who's a little bit stupid at the moment not understanding what you are so clearly trying to tell me :)
I assume that $visitor is the current user.
PHP:
$visitor = XenForo_Visitor::getInstance()->toArray();
var_dump( $visitor );
Result:
Code:
array(48) {
  ["user_id"]=>
  int(1)
  ["username"]=>
  string(5) "Simon"
  ["email"]=>
  string(16) "xxxxxxx@gmail.com"
  ["gender"]=>
  string(0) ""
  ["custom_title"]=>
  string(0) ""
  ["language_id"]=>
  int(1)
  ["style_id"]=>
  int(0)
etc.
Or maybe I don't understand your question ? :confused: