Jump to:

3431 Posts in 1025 Topics by 864 members

Template Questions

SilverStripe Forums » Template Questions » if CurrentMember = [SOLVED]

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 5109 Views
  • socks
    Avatar
    Community Member
    190 Posts

    if CurrentMember = [SOLVED] Link to this post

    I'm trying to do something I thought would be super simple, but I can't figure it out.

    I'd like to show certain text in a template if the CurrentMember is a specific Security Group (ie. Franchisees).

    Tried:
    <% if CurrentMember = Franchisees %>,
    <% if CurrentMember.inGroup(2) %>
    and writing a simple function

    Teach me your ways o wise ones

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: if CurrentMember = [SOLVED] Link to this post

    Im not shure if this is the best approach, but I would do the following.

    Create a method in Page_Controller:

    function MemberGroupText(){
    $member = Member::currentUser();
          
    $sqlQuery = new SQLQuery();
          
    $sqlQuery->select = array('GroupID');
    $sqlQuery->from = array("Group_Members");
    $sqlQuery->where = array("MemberID =".$member->ID);
          
    $rawSQL = $sqlQuery->sql();
    $result = $sqlQuery->execute();
    $groupid = $result->first();
       
    return _t('MEMBERGROUPTEXT.'.$groupid['GroupID'], 'Default text');
    }

    create a language file in mysite/lang/ like en_US.php or nl_NL.php

    $lang['en_US']['MEMBERGROUPTEXT']['2'] = 'Welcometext group 2';
    $lang['en_US']['MEMBERGROUPTEXT']['3'] = 'Welcometext group 3';

    In your template simply call the method when logged in with :

    <% if CurrentMember %>
    $MemberGroupText
    <% end_if %>

    In this way you always have a default text when a grouptext is not there and you can simply add textstrings for new groups in your language file..

  • socks
    Avatar
    Community Member
    190 Posts

    Re: if CurrentMember = [SOLVED] Link to this post

    Thanks for your time and effort.

    I'm not sure I want to go thru the trouble of having a separate file like the en_US.php just to store 1 word or an empty string.

    Allow me to elaborate,

    I'm replicating the "You're logged in as Martijn - Log Out - Profile" (as seen at the top of this page) but there is one group that isn't allowed to change their profile. I have blocked access via the CMS to that page, but I'd like to hide the "Profile" link from that security group.

    so something like:

    <% if Group2 %>
       You're logged in as $CurrentMember.FirstName -
       <a href="Security/logout/">Log Out</a> -
    <% else %>
       You're logged in as $CurrentMember.FirstName -
       <a href="Security/logout/">Log Out</a> -
       <a href="edit-details-page">Profile</a>
    <% end_if %>

       

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: if CurrentMember = [SOLVED] Link to this post

    <% control Member %>
    <% if inGroup(2) %>
    group 2
    <% else_if inGroup(3) %>
    group 3
    <% end_if %>
    <% end_control %>

    But in this way you only hide the links, you need to change the php code to disallow profile access..
    They could go to the profilepage by entering the url..

  • socks
    Avatar
    Community Member
    190 Posts

    Re: if CurrentMember = [SOLVED] Link to this post

    "you need to change the php code to disallow profile access..."
    I did this...Profile page > Access tab > Who can view this page > Only these people > I selected two groups so the 3rd group can't view the Profile page.

    I had to change to CurrentMember, Member wasn't returning anything. And the Else_if wasn't working (although I don't need it for my situation). Did a quick check in the forums and it seems that they're working on a patch to get an Else_if to work.

    so the end result is this:

    <% control CurrentMember %>
       <% if inGroup(1) %>
          message
       <% else %>
          alternate message
       <% end_if %>
    <% end_control %>

    THANK YOU!!

    5109 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.