Registering a user through CB API

Community Builder provides some basic API for properly and easily creating or editing existing users while maintaining a complete structure within Joomla and Community Builder it self. Users created and edited in this manner will be completely functional and even be susceptible to basic fields validation!

To create a user you must first Include API externally if you are creating a user outside of a Community Builder plugin followed by including the Plugin & Class. Now that you've loaded the API you can include the following function for easy creation of users.

function registerUser( $firstName, $lastName, $email, $username, $password, $approve = 0, $confirm = 0, $usergroups = array() ) {
	global $_CB_framework, $_PLUGINS, $ueConfig;

	$approval				=	( $approve == 2 ? $ueConfig['reg_admin_approval'] : $approve );
	$confirmation			=	( $confirm == 2 ? $ueConfig['reg_confirmation'] : $confirm );	
	$user					=	new \CB\Database\Table\UserTable();
	
	$user->set( 'username', $username );
	$user->set( 'email', $email );
	$user->set( 'name', trim( $firstName . ' ' . $lastName ) );
	$user->set( 'gids', array( (int) $_CB_framework->getCfg( 'new_usertype' ) ) );
	$user->set( 'sendEmail', 0 );
	$user->set( 'registerDate', $_CB_framework->getUTCDate() );
	$user->set( 'password', $user->hashAndSaltPassword( $password ) );
	$user->set( 'registeripaddr', cbGetIPlist() );

	if ( $approval == 0 ) {
		$user->set( 'approved', 1 );
	} else {
		$user->set( 'approved', 0 );
	}

	if ( $confirmation == 0 ) {
		$user->set( 'confirmed', 1 );
	} else {
		$user->set( 'confirmed', 0 );
	}

	if ( ( $user->get( 'confirmed' ) == 1 ) && ( $user->get( 'approved' ) == 1 ) ) {
		$user->set( 'block', 0 );
	} else {
		$user->set( 'block', 1 );
	}
	
	$_PLUGINS->trigger( 'onBeforeUserRegistration', array( &$user, &$user ) );

	if ( $user->store() ) {
		if ( $user->get( 'confirmed' ) == 0 ) {
			$user->store();
		}

		$messagesToUser		=	activateUser( $user, 1, 'UserRegistration' );

		$_PLUGINS->trigger( 'onAfterUserRegistration', array( &$user, &$user, true ) );

		if ( $user->get( 'block' ) == 1 ) {
			return false;
		} else {
			return true;
		}
	}
	
	return false;
}

Editing a user takes an additional step, you'll first need to first Establishing $user object once the object has been established you'll need to use the following modified function with $user object passed to it.

function editUser( $user, $name ) {
	$oldUserComplete		=	new \CB\Database\Table\UserTable();

	foreach ( array_keys( get_object_vars( $user ) ) as $k ) {
		if ( substr( $k, 0, 1 ) != '_' ) {
			$oldUserComplete->set( $k, $user->get( $k ) );
		}
	}
	
	$user->set( 'name', $name );
	
	$_PLUGINS->trigger( 'onBeforeUserUpdate', array( &$user, &$user, &$oldUserComplete, &$oldUserComplete ) );
	
	if ( ! $user->store() ) {
		return false;
	}
	
	$_PLUGINS->trigger( 'onAfterUserUpdate', array( &$user, &$user, $oldUserComplete ) );
	
	return true;
}

The newly created function will now register a new user or edit an existing user; there are many applications for such API such as external site integrations. In addition to the default Joomla and CB fields you can also add additional fields using the following method.

$user->set( 'FIELD_NAME', FIELD_VALUE );
Facebook Twitter Google LinkedIn