PDA

View Full Version : [How-to] Add a multiselect field in vBulletin options


Coroner
23 Sep 2008, 15:03
I wrote this article 'cause I didn't found anything else but it was needed for mod I wrote.

Before I start - will have to say this is an example for a forumchooser.

In our product under options, create an option like this:
<setting varname="your_setting_varname" displayorder="1">
<datatype>free</datatype>
<optioncode>multiselect:eval
$options = construct_forum_chooser_options(1);</optioncode>
<defaultvalue>0</defaultvalue>
</setting>
I'll use: multiselect:eval.

Ok, we finished our first part and need 2 new plugins.

The first plugin we used is admin_options_print.
<plugin active="1" product="yourproduct">
<title>expand options for multiselect</title>
<hookname>admin_options_print</hookname>
<phpcode><![CDATA[if (preg_match ('#^(multiselect):(eval)(\r\n|\n|\r)(.*)$#siU', $setting['optioncode'], $matches))
{
$options = null;
eval ($setting['optiondata']);
// this is for multiselect options
$title = $description;
$array = construct_forum_chooser_options (0);
$selected = explode (',', $setting['value']);
$name .= "[]";
$htmlise = 0;
$size = 10;
$multiple = true;
global $vbulletin;
$uniqueid = fetch_uniqueid_counter ();
$select = "<div id=\"ctrl_$name\"><select name=\"$name\" id=\"sel_{$name}_$uniqueid\" tabindex=\"1\" class=\"bginput\"" . iif($size, " size=\"$size\"") . iif($multiple, ' multiple="multiple"') . iif($vbulletin->debug, " title=\"name=&quot;$name&quot;\"") . ">\n";
$select .= construct_select_options ($array, $selected, $htmlise);
$select .= "</select></div>\n";
print_label_row ($title, $select, '', 'top', $name);
$handled = true;
}]]></phpcode>
</plugin>
See the code above and take a look at this three lines:
$array = construct_forum_chooser_options (0);
$selected = explode (',', $setting['value']);
$name .= "[]";
The 1st line are our options array, filled with information coming from the function "construct_forum_chooser_options(0)". If you wanna have your own, create an array with your options.
2nd line: our value (saved later in the options will implode by a comma like: 5,8,11,26,.. we need to explode this value to get the selected values back.
3rd line: this is needed for multiselect

The next plugin is called: admin_options_processing.
<plugin active="1" product="yourproduct">
<title>save our multiselect values</title>
<hookname>admin_options_processing</hookname>
<phpcode><![CDATA[
if (preg_match ('/multiselect/i', $oldsetting['optioncode']))
{
if (is_array ($settings["$oldsetting[varname]"])) $settings["$oldsetting[varname]"] = implode (',', $settings["$oldsetting[varname]"]);
}]]></phpcode>
</plugin>
This plugin will implode our selected values with a comma.

Regards

Stoebi
10 Oct 2008, 18:38
This is great. Thank you very much for sharing :)


Regards,

Stoebi

Blackhat
28 Oct 2008, 15:52
can this be used in the user cp > options to make a "forum chooser" and which hook should I use?

ReCom
24 Nov 2008, 10:31
A setting with datatype "bitfield" can create a group of checkboxes (multiselection) in vBulletin Options.

Coroner
18 Jul 2009, 10:14
I didn't mean a forumchooser (Selectrow) or a multiple checkbox.

See image for the "multiselect field" what I mean.

Simon Lloyd
09 Oct 2013, 02:07
The chooser code is great but i am unsure how to call it in a plug in, normally i'd have the user enter the forum id's and use something like $allowedforums = explode(",", $vbulletin->options['sens_forumid']);
if (in_array($threadinfo['forumid'], $allowedforums))
How do i use the information of the selected forums creating a multiselect like this?

ozzy47
09 Oct 2013, 02:13
I believe what you are after is something this.

if(in_array($GLOBALS['forumid'], explode(',', $vbulletin->options['MY_OPTION_NAME'])))
{
Your Code Here
}

Simon Lloyd
09 Oct 2013, 02:14
That works with the above?

ozzy47
09 Oct 2013, 02:17
It should, the code I provided, works in my mods that I have a forum chooser in options, via a multi select field.

Simon Lloyd
09 Oct 2013, 02:23
Hmmm, i tried a variation of the code i provided and it didnt work, the code you provided gives an error Parse error: syntax error, unexpected T_IF in /home/thecodec/public_html/forumz/showthread.php(112) : eval()'d code on line 31


--------------- Added 1381281909 at 1381281909 ---------------

Errrr.....ummmm, guess who added an extra F to his own IF function!!!

My apologies!

ozzy47
09 Oct 2013, 02:26
Ok here is how I do a forum chooser.

First create the option, and in the field, Option Code add the following:
"; $forums = array(0 => $vbphrase['none']);
if (is_array($vbulletin->forumcache))
{
foreach($vbulletin->forumcache AS $forumid => $forum)
{
$forums["$forum[forumid]"] = construct_depth_mark($forum['depth'], '--') . ' ' . $forum['title'];
}
}
$right = "<select name=\"setting[$setting[varname]][]\" id=\"".$setting[varname]."\" tabindex=\"1\" class=\"bginput\" size=\"20\" style=\"width:300px;\" multiple=\"multiple\">\n";
$right .= construct_select_options($forums, explode(',',$vbulletin->options[$setting[varname]]), false);
$right .= "</select>\n"; $null = "

Then you will need to do the plugin for admin_options_processing
$setforums = 'VARNAME FOR YOUR SETTING';
if ($oldsetting['varname'] == $setforums)
{
if (in_array(0, $settings[$setforums]))
{
$settings[$setforums] = '0';
}
else
{
$settings[$setforums] = implode(',',$settings[$setforums]);
}
}
$setforums = '';

Then you can use the code I provided earlier.

Simon Lloyd
09 Oct 2013, 02:35
Lol, thanks, did you see my note? :)

ozzy47
09 Oct 2013, 02:37
Not until well after I had replied. :)

Easy5s.net
20 Jun 2015, 12:45
Ok here is how I do a forum chooser.

First create the option, and in the field, Option Code add the following:
"; $forums = array(0 => $vbphrase['none']);
if (is_array($vbulletin->forumcache))
{
foreach($vbulletin->forumcache AS $forumid => $forum)
{
$forums["$forum[forumid]"] = construct_depth_mark($forum['depth'], '--') . ' ' . $forum['title'];
}
}
$right = "<select name=\"setting[$setting[varname]][]\" id=\"".$setting[varname]."\" tabindex=\"1\" class=\"bginput\" size=\"20\" style=\"width:300px;\" multiple=\"multiple\">\n";
$right .= construct_select_options($forums, explode(',',$vbulletin->options[$setting[varname]]), false);
$right .= "</select>\n"; $null = "

Then you will need to do the plugin for admin_options_processing
$setforums = 'VARNAME FOR YOUR SETTING';
if ($oldsetting['varname'] == $setforums)
{
if (in_array(0, $settings[$setforums]))
{
$settings[$setforums] = '0';
}
else
{
$settings[$setforums] = implode(',',$settings[$setforums]);
}
}
$setforums = '';

Then you can use the code I provided earlier.




$setforums = 'check_forums';
if ($oldsetting['varname'] == $setforums)
{
if (in_array(0, $settings[$setforums]))
{
$settings[$setforums] = '0';
}
else
{
$settings[$setforums] = implode(',',$settings[$setforums]);
}
}
$setforums = '';


i user hook newthread_start with code

if (in_array($foruminfo['forumid'], explode(',', $vbulletin->options['check_forums'])))
{
if ($vbulletin->userinfo['dola'] < $vbulletin->options['newthread_dola'])
{
eval(standard_error(fetch_error('no_dola')));
}
}

but not work :confused: