MyBB Community Forums

Full Version: "Enter Usergroup IDs, seperated by a comma"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5
Hi guys,

How would I go about selecting multiple usergroup ID's by using a comma between each one, for example: "Enter the usergroup ID's you'd like to ban, separated by a comma."

I need to perform an if statement where if the user is in one of those usergroups, return true. If there not, return false.

Anyone have any idea how I'd go about this?

Thanks Smile
$list = '1,2,7';

if(check_groups($list))
{
	error('You are banned');
}

function check_groups($comma='')
{
	if(!$comma)
	{
		return true;
	}
	$comma = array_unique(array_map('intval', explode(',', $comma)));
	if(!$comma)
	{
		return false;
	}

	global $mybb;

	$usersgroups = array();
	$usersgroups[] = $mybb->user['usergroup'];

	if($mybb->user['additionalgroups'])
	{
		$additionalgroups = explode(',', $mybb->user['additionalgroups']);
		foreach((array)$additionalgroups as $ag)
		{
			$usersgroups[] = $ag;
		}
	}

	$valid = false;
	foreach((array)$usersgroups as $group)
	{
		if(in_array($group, $usersgroups))
		{
			$valid = true;
			break;
		}
	}
	return $valid;
}

Newpoints uses a function, if you are going to release it under a compatible license you may make use of it. PluginLibrary has one as well, if you are willing to require the file for the plugin to work propertly.
Best way would be to explode the string into an array then use in_array(). Of course, we have to consider additional user groups too...

$groupsToTakeActionOn = explode(',', $mybb->input['whatever']);

$usersGroups = array_merge($mybb->user['usergroups'], explode(',', $mybb->user['additionalgroups']));

$canDo = true; //set to true initially - if the users has been disallowed at all, it will be changed to false below...

foreach ($usersGroups as $usersGroup)
{
    if (in_array($usersGroup, $groupsToTakeActionOn))
    {
        $canDo = false;
    }
}

if (!$canDo)
{
    error_no_permission();
}

That SHOULD work - hopefully you can make sense of it and remember I haven't tested it at all Toungue

EDIT: beaten - the above may be a nicer solution Toungue
Cheers guys, +rep to you both Smile
Also another quick question, how would you go about finding a users username by typing only some of it in?

For example, If I were searching for the user Vernier, when I type in something like Vern it'd display all the results with that in it's username. Like the "Search by Username" does Toungue

Thanks Smile
Take a look at the JS used in the registration page. I do believe we have a build in function or class to handle that type of thing.
Just replicate what MyBB does. First you inserts your textare and put a unique ID to it:
<input type="text" class="textbox" name="username" id="username" value="" />

You include the js file:
<script type="text/javascript" src="{$settings['bburl']}/jscripts/autocomplete.js?ver=1600"></script>

And then you insert the js code:
<script type="text/javascript">
<!--
	if(use_xmlhttprequest == "1")
	{
		new autoComplete("username", "{$settings['bburl']}/xmlhttp.php?action=get_users", {valueSpan: "username"});
	}
// -->
</script>

Edit: in this case, the unique ID is "username".
Cheers for the replys

I tried this in a template:
<script type="text/javascript" src="{$settings['bburl']}/jscripts/autocomplete.js?ver=1600"></script>
<script type="text/javascript">
<!--
    if(use_xmlhttprequest == "1")
    {
        new autoComplete("username", "{$settings['bburl']}/xmlhttp.php?action=get_users", {valueSpan: "username"});
    }
// -->
</script>
<input type="text" class="textbox" name="username" id="username" value="" />

Then I used the $templates->get() function to get that template, however when I start typing an existent username in the textarea, it doesn't autocomplete.

Cheers Smile
Paste the full page code here (the ouput, source code).
Thanks for the reply, Omar.

I'm trying to implement it in one of my plugins, so just testing it out currently.

Here's a snippet:
if ($mybb->input['action'] == "addnegative") {
	$page->output_header($lang->negative_rep_header);
	$page->output_nav_tabs($sub_tabs, 'negative_rep');
	$negativeform = new Form("index.php?module=tools-easyrep&amp;action=addnegative", "post", "easyrep");
			eval("\$page = \"".$templates->get('testing_auto_completion')."\";");
		output_page($page);


Then in the template "testing_auto_completion" I have:
<script type="text/javascript" src="{$settings['bburl']}/jscripts/autocomplete.js?ver=1600"></script>
<script type="text/javascript">
<!--
    if(use_xmlhttprequest == "1")
    {
        new autoComplete("username", "{$settings['bburl']}/xmlhttp.php?action=get_users", {valueSpan: "username"});
    }
// -->
</script>
<input type="text" class="textbox" name="username" id="username" value="" />

Thanks again Smile
Pages: 1 2 3 4 5