Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: Cmely - huhtikuu 18, 2014, 03:42:37 IP

Otsikko: Show link only if user belong to a specific group
Kirjoitti: Cmely - huhtikuu 18, 2014, 03:42:37 IP
Hi,

On my memberlist (here (http://www.lux-optima.com/mlist)), I show a link ("view picture") under the avatar of the member. For the moment this link is shown for any member of the forum.

What would be the code to add in order to show the same link only if the member belong to a given group ?

Thank you for your help :)
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Sir Osis of Liver - huhtikuu 18, 2014, 11:44:23 IP


if ($user_info['groups']['0'] == '5')


Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Cmely - huhtikuu 19, 2014, 05:19:36 AP
Thank you for your answer Krash :)

What the ['0'] is for ?
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Cmely - huhtikuu 19, 2014, 06:06:00 AP
We're progressing  ;D ...

I've been able to check if the member belongs to a primary membergroup, but what I really need is to test if the member belong to a group but not a primary one (and not postcount based ones too).

I've tried something like this :
if (in_array(36, $member['groups']['id'] ))
echo '
<a href="' . $scripturl .  '?action=gallery;sa=myimages;u=',$member['id'], '">', $txt['smfgallery_profilelink'], '</a><br />';


('36' being the id_group)

...but I guess I'm not on the good track... :o
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: maximus23 - huhtikuu 19, 2014, 07:30:07 AP

Verify members groups.

Simple Examples :



global $user_info ,$context ;

if(in_array(1, $user_info['groups']))

{

echo' User : '. $user_info['id'] .' Name : ' . $context['user']['name'];

}


1 = id group

:)

Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Cmely - huhtikuu 19, 2014, 09:31:36 AP
Thank you for your help Maximus  :)

Just to be sure and also because I'm afraid I may have not been very clear  in my first post  :o ;D : if I understand correctly the code in your answer, it check if the logged in user belongs to membergroup '1'. Is that correct ?

What I rather wish is something to test if the member from whom I displays information  belongs to this group. If you have a look on my memberlist (http://www.lux-optima.com/index.php), it will be easier to understand. I'm showing member's informations like Name, avatar, email,  ... and it would like to add some extra information only if this member belongs to membergroup 'X'.

Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: maximus23 - huhtikuu 19, 2014, 10:41:52 AP

Verify action by group id for add extra information by user :



if ($member['group_id'] == 1)
{
  echo'Action for Administrator group';
}

if ($member['group_id'] == 2)
{
  echo'Action for Moderator group';
}

if ($member['group_id'] == 36)

{
echo '
<a href="' . $scripturl .  '?action=gallery;sa=myimages;u=',$member['id'], '">', $txt['smfgallery_profilelink'], '</a>';
}


:)
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Dzonny - huhtikuu 19, 2014, 10:44:21 AP
I would try to print_r member variable somewhere, just to see what can be used for what...
Think that $member['group_id'] should do the trick? There's also post_group which can be used, but like I sad try to print_r it, and you'll see what I'm talking about.
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Cmely - huhtikuu 19, 2014, 11:48:09 AP
the problem with member['group_id'] is that it refer only to the primary membergroups, but not to additional group...and of course (because it wouldn't be fun otherwise  ;D ) I need to check also in additionnal groups...

the field "additional_group" in smf_member table is an array, this is why I was trying with "if(in_array...."
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Cmely - huhtikuu 19, 2014, 12:08:41 IP
Lainaus käyttäjältä: Dzonny - huhtikuu 19, 2014, 10:44:21 AP
I would try to print_r member variable somewhere, just to see what can be used for what...
Think that $member['group_id'] should do the trick? There's also post_group which can be used, but like I sad try to print_r it, and you'll see what I'm talking about.

Dzonny, this is what I get when I do a print_r (I show the relevant part) :
[group] => [group_color] => [group_id] => 0 [post_group] => Newborn Baby [post_group_color] => [group_stars] => * [warning] => 0 [warning_status] => [additional_groups] => Array ( [id] => Array ( [0] => 33 [1] => 35 ) [stars] => Array ( [0] => * [1] => * ) [count_based_stars] => * )
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: margarett - huhtikuu 19, 2014, 12:13:07 IP
The best way is to merge together the "group_id" and the additional groups in a single array, then you can check if the membergroup you want is present.
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Cmely - huhtikuu 19, 2014, 12:49:49 IP
Lainaus käyttäjältä: margarett - huhtikuu 19, 2014, 12:13:07 IP
The best way is to merge together the "group_id" and the additional groups in a single array, then you can check if the membergroup you want is present.

Ok... :o...and how do I merge them together ? As you may already have noticed it, I'm not a great expert in this domain  ;D
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: margarett - huhtikuu 19, 2014, 01:02:22 IP
Actually.... Where did you get this from?
[group] => [group_color] => [group_id] => 0 [post_group] => Newborn Baby [post_group_color] => [group_stars] => * [warning] => 0 [warning_status] => [additional_groups] => Array ( [id] => Array ( [0] => 33 [1] => 35 ) [stars] => Array ( [0] => * [1] => * ) [count_based_stars] => * )

In a clean SMF install, the additional membergroups are not fetched for display anywhere...
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Cmely - huhtikuu 19, 2014, 01:06:50 IP
I did a print_r ($member)
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: margarett - huhtikuu 19, 2014, 01:10:23 IP
In which file? Memberlist.template.php?

Can you point me your list of MODs, please?
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Cmely - huhtikuu 19, 2014, 01:15:21 IP
Lainaus käyttäjältä: margarett - huhtikuu 19, 2014, 01:10:23 IP
In which file? Memberlist.template.php?

Yes.
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Cmely - huhtikuu 19, 2014, 01:19:39 IP
Here is the list of mods. Maybe what you're looking for is the mod 'Multiple Stars' (allow you to show several membergroups in your post profile)
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Kays - huhtikuu 19, 2014, 01:30:27 IP
Here's a slightly different approach to this subject.

http://custom.simplemachines.org/mods/index.php?mod=2930

Create a custom permission, assign it only to that group and use that as your check. ;)

Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: margarett - huhtikuu 19, 2014, 01:51:11 IP
Lainaus käyttäjältä: Cmely - huhtikuu 19, 2014, 01:19:39 IP
Maybe what you're looking for is the mod 'Multiple Stars' (allow you to show several membergroups in your post profile)
Yup, that should be the one, yes.

Lainaus käyttäjältä: Kays - huhtikuu 19, 2014, 01:30:27 IP
Here's a slightly different approach to this subject.
http://custom.simplemachines.org/mods/index.php?mod=2930
Create a custom permission, assign it only to that group and use that as your check. ;)
Probably also a nice idea ;)
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Cmely - huhtikuu 19, 2014, 02:02:55 IP
Lainaus käyttäjältä: Kays - huhtikuu 19, 2014, 01:30:27 IP
Here's a slightly different approach to this subject.

http://custom.simplemachines.org/mods/index.php?mod=2930

Create a custom permission, assign it only to that group and use that as your check. ;)

Thanks for the idea. But I'm afraid it won't work : those custom permission will apply only to the logged in user, right ? And, based on your example, will check if the logged in user belong to that group ?
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Cmely - huhtikuu 19, 2014, 02:08:18 IP
Except if I can use those permission like this :

if $MEMBER (allowedTo('view_this'))
echo 'Yes, you can see this.';


...but my syntax is certainly wrong...maybe you could suggest me how to do ?
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Kays - huhtikuu 19, 2014, 02:35:10 IP
Dang, you are correct. And that won't work since allowedTo() applies to the member viewing the page.

So you will need to use in_array() on the appropriate group in the array posted above. ::)

One thing though When doing a print_r use <pre> which will format the info in a friendlier manner.


echo '<pre>', print_r($member), '</pre>';
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Cmely - huhtikuu 19, 2014, 02:52:05 IP
Lainaus käyttäjältä: Kays - huhtikuu 19, 2014, 02:35:10 IP
Dang, you are correct. And that won't work since allowedTo() applies to the member viewing the page.

So you will need to use in_array() on the appropriate group in the array posted above. ::)

One thing though When doing a print_r use <pre> which will format the info in a friendlier manner.


echo '<pre>', print_r($member), '</pre>';


I had my hopes up for a while... ??? :o ;D
Thank for the tip (about 'pre') : it's much better like that...

This is what I get :
[group] => Administrator
        [group_color] => #BFA830
        [group_id] => 1
        [post_group] => Member 2.0
        [post_group_color] =>
        [group_stars] => *
        [warning] => 0
        [warning_status] =>
        [additional_groups] => Array
            (
                [id] => Array
                    (
                        [0] => 29
                    )

                [stars] => Array
                    (
                        [0] => *
                    )

                [count_based_stars] => *
            )


What I have in [Additional_groups] doesn't feel right to me. What do you think ? Should be more groups here ...
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Kays - huhtikuu 19, 2014, 03:11:52 IP
Lainaa
Should be more groups here

Those are the groups "you" belong to. Is that correct? It will change depending on the member.

So if the only groups to check are group_id and additional_groups. This can be done like so:


$check_groups = $member['additional_groups']['id'];
$check_groups[] = $member['group_id'];

// An example of usage
if (in_array(35, $check_groups))
  echo 'Yes, this member is in that group.';

Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Cmely - huhtikuu 19, 2014, 05:44:04 IP
I think I was right, something wasn't correct. I've uninstalled the mod "Multiple Stars" that messed up things and I replaced it with the mod "Stars and Badges".
And now, when I do a print_r (with 'pre'  ;) ) I get this (for the same member than previously) :

[group] => Administrator
        [group_color] => #BFA830
        [group_id] => 1
        [post_group] => Member 2.0
        [post_group_color] =>
        [additional_groups] => Array
            (
                [id] => Array
                    (
                        [0] => 29
                        [1] => 36
                    )

                [stars] => Array
                    (
                        [0] => *
                        [1] => *
                    )

                [name] => Array
                    (
                        [0] => Founder
                        [1] => Featured_1
                    )

                [ag_color] => Array
                    (
                        [0] =>
                        [1] =>
                    )

            )

        [group_stars] => *
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Kays - huhtikuu 20, 2014, 08:46:58 AP
Sorry, I was out for the rest of the evening. But I did get a chance this morning to look through the code to get a better idea of how things are put together there.

Are the additional groups correct now? If you are seeing the results of the print_r for each member on the page. You can go to their profile to verify which additional groups they belong to.
Otsikko: Re: Show link only if user belong to a specific group
Kirjoitti: Cmely - huhtikuu 20, 2014, 10:15:53 AP
Thanks to your help, it's much better but I still have some bugs. If you have a look on the memberlist page (http://www.lux-optima.com/mlist/sort=real_name;start=50), you'll see featured members (quarter red circle with a star). Those have a link to their picture. Those member belong to the group "Featured".

But, it doesn't work if the member belongs to several other groups.

I used your example and modified it a little bit, like that :

$check_groups = $member['additional_groups_full']['0'];

and made this test :
if (in_array(36, $check_groups))
echo '


You'll understand (I hope  :o ;D ) why I did that, if you look at what I get now when I debug $check_groups (I had to make some other changes somewhere else, because my membergroups were a little bit messed up).

This is for one member :
Array
        (
            [0] => Array
                (
                    [group_id] => 35
                    [group_color] => #94abea
                    [group] => Pro Member
                    [stars] => 1#pro_member.png
                    [stars_img] => *
                )

            [1] => Array
                (
                    [group_id] => 36
                    [group_color] =>
                    [group] => Featured_1
                    [stars] => 1#featured_1
                    [stars_img] => *
                )

        )


And this is for one other :
Array
        (
            [0] => Array
                (
                    [group_id] => 35
                    [group_color] => #94abea
                    [group] => Pro Member
                    [stars] => 1#pro_member.png
                    [stars_img] => *
                )

            [1] => Array
                (
                    [group_id] => 36
                    [group_color] =>
                    [group] => Featured_1
                    [stars] => 1#featured_1
                    [stars_img] => *
                )

            [2] =>
            [3] =>
            [4] =>
            [5] =>
            [6] =>
            [7] =>
            [8] =>
            [9] => Array
                (
                    [group_id] => 35
                    [group_color] => #94abea
                    [group] => Pro Member
                    [stars] => 1#pro_member.png
                    [stars_img] => *
                )

        )


As you can see, I get one more field for each member, with irrelevant informations in them...and I don't really know what to do with that...
If you have some ideas, I'll be glad !