News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Show link only if user belong to a specific group

Started by Cmely, April 18, 2014, 03:42:37 PM

Previous topic - Next topic

Cmely

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 ?

Kays

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>';

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Cmely

Quote from: Kays on April 19, 2014, 02:35:10 PM
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 ...

Kays

Quote
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.';


If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Cmely

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] => *

Kays

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.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Cmely

Thanks to your help, it's much better but I still have some bugs. If you have a look on the memberlist page, 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 !


Advertisement: