? autoassignrole-983612.patch
Index: autoassignrole.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/autoassignrole/autoassignrole.admin.inc,v
retrieving revision 1.11
diff -u -p -r1.11 autoassignrole.admin.inc
--- autoassignrole.admin.inc	14 Nov 2010 16:44:51 -0000	1.11
+++ autoassignrole.admin.inc	26 Jan 2011 23:12:31 -0000
@@ -120,7 +120,7 @@ function autoassignrole_user_settings() 
     '#title' => t('Selection method'),
     '#default_value' => variable_get('autoassignrole_user_selection', 0),
     '#description' => t('The type of form elements the end user will be presented with.'),
-    '#options' => array(0 => t('Radio Buttons'), 1 => t('Selection Box'), 2 => t('Check boxes')),
+    '#options' => array(0 => t('Radio Buttons / Checkboxes (depending on the choice above)'), 1 => t('Selection Box')),
   );
 
   $form['autoassignrole_user_required'] = array(
@@ -134,7 +134,7 @@ function autoassignrole_user_settings() 
   $form['autoassignrole_user_sort'] = array(
     '#type' => 'radios',
     '#title' => t('Sorting'),
-    '#default_value' => variable_get('autoassignrole_user_selection', 'SORT_ASC'),
+    '#default_value' => variable_get('autoassignrole_user_sort', 'SORT_ASC'),
     '#description' => t('Default sort order of roles the user will see.'),
     '#options' => array(
       'SORT_ASC' => t('Ascending'),
@@ -168,6 +168,19 @@ function autoassignrole_user_settings() 
     '#maxlength' => 128,
     '#required' => FALSE,
   );
+  
+   $form['autoassignrole_show_roles_on_auto_choose'] = array(
+    '#type' => 'radios',
+    '#title' => t('Display roles field when disabled'),
+    '#default_value' => variable_get('autoassignrole_show_roles_on_auto_choose', t('Show')),
+    '#description' => t('You can pass the role you want in the url, like user/register/myrole.
+That way, the registration form will automatically select this role and the role field will be greyed out.
+This option lets you choose whether the roles field is shown when a role argument is passed in the URL'),
+    '#options' => array(
+      1 => t('Show'),
+      0 => t('Hide'),
+    ),
+  );
 
   return system_settings_form($form);
 }
Index: autoassignrole.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/autoassignrole/autoassignrole.module,v
retrieving revision 1.22
diff -u -p -r1.22 autoassignrole.module
--- autoassignrole.module	14 Nov 2010 21:25:32 -0000	1.22
+++ autoassignrole.module	26 Jan 2011 23:12:31 -0000
@@ -36,6 +36,7 @@ function autoassignrole_menu() {
     'page callback' => 'autoassignrole_admin_block_page',
     'file' => 'autoassignrole.admin.inc',
   );
+  
   $items['admin/config/autoassignrole/auto'] = array(
     'title' => 'Auto role assignment',
     'description' => 'Configure which role(s) are automatically assigned on user creation.',
@@ -56,6 +57,174 @@ function autoassignrole_menu() {
   return $items;
 }
 
+/*
+* Implements hook_form_FORM_ID_form_alter()
+*
+* Adds a "Choose your role" radio field 
+*/
+
+function autoassignrole_form_user_register_form_alter(&$form, &$form_state) {
+  if (!variable_get("autoassignrole_user_active", 0)) {
+    return;
+  }
+
+  $allowed_roles = variable_get("autoassignrole_user_roles", array());
+  $roles = user_roles(TRUE);
+  $roles_names = array();
+  $revert_roles = array();
+  
+  foreach ($allowed_roles as $key => $role) {
+    if ($role) {
+      $roles_names[$role] = $roles[$key];
+      $revert_roles[$roles[$key]] = $role;
+    }
+  }
+  
+  switch (variable_get('autoassignrole_user_sort', 'SORT_ASC')) {
+    case 'SORC_ASC':
+      asort($roles_names);
+    break;
+
+    case 'SORT_DESC':
+      arsort($roles_names);
+    break;
+  }
+    
+  $is_multiple = variable_get('autoassignrole_user_multiple', 0);
+  $selection_type = variable_get('autoassignrole_user_selection', 0);
+  
+  
+  switch ($selection_type) {
+    case 1:
+      $stype = 'select';
+      // Multiple select doesn't work for the time being, forcing checkboxes if multiple
+      if ($is_multiple) {
+        $stype = 'checkboxes';
+      }
+    break;
+  
+    case 0:
+    default:
+      $stype = 'radios';
+      if ($is_multiple) {
+        $stype = 'checkboxes';
+      }
+    break;
+  }
+  
+  $form['aar_fieldset'] = array(
+        '#type' => 'fieldset',
+        '#title' => variable_get('autoassignrole_user_fieldset_title', t('User Roles')),
+        '#weight' => '0',
+        );
+        
+  $form['aar_fieldset']['aar_user_roles'] = array(
+        '#type' => $stype,
+        '#options' => $roles_names,
+        '#title' => variable_get('autoassignrole_user_title', t('Role')),
+        '#required' => variable_get('autoassignrole_user_required', 0),
+        '#multiple' => $is_multiple,
+        '#description' => variable_get('autoassignrole_user_description', t('Select a role')),
+        );
+  
+  if (arg(2)) {
+      $default_role = arg(2);
+      if (array_key_exists($default_role, $revert_roles)) {
+          $default_roleid = $revert_roles[$default_role]; // Verify if index exists !
+          
+          $show_on_url = variable_get('autoassignrole_show_roles_on_auto_choose', 0);
+      
+          if ($show_on_url == 0) {
+            $form['aar_fieldset']['#attributes'] = array('style' => 'display:none;');
+          }
+          
+          $form['aar_fieldset']['#disabled'] = TRUE;
+          $form['aar_fieldset']['aar_user_roles']['#default_value'] = $default_roleid;
+      }
+  }
+  
+  $perms = user_role_permissions($roles_names);
+  
+  
+  if (module_exists("profile2")) {
+    foreach (profile2_get_types() as $type_name => $profile_type) {
+      $type_name = check_plain($profile_type->type);
+      $roleconditions = get_role_id_for_profile_type($type_name, $perms, $roles_names);
+      
+      if (!$is_multiple) {
+        $visiblecondition = array( ':input[name="aar_user_roles"]' => $roleconditions);
+      }
+      else {
+        $visiblecondition = array();
+        foreach ($roleconditions as $key => $cond) {
+          $c = array();
+          switch ($stype) {
+            case "checkboxes":
+              $c = array(':input[name="aar_user_roles[' . $cond["value"] . ']"]' => array('checked' => TRUE));
+            break;
+            case "select":
+              //Doesn't work for the time being (can't find how to access multiple selected options)
+              $c = array(':input[name="aar_user_roles[]"]' => array("value" => array($cond["value"])));
+            break;
+          }
+          array_push($visiblecondition, $c);
+        }
+      }
+  
+      if (sizeof($roleconditions) > 0) {        
+        $form['profile_' . $type_name]['#states'] = array('visible' => $visiblecondition);
+      }
+    }
+  }
+  
+  $form['#submit'][] = 'autoassignrole_register_submit_handler';
+  
+}
+
+function autoassignrole_register_submit_handler(&$form, &$form_state) {
+  
+  $roles = array();
+  $aar_roles = $form_state['input']['aar_user_roles'];
+  $user_roles = user_roles(TRUE);
+  if (!is_array($aar_roles)) {
+    $roles[$aar_roles] = $user_roles[$aar_roles];
+  }
+  else{
+    foreach ($aar_roles as $rid) {
+      if (!empty($rid) && array_key_exists($rid, $user_roles)) {
+        $roles[$rid] = $user_roles[$rid];
+      }
+    }
+  }
+  
+  $new_account = user_load($form_state['user']->uid);
+  user_save($new_account, array('roles' => $roles));
+}
+
+
+/*
+* Get roles id that have the permission edit own *profile* for a given profile.
+* This function is called in the user_register_form_alter above, to assign #states
+* to the right Profile 2 fields.
+*/
+
+function get_role_id_for_profile_type($ptype, $perms, $roles_names) {
+  
+  $conditions = array();
+  
+  foreach ($perms as $roleid => $v) {
+    $rolename = $roles_names[$roleid];
+    foreach ($v as $key => $val) {
+      if ($key == "edit own $ptype profile") {
+        array_push($conditions, array('value' => "$roleid"));
+      }
+    }
+  }
+  
+  return $conditions;
+}
+
+
 /**
  * Implements hook_user_presave().
  */
