Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: shaythong8 on August 11, 2009, 02:38:54 AM

Title: Guests Only Block
Post by: shaythong8 on August 11, 2009, 02:38:54 AM
Hi I want to manually create a block on my theme but I just don't know how to make it guest only. On MyBB I was able to use "<if $GLOBALS['mybb']->user['usergroup'] == 1 then></if>" with a plugin for it. Is there a similar syntax for SMF?
Title: Re: Guests Only Block
Post by: JBlaze on August 11, 2009, 02:43:34 AM
$context['user']['is_guest']

OR

$user_info['is_guest']
Title: Re: Guests Only Block
Post by: shaythong8 on August 11, 2009, 03:53:18 AM
Thank you JBlaze. The guest block is now working 100%. :D
Title: Re: Guests Only Block
Post by: JBlaze on August 11, 2009, 05:26:24 PM
I'll go ahead and mark this solved then ;D
Title: Re: Guests Only Block
Post by: shaythong8 on August 25, 2009, 03:52:30 AM
How do I make the code to display on all pages except the registration page? Is it possible?
Title: Re: Guests Only Block
Post by: Arantor on August 25, 2009, 08:41:08 AM
Where is the code and what have you used exactly?
Title: Re: Guests Only Block
Post by: shaythong8 on September 03, 2009, 05:02:12 AM
This is what I'm using in the header of index.template.php:

if ( $context['user']['is_guest'] ) {
echo '<div class="categoryframe tborder" style="margin: 0 auto;">
<h3 class="catbg headerpadding">Welcome</h3>
<table cellspacing="1" class="bordercolor boardsframe">
   <tr>
      <td class="windowbg" style="padding: 5px;">
Join the community today and you will be able to post and have your own profile!
      </td>
   </tr>
</table></div>';
}
Title: Re: Guests Only Block
Post by: Arantor on September 03, 2009, 07:09:50 AM
Change the first line from:

if ( $context['user']['is_guest'] ) {

To:
if ( $context['user']['is_guest'] && (empty($context['current_action']) || $context['current_action'] != 'register' ) ) {

This says if we're a guest, and (we have no current page as we're on the front page, or we're on a subpage that isn't register), display the block.
Title: Re: Guests Only Block
Post by: shaythong8 on September 03, 2009, 03:37:38 PM
Thank you it works. I'm not exactly sure what you mean by "we have no current page as we're on the front page".
Title: Re: Guests Only Block
Post by: Arantor on September 03, 2009, 04:07:19 PM
It works by examining the current action. If you're on the main page, there is no current action because the current page is just 'index.php', not 'index.php?action=something', thus in my offhand way 'we have no current page'.

That's what the check is doing - the page didn't specify a current action.
Title: Re: Guests Only Block
Post by: shaythong8 on September 03, 2009, 04:16:48 PM
So that basically means that the code makes it so it displays on the index page or does this just set it so that it makes the next part of the code hide the block on the registration page?
Title: Re: Guests Only Block
Post by: Arantor on September 03, 2009, 04:36:52 PM
It doesn't set anything.

The first check is whether the user is a guest, if false, it just skips over the entire block.

Assuming the user is a guest, we then look at the condition.

The situation is thus: the user is either on: 1) the registration page, 2) another page where action=something, or 3) the front page.

So if the user is on the front page (no action has been defined because the URL is simply index.php) or they're on another page (index.php?action=something), we might be displaying the block. We just need to check that the page they're on isn't the registration page.

Here's the relevant code.
if ( $context['user']['is_guest'] && (empty($context['current_action']) || $context['current_action'] != 'register' ) ) {

To write that line of code out in full English:
If the user is a guest, and (either the user is on index.php, or they're on index.php?action=something but something isn't equal to 'register') do this bunch of code