MyBB Community Forums

Full Version: optionscode values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Probably this has been asked more times but for me is still very difficult to find information.

So, anyone knows a place to look for all available possibilities for optionscode parameter for forms elements. If it is a documentation with explanation on each possible value and examples, it would be fantastic.

Besides I'm looking for these ones so maybe someone can point me too to a file included in mybb that contains examples or a plugin that uses any of this form elements: a hidden form element, a multiselect forum (this one I know is on some file for the search function), a multiselect for user groups, a multiselect for users.
I start myself with the optionscode values I know.

yes/no switch
$new_setting = array(
	'name' => 'setting_yesno',
	'title' => 'yes/no switch',
	'description' => 'A yes/no switch. Radios with two options yes/no. Values 0/1',
	'optionscode' => 'yesno',
	'value' => '0',
	'disporder' => 0,
	'gid' => $my_settings_group
);
This is available too as on/off, changing the value of optionscode
'optionscode' => 'onoff',

text input
$new_setting = array(
	'name' => 'setting_text',
	'title' => 'Text input',
	'description' => 'A text input box to enter simple text options',
	'optionscode' => 'text',
	'value' => '',
	'disporder' => 0,
	'gid' => $my_settings_group
);

text area
$new_setting = array(
	'name' => 'setting_text_area',
	'title' => 'Text area',
	'description' => 'A text area to input more than one line text',
	'optionscode' => 'textarea',
	'value' => '',
	'disporder' => 0,
	'gid' => $my_settings_group
);

Custom radio buttons
$new_setting = array(
	'name' => 'setting_radio',
	'title' => 'Radio buttons',
	'description' => 'Radio buttons choice options',
	'optionscode' => 'radio \n 1=option1 \n 2=option2 \n 3=option3',
	'value' => '1',
	'disporder' => 0,
	'gid' => $my_settings_group
);

Similar to radios, select combobox
$new_setting = array(
    'name' => 'setting_select',
    'title' => 'Select combobox',
    'description' => 'Select combobox to choose an option',
    'optionscode' => 'select \n 1=option1 \n 2=option2 \n 3=option3',
    'value' => '1',
    'disporder' => 0,
    'gid' => $my_settings_group
);

I don't know how to use checkboxes. If I create one of those it acts as multiselect checkboxes, but when I save the settings only the last option is saved. If you only need one value, a simple checkbox, then you would not have the problem of multiselects and works fine.

This would be the code, but the logic is not working properly, so this need something more I don't know.
Quote:$new_setting = array(
'name' => 'setting_checkboxes',
'title' => 'Checkboxes',
'description' => 'Checkboxes to choose an option',
'optionscode' => 'checkbox \n 1=option1 \n 2=option2 \n 3=option3',
'value' => '1',
'disporder' => 0,
'gid' => $my_settings_group
);
Maybe there is a way to indicate it is multiselect or not multiselect??
Checkboxes is a known issue:
http://dev.mybb.com/issues/675

But, as mentioned in that bug report, you can use PHP to create them. An example of using PHP inside the options code can be found in the stock ACP options for timezoneoffset and searchtype.

timezoneoffset:
php
<select name=\"upsetting[{$setting['name']}]\">
<option value=\"-12\" ".($setting['value'] == -12?"selected=\"selected\"":"").">GMT -12:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, -12).")</option>
<option value=\"-11\" ".($setting['value'] == -11?"selected=\"selected\"":"").">GMT -11:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, -11).")</option>
<option value=\"-10\" ".($setting['value'] == -10?"selected=\"selected\"":"").">GMT -10:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, -10).")</option>
<option value=\"-9\" ".($setting['value'] == -9?"selected=\"selected\"":"").">GMT -9:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, -9).")</option>
<option value=\"-8\" ".($setting['value'] == -8?"selected=\"selected\"":"").">GMT -8:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, -8).")</option>
<option value=\"-7\" ".($setting['value'] == -7?"selected=\"selected\"":"").">GMT -7:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, -7).")</option>
<option value=\"-6\" ".($setting['value'] == -6?"selected=\"selected\"":"").">GMT -6:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, -6).")</option>
<option value=\"-5\" ".($setting['value'] == -5?"selected=\"selected\"":"").">GMT -5:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, -5).")</option>
<option value=\"-4\" ".($setting['value'] == -4?"selected=\"selected\"":"").">GMT -4:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, -4).")</option>
<option value=\"-3.5\" ".($setting['value'] == -3.5?"selected=\"selected\"":"").">GMT -3:30 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, -3.5).")</option>
<option value=\"-3\" ".($setting['value'] == -3?"selected=\"selected\"":"").">GMT -3:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, -3).")</option>
<option value=\"-2\" ".($setting['value'] == -2?"selected=\"selected\"":"").">GMT -2:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, -2).")</option>
<option value=\"-1\" ".($setting['value'] == -1?"selected=\"selected\"":"").">GMT -1:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, -1).")</option>
<option value=\"0\" ".($setting['value'] == 0?"selected=\"selected\"":"").">GMT (".my_date($mybb->settings['timeformat'], TIME_NOW, 0).")</option>
<option value=\"+1\" ".($setting['value'] == 1?"selected=\"selected\"":"").">GMT +1:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 1).")</option>
<option value=\"+2\" ".($setting['value'] == 2?"selected=\"selected\"":"").">GMT +2:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 2).")</option>
<option value=\"+3\" ".($setting['value'] == 3?"selected=\"selected\"":"").">GMT +3:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 3).")</option>
<option value=\"+3.5\" ".($setting['value'] == 3.5?"selected=\"selected\"":"").">GMT +3:30 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 3.5).")</option>
<option value=\"+4\" ".($setting['value'] == 4?"selected=\"selected\"":"").">GMT +4:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 4).")</option>
<option value=\"+4.5\" ".($setting['value'] == 4.5?"selected=\"selected\"":"").">GMT +4:30 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 4.5).")</option>
<option value=\"+5\" ".($setting['value'] == 5?"selected=\"selected\"":"").">GMT +5:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 5).")</option>
<option value=\"+5.5\" ".($setting['value'] == 5.5?"selected=\"selected\"":"").">GMT +5:30 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 5.5).")</option>
<option value=\"+5.75\" ".($setting['value'] == 5.75?"selected=\"selected\"":"").">GMT +5:45 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 5.75).")</option>
<option value=\"+6\" ".($setting['value'] == 9?"selected=\"selected\"":"").">GMT +6:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 6).")</option>
<option value=\"+7\" ".($setting['value'] == 7?"selected=\"selected\"":"").">GMT +7:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 7).")</option>
<option value=\"+8\" ".($setting['value'] == 8?"selected=\"selected\"":"").">GMT +8:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 8).")</option>
<option value=\"+9\" ".($setting['value'] == 9?"selected=\"selected\"":"").">GMT +9:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 9).")</option>
<option value=\"+9.5\" ".($setting['value'] == 9.5?"selected=\"selected\"":"").">GMT +9:30 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 9.5).")</option>
<option value=\"+10\" ".($setting['value'] == 10?"selected=\"selected\"":"").">GMT +10:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 10).")</option>
<option value=\"+10.5\" ".($setting['value'] == 10.5?"selected=\"selected\"":"").">GMT +10:30 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 10.5).")</option>
<option value=\"+11\" ".($setting['value'] == 11?"selected=\"selected\"":"").">GMT +11:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 11).")</option>
<option value=\"+12\" ".($setting['value'] == 12?"selected=\"selected\"":"").">GMT +12:00 Hours (".my_date($mybb->settings['timeformat'], TIME_NOW, 12).")</option>
</select>

searchtype
php
<select name=\"upsetting[{$setting['name']}]\"><option value=\"standard\">".($lang->setting_searchtype_standard?$lang->setting_searchtype_standard:"Standard")."</option>".($db->supports_fulltext("threads") && $db->supports_fulltext_boolean("posts")?"<option value=\"fulltext\"".($setting['value']=="fulltext"?" selected=\"selected\"":"").">".($lang->setting_searchtype_fulltext?$lang->setting_searchtype_fulltext:"Full Text")."</option>":"")."</select>
Ok, going to Settings->Modify Settings in ACP and editing one of them I actually have realize I'm able to look to all available possibilities, which are: Text, Textarea, Yes / No Choice, On / Off Choice, Selection Box, Radio Buttons, Checkboxes (no multiselect), Language Selection Box, Administration Language Selection Box and Control Panel Style Selection Box.

So there is no multiselect available. Wish there where some more options just there for us to use.

I understand the code you provide here but I don't know what I have to do. The multiselects I want are for plugin settings and by now for plugin settings the only thing I'm doing is inside _install function.

The timezone offset is not a simple select? (only one option can be chosen)

$insertarray = array(
	'name' => 'myplugin_setting',
	'title' => 'Just one setting.',
	'description' => 'This is a text setting.',
	'optionscode' => 'text',
	'value' => '',
	'disporder' => 1,
	'gid' => $myplugingroup
);
$db->insert_query("settings", $insertarray);
And with that I have the input text box in the plugin change settings page (and in its correct order specified with disporder), when the settings are saved it is stored in database, and anytime it is available in $mybb->settings global.

So I don't know how to fit the select code inside the settings.

Could you tell me with is the exactly place of your examples, and if possible the file too? I can't find it. Will go on looking for it.

-------------------------

The other thing I still don't know how to do is to hide a setting. This is for a setting that I want to set programatically but not available for the user for edition (and I suppose this actually should be done invisible both for change settings and modify settings sections).
You put the php inside the optionscode section.
(2011-05-07, 01:28 AM)Dylan M. Wrote: [ -> ]You put the php inside the optionscode section.

I'm not able to understand how to do it correctly. This is not working as an example:

$insertarray = array(
	'name' => 'myplugin_foo',
	'title' => 'Foo Setting',
	'description' => 'A foo setting to try custom PHP inside optionscode.',
	'optionscode' => "php
<select name=\"upsetting[{$setting['name']}]\"><option value=\"standard\">".($lang->setting_searchtype_standard?$lang->setting_searchtype_standard:"Standard")."</option>".($db->supports_fulltext("threads") && $db->supports_fulltext_boolean("posts")?"<option value=\"fulltext\"".($setting['value']=="fulltext"?" selected=\"selected\"":"").">".($lang->setting_searchtype_fulltext?$lang->setting_searchtype_fulltext:"Full Text")."</option>":"")."</select>",
	'value' => 'standard',
	'disporder' => 1,
	'gid' => $myplugin_settings_group
);
$db->insert_query("settings", $insertarray);

Not working. And I suppose it is because I miss something on the way I should add that. Maybe some escape I'm not considering? If I look in database what is stored is:

Quote:php
<select name="upsetting[]"><option value="standard">Standard</option><option value="fulltext">Full Text</option></select>

so any filter has removed parts of the string before storing on database. Or just the php has been evaluated to blanks but it must be stored in database as it is and not evaluated. What I'm exactly missing?
$db->escape_string is what is needed. I don't know exactly what I was doing wrong but took me sometime until the proper escaping and combination of quotes and single quotes Sad

This is an example.
$insertarray = array(
	'name' => 'myplugin_foo',
	'title' => 'Foo Setting',
	'description' => 'A foo setting to try custom PHP inside optionscode.',
	'optionscode' => $db->escape_string('php
<select name=\"upsetting[{$setting[\'name\']}]\">
<option value=\"option1\">One</option>
<option value=\"option2\">Two</option>
<option value=\"option3\">Three</option>
</select>'),
	'value' => 'option1',
	'disporder' => 8,
	'gid' => $myplugin_settings_group
);
$db->insert_query("settings", $insertarray);

And still have not tried multiple
I can't find a way to deal with multivalue. This is what I have now:

$insertarray = array(
	'name' => 'myplugin_foo',
	'title' => 'Foo Setting',
	'description' => 'A foo setting to try custom PHP inside optionscode.',
	'optionscode' => $db->escape_string('php
<select multiple name=\"upsetting[myplugin_foo]\">
<option value=\"option1\"".($setting[\'value\'] == "option1" ? "selected=\"selected\"":"").">One</option>
<option value=\"option2\"".($setting[\'value\'] == "option2" ? "selected=\"selected\"":"").">Two</option>
<option value=\"option3\"".($setting[\'value\'] == "option3" ? "selected=\"selected\"":"").">Three</option>
<option value=\"option4\"".($setting[\'value\'] == "option4" ? "selected=\"selected\"":"").">Four</option>
<option value=\"option5\"".($setting[\'value\'] == "option5" ? "selected=\"selected\"":"").">Five</option>
</select>'),
	'value' => 'option1',
	'disporder' => 8,
	'gid' => $myplugin_settings_group
);
$db->insert_query("settings", $insertarray);

And with that I get a multivalue select in the form but it will only save the last value of all the values selected. I thought I would be able to use some hook to check the value of that form element in $_POST variable to write it to database myself but when I check $_POST variable it has just the wrong value with only the last selected instead of all options selected.

Isn't there any way to do that? If this is something that is known it can't be done at least there, I would like to know to try something different.

Someone knows where I could find the actual selected values on the multiselect form on submission? or in which hook maybe?

I'm going to try to check the content of $_POST in some other hook.
I have managed to do it this way.

A hook to serialize my multiple inputs (this is done so I can use as many multiple input settings as I want, though the example shows only one setting).
$plugins->add_hook("admin_config_settings_change", "myplugin_settings_serialize_multiple");

function myplugin_settings_serialize_multiple()
{
	global $mybb;

	if(is_array($mybb->input['upsetting']))
	{
		foreach($mybb->input['upsetting'] as $name => $value)
		{
			if (is_array($value))
			{
 				$mybb->input['upsetting'][$name] = serialize($value);
			}
		}
	}
}

And on _install my multiple setting:
$insertarray = array(
	'name' => 'myplugin_foo',
	'title' => 'Foo Setting',
	'description' => 'A foo setting to try custom PHP inside optionscode.',
	'optionscode' => $db->escape_string('php
<select multiple name=\"upsetting[myplugin_foo][]\">
<option value=\"option1\"" . ($setting[\'value\'] != "" && in_array("option1", unserialize($setting[\'value\'])) ? "selected=\"selected\"":"") . ">One</option>
<option value=\"option2\"" . ($setting[\'value\'] != "" && in_array("option2", unserialize($setting[\'value\'])) ? "selected=\"selected\"":"") . ">Two</option>
<option value=\"option3\"" . ($setting[\'value\'] != "" && in_array("option3", unserialize($setting[\'value\'])) ? "selected=\"selected\"":"") . ">Three</option>
<option value=\"option4\"" . ($setting[\'value\'] != "" && in_array("option4", unserialize($setting[\'value\'])) ? "selected=\"selected\"":"") . ">Four</option>
<option value=\"option5\"" . ($setting[\'value\'] != "" && in_array("option5", unserialize($setting[\'value\'])) ? "selected=\"selected\"":"") . ">Five</option>
</select>'),
	'value' => '',
	'disporder' => 1,
	'gid' => $myplugin_settings_group
);
$db->insert_query("settings", $insertarray);


I think the hook would not be needed if I mod settings.php changing lines 852-853 from:
$value = $db->escape_string($value);
$db->update_query("settings", array('value' => $value), "name='".$db->escape_string($name)."'");
to
$value = is_array($value) ? serialize($value) : $value;
$value = $db->escape_string($value);
$db->update_query("settings", array('value' => $value), "name='".$db->escape_string($name)."'");

So if a setting is an array (multiple selects, multiple checkboxes) then it is serialized before being stored. Then developers can do whatever they want with that data just unserializing it before they use it. So with custom php as on this example it would be easy to create multivalue settings.


I'm totally new to mybb coding so, is this a proper way to do it?
Personally I would recommend only serializing your own plugin settings in that hook. The 'admin_config_settings_change' hook will be called whenever settings are saved, so also settings from core and other plugins. If other developers want to do other stuff with their settings in that hook but your plugin runs first, their code will break if it's an array.

function myplugin_settings_serialize_multiple()
{
    global $mybb;
    $mybb->input['upsetting']['your_setting1'] = serialize($mybb->input['upsetting']['your_setting1']);
    $mybb->input['upsetting']['your_setting2'] = serialize($mybb->input['upsetting']['your_setting2']);
}
Good point I haven't realize that. Smile

With that in mind it is better to explicitly only work with my own settings as you suggest with corresponding checks to be sure the settings exists.
Yes, an if such as:
if(isset($mybb->settings['whatever_setting_is']) && !empty($mybb->settings['whatever_setting_is']))
{
    // do whatever here...
}

Is always a good idea.