PDA

View Full Version : Create checkbox group at your hack's options (usergroup selection for example)


FractalizeR
07 Aug 2008, 15:29
Hello.

In this article I will show how to create a checkbox group in your hack's options. It's easy. Just do the following:

1. Create an option. Datatype validation type = free.

2. Enter the following code to Option code:
" . eval('$options="";
$fritems = array(
"Test1Code" => "Test option checkbox",
"Test2Code" => "Another test option checkbox",
);
foreach($fritems AS $fritem_id => $fritem_name)
{
$options .= "\\t\\t<label for=\\"setting[$setting[varname]]$fritem_id\\" title=\\"item id: $fritem_id\\"><input type=\\"checkbox\\" tabindex=\\"1\\" name=\\"setting[$setting[varname]]"."[]\\" id=\\"setting[$setting[varname]][$fritem_id]\\" value=\\"$fritem_id\\"" . iif(strpos(",$setting[value],", ",$fritem_id,") !== false, \' checked="checked"\') . iif($vbulletin->debug, " title=\\"name=&quot;setting[$setting[varname]]&quot;\\"") . " />$fritem_name</label><br />\\n";
}
return "<span class=\\"smallfont\\">\\n$options\\t</span>";') . "<input type=\"hidden\" name=\"setting[$setting[varname]][]\" value=\"-1\" />

3. Save option. Now you will see your group of checkboxes. You can add more items to $fritems array. Array key will be the code of the option. Array value is the text to display near checkbox. Please note: all strings added to $fritems array should be double-quoted (because all there is inside single-quoted eval function) or, if you need single-quotes, escape them.


4. Now create a admin_options_processing hook with the following code:
if (is_array($settings['my_setting_name']))
{
$settings['my_setting_name'] = implode(',', $settings['my_setting_name']);
}



5. That's all. Now $vbulletin->settings['my_setting_name'] will be a string like "Test1Code,Test1Code" depending on what user checked at options page.

A little moment on option text localization. Instead of $fritems = array(
"Test1Code" => "Test option checkbox",
"Test2Code" => "Another test option checkbox",
);
you can write
$fritems = array(
"Test1Code" => $settingphrase["my_setting_phrase_name1"],
"Test2Code" => $settingphrase["my_setting_phrase_name2"]
);


Just create phrases at VBulletin settings group with names my_setting_phrase_name1 and my_setting_phrase_name1:

<phrasetype name="vBulletin Settings" fieldname="vbsettings">
<phrase name="my_setting_phrase_name1" date="1218041520" username="FractalizeR" version=""><![CDATA[Test option checkbox]]></phrase>
<phrase name="my_setting_phrase_name2" date="1218040714" username="FractalizeR" version=""><![CDATA[Another test option checkbox]]></phrase>
</phrasetype>

Now a small example on how to list all usergroups and allow user to check some usergroups (take this text to Option code):

" . eval('$options ="";
foreach($vbulletin->usergroupcache AS $usergroupid => $usergroup)
{
$options .= "\\t\\t<label for=\\"setting[$setting[varname]]$usergroupid\\" title=\\"usergroupid: $usergroupid\\"><input type=\\"checkbox\\" tabindex=\\"1\\" name=\\"setting[$setting[varname]]"."[]\\" id=\\"setting[$setting[varname]]$usergroupid\\" value=\\"$usergroupid\\"" . iif(strpos(",$setting[value],", ",$usergroupid,") !== false, \' checked="checked"\') . iif($vbulletin->debug, " title=\\"name=&quot;setting[$setting[varname]]&quot;\\"") . " />$usergroup[title]</label><br />\\n";
}
return "<span class=\\"smallfont\\">\\n$options\\t</span>";') . "<input type=\"hidden\" name=\"setting[$setting[varname]][]\" value=\"-1\" />

All comments are welcome!

Stagehandspace
14 Aug 2008, 17:06
Just what i been looking for, shame I havent got a clue..lol..:(

cool tut tho' :)

ZomgStuff
15 Aug 2008, 16:23
Can you provide a screenshot of how this would look?

FractalizeR
15 Aug 2008, 18:18
Attached to post

Reeve of shinra
15 Aug 2008, 18:20
/bookmarked

this is handy to have!

Antivirus
23 Aug 2008, 17:32
Fractalizer, very nice! I am wondering however why you don't just loop through each usergroup and call vbulletin's print_checkbox_row() function to handle the checkbox creation. It's located within adminfunctions.php :)

FractalizeR
23 Aug 2008, 19:33
Fractalizer, very nice! I am wondering however why you don't just loop through each usergroup and call vbulletin's print_checkbox_row() function to handle the checkbox creation. It's located within adminfunctions.php :)

I am not sure :) May be because this method was extracted from one hack a long time ago. And after some time I extended it a bit and decided to publish a how-to.

veenuisthebest
24 Sep 2008, 07:04
Thanks for this tutorial.

in step 4 i.e.

if (is_array($settings['my_setting_name']))
{
$settings['my_setting_name'] = implode(',', $settings['my_setting_name']);
}

What to write in place of "my_setting_name"and how to check which options are checked from within php page.

I also wanted to know how to use this option code
bitfield:nocache|allowedbbcodesfull

This bitfield type code automatically generates a set of check boxes of allowed bbcodes to choose from. Now how to implement onto our page.

Thank you

FractalizeR
24 Sep 2008, 09:39
my_setting_name is the setting name you provided when creating setting. As for bitfield - I am not sure it is supported. Where is this example from?

veenuisthebest
24 Sep 2008, 10:13
thanks for your reply !

alright so $settings['my_setting_name'] is the field setting name, fine.

1. And in the option code I see "setting[$setting[varname]", does varname needs to be replaced by something ?

2. How do I check for the checked options from the php page ? Like for boolean we do :-

if($vbulletin->options['mysetting_enable']
{
enabled = true;
}
else
{
enabled = false;
}

How to achieve similar for checkbox ?

3. umm.. not sure what you mean by bitfield supported. See in admincp->Social Group Options and Album Options and Visitor Message options. They all have a setting of "Allowed BBCODES", their option code is set to bitfield:nocache|allowedbbcodesfull and it automatically outputs a set of checkboxes of bbcodes.
I did the same and it works but as in my 2nd ques, how do I check it ?

Thank you

FractalizeR
24 Sep 2008, 12:06
1. And in the option code I see "setting[$setting[varname]", does varname needs to be replaced by something ?
No, leave it as is. This is for VBulletin Debug mode.


2. How do I check for the checked options from the php page ? Like for boolean we do :-
if($vbulletin->options['mysetting_enable']
{
enabled = true;
}
else
{
enabled = false;
}

How to achieve similar for checkbox ?
Your $vbulletin->options['my_setting_name'] will contain comma delimited list of checked options. explode it and use.

3. umm.. not sure what you mean by bitfield supported. See in admincp->Social Group Options and Album Options and Visitor Message options. They all have a setting of "Allowed BBCODES", their option code is set to bitfield:nocache|allowedbbcodesfull and it automatically outputs a set of checkboxes of bbcodes.
I did the same and it works but as in my 2nd ques, how do I check it ?

Thank you

This is VBulletin internal feature I never used.

veenuisthebest
26 Sep 2008, 20:10
Thanks for your replies.

Can you please tell me what to do after creating the plugin at admin_options_processing hook.

Is this correct ?

if(explode(',', $vbulletin->options['show_settings']))
{
$showchecked = true;
}
else
{
$showchecked = false;
}

What I want to achieve is quite simple but i am not able to figure it out.

See.. I am showing 3 radio buttons via template. Simple !

Now.. I have created 3 checkboxes (corresponding to radio buttons) so that whichever is checked THAT radio button would show. How do I achieve this ? What should I wrap around my radio button code in template so that the corresponding one shows only when it is allowed (checked in admincp).

Thank you

FractalizeR
29 Sep 2008, 09:50
$radioboxesToShow = explode(',', $vbulletin->options['my_radioboxes_to_show']);
if(isset($radioboxesToShow['radio_1'])) {
$show['radio_1'] = true;
}

Something like this I think. Emded all your radios in templates surrounded by if condition="show[radio_1]"

veenuisthebest
30 Sep 2008, 15:09
oh i still can't get it to work. See this is all what I had done:-

1. I created a setting $vbulletin->options['vin_mysetting'], Free datatype, with the following option code:-

" . eval('$options="";
$fritems = array(
"option1" => "First Radio",
"option2" => "Second Radio",
"option3" => "Third Radio",
);
foreach($fritems AS $fritem_id => $fritem_name)
{
$options .= "\\t\\t<label for=\\"setting[$setting[varname]]$fritem_id\\" title=\\"item id: $fritem_id\\"><input type=\\"checkbox\\" tabindex=\\"1\\" name=\\"setting[$setting[varname]]"."[]\\" id=\\"setting[$setting[varname]][$fritem_id]\\" value=\\"$fritem_id\\"" . iif(strpos(",$setting[value],", ",$fritem_id,") !== false, \' checked="checked"\') . iif($vbulletin->debug, " title=\\"name=&quot;setting[$setting[varname]]&quot;\\"") . " />$fritem_name</label><br />\\n";
}
return "<span class=\\"smallfont\\">\\n$options\\t</span>";') . "<input type=\"hidden\" name=\"setting[$setting[varname]][]\" value=\"-1\" />

2. Then I created a hook at admin_options_processing with the following code:-

if (is_array($settings['vin_mysetting']))
{
$settings['vin_mysetting'] = implode(',', $settings['vin_mysetting']);
}

3. Then in my file.php I did this (taking just "option1" for now.) :-

$radioboxesToShow = explode(',', $vbulletin->options['vin_mysetting']);
if(isset($radioboxesToShow['option1'])) {
$show['option1'] = true;
}

4. Then at atlast, in the associated template, I did this:-

<if condition="$show['option1']">
<input type="radio" name="radiotype" value="0"/>My First Radio<br />
</if>


It never shows up now, whether checked or unchecked !

Thank you

Bravo
03 Nov 2008, 11:50
Thanks for the info, Ive been in need of this type of info... now I just need to figure out how to create options in the UserGroup and User settings