Posts: 1,859
Threads: 113
Joined: Nov 2011
Reputation:
194
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
MyBB Support Technician
Please do not PM me for support.
MyBBatically - A quick and easy way to update your board!
Posts: 9,892
Threads: 399
Joined: Jan 2010
Reputation:
546
2012-08-13, 06:58 PM
(This post was last modified: 2012-08-13, 06:58 PM by Omar G..)
$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.
Soporte en Español
Discord at omar.gonzalez ( Omar G.#6117 ); Telegram at @omarugc ;
Posts: 15,129
Threads: 240
Joined: Jun 2009
Reputation:
703
2012-08-13, 06:58 PM
(This post was last modified: 2012-08-13, 06:59 PM by Euan T.)
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
EDIT: beaten - the above may be a nicer solution
Posts: 1,859
Threads: 113
Joined: Nov 2011
Reputation:
194
Cheers guys, +rep to you both
MyBB Support Technician
Please do not PM me for support.
MyBBatically - A quick and easy way to update your board!
Posts: 1,859
Threads: 113
Joined: Nov 2011
Reputation:
194
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
Thanks
MyBB Support Technician
Please do not PM me for support.
MyBBatically - A quick and easy way to update your board!
Posts: 15,129
Threads: 240
Joined: Jun 2009
Reputation:
703
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.
Posts: 9,892
Threads: 399
Joined: Jan 2010
Reputation:
546
2012-08-15, 10:39 PM
(This post was last modified: 2012-08-15, 10:40 PM by Omar G..)
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".
Soporte en Español
Discord at omar.gonzalez ( Omar G.#6117 ); Telegram at @omarugc ;
Posts: 1,859
Threads: 113
Joined: Nov 2011
Reputation:
194
2012-08-15, 11:02 PM
(This post was last modified: 2012-08-15, 11:02 PM by Vernier.)
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
MyBB Support Technician
Please do not PM me for support.
MyBBatically - A quick and easy way to update your board!
Posts: 9,892
Threads: 399
Joined: Jan 2010
Reputation:
546
Paste the full page code here (the ouput, source code).
Soporte en Español
Discord at omar.gonzalez ( Omar G.#6117 ); Telegram at @omarugc ;
Posts: 1,859
Threads: 113
Joined: Nov 2011
Reputation:
194
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&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
MyBB Support Technician
Please do not PM me for support.
MyBBatically - A quick and easy way to update your board!
|