Change theme background for boards accessible to particular member group(s) only

Started by BeautifulBoy, February 14, 2015, 01:21:56 AM

Previous topic - Next topic

BeautifulBoy

SMF-version: 2.0.9
Theme: Core-based


Hello smart SMF people,

I am going to need quite some text to explain what I have been trying to achieve, with my limited knowledge of PHP and the structure of SMF, during last week.

As the topic subject says, I want to find an elegant way to change the background of a SMF theme on certain forum boards which are only accessible to certain member groups.

I know that SMF has the built-in option to apply a different theme to particular boards. Drawback of this option is that it overrides the preference of a user for a particular theme.

What I would prefer is a "smart theme" that changes its background if a board is only accessible to special member group(s).

Here are visual examples of what I mean to achieve:

1. A standard board, accessible to all registered forum members:



2. A board only accessible to Official Club Members (OCM), made visible by the background image:



I "feel" that it must be possible to include this within a theme with a simple and elegant php & css solution, and I almost have it working (Ahum  :P ).

Here is what I have come up with so far:

In the Core-based SMF theme I added the following lines, after the last css-file call:


// EDIT OCM-boards require an additional stylesheet.
   if BOOLEAN
      echo '
   <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/ocm.css" />';


with only the following text in the ocm.css-file

body
{
   background: url(../images/ocm-lbg-48x96.png);
}



This actually works if I replace BOOLEAN with something that is true, like 1=1:


// EDIT OCM-boards require an additional stylesheet.
   if "1" == "1"
      echo '
   <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/ocm.css" />';


This code makes the background of the forum change into picture no. 2

But now it will appear like that on all boards, regardless of the board-specific member-group permissions.

So the BOOLEAN expression in the if-then construction should become, expressed in layman's words:


IF user is permitted to access the current board, because he is member of member-group OCM
THEN change forum background



As stated in the beginning, my knowledge of PHP and the structure of SMF is limited, but that hasn't stopped me from undertaking some trial-and-error experiments... ;-)

In the SMF-forum-database, the permitted member_groups of a board, seems to be an array of numbers, where the numbers represent a member_group. In my case, the OCM member group has the number 9.
So I thought, that this might work:


// EDIT OCM-boards require an additional stylesheet.
   if (in_array(9, $board_info['member_groups'], false))
      echo '
   <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/ocm.css" />';



*** I can hear the loud laughter of the experienced SMF developers and MODers ***  ;D

Of course this didn't work... ;-)

Adding $board_info to the global variables didn't help either.

So then I dug a little deeper into the SMF-sources, and tried these (more loud laughter expected ;) ):


// EDIT OCM-boards require an additional stylesheet.
   if ($board_info['use_permissions'])
      echo '
   <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/ocm.css" />';


and


// EDIT OCM-boards require an additional stylesheet.
   if ($context['use_permissions'])
      echo '
   <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/ocm.css" />';


OK, you can stop laughing now...  ;D

That didn't have the effect I hoped for either, of course, and at that point I realised that I did not really know what i was doing, and that it must be more difficult than that...  :P

So now I am throwing down the gauntlet...  8)

And I hope that one of you, the SMF-coding wizards, can tell me if it is at all possible to do what I have in mind, and if so, how to do it...  ;)

Thanks for reading this far down anyway...  :)


Perhaps a final explanation for the reason why I would like to implement this.
Firstly, it just seems to be an elegant solution.
Secondly, if themes can automatically change their backgrounds to the board characteristics, it is possible for members to hold on to their favorite theme AND always be made aware that they are visiting a board with special permissions.

Link to current forum [nofollow]

Sir Osis of Liver

Quote from: BeautifulBoy on February 14, 2015, 01:21:56 AM
change the background of a SMF theme on certain forum boards

If that's all you need, why not just say so?  You might have gotten a quicker response.

index.template.php -



/// Output any remaining HTML headers. (from mods, maybe?)
echo $context['html_headers'];

if ($context['current_board'] == 15)
$bgcolor = 'red';

echo '
</head>
<body style="background-color: '. $bgcolor .' ">';
}

function template_body_above()



Just add a color (or background url) for each board.

Elegant it's not.

Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Steve

DO NOT pm me for support!

BeautifulBoy


Thanks for the "$context['current_board']" suggestion Krash, that got me a little further!

I have used that to create this working solution in:

index.template.php -

after

   // RTL languages require an additional stylesheet.
   if ($context['right_to_left'])
      echo '
   <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/rtl.css" />';

insert

// EDIT OCM-boards require an additional stylesheet.

  $ocmboards = array(6, 7, 8, 9);

  if (in_array($context['current_board'], $ocmboards, false))

      echo '
   <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/ocm.css" />';

// END of EDIT OCM-boards require an additional stylesheet.

before

   // Here comes the JavaScript bits!



still with only the following text in the ocm.css-file

body
{
   background: url(../images/ocm-lbg-48x96.png);
}



Although this solution works quite elegantly already, it has the drawback that the array of accessible boards will have to be updated manually each time that board permissions are changed.

Is there really no way to extract the permitted membergroup(s) for the "current board" from the SMF database, from within the template?


BeautifulBoy

I think that I have got it!! :)

I have made the following change in:

index.template.php (of my Core-based template)

after

   // RTL languages require an additional stylesheet.
   if ($context['right_to_left'])
      echo '
   <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/rtl.css" />';

insert

// EDIT OCM-boards require an additional stylesheet.
   if ((in_array(9, $board_info['groups'], false)) AND !(in_array(0, $board_info['groups'], false)))
      echo '
   <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/ocm.css" />';
// END of EDIT OCM-boards require an additional stylesheet.

before

   // Here comes the JavaScript bits!



with only the following text in the ocm.css-file

body
{
   background: url(../images/ocm-lbg-48x96.png);
}


This code checks if membergroup 9 is among the membergroups that are allowed to see the current board, AND it checks if membergroup 0 (the "simple" registered users) is NOT among the membergroups that are allowed to see the current board. In that case, it must be a board with limited access permissions, so the background can be changed, by adding the ocm.css file to list of style sheets.

Thanks for the help, and hopefully it will be useful to other SMF-fanatics...  ;)

pittu

How to modify this code so I can REPLACE the original index.css file with my css file BASED on certain forum?



// EDIT OCM-boards require an additional stylesheet.

  $ocmboards = array(3, 7);

  if (in_array($context['current_board'], $ocmboards, false))

      echo '
   <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/ocm.css" />';

// END of EDIT OCM-boards require an additional stylesheet.

pittu

I came up with this... working:

// EDIT OCM-boards require an additional stylesheet.

  $ocmboards = array(3, 7);

  if (in_array($context['current_board'], $ocmboards, false))

      echo '
   <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/white.css" />';

else

echo '
   <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/index', $context['theme_variant'], '.css?fin20" />';

// END of EDIT OCM-boards require an additional stylesheet.

BeautifulBoy

Nice modification pittu!  :)

Did you change a lot in that alternative css-file, to alter the appearance of your theme?


BeautifulBoy

Quote from: BeautifulBoy on February 15, 2015, 03:31:39 AM
I think that I have got it!! :)

I have made the following change in:

index.template.php (of my Core-based template)

after

   // RTL languages require an additional stylesheet.
   if ($context['right_to_left'])
      echo '
   <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/rtl.css" />';

insert

// EDIT OCM-boards require an additional stylesheet.
   if ((in_array(9, $board_info['groups'], false)) AND !(in_array(0, $board_info['groups'], false)))
      echo '
   <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/ocm.css" />';
// END of EDIT OCM-boards require an additional stylesheet.

before

   // Here comes the JavaScript bits!



with only the following text in the ocm.css-file

body
{
   background: url(../images/ocm-lbg-48x96.png);
}


This code checks if membergroup 9 is among the membergroups that are allowed to see the current board, AND it checks if membergroup 0 (the "simple" registered users) is NOT among the membergroups that are allowed to see the current board. In that case, it must be a board with limited access permissions, so the background can be changed, by adding the ocm.css file to list of style sheets.

Thanks for the help, and hopefully it will be useful to other SMF-fanatics...  ;)

While implementing this solution at another forum, I noticed that I forgot to mention one other adjustment that is necessary for it to work:

In the global variables list of the function template_html_above()

function template_html_above()
{
   global $context, $settings, $options, $scripturl, $txt, $modSettings;


Add $board_info to the list of global variables:

function template_html_above()
{
   global $context, $board_info, $settings, $options, $scripturl, $txt, $modSettings;

Advertisement: