Hi. I have a form that I want to use to create a new Joomla user.
Explanation:
User arrives at a form applying for a market stall. Here, they enter all the information required which just happens to include their name and email address. I'd like to use this information to then create them as a user in Joomla (rather than having to use the built in form).
So I was thinking I could use com_users and use the registration method. Joomla would then handle everything for me :)
The code.
All sitting in the FormModel
class MarketHelper
{
/**
* PAUL get the component model
**/
public static function getModel($name, $path = JPATH_COMPONENT_ADMINISTRATOR, $component = 'com_market')
{
// load some joomla helpers
JLoader::import('joomla.application.component.model');
// load the model file
JLoader::import( $name, $path . '/model' );
// return instance
return JModelLegacy::getInstance( $name, $component.'models' );
}
/**
* PAUL Create the user array
*/
public static function createUser($new)
{
// load the user regestration model
$model = self::getModel('registration', JPATH_ROOT. '/components/com_users', 'Users');
// JLoader::import('registration', JPATH_ROOT. '/components/com_users', 'Users');
$password = ApplicationHelper::getHash(UserHelper::genRandomPassword());
$config = ComponentHelper::getParams('com_users');
$defaultUserGroup = $config->get('new_usertype', 2);
$userData = array(
"name" => $data['forename'].' '.$data['surname'],
"username" => $data['forename'].' '.$data['surname'],
"password" => $password,
"password2" => $password, //confirm password
"email" => $data['emailaddress'],
"sendEmail" => 1,
"groups" => array($defaultUserGroup));
// register the new user
$userId = $model->register($data);
// if user is created
if ($userId > 0)
{
return $userId;
}
return $model->getError();
}
}
and further down in the model when the form is saved
public function save($data)
{
blah blah blah ......
if ($table->save($data) === true)
{
// setup new user - This is using the market helper methods!
// JLoader::register('MarketHelper',dirname(JPATH_COMPONENT.DS.'helper'.DS.'MarketHelper.php'));
$newUser = array(
'username' => $validData['username'],
'name' => $validData['name'],
'email' => $validData['email']);
$userId = MarketHelper::createUser($newUser); // PAUL Why can't it find this class??
if (!is_int($userId))
{
$this->setMessage($userId, 'error');
}
blah blah blah ....
I'm sure there's lots wrong here (am I putting this in the right place etc) but at this stage I get the following error :
Call to a member function register() on bool
Can anyone help me please.
I realise I could just get the $newUser array and spit it to the tables bt I want Joomla to process the registration 'properly'
Cheers - Paul