Skip to Content Skip to Menu

[SOLVED] Creating a user from a form

  • fribse2011
  • fribse2011
  • OFFLINE
  • Posts: 921
  • Thanks: 79
  • Karma: 8
7 years 9 months ago - 7 years 8 months ago #290003 by fribse2011
[SOLVED] Creating a user from a form was created by fribse2011
I'm trying to create a form in Chronoforms, that will create a very specific type of user on our site, that is automatically approved and confirmed,
I've searched to find the right way to create a user, and this is what I came to:
Code:
global $_CB_framework, $_PLUGINS, $ueConfig; $user = new \CB\Database\Table\UserTable(); $user->set( 'username', $username ); $user->set( 'email', $email ); $user->set( 'name', $name ); $user->set( 'gids', array( (int) '9' ) ) ); $user->set( 'sendEmail', 0 ); $user->set( 'registerDate', $_CB_framework->getUTCDate() ); $user->set( 'password', $user->hashAndSaltPassword( $password ) ); $user->set( 'registeripaddr', cbGetIPlist() ); $user->set( 'approved', 1 ); $user->set( 'confirmed', 1 ); $_PLUGINS->trigger( 'onBeforeUserRegistration', array( &$user, &$user ) ); if ( $user->store() ) { $messagesToUser = activateUser( $user, 1, 'UserRegistration' ); $_PLUGINS->trigger( 'onAfterUserRegistration', array( &$user, &$user, true ) ); return true; } return false;
Is that all it takes? Or am I missing something? Is the GID line correct (the ID of the group is 9 here).
The line '$messagesToUser', is that just sending a mail to the user with an activation link?
Is it possible to just activate the user? And if so, how?
It should use the proper triggers from CB, right?

Best regards
Fribse

Frømandsklubben Nikon, www.nikondyk.dk
We're a volunteer driven diving club in Ishøj, Denmark.
Har du brug for en dykkerklub der rummer alle, så kom ned til os.
Last edit: 7 years 8 months ago by fribse2011.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48444
  • Thanks: 8279
  • Karma: 1443
7 years 9 months ago #290013 by krileon
Replied by krileon on topic Creating a user from a form
The below tutorials is up to date on how to register a user through api.

www.joomlapolis.com/documentation/279-community-builder/tutorials/18362-registering-a-user-through-cb-api

The line '$messagesToUser', is that just sending a mail to the user with an activation link?

No, the email is handled by and sent in activateUser. The result of activateUser is an array of messages to immediately display like the message they'd normally see after a normal frontend registration.

Is it possible to just activate the user? And if so, how?

Ensure block is 0, confirmed is 1, and approved is 1. They should be active on ->store() in that case.

It should use the proper triggers from CB, right?

Yes, you're already firing the before and after triggers.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
The following user(s) said Thank You: asierraserna

Please Log in or Create an account to join the conversation.

  • fribse2011
  • fribse2011
  • OFFLINE
  • Posts: 921
  • Thanks: 79
  • Karma: 8
7 years 9 months ago #290018 by fribse2011
Replied by fribse2011 on topic Creating a user from a form
Hi Kyle
Thankyou, that was the tutorial I looked at, I just wanted to simplify it a bit, so that I can put it straight into my form.
I'll try it out on my test site, so that I won't mess everything up :-)

Best regards
Fribse

Frømandsklubben Nikon, www.nikondyk.dk
We're a volunteer driven diving club in Ishøj, Denmark.
Har du brug for en dykkerklub der rummer alle, så kom ned til os.

Please Log in or Create an account to join the conversation.

  • fribse2011
  • fribse2011
  • OFFLINE
  • Posts: 921
  • Thanks: 79
  • Karma: 8
7 years 9 months ago - 7 years 9 months ago #290019 by fribse2011
Replied by fribse2011 on topic Creating a user from a form
Well, I'm not a very good coder, but I'm making a stab at it.

Using the code as it was, gave me this message:
Class 'CB\Database\Table\UserTable' not found

I then changed it to
Code:
<?php global $_CB_framework, $_PLUGINS, $ueConfig, $mainframe ; include_once( JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php' ); cbimport( 'cb.html' ); $user = new \CB\Database\Table\UserTable(); $user->set( 'username', $username ); $user->set( 'email', $email ); $user->set( 'name', $name ); $user->set( 'gids', array( "9" ) ); $user->set( 'sendEmail', 0 ); $user->set( 'registerDate', $_CB_framework->getUTCDate() ); $user->set( 'password', $user->hashAndSaltPassword( $password ) ); $user->set( 'registeripaddr', cbGetIPlist() ); $user->set( 'approved', 1 ); $user->set( 'confirmed', 1 ); $user->set( 'cb_type', 'Prøve' ); $user->set( 'cb_cell' , $mobil ); $user->set( 'cb_adresse', $adresse ); $user->set( 'cb_postnummer' , $post ); $user->set( 'cb_birthday' , $foedsel ); $user->set( 'cb_aktiv' , 0 ); $user->set( 'cb_trial' , $_CB_framework->getUTCDate() ); $_PLUGINS->trigger( 'onBeforeUserRegistration', array( &$user, &$user ) ); if ( $user->store() ) { $messagesToUser = activateUser( $user, 1, 'UserRegistration' ); $_PLUGINS->trigger( 'onAfterUserRegistration', array( &$user, &$user, true ) ); return true; } return false; ?>
That made it run without errors, but it didn't create the user, not sure why, except maybe the gid is not given?

Best regards
Fribse

Frømandsklubben Nikon, www.nikondyk.dk
We're a volunteer driven diving club in Ishøj, Denmark.
Har du brug for en dykkerklub der rummer alle, så kom ned til os.
Last edit: 7 years 9 months ago by fribse2011.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48444
  • Thanks: 8279
  • Karma: 1443
7 years 9 months ago #290044 by krileon
Replied by krileon on topic Creating a user from a form
Doesn't look like CB API was loaded. The IF check in the below tutorial is very important as if it can't find CBs API to load it'll just error.

www.joomlapolis.com/documentation/279-community-builder/tutorials/18357-including-cb-api-for-usage-outside-of-cb


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

  • fribse2011
  • fribse2011
  • OFFLINE
  • Posts: 921
  • Thanks: 79
  • Karma: 8
7 years 9 months ago #290071 by fribse2011
Replied by fribse2011 on topic Creating a user from a form
I've gotten to this, but when it tries to store the user, I just get a 'fail'.
When I was logged in, I found that it replaced the fields on my own user, I thought the line:
$user = new \CB\Database\Table\UserTable();
Was to create a new object?
Code:
<?php global $_CB_framework, $_PLUGINS, $ueConfig, $mainframe ; if ( ( ! file_exists( JPATH_SITE . '/libraries/CBLib/CBLib/Core/CBLib.php' ) ) || ( ! file_exists( JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php' ) ) ) { echo 'CB not installed'; return; } include_once( JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php' ); cbimport( 'cb.html' ); $user = new \CB\Database\Table\UserTable(); $user->set( 'username', $username ); $user->set( 'email', $email ); $user->set( 'name', $name ); $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() ); $user->set( 'approved', 1 ); $user->set( 'confirmed', 1 ); $user->set( 'block', 0 ); $user->set( 'cb_type', 'Prøve' ); $user->set( 'cb_cell' , $mobil ); $user->set( 'cb_adresse', $adresse ); $user->set( 'cb_postnummer' , $post ); $user->set( 'cb_birthday' , $foedsel ); $user->set( 'cb_aktiv' , 0 ); $_PLUGINS->trigger( 'onBeforeUserRegistration', array( &$user, &$user ) ); if ( $user->store() ) { $messagesToUser = activateUser( $user, 1, 'UserRegistration' ); $_PLUGINS->trigger( 'onAfterUserRegistration', array( &$user, &$user, true ) ); echo "success"; return true; } echo "fail"; return false; ?>

Best regards
Fribse

Frømandsklubben Nikon, www.nikondyk.dk
We're a volunteer driven diving club in Ishøj, Denmark.
Har du brug for en dykkerklub der rummer alle, så kom ned til os.

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum