--- gigya.admin.inc?revision=1.1.2.15	2009-11-20 13:09:28.000000000 -0600
+++ gigya.admin.inc	2009-11-22 18:41:28.000000000 -0600
@@ -534,13 +534,12 @@ function gigya_admin_form() {
     );
   }
   
+  // Start filling the mapping part
+  $fields = array('--Not mapped--', 'username');
+  
   if (module_exists('profile')) {
-    $form['profile'] = array(
-      '#type' => 'fieldset',
-      '#title' => t('Gigya <=> Drupal Profile Mappings'),
-      '#collapsible' => TRUE,
-      '#collapsed' => FALSE,
-    );
+  
+    // Gather profile fields (profile module)
     if ($profile_categories = profile_categories()) {
       $profile_fields = array();
       $category_names = array();
@@ -550,12 +549,7 @@ function gigya_admin_form() {
         if ($profiles && $profiles[$category['name']]) {
           foreach ($profiles[$category['name']] as $field_name => $profile) {
             if (is_array($profile)) {
-              $form['profile']['gigya_profile_'. $field_name] = array(
-                '#type' => 'select',
-                '#title' => $profile['#title'],
-                '#options' => array_merge(array(0 => ''), _gigya_get_profile_fields()),
-                '#default_value' => variable_get('gigya_profile_'. $field_name, ''),
-              );
+              $fields[] = $field_name;
             }
           }
         }
@@ -565,11 +559,54 @@ function gigya_admin_form() {
       drupal_set_message(t("Although the profile module is enabled, You currently have no profile categories created"));
     }
   }
+
+  // Gather content profile fields
+  if (module_exists('content') && module_exists('content_profile')) {
+    $profile_types = content_profile_get_types('names');
+    if (!empty($profile_types)) {
+      $profile_type = key($profile_types);
+      $typeinfo = content_types($profile_type);
+      $fields = array_merge($fields, array_keys($typeinfo['fields']));
+    }
+  }
+    
+  if (!empty($fields)) {
+    // Build the mapping list
+    $form['bio_mapping'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Gigya <=> Drupal Profile Mappings'),
+      '#collapsible' => TRUE,
+      '#collapsed' => FALSE,
+    );
+
+    foreach (_gigya_get_profile_fields() as $field_key => $field_name) {
+      if ($field_name != 'email') {
+        $form['bio_mapping']['gigya_bio_'. $field_name] = array(
+          '#type' => 'select',
+          '#title' => $field_name,
+          '#options' => array_combine($fields, $fields),
+          '#default_value' => variable_get('gigya_bio_'. $field_name, ''),
+        );
+      }
+      if ($field_name == 'gender') {
+        $form['bio_mapping']['gigya_bio_gender'] = array_merge($form['bio_mapping']['gigya_bio_gender'], 
+        array('#description' => t('Gender CCK field must have keys "m" and "f". Labels can be whatever needed')));
+      }
+      if ($field_name == 'nickname') {
+        $form['bio_mapping']['gigya_bio_nickname'] = array_merge($form['bio_mapping']['gigya_bio_nickname'], 
+        array('#description' => t('You may want to map this to "username"')));
+      }
+      if (strpos($field_name, 'birth') !== false) {
+        $form['bio_mapping']['gigya_bio_'. $field_name] = array_merge($form['bio_mapping']['gigya_bio_'. $field_name], 
+        array('#description' => t('You may map month, day, year to the same date CCK field')));
+      }
+    }
+  }
   else {
-    $form['from'] = array(
+    $form['bio_mapping'] = array(
       '#type' => 'item',
-      '#title' => t('Profile Module Disabled'),
-      '#value' => t('If you wish to map profile settings to gigya settings, please enable the profile module'),
+      '#title' => t('Profile Modules Disabled'),
+      '#value' => t('If you wish to map profile settings to gigya settings, please enable the Profile module and/or the Content Profile module'),
     );
   }
   $form['#submit'][0] = 'gigya_admin_settings_form_submit';
--- gigya.module.orig	2009-11-17 17:10:20.000000000 -0600
+++ gigya.module	2009-11-22 16:33:02.000000000 -0600
@@ -538,6 +538,7 @@ function gigya_form_alter(&$form, $form_
     if (isset($bio['email'])) {
       $form['account']['mail']['#default_value'] = $bio['email'];
     }
+
     //link gigya login provider profile information to the drupal proivder, if the profile module is enabled
     if (module_exists('profile')) {
       if ($profile_categories = profile_categories()) {
@@ -547,9 +548,10 @@ function gigya_form_alter(&$form, $form_
           if ($profiles && $profiles[$category['name']]) {
             foreach($form[$cat_name] as $arr_name => $arr_val) {
               if (is_array($arr_val)) {
-                if (variable_get('gigya_profile_'. $arr_name, '') != '0') {
-                  $bio_assoc = variable_get('gigya_profile_'. $arr_name, '');
-                  $form[$cat_name][$arr_name]['#default_value'] = $bio[$bio_assoc];
+                foreach (_gigya_get_profile_fields() as $field_key => $field_name) {
+                  if ($arr_name == variable_get('gigya_bio_'. $field_name, '')) {
+                    $form[$cat_name][$arr_name]['#default_value'] = $bio[$field_name];
+                  }
                 }
               }
             }
@@ -557,6 +559,50 @@ function gigya_form_alter(&$form, $form_
         }
       }
     }
+
+    // TODO remove this dd
+    // dd('------Loop------');
+    foreach (_gigya_get_profile_fields() as $field_key => $field_name) {
+      if (!isset($bio[$field_name]))
+        continue; // skip empty values from XML call
+      $var_name = 'gigya_bio_'. $field_name;
+      $mapped_field = variable_get($var_name, '');
+      /* TODO remove these drupal debug 
+      dd('----Field item----'); 
+      dd('----var_name:'); dd($var_name); 
+      dd('----mapped_field:'); dd ('"' . $mapped_field . '"');
+      dd('----value:'); dd ($bio[$field_name]);
+      dd('END Field item----'); 
+      */
+      if (strpos($mapped_field, 'field_') === 0) {
+        if ($var_name == 'gigya_bio_birthMonth' || $var_name == 'gigya_bio_birthDay' || $var_name == 'gigya_bio_birthYear') {
+          // date parts go to a birthdate CCK field
+          $form[$mapped_field][0]['#default_value'] = array('month' => $bio['birthMonth'], 'day' => $bio['birthMonth'], 'year' => $bio['birthYear']);
+        }
+        else {
+          if (strpos($var_name, 'gigya_bio_gender') === 0) {
+            // Gender CCK field, must have keys 'm' and 'f'. Labels can be whatever needed.
+            $form[$mapped_field]['#default_value'][0]['value'] = $bio[$field_name];
+          }
+          else {
+            // Regular text CCK field
+            $form[$mapped_field][0]['#default_value']['value'] = $bio[$field_name];
+          }
+        }
+      }
+      elseif (strpos($mapped_field, 'username') === 0) {
+        // when nickname is mapped to username
+        $form['account']['name']['#default_value'] = $bio[$field_name];
+        // TODO remove this dd
+        // dd('username!'); dd($form['account']['name']);
+      }
+    }
+    // TODO remove this dd
+    // dd('------END Loop------');
+
+    // TODO remove this dd
+    dd('---Form!'); dd($form);
+
     unset($form['account']['pass']);
     unset($form['pass']);
     $form['mail']['#required'] = FALSE;
--- gigya.install?revision=1.1.2.13	2009-11-22 16:51:17.000000000 -0600
+++ gigya.install	2009-11-22 18:05:33.000000000 -0600
@@ -108,6 +108,38 @@ function gigya_update_6005() {
 }
 
 /**
+ * Updates weight and migrates existing variables
+ */
+function gigya_update_6006() {
+  $items = array();
+  
+  // set weight to content profile registration + 1
+  switch ($GLOBALS['db_type']) {
+    case 'mysqli':
+    case 'mysql':
+    case 'pqsql':
+      $items[] = update_sql("UPDATE {system} SET weight = 2 WHERE name = 'gigya'");
+      break;
+  }
+  
+  // migrate existing mapping variables to the new variables
+  if (module_exists('profile')) {
+    $result = _profile_get_fields($category, TRUE);
+    $weight = 1;
+    $fields = array();
+    while ($field = db_fetch_object($result)) {
+      $bio_assoc = variable_get('gigya_profile_'. $field->name, '');
+      if (!empty($bio_assoc)) {
+        variable_set('gigya_bio_'. $bio_assoc, $arr_name);
+      }
+    }
+  }
+
+  $items[] = array('success' => TRUE, 'query' => 'Migrated mapping variables of core profile fields.');
+  return $items;
+}
+
+/**
  *
  //don't really need this at all.... since we store the user picture in the user table
 function gigya_update_6001() {
