Advertisement:

Show link only if user belong to a specific group

Aloittaja Cmely, huhtikuu 18, 2014, 03:42:37 IP

« edellinen - seuraava »

Cmely

Hi,

On my memberlist (here), 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 :)

Sir Osis of Liver

When in Emor, do as the Snamors.
                              - D. Lister

Cmely

Thank you for your answer Krash :)

What the ['0'] is for ?

Cmely

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

maximus23


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

:)

Pas de support par PM ou Courrier...Veuillez utiliser le forum pour vous avoir une réponse rapide à votre demande d'aide. Merci.
Amitiés et à Bientôt...
No support by PM or Mail...You will get better and faster responses in the support forums. Thank you.
Have a nice day...

Cmely

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, 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'.


maximus23


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


:)
Pas de support par PM ou Courrier...Veuillez utiliser le forum pour vous avoir une réponse rapide à votre demande d'aide. Merci.
Amitiés et à Bientôt...
No support by PM or Mail...You will get better and faster responses in the support forums. Thank you.
Have a nice day...

Dzonny

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.

Cmely

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...."

Cmely

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

margarett

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.
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

LainaaOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Cmely

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

margarett

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...
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

LainaaOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair


margarett

In which file? Memberlist.template.php?

Can you point me your list of MODs, please?
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

LainaaOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair


Cmely

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)

Kays

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. ;)


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

margarett

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 ;)
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

LainaaOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Cmely

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 ?

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

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 ...

Kays

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


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: