The following works for action:
If UserRole is "X" *AND* UserRole is "Y" then print HELLO WORLD

<?php
global $user;

if ((in_array('X', $user->roles)) && (in_array('Y', $user->roles))) 
{
print "HELLO WORLD";
}
?>

but what I need is:
If UserRole is "X" *AND* AuthorRole is "Y" then then print HELLO WORLD

Any ideas?

Many Thanks,
abramo

Comments

cog.rusty’s picture

Here's something I found for getting the author, in chill35's site

http://11heavens.com/taxonomy/term/72

Make sure that a particular node is in the scope of your function, or else author is meaningless. Use $author instead of $user, and then use in_array('Y', $author->roles) in the second case.

abramo’s picture

<?php
global $user;

$node = node_load();
$author = user_load(array('uid' => $node->uid));

if ((in_array('X', $user->roles)) && (in_array('Y', $author->roles)))

{
print "HELLO WORLD";
}
?>

As this stands it does not work, but obviously I have done something wrong?

cog.rusty’s picture

node_load() needs to be told which node. Either a nid (number of variable), or an array of conditions like the ones in user_load.

abramo’s picture

with your valuable assistance I have resolved this issue !! (please see my post below).

All the best,
abramo

mike.hobo’s picture

So if the too are in different roles you want Hello world?

if ((in_array('x', $GLOBALS['user']->roles)) && (in_array('y', $user->roles)))
{
print "HELLO WORLD";
}

If this is in a node then it should work, GLOBALS being the user viewing the node and the other the author.

abramo’s picture

but I could not make it work. Yes, this snip goes in a node (via template) and if

global $user;

is included, it simply does not perform, and if it is not included it returns error:

warning: in_array() [function.in-array]: Wrong datatype for second argument in . . . 

My current setup functions properly if only the UserRole(s) are required.

Any ideas?

Thanks,
abramo

abramo’s picture

<?php
global $user;
$author = user_load(array('uid' => $node->uid));


if ((in_array('X', $GLOBALS['user']->roles)) && (in_array('Y', $author->roles)))
{

print "HELLO WORLD";
}
?>

This is a combination of what cog.rusty and mike.hobo were kind enough to contribute - I have really much appreciated their assistance !! Thanks guys !!

All the best,
abramo

cog.rusty’s picture

Good. $node is already defined if the code is inside node.tpl.php or when you are on a full node page.

abramo’s picture

To display content within a node according to User Role AND Author Role - when for example User Role has to be "X" and at the same time Author Role has to be "Y" - use the following snippet in your template for the Content Type in question:

<?php
global $user;
$author = user_load(array('uid' => $node->uid));

if ((in_array('X', $user->roles)) && (in_array('Y', $author->roles)))
{
print "TRUE";
}

elseif (in_array('A', $user->roles)) && (in_array('B', $author->roles)))
{
print "FALSE";
}
?>

Enjoy!
abramo

droople’s picture