Change Juser group by radio button options

vipzeus

Member
Hi.

I have a form where users can add (for first time) and edit the billing details.

On this form i have a radiogroup element with 2 options

1. User
2. Company

By default is 1. user and belong at the register joomla group.

What i need is when user choose the second option (2. company) to change the j! user group as company on the submit form.

Any suggest is welcome:)
 
I'd use a form plugin with some php code :

PHP:
jimport('joomla.user.helper');
$userid = JRequest::getInt('list___userid_element');
$groups = (array) JRequest::getVar('list__radio');
JUserHelper::setUserGroups($userId, $groups);

I've not tested that - the only thing that might be an issue is the the logged in user may have to already belong to the group you are settiing the $userId to.
In that case you would have to write your own sql query to manually set the user groups.


-Rob
 
Use php form plugin looks a good idea.
the only thing that might be an issue is the the logged in user may have to already belong to the group you are settiing the $userId to.
I think a advise message such " Your change will take effect at the next login" with condition at the radio element can be a solution.

I'm not familiar with php and what i have for the moment is:

jimport('joomla.user.helper');
$userid = JRequest::getInt('comdetails___userid');
$groups = (array) JRequest::getVar('comdetails___billingaccount');
JUserHelper::setUserGroups($userId, $groups);

How can UPDATE the xxxx_users___usertype column if value of comdetails___billingaccount = 1 or comdetails___billingaccount = 2 ?
 
hi I general to update a fabrik element you do (this is taken from http://fabrikar.com/wiki/index.php/Form_plugin_php):

Code:
[COLOR=#000088]$formModel[/COLOR][COLOR=#339933]->[/COLOR][COLOR=#004000]updateFormData[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]'[/COLOR][COLOR=#0000ff]xxxx_users___usertype column'[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#000000][COLOR=#0000BB]$groups[/COLOR][/COLOR][COLOR=#339933],[/COLOR] [COLOR=#000000][B]true[/B][/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]   [COLOR=#666666][I]

//to update data in a join (with id = 1) use this syntax:[/I][/COLOR] [COLOR=#000088]
$formModel[/COLOR][COLOR=#339933]->[/COLOR][COLOR=#004000]updateFormData[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]"join.1.[/COLOR][COLOR=#0000ff]xxxx_users___usertype column"[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#000000][COLOR=#0000BB]$groups[/COLOR][/COLOR][COLOR=#339933],[/COLOR] [COLOR=#000000][B]true[/B][/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR]

However please note that #__users.usertype is deprecated and not used in Joomla 2.5, all the user's group information is stored in #__usergroups.
 
Thank you very much Rob for your note.
However please note that #__users.usertype is deprecated and not used in Joomla 2.5, all the user's group information is stored in #__usergroups.
I correct you here about the right table is stored in #user_usergroup_map.
#_usergroups store the new groups created from joomla's backend.

After some experiments i note #_usergroup_map add, update, delete users group assignment.
Infact in joomla 1.6 - 1.7 - 2.5 one user may belong on more usergroups.
Thats meen my request for the php form plugin is more complicate.

in the #_usergroup_map we have 2 columns.
user_id (Foreign Key to #__user.id)
group_id (Foreign Key to #__group.id)

All registered users by default belong in the second group n.2 (Registered)

In my fabrikForm i have the follows elements:
comdetails___userid (Hidden) user element
comdetails___billingaccount radiogroup element where user is able to choose the options
Options:
1 User (Default) => usegroup_map = 2
2 Company => usergroup_map = 10

If user choose the second option (2 Company) then i have to create 1 query.

Something as:
If radiogroup has value (2) then UPDATE in #_user_usergroup_map the group_id with 10 filtred bu userid.

I know the syntax is not a php syntax.
I need help with this please.
 
perhaps something like this:

PHP:
jimport('joomla.user.helper');
$radio = (int) JRequest::getVar('comdetails___billingaccount');
$userid = (int) JRequest::getVar('comdetails___userid');

if ($radio === 2)
{
  $groups = JUserHelper::getUserGroups($userid);
  $groups[] = 10;
  JUserHelper::setUserGroups($userId, $groups);  
}
</span></span>
 
After submit the form with the option 2 (Company) i have the follow error:

JUser: :_load: Unable to load user with ID: 1
 
Does it actually modify the group membership correctly?

What page is it redirecting to after submission? The list?

-- hugh
 
its probably quickest if I take a look
Can you pm me an admin login, and tell me which form this is form?

-rob
 
Your first element is a radio list, so it posts data as an array, so I changed the plugin code to:

Code:
jimport('joomla.user.helper');
$radio = (array) JRequest::getVar('comdetails___billingaccount');
$radio = $radio[0];
$userid = (int) JRequest::getVar('comdetails___userid');

if ($radio === 2)
{
  $groups = JUserHelper::getUserGroups($userid);
  $groups[] = 10;
  JUserHelper::setUserGroups($userId, $groups);  
}

I don't see the error message with that
 
hmm this took a while to work out!:

Code:
$radio = (array) JRequest::getVar('comdetails___billingaccount');
$radio = (int) $radio[0];
$userid =  JRequest::getVar('comdetails___userid');
$userid = (int) $userid[0];
if ($radio === 2)
{
    $db = JFactory::getDbo();
    $sql = "INSERT INTO #__user_usergroup_map (user_id, group_id) VALUES ($userid, 10) ON DUPLICATE KEY UPDATE user_id = $userid";
    $db->setQuery($sql);
    $db->query();
}
For some reason I couldn't get the JUserHelper::setUserGroups() method to work. So I reverted to using a simple sql query. This may mean that users have to log out and in again though for the acl to be updated.
 
I test and looks work now without user have to log out.
Now user belong in 2 usergroups, what i need is to UPDATE the existing usergroup with new.
So, each user must assigned in one usergroup.

$sql = "INSERT INTO #__user_usergroup_map (user_id, group_id) VALUES ($userid, 10) ON DUPLICATE KEY UPDATE user_id = $userid";
Can i change INSERT INTO with UPDATE ?
 
I'm assuming you mean if you are setting the group to 10, you need to remove all other groupid's for that user. In which case, you'd want to add a second query:

PHP:
    $sql = "DELETE FROM #__user_usergroup_map WHERE user_id = $userid AND group_id != 10";
    $db->setQuery($sql);
    $db->query();

-- hugh
 
Hi Hugh.
This is work properly. Now i have One usergroup for each user.
I wonder if its simpliest to UPDATE the existing usergroup for each user.
Is it possible to do this with one query?
 
the ON DUPLICATE KEY part of my query will update the record for you OR insert a new one if no duplicate key found
 
Ok, Im document about this and your code with ON DUPLICATE KEY give good performance with minimal risk of errors. :)

Thank you very much.
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top