|
Tutorials -
General
|
|
Thursday, 01 April 2010 07:51 |
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 import the CB API 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.
| Create User |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
function registerUser( $fname, $lname, $email, $username, $password ) { global $_CB_framework, $_CB_database; $row = new moscomprofilerUser( $_CB_database ); $row->usertype = $_CB_framework->getCfg( 'new_usertype' ); $row->gid = $_CB_framework->acl->get_group_id( $row->usertype, 'ARO' ); $row->confirmed = 1; $row->approved = 1; $row->block = 0; $row->sendEmail = 0; $row->registerDate = date( 'Y-m-d H:i:s', $_CB_framework->now() ); $row->name = $fname . ' ' . $lname; $row->firstname = $fname; $row->lastname = $lname; $row->username = $username; $row->email = $email; $row->password = cbHashPassword( $password ); if ( ! $row->store() ) { trigger_error( sprintf( 'registerUser SQL Error: %s', $row->getError() ), E_USER_ERROR ); } }
|
Editing a user takes an additional step, you'll first need to first Establish $user object once the object has been established you'll need to use the following modified function with $user object passed to it.
| Edit User |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
function registerUser( $user, $fname, $lname, $email, $username, $password ) { global $_CB_framework, $_CB_database; $row = new moscomprofilerUser( $_CB_database ); if ( $user->id ) { $row->load( $user->id ); } $row->usertype = $_CB_framework->getCfg( 'new_usertype' ); $row->gid = $_CB_framework->acl->get_group_id( $row->usertype, 'ARO' ); $row->confirmed = 1; $row->approved = 1; $row->block = 0; $row->sendEmail = 0; $row->registerDate = date( 'Y-m-d H:i:s', $_CB_framework->now() ); $row->name = $fname . ' ' . $lname; $row->firstname = $fname; $row->lastname = $lname; $row->username = $username; $row->email = $email; $row->password = cbHashPassword( $password ); if ( ! $row->store() ) { trigger_error( sprintf( 'registerUser SQL Error: %s', $row->getError() ), E_USER_ERROR ); } }
|
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.
1
|
$row->cb_myfield = 'Test Value';
|
|
|
Last Updated on Thursday, 01 April 2010 08:08 |