Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

if CurrentMember = [SOLVED]


Go to End


5 Posts   12781 Views

Avatar
socks

Community Member, 191 Posts

4 October 2009 at 9:53pm

Edited: 05/10/2009 10:34am

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

Avatar
Martijn

Community Member, 271 Posts

5 October 2009 at 1:22am

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

Avatar
socks

Community Member, 191 Posts

5 October 2009 at 6:48am

Edited: 05/10/2009 6:49am

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

Avatar
Martijn

Community Member, 271 Posts

5 October 2009 at 9:50am

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

Avatar
socks

Community Member, 191 Posts

5 October 2009 at 10:33am

Edited: 05/10/2009 10:34am

"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!! :-)