--- D:/Documents/Graphisme/Perso/Projects/Drupal Dev/sites/all/modules/autoassignrole/autoassignrole_ori.module	dim. nov. 14 21:25:32 2010
+++ D:/Documents/Graphisme/Perso/Projects/Drupal Dev/sites/all/modules/autoassignrole/autoassignrole.module	sam. janv. 22 21:24:26 2011
@@ -1,83 +1,287 @@
-<?php
-// $Id: autoassignrole.module,v 1.22 2010/11/14 21:25:32 cyberswat Exp $
-
-/**
- * @file
- *
- * The main autoassignrole.module file
- *
- * Designate a role to assign all new users to in addition to providing a
- * mechanism for new users to select a role or role for their account.
- */
-
-/**
- * Implements hook_permission().
- */
-function autoassignrole_permission() {
-  return array(
-    'administer autoassignrole' =>  array(
-      'title' => t('Administer auto assign role'),
-      'description' => t('Perform administration tasks for auto assign role.'),
-    ),
-  );
-}
-
-/**
- * Implements hook_menu().
- *
- * @return array
- */
-function autoassignrole_menu() {
-  $items = array();
-  $items['admin/config/autoassignrole'] = array(
-    'title' => 'Auto assign role',
-    'description' => 'Configure auto role assignment.',
-    'access arguments' => array('administer autoassignrole'),
-    '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.',
-    'access arguments' => array('administer autoassignrole'),
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('autoassignrole_auto_settings'),
-    'file' => 'autoassignrole.admin.inc',
-  );
-  $items['admin/config/autoassignrole/user'] = array(
-    'title' => 'Allow user to choose',
-    'description' => 'Configure the ability for an end user to choose their own roles.',
-    'access arguments' => array('administer autoassignrole'),
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('autoassignrole_user_settings'),
-    'file' => 'autoassignrole.admin.inc',
-  );
-
-  return $items;
-}
-
-/**
- * Implements hook_user_presave().
- */
-function autoassignrole_user_presave(&$edit, $account, $category) {
-  // Use case http://drupal.org/node/971622
-  // Make sure we only assign roles automatically when enabled to do so.
-  if (variable_get('autoassignrole_auto_active', 0)) {
-
-    // Use case http://drupal.org/node/944864
-    // Only assign roles if this is a new account.
-    if (isset ($account->is_new) && !empty($account->is_new)) {
-      // Get the existing user roles with the exception of the anonymous role.
-      $user_roles = user_roles(TRUE);
-      $auto_roles = variable_get('autoassignrole_auto_roles', array());
-      // Add autoassignrole_auto_roles to the $edit['roles'] array if they are
-      // valid.
-      foreach ($auto_roles as $rid) {
-        // Dealing with checkbox values so anything greater than zero is valid
-        if (!empty($rid) && array_key_exists($rid, $user_roles)) {
-          $edit['roles'][$rid] = $user_roles[$rid];
-        }
-      }
-    }
-  }
-}
+<?php
+// $Id: autoassignrole.module,v 1.22 2010/11/14 21:25:32 cyberswat Exp $
+
+/**
+ * @file
+ *
+ * The main autoassignrole.module file
+ *
+ * Designate a role to assign all new users to in addition to providing a
+ * mechanism for new users to select a role or role for their account.
+ */
+
+/**
+ * Implements hook_permission().
+ */
+function autoassignrole_permission() {
+  return array(
+    'administer autoassignrole' =>  array(
+      'title' => t('Administer auto assign role'),
+      'description' => t('Perform administration tasks for auto assign role.'),
+    ),
+  );
+}
+
+/**
+ * Implements hook_menu().
+ *
+ * @return array
+ */
+function autoassignrole_menu() {
+  $items = array();
+  $items['admin/config/autoassignrole'] = array(
+    'title' => 'Auto assign role',
+    'description' => 'Configure auto role assignment.',
+    'access arguments' => array('administer autoassignrole'),
+    '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.',
+    'access arguments' => array('administer autoassignrole'),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('autoassignrole_auto_settings'),
+    'file' => 'autoassignrole.admin.inc',
+  );
+  $items['admin/config/autoassignrole/user'] = array(
+    'title' => 'Allow user to choose',
+    'description' => 'Configure the ability for an end user to choose their own roles.',
+    'access arguments' => array('administer autoassignrole'),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('autoassignrole_user_settings'),
+    'file' => 'autoassignrole.admin.inc',
+  );
+
+  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:
+		 if($is_multiple){
+			$stype = 'checkboxes';
+		 }else{
+			 $stype = 'radios';
+		 }
+	 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 !
+		  $form['aar_fieldset']["#disabled"] = TRUE;
+		  drupal_set_message("Default value : $default_roleid");
+		  $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';
+  if(array_key_exists("aar_user_roles",$form_state["input"])){
+	  if(!is_array($form_state["input"]["aar_user_roles"])){
+		  drupal_set_message("Only value :".$form_state["input"]["aar_user_roles"]);
+	  }else{
+	  	_aar_debug_show_recursive_array($form_state["input"]["aar_user_roles"]);
+	  }
+  }
+}
+
+
+function autoassignrole_register_submit_handler(&$form, &$form_state){
+	/*autoassignrole_form_submit_cleanup($form, $form_state);
+	
+   if (isset($form_state['user']->uid)) {
+	  
+  }else{
+	  drupal_set_message("Problem retrieving newly created user id");
+  }*/
+
+  drupal_set_message("user id : ".$form_state['user']->uid);
+  $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));
+}
+
+
+/*
+* Useful for debugging variable containing arrays
+* Option maxlevel to limit the recursion if data is too complex
+*/
+function _aar_debug_show_recursive_array($array,$maxlevel=3,$baselevel=0){
+	$a = $array;
+	$level = $baselevel;
+	foreach($a as $k=>$v){
+		$fleche = "";
+		for($i = 0;$i<=$level;$i++){
+			$fleche .= "---";
+		}
+		$fleche .= ">";
+		drupal_set_message("$fleche $k : $v");
+		
+		if(is_array($v) && $level <= $maxlevel){
+			showExpandedArray($v,$maxlevel,$level+1);
+		}
+	}
+}
+
+/*
+* 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().
+ */
+function autoassignrole_user_presave(&$edit, $account, $category) {
+  // Use case http://drupal.org/node/971622
+  // Make sure we only assign roles automatically when enabled to do so.
+  if (variable_get('autoassignrole_auto_active', 0)) {
+
+    // Use case http://drupal.org/node/944864
+    // Only assign roles if this is a new account.
+    if (isset ($account->is_new) && !empty($account->is_new)) {
+      // Get the existing user roles with the exception of the anonymous role.
+      $user_roles = user_roles(TRUE);
+      $auto_roles = variable_get('autoassignrole_auto_roles', array());
+      // Add autoassignrole_auto_roles to the $edit['roles'] array if they are
+      // valid.
+      foreach ($auto_roles as $rid) {
+        // Dealing with checkbox values so anything greater than zero is valid
+        if (!empty($rid) && array_key_exists($rid, $user_roles)) {
+          $edit['roles'][$rid] = $user_roles[$rid];
+        }
+      }
+    }
+  }
+}
