As i've said point 6 of the above approach.
Some information first:
I know the code may be lacking something but you can improve it. F.e. new groups can be assigned by checbox and below code only after a "B" user had been registered.
In order for it to work we need to put this code below in FieldPrepareStore or onCCK_FieldPrepareStore place. It can be found as i remember in 42 field,
but i'havent tested it on this field rahter i created my own plugin called php_code and in it's file php_code.php used on onCCK_FieldPrepareStore function. I put some code there or used eval function for custom field i'had created. So different approaches are good. Whateve approach below code should work the same.
$_POST['parent_user_id'] - it is my way to be sure whether i am in the right form - maybe it is not necessary
$_POST['user_groups'][0]=="18" - not necessary too - it is another way for me to determine if i am in a right form - you can drop this piece of code alongside with
$_POST['parent_user_id'] part.
Update: Watch out! If
$_POST['user_groups'] can be without
[0] when the corresponding field is
hidden not tree-like with checkboxes they are presented in the $_POST['user_groups'] variable as an array not string. Then it probably could be
$_POST['user_groups']="18" or
$_POST['user_groups']="18,19,20" or so
.If you're a beginner remove this part with 18 :)
19,17 - ids of groups B and B2 - you should have already known about from the previous posts what i mean. You of course can actually have your own ids (If you are more beginner than i am i wrote this).
You can use for your purposes only one group f.e
17 id and remove all
19 related parts
I used checkboxes but you can use f.e select field.
if (isset($_POST['id'])&&isset($_POST['parent_user_id'])&&$_POST['user_groups'][0]=="18") {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('user_id', 'group_id')));
$query->from($db->quoteName('#__user_usergroup_map'));
$query->where($db->quoteName('user_id') . ' = '. $_POST['id']);
$db->setQuery($query);
$row = $db->loadRowList();
if (isset($_POST['checkbox_one'][0])&& $_POST['checkbox_one'][0] == "yes") {
$assign_to_group_1=true;
} else {
$assign_to_group_1=false;
}
if (isset($_POST['checkbox_two'][0])&& $_POST['checkbox_two'][0] == "yes") {
$assign_to_group_2=true;
} else {
$assign_to_group_2=false;
}
$is_group_1_already_assigned=false;
for ($i=0;$i if ($row[$i][1]=="17") $is_group_1_already_assigned=true;
}
$is_group_2_already_assigned=false;
for ($i=0;$i if ($row[$i][1]=="19") $is_group_2_already_assigned=true;
}
if ($assign_to_group_1===true&&$is_group_1_already_assigned===false) {
$query = $db->getQuery(true);
$columns = array('user_id', 'group_id');
$values = array($_POST['id'], 17);
$query
->insert($db->quoteName('#__user_usergroup_map'))
->columns($db->quoteName($columns))
->values(implode(',', $values));
$db->setQuery($query);
$db->query();
}
if ($assign_to_group_1===false&&$is_group_1_already_assigned===true) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$conditions = array(
$db->quoteName('user_id') . '='.$_POST['id'],
$db->quoteName('group_id') . '=17'
);
$query->delete($db->quoteName('#__user_usergroup_map'));
$query->where($conditions);
$db->setQuery($query);
$result = $db->query();
}
if ($assign_to_group_2===true&&$is_group_2_already_assigned===false) {
$query = $db->getQuery(true);
$columns = array('user_id', 'group_id');
$values = array($_POST['id'], 19);
$query
->insert($db->quoteName('#__user_usergroup_map'))
->columns($db->quoteName($columns))
->values(implode(',', $values));
$db->setQuery($query);
$db->query();
}
if ($assign_to_group_2===false&&$is_group_2_already_assigned===true) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$conditions = array(
$db->quoteName('user_id') . '='.$_POST['id'],
$db->quoteName('group_id') . '=19'
);
$query->delete($db->quoteName('#__user_usergroup_map'));
$query->where($conditions);
$db->setQuery($query);
$result = $db->query();
}
}