Creating usergroup while user registration in joomla

I would like to create new usergroup in joomla with customized registration form(juser plugin of fabrik) and assign user to this group(this part is missed, really don`t know how to add it). I have wrote some script which was included in the php plugin of form. but not sure where is a error. It would be helpful to get some info where is a error.
PHP:
// Get a db connection.
$db =JFactory::getDbo();// Create a new query object.
$query = $db->getQuery(true);

// Insert columns.
$columns = array('parentid','lft','rgt','title');
$title ='{user_registration_form___companyid}';
$child_query1 ="SELECT max('id') FROM '#__usergroups'";
$newgroupid = $child_query1+1;
$parent_id ='2';

$db->setQuery($child_query1);
$max_rgt = $db->loadResult();

// calculate left and rgt for new entry
$new_lft = $max_rgt;
$new_rgt = $max_rgt +1;

// update lft and rgt of all entries having lft , rgt greater than max_rgt
$upd1 ="UPDATE '#__usergroups' SET 'lft' = 'lft' + 2 WHERE 'lft' >= " $max_rgt;
$upd2 ="UPDATE '#__usergroups' SET 'rgt' = 'rgt' + 2 WHERE 'rgt' >= " $max_rgt;


// Insert values.
$values = array($parent_id, $new_lft, $new_rgt, $title );


// Prepare the insert query.
$query
->insert($db->quoteName('usergroups'))->columns($db->quoteName($columns))->values(implode(',', $db->quote($values)));

// Reset the query using our newly populated query object.
$db->setQuery($query);

try{// Execute the query
$result = $db->execute();//use $db->query() in Joomla2.5}catch(Exception $e){// catch any database errors.}
$newid =(int)$db->insertid();//get new record id
 
You'd be better off using the J! JTable class, and doing the normal J! style bind(), check() and store().

Code:
$groupTitle = $formModel->formData['user_registration_form___companyid'];
if (!empty($groupTitle)) {
   var $usergroupTable = new JTable::getInstance('UserGroup', 'JTable');
   $data = array(
      'id' => 0,
      'title' => $groupTitle,
      'parent_id' => '2',
      'tags' => null
   );
   if (!$usergroupTable->bind($data)) {
      return false;
   }
   if (!$usergroupTable->check()) {
      return false;
   }
   if (!$usergroupTable->store()) {
      return false;
   }
}

You'd probably want to add some more feedback, but that's the gist of it.

Basically, let J! do the table management. Doing the store() will handle rebuilding the nested set tree.

-- hugh
 
Hugh, thank you for the feedback.

Tried to make some changes but it is not working. Is it necessary to add something.

$groupTitle = $formModel->formData('testgroup___companyid');


if (!empty($groupTitle)) {
var $usergroupTable = new JTable::getInstance('UserGroup', 'jos_usergroups');
$data = array(
'id' => max(id)+1,
'title' => $groupTitle,
'parent_id' => '2'
'lft' => max(lft)+2,
'rgt' => max(rgt)+2,

);
if (!$usergroupTable->bind($data)) {
return false;
}
if (!$usergroupTable->check()) {
return false;
}
if (!$usergroupTable->store()) {
return false;
}
}
 
Basically, let J! do the table management. Doing the store() will handle rebuilding the nested set tree.
Did you try exactly Hugh's code?
Code:
   var $usergroupTable = new JTable::getInstance('UserGroup', 'JTable');
   $data = array(
      'id' => 0,
      'title' => $groupTitle,
      'parent_id' => '2',
      'tags' => null
   );
instead of
Code:
var $usergroupTable = new JTable::getInstance('UserGroup', 'jos_usergroups');
$data = array(
'id' => max(id)+1,
'title' => $groupTitle,
'parent_id' => '2'
'lft' => max(lft)+2,
'rgt' => max(rgt)+2,

);
I think id, lft and rgt are handled by Joomla.
 
I tried all cases but in vain.

May be I need to initialize something to use JTable.

Also I tried this code below in phpmyadmin this working and would like to make some dynamic code which I can put in php plugin and read values from fabrik form.

Code:
SET @parent_name = 'Public';

SET @new_name = 'NewGroup

SELECT @ins_id := id, @ins_lft := lft, @ins_rgt := rgt
            FROM jos_usergroups
            WHERE parent_id = @parent_name;

SELECT @new_id := MAX(id) + 1 FROM jos_usergroups;

               UPDATE jos_usergroups SET rgt=rgt+2 WHERE rgt>=@ins_rgt;
               UPDATE jos_usergroups SET lft=lft+2 WHERE lft>@ins_rgt;
  
INSERT INTO jos_usergroups (id,parent_id,title,lft,rgt)

VALUES (@new_id,@ins_id,@new_name,@ins_rgt,@ins_rgt+1);
 
Ah, do
Code:
  $usergroupTable = JTable::getInstance('UserGroup', 'JTable');
   $data = array(
      'id' => 0,
      'title' => $groupTitle,
      'parent_id' => '2',
      'tags' => null
   );
 
Troester, thanks for corrections, but it is still not working, is it necessary to retrieve this constant?

JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');

How it is possible to check all pre-requisities to be sure that JTable will work.
 
Ooops, my bad, I wrote that off the top of my head, and accidentally got JS and PHP syntax mixed up, so yes, Troester's correction is right.

I just tested the code, and it works.

As I said, you'll probably want to add some feedback, so instead of just returning false, do ...

Code:
$groupTitle = $formModel->formData['user_registration_form___companyid'];
if (!empty($groupTitle)) {
   $app = JFactory::getApplication();
   $usergroupTable = JTable::getInstance('UserGroup', 'JTable');
   $data = array(
      'id' => 0,
      'title' => $groupTitle,
      'parent_id' => '2',
      'tags' => null
   );
   if (!$usergroupTable->bind($data)) {
      $app->enqueueMessage("Bind failed: " . $usergroupTable->getError());
      return false;
   }
   if (!$usergroupTable->check()) {
      $app->enqueueMessage("Check failed: " . $usergroupTable->getError());
      return false;
   }
   if (!$usergroupTable->store()) {
      $app->enqueueMessage("Store failed: " . $usergroupTable->getError());
      return false;
   }
}

-- hugh
 
When do you run your php plugin (onXX)?
Maybe you have to use $groupTitle = $formModel->formData['companyid']; (without table name)

I just tested, it's working fine here (on BeforeStore, without table name)
Code:
$app = JFactory::getApplication();
$groupTitle = $formModel->formData['name'];
if (!empty($groupTitle)) {
   $usergroupTable = JTable::getInstance('UserGroup', 'JTable');

   $mydata = array(
      'id' => 0,
      'title' => $groupTitle,
      'parent_id' => '2',
      'tags' => null
   );
   if (!$usergroupTable->bind($mydata)) {
    $app->enqueueMessage("Bind failed: " . $usergroupTable->getError());
      return false;
   }
   if (!$usergroupTable->check()) {
      $app->enqueueMessage("check failed: " . $usergroupTable->getError());
      return false;
   }
   if (!$usergroupTable->store()) {
      $app->enqueueMessage("store failed: " . $usergroupTable->getError());
      return false;
   }
    $app->enqueueMessage("group created " . $groupTitle);
    return true;
}
$app->enqueueMessage("no empty group created");
For testing form plugins you can enable Joomla System debugging, Fabrik debug and then append &fabrikdebug=2 to your form URL.
This way you'll see syntax errors, debug output etc because the form is not redirecting.
 
Yes, it is working on the Dev environment. I have put it on the Test and got error message "could not alter user group to 1234567 as you are not assigned to that group".
 
Changed Parent_id to Public group`s ID and it was created. But I need to add it to Registered.
And need to know how to add user to created group.
 
Are you also running the juser form plugin? That's where that error message is probably coming from - either our juser plugin, or J!'s user management code.

The problem being that you can't add a user to a group that user is not authorized for.

You would usually work round this with the "Group whitelist" in the juser plugin settings, but if you are trying to add the user to a group you created on the fly, I'm not sure that would work.

-- hugh
 
Yes, I am using juser form plugin while this process.

The scenario which I would like to realize is:

While registration User put the name of the company and this value becomes the name of the group.

The next step is to assign user to the group which was created.

I would like to be sure when this approach will work.

I found that it is possible to catch the session of the user and assign to the group.


http://fabrikar.com/forums/index.php?threads/change-joomla-user-group.38461/#post-193420

This code from the link:

PHP:
// Get the connected user
$user = JFactory::getUser();
// Add the user in the group ID = x
JUserHelper::addUserToGroup($user->get('id'), x );
// reload the user object so the user is connected with its new access rights
$session = JFactory::getSession();
$session->set('user', new JUser($user->get('id')));
 
That may be the way you need to do it. As I said in my last response, the juser plugin simply won't let you add a user to a group they are not authorized for - it's a security issue, see the tooltip on the "User group field" in the juser plugin settings , so people can't spoof that data on a form submission and add themselves to (say) Super Admins. I suspect that trying to add them to a group you are creating on the fly isn't going to play nicely with that, even tho we provide a whitelisting mechanism. Even if they are authorized for the "parent" group of the new group, I suspect J! won't pick up any changes you make during processing, as it will have cached the group / auth levels prior to your code running.

So yes, adding an onAfterProcess plugin which manually adds them to that group may be what you need to do.

We've about reached the limit of Standard support on this. We don't usually provide this kind of custom coding support in Standard, you should consider getting a Pro sub if you need more assistance.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top