News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

If User Global Moderator Echo??

Started by virtualxtc, April 06, 2008, 02:22:23 AM

Previous topic - Next topic

virtualxtc

The following code im using for admin only options.
<?php
global $context;
if(
$context['user']['is_admin'])
{
echo 
'blah blah blah';
} else {
echo 
'blah blah blah';
}
?>


I would like to have the same sort of thing for the global moderators.
Is there something a can add so it will show up for global mods as well as admins.

ive tried...
is_mod
is_moderator
is_global moderator

Can anyone help me out?

Charles Hill

#1

<?php
$allowed_groups 
= array(12);
$can_see FALSE;
foreach (
$allowed_groups as $allowed)
if (in_array($allowed$user_info['groups'])) {
$can_see TRUE;
break;
}
if ($can_see) {

        }
?>



Where the numbers in the allowed_groups array are the IDs of the membergroups you want to allow to see whatever is within the brackets of the if can_see statement.

virtualxtc

Thanks, that works great!

One thing,
Its working on other pages but not the forum page.

Any ideas?

ccbtimewiz


Charles Hill

Whatever function you put this into, it must call $user_info as a global.

virtualxtc

Quote from: ccbtimewiz on April 06, 2008, 07:52:11 PM
What are you trying to do? o.O

Basicly I have a forum embedded within a header and footer. On the header is some admin options which i want to be visible to different membergroups of the forum.

The suggested code works fine on the pages without the embedded forum but not on the forum page.

Quote from: Charles Hill on April 06, 2008, 10:36:37 PM
Whatever function you put this into, it must call $user_info as a global.
How exactly would you do it?
I tired adding this into the suggested code without success.
global $user_info;

Any ideas / solutions?

ccbtimewiz

#6
Try this out.

<?php
global $context$txt$user_info;
$allowed_groups = array(12);
foreach (
$allowed_groups as $allowed)
if (
in_array($allowed$user_info['groups'])) {
     echo 
'If the user is in these groups, echo this stuff.'; }
elseif (!
in_array($allowed$user_info['groups'])) {
     echo 
'Else if the user isn't a part of these groupslet's show them this instead.'; }
?>


And for pages ON the forum, do this:

Find:
Code (any template) Select
<?php

Break two lines, add after:
Code (any template) Select
function show_to_mods(); {
global $context, $txt, $user_info;
$allowed_groups = array(1, 2);
foreach ($allowed_groups as $allowed)
if (in_array($allowed, $user_info['groups'])) {
     echo 'If the user is in these groups, echo this stuff.'; }
elseif (!in_array($allowed, $user_info['groups'])) {
     echo 'Else if the user isn't a part of these groups, let's show them this instead.'; }
}


Then add this where you want the code to function:

echo show_to_mods();

Charles Hill

Using as few globals as possible is always a good idea... I don't think you need to call $txt or $context here.

ccbtimewiz

I called those down because most likely the information that he was going to put had to do with either $txt or $context, seeing how those two variables are the most common in SMF.

virtualxtc

#9
Its working but there are still some issues.

1. If the user isn't in that specified group the message that is displayed is getting displayed twice.

2. This message is being displayed for both allowed member groups and not allowed.
Quoteecho 'Else if the user isn't a part of these groups, let's show them this instead.';

ccbtimewiz

Note that $user_info looks at BOTH primary group and additional groups.

My only solution would be to use what I sad before, but with this alteration:

Find:
Code (any template) Select
<?php

Replace with:
Code (any template) Select
<?php

function show_to_mods(); {
global 
$context$txt$user_info;
$allowed_groups = array(12);
foreach (
$allowed_groups as $allowed)
if (
in_array($allowed$user_info['groups'])) {
     echo 
'If the user is in these groups, echo this stuff.'; }
else {
     echo 
'Else if the user isn't a part of these groupslet's show them this instead.'; }
}

metallica48423

if you're not using $context or $txt, theres no sense in globaling them.

Justin O'Leary
Ex-Project Manager
Ex-Lead Support Specialist

QuoteMicrosoft wants us to "Imagine life without walls"...
I say, "If there are no walls, who needs Windows?"


Useful Links:
Online Manual!
How to Help us Help you
Search
Settings Repair Tool

ccbtimewiz

Quote from: metallica48423 on April 08, 2008, 03:05:31 PM
if you're not using $context or $txt, theres no sense in globaling them.

Not in the code I wrote, no. However...

Quote from: virtualxtc on April 06, 2008, 02:22:23 AM
The following code im using for admin only options.
<?php
global $context;
if(
$context['user']['is_admin'])
{
echo 
'blah blah blah';
} else {
echo 
'blah blah blah';
}
?>


I would like to have the same sort of thing for the global moderators.
Is there something a can add so it will show up for global mods as well as admins.

ive tried...
is_mod
is_moderator
is_global moderator

Can anyone help me out?

It appears as if his own custom code will in fact, use these variables. So I called them down.

metallica48423

if his code will use them, then yes he will want to global it, provided his code is in a different scope than where $context or $txt is defined.

:)
Justin O'Leary
Ex-Project Manager
Ex-Lead Support Specialist

QuoteMicrosoft wants us to "Imagine life without walls"...
I say, "If there are no walls, who needs Windows?"


Useful Links:
Online Manual!
How to Help us Help you
Search
Settings Repair Tool

virtualxtc

thanks for all your help!
Appreciate it!

franklinrony

Quote from: ccbtimewiz on April 07, 2008, 12:18:49 PM
Try this out.

<?php
global $context$txt$user_info;
$allowed_groups = array(12);
foreach (
$allowed_groups as $allowed)
if (
in_array($allowed$user_info['groups'])) {
     echo 
'If the user is in these groups, echo this stuff.'; }
elseif (!
in_array($allowed$user_info['groups'])) {
     echo 
'Else if the user isn't a part of these groupslet's show them this instead.'; }
?>


And for pages ON the forum, do this:

Find:
Code (any template) Select
<?php

Break two lines, add after:
Code (any template) Select
function show_to_mods(); {
global $context, $txt, $user_info;
$allowed_groups = array(1, 2);
foreach ($allowed_groups as $allowed)
if (in_array($allowed, $user_info['groups'])) {
     echo 'If the user is in these groups, echo this stuff.'; }
elseif (!in_array($allowed, $user_info['groups'])) {
     echo 'Else if the user isn't a part of these groups, let's show them this instead.'; }
}


Then add this where you want the code to function:

echo show_to_mods();
thanks work fine my function :)
http://twitter.com/franklinrony

Entra y aprende a ser un webmaster
www.sv-blog.com
--Si pides ayuda al menos agradece el soporte recibido----

ccbtimewiz


metallica48423

virtualxtc, did that work for you?
Justin O'Leary
Ex-Project Manager
Ex-Lead Support Specialist

QuoteMicrosoft wants us to "Imagine life without walls"...
I say, "If there are no walls, who needs Windows?"


Useful Links:
Online Manual!
How to Help us Help you
Search
Settings Repair Tool

virtualxtc

#18
well it did to an extent...

It shows what i want only for the allowed membergroups which is awesome, thanks!

But im having issues with the "elseif"

Anything i put in there is applying twice to the not allowed membergroups and also in the allowed membergroups.

Do you know what I mean?

metallica48423

#19
so both the if and the elseif are true?

try:


<?php
global $context$txt$user_info;

$allowed_groups = array(12);

foreach (
$allowed_groups as $allowed)
{
     if (
in_array($allowed$user_info['groups'])) 
     {
          echo 
'If the user is in these groups, echo this stuff.'
          break;
     }
     else 
          echo 
'Else if the user isn\'t a part of these groups, let\'s show them this instead.';
}
?>


Its being evaluated twice because theres two checks, one on each group.  The break; should stop it if it evaluates to true even once.  Theres a better way to do this though, as i believe you could pass the array directly ie:


<?php
global $context$txt$user_info;

if (
in_array(array(12), $user_info['groups'])) 
     echo 
'If the user is in these groups, echo this stuff.'
 else 
     echo 
'Else if the user isn\'t a part of these groups, let\'s show them this instead.';

?>



This way feeds the array straight into the function so that it checks them both at once, rather than cycling through them.  This is likely faster.

You don't need to do an elseif unless the second if requires a seperate check altogether, because otherwise it is inferred that the original statement was false, by the else.

either of those should work, though the first would still pass unfavorable double results if the first check or both fail.
Justin O'Leary
Ex-Project Manager
Ex-Lead Support Specialist

QuoteMicrosoft wants us to "Imagine life without walls"...
I say, "If there are no walls, who needs Windows?"


Useful Links:
Online Manual!
How to Help us Help you
Search
Settings Repair Tool

Advertisement: