MyBB Community Forums

Full Version: Settings help - usergroup dropdown
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to make a 'premium member' plug-in but for some reason I can not get the Usergroups to be populated into a 'setting'

Here is what I am trying in the plugin activate:

function premium_member_activate()
{
	global $db;
	$info = premium_member_info();

	$result = $db->query("SELECT * FROM ".TABLE_PREFIX."usergroups ORDER BY gid");

	$group_options = 'select';
	while ($row = $db->fetch_array($result))
	{
		$group_options .= '\r'.$row['gid'].'='.str_replace(' /', '', $row['title']);
	}

Then I dump the $group_options variable into the 'optionscode' key

	$premium_member_setting_2 = array(
		'sid'			=> NULL,
		'name'			=> 'premium_member_moveinto',
		'title'			=> 'Premium Group',
		'description'	=> 'Group to move Premium Members into (if you edit groups, you must run the Update Groups option).',
		'optionscode'	=> $group_options,
		'value'			=> '2',
		'disporder'		=> '2',
		'gid'			=> intval($gid),
	);


The data in the SQL table looks fine, but its not parsing in the Change Setting page.

select
1=Unregistered Not Logged In
2=Registered
3=Super Moderators
4=Administrators
5=Awaiting Activation
6=Moderators
7=Banned
8=Gold Member
9=Platinum Member
10=Vendor

Any help? The string replace of the ' /' is to deal with the default Unregistered / Not Logged In group
May I ask what your plugin does besides create these premium member groups?
it does not create the groups. you have to make the groups you want using the standard usergroup controls. then you can activate the plugin and it will populate the settings with groups available at the time of activation and modify the user table to store expiration dates.

in the settings, you use this dropdown i am trying to get to work to tell the system which group to add to the users additionalgroups. you will also be able to set an expiration time (i.e. 1 day, 1 month, 6 months, 1 year, etc).

Then a separate Paypal IPN file will handle incoming payments and once verified, add the specified premium group to the users additionalgroups and set the users expiration date as now() + admin spec'd time.

A new payment page (or even a custom payment button) will be used to send the user (and username) to paypal for payment.

a randomly run hook will process user expirations and remove the premium group from the users additionalgroups and reset the expiration

hopefully i can get it to work. i dont need your huge myps or other similar systems. i just need a basic premium member plugin.
Quote:
select
1=Unregistered Not Logged In
2=Registered
3=Super Moderators
4=Administrators
5=Awaiting Activation
6=Moderators
7=Banned
8=Gold Member
9=Platinum Member
10=Vendor
Is that code you posted in your first post what you want or what you are currently getting? If you post a code snippet of what you are currently getting and another code snippet of what you want it to look like, it'll be easier to debug.

Also, do you have a $db->insert_query call after:
    $premium_member_setting_2 = array(
        'sid'            => NULL,
        'name'            => 'premium_member_moveinto',
        'title'            => 'Premium Group',
        'description'    => 'Group to move Premium Members into (if you edit groups, you must run the Update Groups option).',
        'optionscode'    => $group_options,
        'value'            => '2',
        'disporder'        => '2',
        'gid'            => intval($gid),
    );
If you don't, then that's the problem why it's not being added as a setting. You need
$db->insert_query(TABLE_PREFIX."settings", $premium_member_setting_2);

If you could clarify what's going wrong, that would be make it easier Smile
what i am getting in the settings table is the above quoted 'select'. the settings and setting group are all being inserted. however, when i go to change the settings for the plugin, the dropdown does not show, but the setting name/row is present. in the html source, there is zero code where the <option select> should be.

here is the plugin code so far. i still need to add in the 'refresh groups' feature which will basically rebuild the dropdown i am having trouble with.

this entire app is not complete. this plug-in file is almost done, but the IPN processing has not been started yet.
<?php
// Premium Mmebership Plugin
// By [email protected]
// Version 1.0

// This plugin (C) [email protected] 2006.  You may not redistribute this plugin without the permission from [email protected].

function premium_member_info()
{
	return array(
		'name'			=> 'Premium Membership',
		'description'	=> 'Add premium membership for users.  For MyBB 1.2.x',
		'website'		=> 'http://www.pavementsucks.com',
		'author'		=> 'JasonB',
		'authorsite'	=> 'http://www.pavementsucks.com',
		'version'		=> '1.0',
	);
}

function premium_member_activate()
{
	global $db;
	$info = premium_member_info();

	$result = $db->query('SELECT * FROM '.TABLE_PREFIX.'usergroups ORDER BY gid');

	$group_options = 'select';
	while ($row = $db->fetch_array($result))
	{
		$group_options .= '\r\n'.$row['gid'].'='.str_replace(' /', '', $row['title']);
	}


	$setting_group_array = array(
		'name' => str_replace(' ', '_', 'jasonb_'.strtolower($info['name'])),
		'title' => $info[name].' (JasonB)',
		'description' => 'Settings for the '.$info[name].' plugin',
		'disporder' => 1,
		'isdefault' => 'no',
		);
	$db->insert_query(TABLE_PREFIX.'settinggroups', $setting_group_array);
	$gid = $db->insert_id();

	$premium_member_setting_1 = array(
		'sid'			=> NULL,
		'name'			=> 'premium_member_switch',
		'title'			=> 'Premium Member Switch',
		'description'	=> 'This turns on or off this plugin for the whole board.',
		'optionscode'	=> 'onoff',
		'value'			=> 'on',
		'disporder'		=> '1',
		'gid'			=> intval($gid),
	);

	$premium_member_setting_2 = array(
		'sid'			=> NULL,
		'name'			=> 'premium_member_moveinto',
		'title'			=> 'Premium Group',
		'description'	=> 'Group to move Premium Members into (if you edit groups, you must run the Update Groups options).',
		'optionscode'	=> $group_options,
		'value'			=> '2',
		'disporder'		=> '2',
		'gid'			=> intval($gid),
	);

	$premium_member_setting_3 = array(
		'sid'			=> NULL,
		'name'			=> 'premium_member_term',
		'title'			=> 'Premium Member Term',
		'description'	=> 'Legnth is the Premium Membership term (how long until it expires).',
		'optionscode'	=> 'select
86400=1 Day
604800=7 Days
2592000=1 Month
5184000=2 Months
15552000=6 Months
31536000=1 Year
63072000=2 Years
94608000=3 Years',
		'value'			=> '31536000',
		'disporder'		=> '3',
		'gid'			=> intval($gid),
	);

	$db->insert_query(TABLE_PREFIX.'settings', $premium_member_setting_1);
	$db->insert_query(TABLE_PREFIX.'settings', $premium_member_setting_2);
	$db->insert_query(TABLE_PREFIX.'settings', $premium_member_setting_3);
	
	$db->query('ALTER TABLE '.TABLE_PREFIX.'users ADD prem_expire INT( 10 ) NOT NULL');
	rebuildsettings();
}

function premium_member_deactivate()
{
	global $db;
	$info = premium_member_info();

	$result = $db->query('SELECT gid FROM '.TABLE_PREFIX.'settinggroups WHERE name = "'.str_replace(' ', '_', 'jasonb_'.strtolower($info['name'])).'" LIMIT 1');
	$group = $db->fetch_array($result);
	
	if(!empty($group['gid']))
	{
		$db->query('DELETE FROM '.TABLE_PREFIX.'settinggroups WHERE gid = '.$group[gid].' LIMIT 1');
		$db->query('DELETE FROM '.TABLE_PREFIX.'settings WHERE gid = '.$group[gid]);
		rebuildsettings();
		$db->query('ALTER TABLE '.TABLE_PREFIX.'users  DROP prem_expire');
	}

}

$plugins->add_hook('global_end', 'premium_member_run');
function premium_member_run()
{
	global $mybb;
	$rand = mt_rand(0, 10);
	if ($rand > 8 && $mybb->settings['premium_member_switch'] == 'on')
	{
		premium_member_expire();
	}
}

function premium_member_expire()
{
	global $mybb, $db, $cache;
	
	//get setting params
	$settings = $mybb->settings;
	$premium_group = $settings['premium_member_moveinto'];
	
	// Expire Users
	$expire_time = time();
	$query = $db->query('
		SELECT u.uid, u.additionalgroups, u.displaygroup 
		FROM '.TABLE_PREFIX.'users u 
			LEFT JOIN '.TABLE_PREFIX.'usergroups ug ON (u.usergroup = ug.gid) 
		WHERE 
			u.prem_expire > 0 
			AND u.prem_expire < '.$expire_time.'
			AND ug.cancp != "yes"
		');
	while ($row = $db->fetch_array($query))
	{
		$existing_addgroups = explode(',', $row['additionalgroups']);
		$new_addgroups = premium_member_remove_premium_group($existing_addgroups, $premium_group);
		$db->query('UPDATE '.TABLE_PREFIX.'users SET prem_expire=0, additionalgroups="'.implode(',', $new_addgroups).'", displaygroup="'.$new_addgroups[0].'" WHERE uid ='.$row['uid']);
	}
}

function premium_member_remove_premium_group($group_array, $group_remove)
{
    $thisarray = array ();
    foreach($group_array as $value)
        if($value != $group_remove)
            $thisarray[] = $value;
    return $thisarray; 
}



?>
fixed a few typos in the above code ( i converted from " to ' and missed a few edits)
I figured out my problem. The optionscode template does not support over 25 characters. That means 'Unregistered Not Logged In' is too long and the code craps out.

If I edit the database to 'Unregistered Not Logged I', everything works great. I even tested by extending the 'value' part to more characters and its not effecting anything, only the actual 'name' part that is displayed.