PDA

View Full Version : Add Multiple Options Per User (via Bitfields)


akanevsky
20 May 2006, 13:06
Create Multiple Options Per User (via Bitfields)

This How-To should serve as a reference to coders, who have a basic knowledge of PHP and who want to make their own mods.
Help on writing hacks will NOT be provided, and any such posts will be ignored.

Whereever it says mybitoptionsfield, you'll need to replace that with the actual fieldname that you are going to use.

The following step is to create bitfield xml for vBulletin.
Create a file named bitfield_myproductid.xml, where myproductid is the id of your product, with the following content, in ./includes/xml/:
Note: In the <bitfield> tag, name="" must contain the desired title of the option. You are going to use that title to access the options later on. The title must contain ALPHANUMBERIC characters only, and it should not contain spaces. The digit in between the opening and closing <bitfield> tags is the bit value. Each consecutive bit value must be 2 x (Previous Value). Sample valid sequence: 1, 2, 4, 8, 16, 32.
<?xml version="1.0" encoding="ISO-8859-1"?>

<bitfields product="myproductid">
<bitfielddefs>
<group name="misc">
<group name="mybitoptionsfield">
<bitfield name="option1">1</bitfield>
<bitfield name="option2">2</bitfield>
</group>
</group>
</bitfielddefs>
</bitfields>

The following step is to define installation process of the bitfield in your product.
Create a new product and add the following codes as install and uninstall, respectively:

$db->hide_errors();

$db->query_write("ALTER TABLE `" . TABLE_PREFIX . "user` ADD `mybitoptionsfield` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0' AFTER `options`");

require_once(DIR . '/includes/class_bitfield_builder.php');
$myobj =& vB_Bitfield_Builder::init();
$myobj->save($db);
build_forum_permissions();

$db->show_errors();

$db->hide_errors();

$db->query_write("ALTER TABLE `" . TABLE_PREFIX . "user` DROP `mybitoptionsfield`");

require_once(DIR . '/includes/class_bitfield_builder.php');
$myobj =& vB_Bitfield_Builder::init();
$myobj->save($db);
build_forum_permissions();

$db->show_errors();

The following step is to have the new options fetched together with userinfo.
Create a plugin @ fetch_userinfo with the following code:

if (isset($vbulletin->bf_misc['mybitoptionsfield']))
{
foreach ($vbulletin->bf_misc['mybitoptionsfield'] AS $optionname => $optionval)
{
$user["$optionname"] = ($user['mybitoptionsfield'] & $optionval ? 1 : 0);
}
}

The following step is to add new options to UserCP Option Page on Template Level.
In template modifyoptions, add the following code whereever you want the options to appear:

<fieldset class="fieldset">
<legend><label for="cb_option1">OPTION1_HEADING1</label></legend>
<table cellpadding="0" cellspacing="$stylevar[formspacer]" border="0" width="100%">
<tr>
<td>
OPTION1_DESCRIPTION1
</td>
</tr>
<tr>
<td><label for="cb_option1"><input type="checkbox" name="mybitoptionsfield[option1]" value="1" id="cb_option1" $checked[option1] />OPTION1_HEADING1</label><input type="hidden" name="set_options[option1]" value="1" /></td>
</tr>
</table>
</fieldset>

<fieldset class="fieldset">
<legend><label for="cb_option2">OPTION2_HEADING2</label></legend>
<table cellpadding="0" cellspacing="$stylevar[formspacer]" border="0" width="100%">
<tr>
<td>
OPTION2_DESCRIPTION2
</td>
</tr>
<tr>
<td><label for="cb_option2"><input type="checkbox" name="mybitoptionsfield[option2]" value="1" id="cb_option2" $checked[option2] />OPTION2_HEADING2</label><input type="hidden" name="set_options[option2]" value="2" /></td>
</tr>
</table>
</fieldset>

The following step is to add new options to UserCP Option Page on Code Level.
Create a plugin @ profile_updateoptions with the following code:

if (isset($vbulletin->bf_misc['mybitoptionsfield']))
{
$vbulletin->input->clean_gpc('p', 'mybitoptionsfield', TYPE_ARRAY_BOOL);

foreach ($vbulletin->bf_misc['mybitoptionsfield'] AS $key => $val)
{
if (isset($vbulletin->GPC['mybitoptionsfield']["$key"]) OR isset($vbulletin->GPC['set_options']["$key"]))
{
$userdata->set_bitfield('mybitoptionsfield', $key, $vbulletin->GPC['mybitoptionsfield']["$key"]);
}
}
}

The following step is to add new options to AdminCP User Manager.
Create a plugin @ useradmin_edit_column1 with the following code:

if (isset($vbulletin->bf_misc['mybitoptionsfield']))
{
print_table_break('', $INNERTABLEWIDTH);

$mybitoptionsfield = convert_bits_to_array($user['mybitoptionsfield'], $vbulletin->bf_misc['mybitoptionsfield']);
$user = array_merge($user, $mybitoptionsfield);

print_table_header('MYHEADING');
print_yes_no_row('MYOPTION1', 'mybitoptionsfield[option1]', $user['option1']);
print_yes_no_row('MYOPTION2', 'mybitoptionsfield[option2]', $user['option2']);
}

The following step is to have the new options saved when the button is clicked.
Create a plugin @ useradmin_update_save with the following code:

if (isset($vbulletin->bf_misc['mybitoptionsfield']))
{
$vbulletin->input->clean_gpc('p', 'mybitoptionsfield', TYPE_ARRAY_BOOL);

foreach ($vbulletin->GPC['mybitoptionsfield'] AS $key => $val)
{
if (isset($vbulletin->GPC['mybitoptionsfield']["$key"]))
{
$userdata->set_bitfield('mybitoptionsfield', $key, $val);
}
}
}

The following step is to have the new bitfield added to the vBulletin_User_Dm.
Create a plugin @ userdata_start with the following code:

if (isset($this->registry->bf_misc['mybitoptionsfield']))
{
$this->bitfields["mybitoptionsfield"] =& $this->registry->bf_misc['mybitoptionsfield'];
}

Once done, rebuild your btifields.
Now, you should be able to access the new options simply by using $userinfo['mybitoptiontitle'].

>> EOD

-=Sniper=-
20 May 2006, 13:14
bookmarked :) thanks dude

Logikos
20 May 2006, 14:40
You saved the day! Thank you very much Anthony! :)

rogersnm
28 May 2006, 16:31
i just get

Invalid File Specified

when i try to import the xml product file containing:
<?xml version="1.0" encoding="ISO-8859-1"?>

<bitfields product="myproductid">
<bitfielddefs>
<group name="misc">
<group name="mybitoptionsfield">
<bitfield name="option1">1</bitfield>
<bitfield name="option2">2</bitfield>
</group>
</group>
</bitfielddefs>
</bitfields>

akanevsky
28 May 2006, 16:34
Don't import that file, place it under ./includes/xml/ of your forum (filename should be: bitfield_myproductid.xml).

rogersnm
28 May 2006, 16:45
ahh thanks for the quick response

btw myproductid is myproductid

lol

akanevsky
28 May 2006, 16:54
You should use a unique string as your product id, not keep the default one. Two product with the same product id cannot co-exist.

rogersnm
28 May 2006, 17:04
yeah i am just testing i will change afterwards.

do you know how to get 2 radio buttons yes or no?

akanevsky
28 May 2006, 17:06
Please refer to the following text from the first post:

Help on writing hacks will NOT be provided, and any such posts will be ignored.

rogersnm
28 May 2006, 17:07
so how am i supposed to learn :(

akanevsky
28 May 2006, 17:13
You could start by referring to the following thread:
http://www.vbulletin.org/forum/showthread.php?t=99570

Also, if you have specified questions, post them in "Modifications Questions" forum. :)

rogersnm
28 May 2006, 17:14
already have. lol :)....

Cloud-Warrior
26 Feb 2008, 12:07
A great tutorial!

I had two issues, that others might also run into:

* you might have to execute the querys from step 2 manually (as i followed all the steps here to create the product, and didn't acually "install" it via the import product button)

* If you want to create a plugin that changes the template (instead of manually inserting the code into the template modifyoptionslike in step 4) you have to do some extra things - explained in this thread (http://www.vbulletin.org/forum/showthread.php?p=1430865#post1430865)

SorentoUltimate
26 Jan 2010, 16:06
Very Good Article

Neo_obs
29 Jan 2010, 02:05
I have a side question, I know it says no help will be provided. But I want to have an option like "Enable Override" and then my options would override the usergroup options. For instance, if enable override is on and voting is off, the user can't vote whether they have usergroup permissions or not. If the voting is on then they can vote whether they have usergroup permissions or not. This way I can allow certain users to vote even if their usergroup doesn't allow it, or deny them access to vote even if their usergroup allows it.

Broseph
04 Feb 2013, 21:29
Fantastic article. Any chance of doing something similar with vb4.x? Has it even changed much?