Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Coaxs on January 18, 2009, 10:54:27 PM

Title: $context['user']['is_admin'] but what about other membergroups?
Post by: Coaxs on January 18, 2009, 10:54:27 PM
If I want some content to only be visible for a custom membergroup, how/where could I write a function for that membergroup? Or is there a set function already for that? Or where is the $context array pieced together so I can add a custom membergroup to it?

Can I haz $context['user']['is_custommembergroup'] ?
Title: Re: $context['user']['is_admin'] but what about other membergroups?
Post by: Coaxs on January 18, 2009, 11:51:40 PM
So, I'm an admin at my board obviously, and stuck the follwing into index.template.php

if (in_array(1, $user_info['groups']))
      {
         echo '
            <div>
               Some other stuff here that you want to protect.
            </div>';
      }
      else
      {
         echo 'Sorry you are not allowed to see this.';
      }

but all I saw was "Sorry you are not allowed to see this." As, I'm an admin with ID_Group = 1 , I don't know what is going wrong?
Title: Re: $context['user']['is_admin'] but what about other membergroups?
Post by: Charles Hill on January 18, 2009, 11:52:04 PM
$user_info['groups'] is an array of all the groups a user is in (group IDs not names).  Can use in_array($group_id, $user_info['groups']) to see if a user is in a specific group... $group_id in that case would be the ID of the custom group you are asking about.

edit....

if (in_array(1, $user_info['groups']))
      {
         echo '
            <div>
               Some other stuff here that you want to protect.
            </div>';
      }
      else
      {
         echo 'Sorry you are not allowed to see this.';
      }


Will work
Title: Re: $context['user']['is_admin'] but what about other membergroups?
Post by: Coaxs on January 19, 2009, 12:18:41 AM
but the fact it didn't work in index.template.php in the default template folder would indicate...?

Sorry, but it is hard for me to troubleshoot a code that we agree should work but it doesn't :(

Are there other more laborious ways to do this?
Title: Re: $context['user']['is_admin'] but what about other membergroups?
Post by: Coaxs on January 19, 2009, 12:55:08 AM
I'm using smf 1.1.2 by the way for the database.

Anyways, I did the following...


echo array_sum($user_info['groups']);


And all I got back was nothing. I don't suspect that would be normal?

Title: Re: $context['user']['is_admin'] but what about other membergroups?
Post by: Charles Hill on January 19, 2009, 01:00:46 AM
You have to add $user_info to the global thing at the top of whatever function you want to use it in.
Title: Re: $context['user']['is_admin'] but what about other membergroups?
Post by: Coaxs on January 19, 2009, 01:09:20 AM
Thank you!!! I know that is obvious for you, but that should really be added to this document page:

http://docs.simplemachines.org/index.php?topic=789.0
Title: Re: $context['user']['is_admin'] but what about other membergroups?
Post by: SimpleJoe on March 08, 2010, 11:33:37 AM
This is a follow up to coaxs' request to have the info about calling the "global" variables first when doing membergroup functions placed in the docs, I followed the info on the docs page and nothing worked until I was lucky enough to find this post. For the sake of others I hope a note about this can be added. Thank you Charles Hill and coaxs for finally getting the info that saved my day in this post!