Assign users to group from select on registration form
Assign to group from select on registration form
You can assign new users to groups depending on the value of a select-element on the registration form. FUA does not generate this select on your registration form. There are several registration-components for Joomla which have options to add more then the default registration fields to the registration form. Or you can use a template-override to add an extra field to the registration form. When during registration, there is no match, or nothing is selected, the user will be assigned to configured 'default usergroup' (if set). You can create a select-element on the form and make FUA read the value of the field during registration and thus assign the new user to one or more groups. Enter the select-elements name, and its conditional values as shown in this example:
Select on registration form:
HTML code of the select element:
<select name="cb_relationshiptocamp"> <option value="camper">camper</option> <option value="parent">parent</option> <option value="staff">staff</option> <option value="other">other</option> </select>
PHP code to assign the user to a group based on users selection:
$value = JRequest::getVar('cb_relationshiptocamp', ''); if($value=="camper"){ $groups = '12'; }elseif($value=="parent"){ $groups = '13'; }elseif($value=="staff"){ $groups = '14'; }
First the value from the form-element is gathered, then a condition of each combination is tested.
In the above code == means 'is equal to'.
The syntax of the code is PHP.
Add extra field on Joomla registration form
How to make the template override
- In templates/your_template/html/ create a folder 'com_users' (if it does not already exist).
- Inside that folder 'com_users' create a folder 'registration' (if it does not already exist).
- Inside folder templates/your_template/html/com_users/registration/ check if file 'default.php' exists. If not, copy-paste the file there from components/com_users/views/registration/tmpl/default.php.
file: templates/your_template/html/com_users/registration/default.php
line: 43
<?php endforeach;?>
replace with
<?php endforeach;?> <dt> extra field </dt> <dd> <select name="cb_relationshiptocamp"> <option value="camper">camper</option> <option value="parent">parent</option> <option value="staff">staff</option> <option value="other">other</option> </select> </dd>
how to add an extra field to the edit-profile form
Assigning users to more then one usergroup at registration
To assign users to more then one group at registration based on their selection on the registration form, check this code:
$value = JRequest::getVar('cb_relationshiptocamp', ''); if($value=="camper"){ $groups = '12,45,67'; }elseif($value=="parent"){ $groups = '13,67'; }elseif($value=="staff"){ $groups = '14'; }
Reading the select when elements are parsed as an array
When all or some of the form-elements are parsed as an array, this is how to get the value of the field:
$form_elements = JRequest::getVar('form', null, 'post', 'array'); $value = $form_elements['group'];
In this example, the array of form-elements is called 'form'.
HTML:
aaa bbb ccc
Assigning groups with a multi-select element
When using a multiselect to assign users to groups, use code like this:
$values = JRequest::getVar('cb_multi', null, 'post', 'array'); foreach($values as $value){ if($value=="old camper"){ $groups .= '12,'; }elseif($value=="parent"){ $groups .= '13,'; }elseif($value=="staff"){ $groups .= '14,'; } }
Assign users from more then one form elements
If you are using more then one form elements together to assign users to groups, use the code like this:
$gender = JRequest::getVar('gender', ''); $team = JRequest::getVar('team', ''); if($gender=="man" && $team=="team1"){ $groups = '12'; }elseif($gender=="man" && $team=="team2"){ $groups = '13'; }elseif($gender=="woman" && $team=="team1"){ $groups = '14'; }elseif($gender=="woman" && $team=="team2"){ $groups = '15'; }
First the 2 values from the form-elements are gathered, then a condition of each combination is tested.
In the code && means AND. So the first condition would read:
if the gender is 'man' AND the team is 'team1' THEN assign to group 12.
You can also use || which means OR.
Making a difference when assigning from frontend or backend
When you want to assign groups differently when the profile is editted from the:
- frontend (user edits profile)
- backend (administrator edits a users profile)
$app = JFactory::getApplication(); $club = JRequest::getVar('club', ''); if($app->isAdmin()){ //backend if($club=="Olveston"){ $groups = '13'; }else{ //default $groups = '12'; } }else{ //frontend if($club=="Olveston"){ $groups = '14'; }else{ //default $groups = '12'; } }
Using the database
In some situations you might need to get data from the database.
In this example the form-element 'paid' was configured to only be used in the backend for administrators. On the frontend this field is not there, so we need to get the data straight from the database.
$app = JFactory::getApplication(); $club = JRequest::getVar('club', ''); $paid = "no"; if($app->isAdmin()){ //backend $paid = JRequest::getVar('paid', ''); }else{ //frontend $database = JFactory::getDBO(); //get $paid from database $database->setQuery("SELECT cb_paid FROM #__comprofiler WHERE id='$user_id' LIMIT 1"); $rows = $database->loadObjectList(); foreach($rows as $row){ $paid = $row->cb_paid; } } if($club=="Olveston" && $paid=="yes"){ $groups = '13,14'; }elseif($club=="Olveston" && $paid=="no"){ $groups = '13'; }else{ //default $groups = '12'; }
When the userprofile is editted, assign to the current group(s)
When the user-profile is editted, assign the user to the groups the user is already assigned to like this:
$club = JRequest::getVar('club', ''); if($club=='Olveston'){ $groups = '18'; }else{ $groups = $current_groups; }
note that $current_groups can only be used in the 'code to be used when processing update form'-field. When used in the field above that (field 'code to be used when processing registration form') the user is not assigned to any group yet, so there are no current groups and the user will not be assigned to any groups at all. So only use $current_groups in the 'code to be used when processing update form'-field.
Add extra field on Joomla profile edit form
How to make the template override
- In templates/your_template/html/ create a folder 'com_users' (if it does not already exist).
- Inside that folder 'com_users' create a folder 'profile' (if it does not already exist).
- Inside folder templates/your_template/html/com_users/proflie/ check if file 'edit.php' exists. If not, copy-paste the file there from components/com_users/views/profile/tmpl/edit.php.
Adding a extra field to the profile edit form in the template override
file: templates/your_template/html/com_users/profile/edit.php
line: 49
<?php endforeach;?>
replace with
<?php endforeach;?> <fieldset> <dt> extra field </dt> <dd> <select name="cb_relationshiptocamp"> <option value="camper">camper</option> <option value="parent">parent</option> <option value="staff">staff</option> <option value="other">other</option> </select> </dd> </fieldset>
Obviously, on the edit page you would want to display the form elements selection according to whatever value the user has. This depends entirely on what data you are using for that, and how you stored it, so I can not give examples for that. You can use PHP here to get data from the database if needed to show the users selection.
Show the extra field in the profile view
Inside folder templates/your_template/html/com_users/proflie/ check if file 'default_core.php' exists. If not, copy-paste the file there from components/com_users/views/profile/tmpl/default_core.php.
file: templates/your_template/html/com_users/profile/default_core.php
line: 57
</dl>
replace with
<dt> extra field </dt> <dd> field value </dd> </dl>
Obviously, if you would want to display the value according to whatever value the user has, it depends entirely on what data you are using for that, and how you stored it, so I can not give examples for that. You can use PHP here to get data from the database if needed to show the users selection.
Add on profile edit page selector with FUA groups
- Create a template override as described above.
- file: templates/your_template/html/com_users/profile/edit.php
line: 49<?php endforeach;?>
replace with<?php endforeach;?> <fieldset> <dt> group(s) </dt> <dd> <?php //get database $db = JFactory::getDBO(); //get user $user =& JFactory::getUser(); $user_id = $user->get('id'); //get groups $query = $db->getQuery(true); $query->select('id, name'); $query->from('#__fua_usergroups'); $query->where('id<>9'); $query->where('id<>10'); $query->order('ordering'); $fua_groups = $db->setQuery($query); $fua_groups = $db->loadObjectList(); //get users groups $query = $db->getQuery(true); $query->select('group_id'); $query->from('#__fua_userindex'); $query->where('user_id='.$user_id); $users_groups = $db->setQuery($query, 0, 1); $users_groups = $db->loadObjectList(); //make users groups array $users_groups_array = array(); foreach($users_groups as $users_group){ $temp = $users_group->group_id; $temp = str_replace('"', '', $temp); $users_groups_array = explode(',', $temp); } //make select element echo '<select name="fua_usergroups[]" multiple="multiple" size="7">'; foreach($fua_groups as $fua_group){ echo '<option value="'.$fua_group->id.'"'; if(in_array($fua_group->id, $users_groups_array)){ echo ' selected="selected"'; } echo '>'.htmlspecialchars($fua_group->name).'</option>'; } echo '</select>'; ?> </dd> </fieldset>
- Go to the FUA configuration > tab 'users'.
- Select at 'assign to group from select on registration form' the checkbox 'enable'.
- Paste this code in the second textarea:
$values = JRequest::getVar('fua_usergroups', null, 'post', 'array'); foreach($values as $value){ $groups .= $value.','; }
- Click 'save'.