0

Joomla 2.5 Is there a way to add a specific css class to the body which is specific to a logged in user's group.

For example I have four user groups, Individual Premium, Individual Standard, Corporate Premium and Corporate Standard. I want to have something like:

<body class="individual-standard">

or

<body class="corporate-standard">

This is the current body tag:

<body id="ff-<?php echo $fontfamily; ?>" class="<?php echo $fontstyle; ?> <?php echo $pstyle; ?> <?php echo $bgstyle; ?> <?php echo $pageclass; ?> iehandle">

I have tried putting this in the head:

 $user =& JFactory::getUser();
    $pageclass ="";
    if(array_key_exists('individual premium',$user->groups)){
    $pageclass .="individual-premium";
    }
    if(array_key_exists('corporate premium',$user->groups)){
    $pageclass .="corporate-premium";
    }
     if(array_key_exists('corporate standard',$user->groups)){
    $pageclass .="corporate-standard";
    }
     if(array_key_exists('individual standard',$user->groups)){
    $pageclass .="individual-standard";
    }

And this is the body tag:

<body id="ff-<?php echo $fontfamily; ?>" class="<?php echo $fontstyle; ?>  <?php echo $pstyle; ?> <?php echo $bgstyle; ?> <?php echo $pageclass; ?> iehandle">
user1670411
  • 77
  • 2
  • 8

2 Answers2

1

You first get the user groups and than set the class as per below code-

In index.php of template folder

$user =& JFactory::getUser();
    $pageclass ="";
    if(array_key_exists('individual premium',$user->groups)){
    $pageclass .="individual-premium";
    }
    if(array_key_exists('corporate premium',$user->groups)){
    $pageclass .=" corporate-premium";
    }
     if(array_key_exists('corporate standard',$user->groups)){
    $pageclass .=" corporate-standard";
    }
     if(array_key_exists('individual standard',$user->groups)){
    $pageclass .=" individual-standard";
    }

       <body id="ff-<?php echo $fontfamily; ?>" class="<?php echo $fontstyle; ?>  <?php echo $pstyle; ?> <?php echo $bgstyle; ?> <?php echo $pageclass; ?> iehandle">

for example if a user in "corporate premium" and "corporate standard" group page class will be- corporate-premium corporate-standard

Irfan
  • 7,029
  • 3
  • 42
  • 57
  • Thanks @Irfan. I have just updated my question. I needed to add two extra usergroups. I tried your original code, but I was unsure of how to work > into my current body tag. I keep getting something like this: – user1670411 Oct 21 '12 at 23:22
  • @user1670411:I have update the code.If it does not but than, would be great if you can post your full code,How did you apply my code in your? – Irfan Oct 22 '12 at 06:52
  • Sorry, but that is still not working. I tried the code exactly as you have it. – user1670411 Oct 23 '12 at 05:12
  • @user1670411:ok,Could you plese give me the output of $groups=& JFactory::getUser()->groups;print_r($groups) And also what is the output after update; – Irfan Oct 23 '12 at 06:44
  • sorry I am limited in my PHP knowledge. How do I give you the output? – user1670411 Oct 24 '12 at 02:23
  • @Irfan please read: https://stackoverflow.com/a/63914758/2943403 – mickmackusa Oct 15 '20 at 03:39
0

Thinking to "hide" fields, where the "lower" User Groups will hide more and the "higher" will hide less (the highest nothing), it would be better to have it "exclusive" in a pyramidal way, something like:

<?php $user =& JFactory::getUser();
    $pageclass ="";
    if(array_key_exists('Reg-D group ID',$user->groups)){
    $pageclass .="reg-d";
    } else if(array_key_exists('Reg-C group ID',$user->groups)){
         $pageclass .=" reg-c";
         } else if(array_key_exists('Reg-B group ID',$user->groups)){
              $pageclass .=" reg-b";
              } else if(array_key_exists('Reg-A group ID',$user->groups)){
                   $pageclass .=" reg-a";
                   }
?>

Where $user->groups variable returns the User Group ID, here an alternative

I'm not sure about the code, What do you think about ?

joomleb
  • 11
  • 2
  • A tip on coding standards: `else if` as two words is a violation of the PSR-12 standards -- you should be writing `elseif` as one word. Since your condition block is using `elseif` statements, there will only be one string appended; for this reason, I recommend a lookup array that will allow for a much more concise translation of data -- this will be much easier to read and maintain too. – mickmackusa Oct 15 '20 at 03:26
  • So the lookup & translation can look like this: https://3v4l.org/092H6 Also, I'd like to mention another PSR-12 violation: https://stackoverflow.com/a/63914758/2943403 – mickmackusa Oct 15 '20 at 03:38