From 0461917fbc8a465349c6467a309cc5cbbf6afcfd Mon Sep 17 00:00:00 2001
From: Matt G <madmatter23@gmail.com>
Date: Fri, 28 Oct 2011 12:56:57 -0500
Subject: [PATCH] Patched PCP to add compatibility with Profile2 module

---
 pcp-block.tpl.php |   11 ++-
 pcp.admin.inc     |  150 +++++++++++++++++++++++++++++++------
 pcp.install       |    9 +-
 pcp.module        |  219 +++++++++++++++++++++++++++++++++++++++--------------
 4 files changed, 299 insertions(+), 90 deletions(-)
 mode change 100644 => 100755 pcp-block.tpl.php
 mode change 100644 => 100755 pcp.admin.inc

diff --git a/pcp-block.tpl.php b/pcp-block.tpl.php
old mode 100644
new mode 100755
index a2d0f1d..71cce6f
--- a/pcp-block.tpl.php
+++ b/pcp-block.tpl.php
@@ -1,4 +1,5 @@
 <?php
+// $Id$
 
 /**
  * @file
@@ -19,15 +20,15 @@
  */
 ?>
 <style type="text/css">
-  #pcp-percent-bar-wrapper {width: 100%; border: 1px solid #000; padding: 1px;}
-  #pcp-percent-bar { width: <?php print $current_percent; ?>%; height: 10px; background-color: #777;}
+  #pcp-wrapper-<?php print $entity_type . '-' . $bundle; ?> .pcp-percent-bar-wrapper {width: 100%; border: 1px solid #000; padding: 1px;}
+  #pcp-wrapper-<?php print $entity_type . '-' . $bundle; ?> .pcp-percent-bar { width: <?php print $current_percent; ?>%; height: 10px; background-color: #777;}
 </style>
 
-<div id="pcp-wrapper">
+<div id="pcp-wrapper-<?php print $entity_type . '-' . $bundle; ?>" class="pcp-wrapper <?php print 'pcp-' . $entity_type;?> <?php print 'pcp-' . $bundle; ?>">
   <?php print t('!complete% Complete', array('!complete' => $current_percent)); ?>
 
-  <div id="pcp-percent-bar-wrapper">
-    <div id="pcp-percent-bar"></div>
+  <div class="pcp-percent-bar-wrapper">
+    <div class="pcp-percent-bar"></div>
   </div>
 </div>
 
diff --git a/pcp.admin.inc b/pcp.admin.inc
old mode 100644
new mode 100755
index 13ca2b2..d5601ed
--- a/pcp.admin.inc
+++ b/pcp.admin.inc
@@ -1,4 +1,5 @@
 <?php
+// $Id$
 
 /**
  * @file
@@ -6,7 +7,7 @@
  */
 
 /**
- * Form builder; Configure PCP fields.
+ * Form builder; Configure PCP fields for core profile.
  *
  * @ingroup forms
  */
@@ -30,12 +31,13 @@ function pcp_admin_settings($form, &$form_state) {
     );
   }
 
-  $options = pcp_admin_settings_form_data();
   $form['profile_field_settings'] = array(
     '#type' => 'fieldset',
-    '#title' => 'Profile Field Settings',
-    '#description' => empty($options['profile_fields_options']) ? t('There are no profile fields enabled. Please <a href="@link">add some profile fields</a> first.', array('@link' => url('admin/config/people/accounts/fields'))) : t('Checking a profile field below will add that field to the logic of the complete percentage.'),
+    '#title' => 'Core Profile Field Settings',
+    '#description' => t('Checking a profile field below will add that field to the logic of the complete percentage. Please note that these are simply core profile fields, not fields attached to Profile2 profiles.'),
   );
+  
+  $options = pcp_admin_settings_form_data('user');
   if ($options['profile_fields_options']) {
     $form['profile_field_settings']['profile_fields'] = array(
       '#title' => t('Profile Fields'),
@@ -55,6 +57,42 @@ function pcp_admin_settings($form, &$form_state) {
 
 /**
  * Implements hook_form_FORM_ID_alter().
+ * Form builder; Configure PCP fields for profile2 profile types.
+ */
+function pcp_form_profile2_type_form_alter(&$form, &$form_state, $form_id) {  
+  
+  $bundle = $form['type']['#default_value'];
+  
+  $form['profile_complete'] = array(
+    '#type' => 'fieldset',
+    '#title' => 'Profile Complete Settings',
+  );
+  
+  $form['profile_complete']['hide_block_on_complete'] = array(
+    '#type' => 'checkbox',
+    '#title' => 'Hide Block When Complete',
+    '#default_value' => variable_get('pcp_profile2_' . $bundle . '_hide_block_on_complete', 0),
+    '#description' => t('When a user reaches 100% complete of their profile, do you want the profile complete percent block to go away? If so, check this box on.'),
+  );  
+  
+  $options = pcp_admin_settings_form_data('profile2', $bundle);
+  if ($options['profile_fields_options']) {
+    $form['profile_complete']['profile_fields'] = array(
+      '#title' => t('Profile Fields'),
+      '#description' => empty($options['profile_fields_options']) ? t('There are no profile fields enabled. Please <a href="@link">add some profile fields</a> first.', array('@link' => url('admin/config/people/accounts/fields'))) : t('Checking a profile field below will add that field to the logic of the complete percentage.'),
+      '#type' => 'checkboxes',
+      '#options' => $options['profile_fields_options'],
+      '#default_value' => array_flip($options['default_values']),
+    );
+  }
+  
+  $form['#submit'][] = 'pcp_admin_settings_submit';
+  
+  return $form;
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter().
  */
 function pcp_form_user_admin_settings_alter(&$form, &$form_state) {
   if (user_access('administer pcp')) {
@@ -75,7 +113,7 @@ function pcp_form_user_admin_settings_alter(&$form, &$form_state) {
 function pcp_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
   // @todo do we need user access check here?
   $tag = FALSE;
-  $result = pcp_get_tagged_profile_fields($form['#field']['id']);
+  $result = pcp_get_tagged_profile_fields($form['#instance']['id']);
   if (!empty($result)) {
     $tag = TRUE;
   }
@@ -95,17 +133,33 @@ function pcp_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
  * when the pcp_admin_settings_form() function
  * is executed. 
  *
+ * @param $entity_type
+ *   this parameter indicates the source of the profile fields. accepted
+ *   values include user and profile2.
+ * @param $bundle
+ *   (optional) NULL The bundle to return fields for.
+ *
  * @return - assoc array
  *  ['profile_fields_options'] 
  *    - An associative array of all fields created from the profile module.
  *  ['default_values']
  *    - An indexed array of all (if any) default values for the form.
  */
-function pcp_admin_settings_form_data() {
+function pcp_admin_settings_form_data($entity_type, $bundle = NULL) {
   $options = array();
-  $options['profile_fields_options'] = pcp_get_profile_fields();
-  $options['default_values'] = pcp_get_tagged_profile_fields();
-
+  
+  $field_conditions = array(
+    'entity_type' => $entity_type,
+  );
+  
+  // If we're dealing with a profile2 entity, specify the field bundle.
+  if ($entity_type == 'profile2') {
+    $field_conditions = array_merge($field_conditions, array('bundle' => $bundle));
+  }
+  
+  $options['profile_fields_options'] = pcp_get_profile_fields($field_conditions);
+  $options['default_values'] = pcp_get_tagged_profile_fields(NULL, $entity_type, $bundle);
+    
   return $options;
 }
 
@@ -114,23 +168,39 @@ function pcp_admin_settings_form_data() {
  */
 function pcp_admin_settings_submit($form, &$form_state) {
   switch ($form['#form_id']) {
+    
     // admin/config/people/accounts
     case 'user_admin_settings':
       variable_set('pcp_enable_user_picture', $form_state['values']['pcp_enable_user_picture']);
       break;
+    
     // admin/config/people/accounts/fields/[field_name]
     case 'field_ui_field_edit_form':
-      $fid = $form['#field']['id'];
+      $fiid = $form['#field']['id'];
       db_delete('profile_pcp')
-        ->condition('fid', $fid)
+        ->condition('fiid', $fiid)
         ->execute();
 
       if ($form_state['values']['instance']['pcp_tag']) {
         db_insert('profile_pcp')
-          ->fields(array('fid' => $fid))
+          ->fields(array('fiid' => $fiid))
           ->execute();
       }
       break;
+    
+    // admin/structure/profiles/manage/[profile_type]
+    case 'profile2_type_form':
+      // Provides the profile2 pcp data
+      $profile_type = $form_state['values']['type'];
+      variable_set('pcp_profile2_' . $profile_type . '_hide_block_on_complete', $form_state['values']['hide_block_on_complete']);
+
+      // Process the profile fields that have been modified
+      if ($form_state['values']['profile_fields']) {
+        _pcp_save_profile_fields($form_state['values']['profile_fields']);
+      }
+      
+      break;
+      
     // admin/config/people/pcp
     default:
       // Provides the General Settings data
@@ -139,16 +209,52 @@ function pcp_admin_settings_submit($form, &$form_state) {
 
       // Process the profile fields that have been modified
       if (isset($form_state['values']['profile_fields'])) {
-        db_delete('profile_pcp')
-          ->execute();
-        foreach ($form_state['values']['profile_fields'] as $fid) {
-          if ($fid) {
-            db_insert('profile_pcp')
-              ->fields(array('fid' => $fid))
-              ->execute();
-          }
-        }
-        drupal_set_message(t("Your settings have been saved."));
+        _pcp_save_profile_fields($form_state['values']['profile_fields']);
+        
       }
   }
+}
+
+
+/**
+ *  Utility function used to save fields.
+ */
+function _pcp_save_profile_fields($profile_fields) {
+  
+  // Build array of fiids to delete from profile_pcp.
+  $delete_instances = array();
+  foreach ($profile_fields as $fiid => $value) {
+    if ($value == 0) {
+      $delete_instances[] = $fiid;
+    }
+  }
+  
+  // Delete values from profile_pcp.
+  if (sizeof($delete_instances) > 0) {
+    db_delete('profile_pcp')
+      ->condition('fiid', $delete_instances, 'IN')
+      ->execute();
+  }
+  
+  // Get list of values currently in profile_pcp.
+  $result = db_query("SELECT fiid FROM profile_pcp");
+  foreach ($result as $record) {
+    $current_fiids[] = $record->fiid;
+  }
+  
+  // Build query to insert new fiids into profile_pcp.
+  $insert_query = db_insert('profile_pcp')->fields(array('fiid'));
+  
+  // If we'd like to add a field that isn't already in the profile_pcp table,
+  // then add it to the query.
+  foreach ($profile_fields as $fiid => $value) {
+    if ($value != 0 && !in_array($fiid, $current_fiids)) {
+      $insert_query->values(array('fiid' => $fiid));
+    }
+  }
+  
+  // Execute query.
+  $insert_query->execute();
+
+  drupal_set_message(t("Your settings have been saved."));
 }
\ No newline at end of file
diff --git a/pcp.install b/pcp.install
index 25edfa5..0cb2f30 100755
--- a/pcp.install
+++ b/pcp.install
@@ -12,13 +12,13 @@ function pcp_schema() {
   $schema['profile_pcp'] = array(
     'description' => 'Stores PCP required profile fields.',
     'fields' => array(
-      'fid' => array(
+      'fiid' => array(
       'type' => 'int',
       'not null' => TRUE,
-      'description' => 'Field ID',
+      'description' => 'Field Instance ID',
       ),
     ),
-    'primary key' => array('fid'),
+    'primary key' => array('fiid'),
   );
 
   return $schema;
@@ -28,6 +28,5 @@ function pcp_schema() {
  * Implements hook_uninstall()
  */
 function pcp_uninstall() {
-  variable_del('pcp_enable_user_picture');
-  variable_del('pcp_hide_block_on_complete');
+  db_query("DELETE FROM {variable} WHERE name LIKE 'pcp_%");
 }
\ No newline at end of file
diff --git a/pcp.module b/pcp.module
index 6a33da5..7c95841 100755
--- a/pcp.module
+++ b/pcp.module
@@ -60,10 +60,21 @@ function pcp_menu() {
  * Implements hook_block_info().
  */
 function pcp_block_info() {
+  
+  // Create block for core user profiles.
   $blocks['pcp_profile_percent_complete'] = array(
-    'info' => t('Profile Complete Percentage'),
+    'info' => t('Profile Complete Percentage - Core Profile'),
   );
 
+  // Create a block for each profile2 profile type.
+  if ($profile2_entity = entity_get_info('profile2')) {
+    foreach($profile2_entity['bundles'] as $bundle => $values) {
+      $blocks['pcp_profile2_' . $bundle] = array(
+        'info' => t('Profile Complete Percentage - ' . $values['label']),
+      );
+    }  
+  }
+
   return $blocks;
 }
 
@@ -71,12 +82,14 @@ function pcp_block_info() {
  * Implements hook_block_view().
  */
 function pcp_block_view($delta = '') {
+  global $user;
+   
   //The $delta parameter tells us which block is being requested.
   switch ($delta) {
     case 'pcp_profile_percent_complete':
-      global $user;
+     
 
-      $out = pcp_get_complete_percentage_data($user);
+      $out = pcp_get_complete_percentage_data('user', 'user', $user);
       $hide = variable_get('pcp_hide_block_on_complete', 0);
 
       if (($hide && ($out['incomplete'] == 0)) || empty($out)) {
@@ -90,8 +103,38 @@ function pcp_block_view($delta = '') {
 
       $block['subject'] = $subject;
       $block['content'] = $content;
+      
       break;
+      
+    // Generate content for each profile2 profile type's pcp block.
+    default:
+    
+      if ($profile2_entity = entity_get_info('profile2')) {
+        foreach($profile2_entity['bundles'] as $bundle => $values) {
+          if ($delta == 'pcp_profile2_' . $bundle) {
+
+            $out = pcp_get_complete_percentage_data('profile2', $bundle, $user);
+            $hide = variable_get('pcp_profile2_' . $bundle . '_hide_block_on_complete', 0);            
+            
+            // If the block should be hidden, hide it.
+            if (($hide && ($out['incomplete'] == 0)) || empty($out)) {
+              $subject = '';
+              $content = '';
+            }
+            else {
+              $subject = t('Profile Complete');
+              $content = theme('pcp_profile_percent_complete', $out);
+            }
+            
+            $block['subject'] = t('Profile Complete Percentage - ' . $values['label']);
+            $block['content'] = $content;
+          }
+        }  
+      }
+      break;      
   }
+
+  
   return $block;
 }
 
@@ -104,17 +147,26 @@ function pcp_block_view($delta = '') {
  * @return
  *   PCP data array.
  */
-function pcp_get_complete_percentage_data($user) {
-  $entity_fields = field_info_instances('user', 'user');
+function pcp_get_complete_percentage_data($entity_type, $bundle, $user) {
+  $entity_fields = field_info_instances($entity_type, $bundle);
 
-  $profile_fields = pcp_get_tagged_profile_fields();
+  $profile_fields = pcp_get_tagged_profile_fields(NULL, $entity_type, $bundle);
   $additional_fields = pcp_get_additional_tagged_fields();
   $fields = $profile_fields + $additional_fields;
 
-  $user_profile_values = pcp_get_user_profile_values($user->uid);
-  $user_additional_values = pcp_get_user_additional_values($user->uid);
+  
+  // If no fields have been tagged, indicate that the profile is complete.
+  if (!$fields) {
+    $data['current_percent'] = 100;
+    $data['incomplete'] = 0;
+    return $data;
+  }
+  
+  $user_profile_values = pcp_get_user_profile_values($user->uid, $entity_type, $bundle);
+  
+  $user_additional_values = pcp_get_user_additional_values($user->uid, $entity_type, $bundle);
   $user_values = $user_profile_values + $user_additional_values;
-
+  
   $data = array();
 
   // Enumerate empty fields
@@ -129,20 +181,20 @@ function pcp_get_complete_percentage_data($user) {
   if ($empty_fields) {
     $data['uid'] = $user->uid;
 
-    // Pick up a random field, we won't use suffle because it reindex the array
+    // Pick up a random field, we won't use shuffle because it reindex the array
     // keys
-    $fid = array_rand($empty_fields);
+    $fiid = array_rand($empty_fields);
 
-    if (is_numeric($fid)) {
-      $field_title = $entity_fields[$fields[$fid]]['label'];
-      $field_name = $fields[$fid];
+    if (is_numeric($fiid)) {
+      $field_title = $entity_fields[$fields[$fiid]]['label'];
+      $field_name = $fields[$fiid];
     }
-    elseif ($fid == 'user_picture') {
-      $field_title = $fields[$fid]['title'];
-      $field_name = $fields[$fid]['name'];
+    elseif ($fiid == 'user_picture') {
+      $field_title = $fields[$fiid]['title'];
+      $field_name = $fields[$fiid]['name'];
     }
 
-    $data['nextfield_id'] = $fid;
+    $data['nextfield_id'] = $fiid;
     $data['nextfield_title'] = $field_title;
     $data['nextfield_name'] = $field_name;
   }
@@ -154,63 +206,73 @@ function pcp_get_complete_percentage_data($user) {
   $current_percent = number_format(($completed_fields / $fields_count), 2) * 100;
   $next_percent = number_format((($completed_fields + 1) / $fields_count), 2) * 100;
 
+  $data['entity_type'] = $entity_type; 
+  $data['bundle'] = $bundle;
+  
   $data['completed']  = $completed_fields;
   $data['incomplete'] = $empty_fields_count;
   $data['total'] = $fields_count;
 
   $data['current_percent'] = $current_percent;
   $data['next_percent'] = $next_percent;
-
+  
   return $data;
 }
 
 /**
  * Get all the profile fields stored in the system, tagged or not tagged.
  */
-function pcp_get_profile_fields($options = NULL) {
-  $params = array('entity_type' => 'user');
-
-  if ($options) {
-    $params = array_merge($params, $options);
-  }
+function pcp_get_profile_fields($params) {
 
   $instances = field_read_instances($params);
   $fields = array();
   foreach ($instances as $instance) {
-    $fields[$instance['field_id']] = $instance['field_name'];
+    $fields[$instance['id']] = $instance['field_name'];
   }
-
   return $fields;
 }
 
 /**
  * Get all the profile fields that have been tagged.
- * If an $fid is passed in, only the data for that field will be returned.
+ * If an $fiid is passed in, only the data for that field will be returned.
  *
- * @param $fid
- *   The fid of the field data should be returned for. If null, all fields
+ * @param $fiid
+ *   (optional) NULL The field instance id of the field data should be returned for. If null, all fields
  *   are returned.
+ * @param $entity_type
+ *   (optional) NULL The type of entity to return fields for.
+ * @param $bundle
+ *   (optional) NULL The bundle to return fields for.
  *
  * @return
- *   field(s) ID and name.
+ *   field instance ID(s) and name(s).
  */
-function pcp_get_tagged_profile_fields($fid = NULL) {
+function pcp_get_tagged_profile_fields($fiid = NULL, $entity_type = NULL, $bundle = NULL) {
   $query = db_select('field_config_instance', 'fci');
-  $query->addField('fci', 'field_id', 'fid');
+  $query->addField('fci', 'id', 'fiid');
   $query->addField('fci', 'field_name', 'field_name');
-  $query->join('profile_pcp', 'pcp', 'pcp.fid = fci.field_id');
-
-  if (isset($fid)) {
-    $query->condition('fid', $fid);
+  $query->join('profile_pcp', 'pcp', 'pcp.fiid = fci.id');
+  
+  
+  if (isset($entity_type)) {
+    $query->condition('entity_type', $entity_type);
+  }    
+  
+  if (isset($bundle)) {
+    $query->condition('bundle', $bundle);
+  }  
+  
+  if (isset($fiid)) {
+    $query->condition('fiid', $fiid);
   }
 
   $result = $query->execute();
 
   $fields = array();
   foreach ($result as $row) {
-    $fields[$row->fid] = $row->field_name;
+    $fields[$row->fiid] = $row->field_name;
   }
-
+ 
   return $fields;
 }
 
@@ -242,7 +304,7 @@ function pcp_get_additional_tagged_fields() {
   // Enable user picture support.
   if (variable_get('pcp_enable_user_picture', 0)) {
     $fields['user_picture'] = array(
-      'fid' => 'user_picture',
+      'fiid' => 'user_picture',
       'title' => 'User Picture',
       'name' => 'picture_upload',
     );
@@ -255,20 +317,41 @@ function pcp_get_additional_tagged_fields() {
  * Return a users profile field values that have been saved
  * for a given user.
  *
- * @param int $uid - The uid of the user we are returning data for.
+ * @param int $uid
+ *   The uid of the user we are returning data for.
+ * @param $entity_type
+ *   The type of entity to return fields for.
+ * @param $bundle
+ *   (optional) NULL The bundle to return fields for
  *
  * @return assoc array of all profile fields for the user.
  */
-function pcp_get_user_profile_values($uid) {
-  $fields = field_info_instances('user', 'user');
+function pcp_get_user_profile_values($uid, $entity_type, $bundle = NULL) {
+  $fields = field_info_instances($entity_type, $bundle);
   $user = user_load($uid);
   $user_fields = array();
-
-  foreach ($fields as $field) {
-    $fid = $field['field_id'];
-    $user_fields[$fid] = $user->$field['field_name'];
+ 
+  // Grab profile values from core profile fields.
+  if ($entity_type == 'user') {
+    foreach ($fields as $field) {
+      $fiid = $field['id'];
+      $user_fields[$fiid] = $user->$field['field_name'];
+    }
+  }
+  
+  // If user has profile2 profile, grab values from it.
+  elseif ($entity_type == 'profile2' && $profile = profile2_load_by_user($user, $bundle)) { 
+    foreach ($fields as $field_name => $field_values) {
+      if ($field = $profile->$field_name) {
+        $fiid = $field_values['id'];
+        $user_fields[$fiid] = $field['und'][0]['value'];
+      }
+    }
   }
 
+  // Note, entity type user will return empty fields, whereas entity type profile2 will
+  // only return non-empty fields.
+  
   return $user_fields;
 }
 
@@ -276,13 +359,18 @@ function pcp_get_user_profile_values($uid) {
  * Return a users additional field values that have been saved
  * for a given user.
  *
- * @param int $uid - The uid of the user we are returning data for.
+ * @param int $uid
+ *   The uid of the user we are returning data for.
+ * @param $entity_type
+ *   The type of entity to return fields for.
+ * @param $bundle
+ *   (optional) NULL The bundle to return fields for
  *
  * @return assoc array of all profile fields for the user.
  */
-function pcp_get_user_additional_values($uid) {
+function pcp_get_user_additional_values($uid, $entity_type, $bundle = NULL) {
   $values = array();
-  if ($uid) {
+  if ($uid && $entity_type == 'user') {
     $account = user_load($uid);
     $values['user_picture'] = $account->picture;
   }
@@ -302,11 +390,14 @@ function pcp_form_field_ui_field_delete_form_alter(&$form, &$form_state) {
  * We then need to delete the pcp value too
  */
 function pcp_delete_field_requireness($form, &$form_state) {
-  $user_fields = field_read_instances(array('entity_type' => 'user'));
-  foreach ($user_fields as $field) {
-    if ($field['field_name'] == $form_state['values']['field_name']) {
+  $entity_type = $form_state['values']['entity_type'];
+  $bundle = $form_state['values']['bundle'];
+  
+  $fields = field_read_instances(array('entity_type' => $entity_type));
+  foreach ($fields as $field) {
+    if ($field['id'] == $form_state['values']['id']) {
       db_delete('profile_pcp')
-        ->condition('fid', $field['field_id'])
+        ->condition('fiid', $field['id'])
         ->execute();
     }
   }
@@ -319,9 +410,21 @@ function pcp_delete_field_requireness($form, &$form_state) {
  */
 function template_preprocess_pcp_profile_percent_complete(&$variables) {
   if (isset($variables['nextfield_name'])) {
-    $input_name = 'edit-' . str_replace('_', '-', $variables['nextfield_name']);
-    $user_edit_path = 'user/' . $variables['uid'] . '/edit';
-
-    $variables['next_path'] = url($user_edit_path, array('absolute' => TRUE, 'fragment' => $input_name));
+     
+    // Generate URL to edit next field for core user profile.
+    if ($variables['entity_type'] == 'user'){
+      $input_name = 'edit-' . str_replace('_', '-', $variables['nextfield_name']);
+      $user_edit_path = 'user/' . $variables['uid'] . '/edit';
+      $variables['next_path'] = url($user_edit_path, array('absolute' => TRUE, 'fragment' => $input_name));
+    }
+    
+    // Generate URL to edit next field for profile2 profile.
+    elseif ($variables['entity_type'] == 'profile2') {
+      //edit-profile-seeker-profile-field-seeker-address-und-0
+      $bundle = $variables['bundle'];
+      $input_name = 'edit-profile-' . str_replace('_', '-', $bundle) . '-' . str_replace('_', '-', $variables['nextfield_name']) . '-und-0';
+      $profile_edit_path = 'profile-' . $bundle . '/' . $variables['uid'] . '/edit';
+      $variables['next_path'] = url($profile_edit_path, array('absolute' => TRUE, 'fragment' => $input_name));
+    }
   }
 }
-- 
1.7.1

