Index: openid_ax.pages.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/openid_ax/openid_ax.pages.inc,v
retrieving revision 1.16
diff -u -p -r1.16 openid_ax.pages.inc
--- openid_ax.pages.inc	10 Aug 2008 18:33:57 -0000	1.16
+++ openid_ax.pages.inc	10 Oct 2008 20:29:59 -0000
@@ -45,31 +45,34 @@ function openid_ax_form(&$form_state,&$a
   global $user;
   $form_state['ax_response'] = $ax_response;
   $realm = $form_state['post']['openid_realm'];
-  if($realm == '') {
-  	$realm = $_SESSION['openid_ax']['realm'];
-  	unset($_SESSION['openid_ax']['realm']);
+
+  if ($realm == '') {
+    $realm = $_SESSION['openid_ax']['realm'];
+    unset($_SESSION['openid_ax']['realm']);
   }
+
   $form = array();
   $form['intro'] = array(
     '#type' => 'markup',
     '#value' => '<p>'. t('Your following details are being send to %site, would you like to continue?', array('%site' => $realm)) . '</p>'
   );
-  foreach($ax_response as $name=>$value) {
-  	if(strstr($name,'value')){
-	  $form[$name] = array(
-	    '#type' => 'textfield',
-	    '#title' => t(substr(strrchr($name,'value.'),6)),
-	    '#value' => $value,
-	    '#size' => 14,
-	    '#maxlength' => 100
-	  );
+
+  foreach ($ax_response as $name => $value) {
+    if (strstr($name,'value')) {
+      $form[$name] = array(
+        '#type' => 'textfield',
+	'#title' => t(substr(strrchr($name,'value.'),6)),
+	'#value' => $value,
+	'#size' => 14,
+	'#maxlength' => 100
+      );
     }
     else {
       $form[$name] = array(
-	    '#type' => 'hidden',
-	    '#title' => $name,
-	    '#value' => $value
-	  );
+        '#type' => 'hidden',
+	'#title' => $name,
+	'#value' => $value
+      );
     }
   }
   $form['#action'] = url('openid/ax/send');
@@ -93,115 +96,47 @@ function openid_ax_send() {
   module_load_include('inc', 'openid_ax');
   $response = _openid_response();
   $ax_response = openid_ax_fetch_response($response);
-  drupal_goto($_SESSION['openid_ax']['return_to'],$ax_response);
-}
-
-/**
- * Menu callback for openid_ax_persona
- */
-function openid_ax_persona() {
-  return drupal_get_form('openid_ax_persona_form');
+  drupal_goto($_SESSION['openid_ax']['return_to'], $ax_response);
 }
 
 /**
  * Form for creating/editing a persona
  */
-function openid_ax_persona_form(&$form_state) {
-  global $user;
-  $form = array();
-  if(isset($form_state['storage']['personas'])) {
-  	$personas = $form_state['storage']['personas'];
+function openid_ax_persona_form(&$form_state, $user = NULL) {
+  if (!$user) {
+    global $user;
   }
-  elseif(isset($_GET['persona'])) {
-  	$personas = $_GET['persona'];
-  }   
-  else {
-  	$personas = 0;
+
+  $persona = $form = array();
+  $personas = (isset($_GET['persona'])) ? $_GET['persona'] : 0;
+
+  // Retrieving all of the current values we have stored for the persona we are currently viewing.
+  $ax_values = db_query("SELECT * FROM {openid_ax_values} WHERE uid = %d and persona_id = '%d'", $user->uid, $personas);
+  while ($row = db_fetch_array($ax_values)) {
+    $persona[$row['ax_id']] = $row;
   }
-  $persona = db_query("SELECT * FROM {openid_ax_persona} where uid=%d", $user->uid);
-  $options = array(t('Default'), 'openid_ax_create_new' =>t('Create new'), 'manage_ax_personas' => t('Manage AX Personas'));
-  while($persona_value = db_fetch_array($persona)) {
-  	$options[$persona_value['persona_id']] = $persona_value['persona_name']; 
-  }
-  if(count($options) == 3) {
-  	unset($options['manage_ax_personas']);
-  }
-  $form['personas'] = array(
-    '#type' => 'select',
-    '#title' => 'Personas',
-    '#options' => $options,
-    '#default_value' => $personas
-  );
-  $form['select'] = array(
-    '#type' => 'submit',
-    '#value' => 'Select Persona',
-    '#submit' => array('openid_ax_persona_form_submit_select_persona')
-    );
+
+  // Build each of the form elements and make sure we put them in a field set.
+  // Putting them in a fieldset will make sure the screen stays managable.
   $identifiers = db_query("SELECT * FROM {openid_ax_attributes}");
-  while($id = db_fetch_array($identifiers)) {
-  	$identifier[$id['ax_id']] = $id['identifier'];
-  }
-  $ax_values = db_query("SELECT * FROM {openid_ax_values} WHERE uid=%d and persona_id='%d'",$user->uid, $personas);
-  $valueExist = array();
-  while($row = db_fetch_array($ax_values)) {
-  	$valueExist[] = $row['ax_id'];  
-  	$form[$row['vid']] = array(
-	  '#type' => 'textfield',
-	  '#title' => t($identifier[$row['ax_id']]),
-	  '#default_value' => $row['ax_values'],
-	  '#size' => 25,
-	  '#maxlength' => 100
-    );
-	$form['has_value'.$row['vid']] = array(
-	  '#type' => 'hidden',
-	  '#value' => $row['vid'],
-	);
-  	static $count = 0;
-	$count++;
-	static $submit_btn = 1;
-	if($count == 10) {
-	  $form['submit'.$submit_btn] = array(
-        '#type' => 'submit',
-        '#value' => t('Submit'),
-      );
-      $count = 0;
-      $submit_btn++;
-	}
-	$hasValues = TRUE;
-  }
-  if($hasValues){ 
-	$form['intro'] = array(
-	  '#type' => 'markup',
-	  '#value' => t('<strong>To delete a value, delete the value in the particular field and then submit</strong>'),
-	  '#weight' => -1
+  while ($identifier = db_fetch_array($identifiers)) {
+    $key = $identifier['ax_id'];
+    // Build a field set so we can group the relevant fields together on the screen
+    if (!isset($form[$identifier['section']])) {
+      $form[$identifier['section']] = array('#type' => 'fieldset', '#title' => t($identifier['section']), '#collapsible' => TRUE, '#collapsed' => TRUE);
+    }
+    // Set the default value if the value exists in the persona array
+    $default_values = (isset($persona[$key])) ? $persona[$key]['ax_values'] : '';
+    $form[$identifier['section']][$key] = array(
+      '#type' => 'textfield',
+      '#title' => t($identifier['label']),
+      '#description' => t($identifier['description']),
+      '#default_value' => $default_values,
+      '#size' => 25,
+      '#maxlength' => 100
     );
   }
-  foreach($identifier as $key => $value) {
-  	if(!(in_array($key, $valueExist))){
-  	  $form['vac'.$key] = array(
-	    '#type' => 'textfield',
-	    '#title' => t($value),
-	    '#default_value' => '',
-	    '#size' => 25,
-	    '#maxlength' => 100
-      );
-      $form['no_value'.$key] = array(
-	  '#type' => 'hidden',
-	  '#value' => $key,
-	);
-      static $count = 0;
-	  $count++;
-	  static $submit_btn = 1;
-	  if($count == 10) {
-	    $form['submit'.$submit_btn] = array(
-          '#type' => 'submit',
-          '#value' => t('Submit'),
-        );
-        $count = 0;
-        $submit_btn++;
-	  }
-  	}
-  } 
+
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Submit'),
@@ -210,70 +145,57 @@ function openid_ax_persona_form(&$form_s
 }
 
 /**
- * Select submit handler for openid_ax_form
- */
-function openid_ax_persona_form_submit_select_persona(&$form, &$form_state) {
-  global $user;
-  if($form_state['values']['personas']=='openid_ax_create_new') {
-	drupal_goto('user/'.$user->uid.'/persona/create');
-  }
-  elseif($form_state['values']['personas']=='manage_ax_personas') {
-  	drupal_goto('user/'.$user->uid.'/persona/manage');
-  }
-  else {
-  	$form_state['storage']['personas'] = $form_state['values']['personas'];
-  }
-}
-
-/**
  * Submit handler for openid_ax_persona form
  */
 function openid_ax_persona_form_submit(&$form, &$form_state) {
   global $user;
-  $result = db_query("SELECT vid FROM {openid_ax_values} WHERE uid='%d' and persona_id='%d'",$user->uid, $form_state['values']['personas']);
-  static $success;
-  $success = FALSE;
-  while($vid = db_fetch_array($result)) {
-    $ax_value = trim($form_state['values'][$vid['vid']]);
-    if(isset($form_state['values'][has_value.$vid['vid']])){
-      if($ax_value != ''){
-	    $success = db_query("UPDATE {openid_ax_values} SET ax_values='%s' WHERE vid='%d'", $form_state['values'][$vid['vid']], $vid['vid']);	
-	  }
-	  else {
-	    $success = db_query("DELETE FROM {openid_ax_values} WHERE vid='%d'", $vid['vid']);
-	  }
-  	}
-  }
-  $ax_ids = db_query("SELECT ax_id FROM {openid_ax_attributes}");
-  while($ax_id=db_fetch_array($ax_ids)) {
-  	if((isset($form_state['values'][no_value.$ax_id['ax_id']]))&&($form_state['values'][vac.$ax_id['ax_id']] != '')){
-  	  $success = db_query("INSERT INTO {openid_ax_values}(ax_id, uid, persona_id, ax_values) VALUES ('%d', '%d', '%d', '%s')",$ax_id['ax_id'], $user->uid, $form_state['values']['personas'], $form_state['values'][$ax_id['ax_id']]);
-  	} 
-  }
-  if($success) {
-  	drupal_set_message(t('Your persona values have been saved'));
-  }
-}
 
-/**
- * Page callback for creating new persona
- */
-function openid_ax_create_persona() {
-  return drupal_get_form('openid_ax_new_persona_form');  
+  $persona = $form_state['values']['personas'];
+  $current_results = array();
+  $result = db_query("SELECT * FROM {openid_ax_values} WHERE uid= '%d' AND persona_id = '%d'", $user->uid, $persona);
+  while ($row = db_fetch_array($result)) {
+    $current_results[$row['ax_id']] = $row;
+  }
+
+  $result = db_query("SELECT ax_id FROM {openid_ax_attributes}");
+  while ($row = db_fetch_array($result)) {
+    $ax_id = $row['ax_id'];
+    // Checking to determine if the value exists and if it is not currently set in the persona values table.
+    if (isset($form_state['values'][$ax_id]) && strlen($form_state['values'][$ax_id]) && !isset($current_results[$ax_id])) {
+      db_query("INSERT INTO {openid_ax_values} (ax_id, uid, persona_id, ax_values) VALUES ('%d', '%d', '%d', '%s')", $ax_id, $user->uid, $persona, $form_state['values'][$ax_id]);
+    } elseif (isset($form_state['values'][$ax_id]) && strlen($form_state['values'][$ax_id]) && isset($current_results[$ax_id]) && $form_state['values'][$ax_id] != $current_results[$ax_id]['ax_values']) {
+      // Checking if the value exists and it already exists in the persona values table and the value has changed
+      db_query("UPDATE {openid_ax_values} SET ax_values = '%s' WHERE vid = '%d'", $form_state['values'][$ax_id], $current_results[$ax_id]['vid']);
+    }
+    elseif (isset($form_state['values'][$ax_id]) && !strlen($form_state['values'][$ax_id]) && isset($current_results[$ax_id])) {
+      // Checking if the value has been removed from the Persona and if so we remove from the database
+      db_query("DELETE FROM {openid_ax_values} WHERE vid = '%d'", $current_results[$ax_id]['vid']);
+    }
+  }
+  drupal_set_message('Your persona has been saved.');
 }
 
 /**
  * Form for creating new persona
+ *
+ * @param $form_state
+ *      State of the current form
+ * @param $user
+ *      User that the new persona is being created for
  */
-function openid_ax_new_persona_form() {
+function openid_ax_new_persona_form($form_state, $user) {
   $form = array();
   $form['persona'] = array(
-	'#type' =>'textfield',
-	'#title' => t('New Persona name'),
-	'#default_value' => '',
-	'#required' => TRUE,
-	'#size' => 20
-	);
+    '#type' =>'textfield',
+    '#title' => t('New Persona name'),
+    '#default_value' => '',
+    '#required' => TRUE,
+    '#size' => 20
+  );
+  $form['user_id'] = array(
+    '#type' => 'hidden',
+    '#default_value' => $user->uid,
+  );
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Submit'),
@@ -285,127 +207,206 @@ function openid_ax_new_persona_form() {
  * Submit handler for openid_ax_new_persona
  */
 function openid_ax_new_persona_form_submit(&$form, $form_state) {
-  global $user;
-  $persona_exists = db_result(db_query("SELECT * FROM {openid_ax_persona} WHERE uid=%d AND persona_name='%s'", $user->uid, $form_state['values']['persona']));
-  if($persona_exists) {
-  	drupal_set_message(t('A persona with that name already exists. Please select another persona name.'),'error');
+
+  $persona_exists = db_result(db_query("SELECT * FROM {openid_ax_persona} WHERE uid= %d AND persona_name = '%s'", $form_state['values']['user_id'], $form_state['values']['persona']));
+  if ($persona_exists) {
+    drupal_set_message(t('A persona with that name already exists. Please select another persona name.'), 'error');
   }
   else {
-  	$max_persona_id = db_result(db_query("SELECT MAX(persona_id) FROM {openid_ax_persona} WHERE uid='%d'", $user->uid));
-  	$create_persona = db_query("INSERT INTO {openid_ax_persona} (uid, persona_id, persona_name) VALUES ('%d', '%d', '%s')", $user->uid, $max_persona_id+1, $form_state['values']['persona']);
-  	if($create_persona) {
-  	  drupal_set_message(t('New Persona created'));
-  	  drupal_goto('user/'.$user->uid.'/persona');
-  	}
+    $max_persona_id = db_result(db_query("SELECT MAX(persona_id) FROM {openid_ax_persona} WHERE uid = '%d'", $form_state['values']['user_id']));
+    $create_persona = db_query("INSERT INTO {openid_ax_persona} (uid, persona_id, persona_name) VALUES ('%d', '%d', '%s')", $form_state['values']['user_id'], $max_persona_id + 1, $form_state['values']['persona']);
+    if ($create_persona) {
+      drupal_set_message(t('New Persona created'));
+      drupal_goto('user/'. $form_state['values']['user_id'] .'/persona');
+    }
   }
-} 
+}
 
 /**
  * Page to manage AX personas, viz, rename, delete, copy, etc.
  */
-function openid_ax_manage_personas() {
-  global $user;
-  $profiles = (db_query("SELECT * FROM {openid_ax_persona} WHERE uid=%d",$user->uid));
+function openid_ax_manage_personas($user = NULL) {
+  if (!$user) {
+    global $user;
+  }
+
   $header = array (
-    'Persona Name',
-    'Edit Persona Values',
-    'Rename Persona',
-    'Delete Persona',
+    t('Persona Name'),
+    t('Edit Persona Values'),
+    t('Rename Persona'),
+    t('Copy Persona'),
+    t('Delete Persona'),
   );
   $rows['default'] = array (
-  'Default',
-  '<a href="edit?persona=0">Edit</a>',
-  '-N/A-',
-  '-N/A-'
-  );
-  while($profile = db_fetch_array($profiles)) {
-  	$rows[$profile['persona_name']] = array (
-  	  $profile['persona_name'],
-  	  '<a href="edit?persona='.$profile['persona_id'].'">Edit </a>',
-  	  '<a href="rename?p='.$profile['persona_id'].'">Rename</a>',
-  	  '<a href="delete?p='.$profile['persona_id'].'">Delete</a>',
-  	);
+    t('Default'),
+    l(t('Edit'), 'user/'. $user->uid .'/persona/edit', array('query' => array('persona' => 0))),
+    '-N/A-',
+    l(t('Copy'), 'user/'. $user->uid .'/persona/copy/'. 0),
+    '-N/A-'
+  );
+
+  $profiles = db_query("SELECT * FROM {openid_ax_persona} WHERE uid = %d", $user->uid);
+  while ($profile = db_fetch_array($profiles)) {
+    $rows[$profile['persona_name']] = array (
+      $profile['persona_name'],
+      l(t('Edit'), 'user/'. $user->uid .'/persona/edit', array('query' => array('persona' => $profile['persona_id']))),
+      l(t('Rename'), 'user/'. $user->uid .'/persona/rename', array('query' => array('p' => $profile['persona_id']))),
+      l(t('Copy'), 'user/'. $user->uid .'/persona/copy/'. $profile['persona_id']),
+      l(t('Delete'), 'user/'. $user->uid .'/persona/delete', array('query' => array('p' => $profile['persona_id']))),
+    );
   }
-  $content = theme_table($header, $rows);
-  $content .= drupal_get_form('copy_persona_values_form');
-  return $content;
+
+  return theme_table($header, $rows);
 }
 
 /**
- * Form for selecting from to persona for values to be copied 
+ * Display a list of personas in the system
  */
-function copy_persona_values_form() {
-  global $user;
-  $form = array();
-  $persona = db_query("SELECT * FROM {openid_ax_persona} WHERE uid='%d'",$user->uid);
-  $options = array(t('Default'));
-  while($persona_value = db_fetch_array($persona)) {
-  	$options[$persona_value['persona_id']] = $persona_value['persona_name']; 
+function openid_ax_copy_personas($user) {
+
+  $personas = array();
+  $personas_list = db_query("SELECT * FROM {openid_ax_persona} WHERE uid = '%d'", $user->uid);
+  while ($row = db_fetch_object($personas_list)) {
+    $personas[$row->persona_id] = $row->persona_name;
   }
-  $form['intro'] = array(
-    '#type' => 'markup',
-    '#value' => t('<b>Copy values from one persona into another:</b>'),
-  );
-  $form['from_personas'] = array(
-    '#type' => 'select',
-    '#title' => 'From',
-    '#options' => $options,
-  );
-  $form['to_personas'] = array(
-    '#type' => 'select',
-    '#title' => 'To',
-    '#options' => $options,
-  );
-  $form['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Copy'),
-  );
-  return $form;
+
+  // If greater than 0 then we have personas and hence can display them to the user.
+  if (count($personas)) {
+    $header = array(t('Name'), t('Copy'));
+    $rows[] = array(t('Default'), l(t('Copy'), 'user/'. $user->uid .'/persona/copy/'. 0));
+    foreach ($personas as $key => $value) {
+      $rows[] = array($value, l(t('Copy'), 'user/'. $user->uid .'/persona/copy/'. $key));
+    }
+    return theme('table', $header, $rows);
+  }
+
+  return '<div>'. t('You only have your default persona hence you cannot copy from it.') .'</div>';
 }
 
 /**
- * copy_persona_values form submit handler
+ * Build the form with the variables we will be copying over to the new persona
+ *
+ * @param $user
+ *      User object
+ * @param $persona_id
+ *      Persona id we wish to copy from
  */
-function copy_persona_values_form_submit(&$form, $form_state) {
-  global $user;
-  $target_persona = $form_state['values']['to_personas'];
-  $copySrc = db_query("SELECT ax_id,ax_values FROM {openid_ax_values} WHERE uid='%d' AND persona_id='%d'",$user->uid, $form_state['values']['from_personas']);
-  while($vals = db_fetch_array($copySrc)) {
-    $copyTrg = db_query("INSERT INTO {openid_ax_values}(ax_id, uid, persona_id, ax_values)  VALUES ('%d','%d','%d','%s')", $vals['ax_id'], $target_persona, $user->uid, $vals['ax_values']);
+function openid_ax_copy_persona_form($form_state, $user, $persona_id) {
+
+  $form = $values = $options = array();
+  $form['user_id'] = array(
+    '#type' => 'hidden',
+    '#value' => $user->uid
+  );
+
+  $form['persona_id'] = array(
+    '#type' => 'hidden',
+    '#value' => $persona_id,
+  );
+
+  // Check if the persona id is not 0 which is the default then we can copy back to it
+  // Default is not displayed in the user persona table hence we are forced to check for it
+  if (is_numeric($persona_id) && $persona_id) {
+    $options[0] = t('Default');
+  }
+
+  // Select all of the personas that are in the table and are not the one we are currently copying
+  $personas = db_query("SELECT * FROM {openid_ax_persona} WHERE uid = '%d' AND persona_id <> '%d'", $user->uid, $persona_id);
+  while ($row = db_fetch_array($personas)) {
+    $options[$row['persona_id']] = $row['persona_name'];
+  }
+
+  // Display message informing that they cannot copy if there is only the default profile available
+  if (!count($options)) {
+    $form['intro'] = array(
+      '#type' => 'markup',
+      '#value' => t('<b>You only have a default profile, you must create an additional profile in order to copy values</b>'),
+    );
   }
-  drupal_set_message(t('Persona values copied.'));
+  else {
+    $form['copy_to'] = array(
+      '#type' => 'select',
+      '#title' => t('Copy To'),
+      '#options' => $options,
+      '#description' => t('Persona to copy the values too.'),
+    );
+
+    $form['submit'] = array(
+      '#type' => 'submit',
+      '#value' => t('Copy'),
+    );
+
+    // Retrieve the values that this persona already has stored since we do not want to display
+    // any persona fields that do not have information in them since we would be copying blank information
+    $persona_values = db_query("SELECT ax_id, ax_values FROM {openid_ax_values} WHERE uid = '%d' AND persona_id = '%d'", $user->uid, $persona_id);
+    while ($row = db_fetch_array($persona_values)) {
+      $values[$row['ax_id']] = $row['ax_values'];
+    }
+
+    // retrieving all of the attributes from the system
+    $attributes = db_query("SELECT * FROM {openid_ax_attributes} ORDER BY section ASC");
+    while ($row = db_fetch_array($attributes)) {
+      if (isset($values[$row['ax_id']])) {
+        $form[$row['ax_id']] = array(
+          '#type' => 'checkbox',
+          '#return_value' => $row['ax_id'],
+          '#title' => $row['label'],
+          '#description' => $row['description']
+        );
+      }
+    }
+  }
+  return $form;
 }
 
 /**
- * Page call back for renaming persona
+ * Submit handler that will copy the information from one persona to another.
  */
-function openid_ax_rename_personas() {
-  $content = drupal_get_form('openid_ax_rename_persona_form', $_GET['p']);
-  $content .= openid_ax_manage_personas();
-  return $content;	
+function openid_ax_copy_persona_form_submit(&$form, $form_state) {
+  $val = array();
+  // retrieving all of the attributes from the system
+  $values = db_query("SELECT ax_id, ax_values FROM {openid_ax_values} WHERE uid = '%d' AND persona_id = '%d'", $form_state['values']['user_id'], $form_state['values']['persona_id']);
+  while ($row = db_fetch_array($values)) {
+    $val[$row['ax_id']] = $row['ax_values'];
+  }
+  $attributes = db_query("SELECT * FROM {openid_ax_attributes} ORDER BY section ASC");
+  while ($row = db_fetch_array($attributes)) {
+    // Check if it exists and if so check if the value is set and if so copy the value
+    if (isset($form_state['values'][$row['ax_id']]) && $form_state['values'][$row['ax_id']]) {
+      // Courtesy remove incase a value currently exists for this field in the persona we are copying too
+      db_query("DELETE FROM {openid_ax_values} WHERE uid = '%d' AND persona_id = '%d' AND ax_id = '%d'", $form_state['values']['user_id'], $form_state['values']['copy_to'], $row['ax_id']);
+      db_query("INSERT INTO {openid_ax_values} (ax_id, uid, persona_id, ax_values) VALUES ('%d', '%d', '%d', '%s')", $row['ax_id'], $form_state['values']['user_id'], $form_state['values']['copy_to'], $val[$row['ax_id']]);
+    }
+  }
+  drupal_set_message(t('Persona fields have been copied'));
+  $form_state['redirect'] = 'user/'. $form_state['values']['user_id'] .'/persona';
 }
 
 /**
  * Form for renaming persona
  */
-function openid_ax_rename_persona_form(&$form, $form_state) {
-  global $user;
-  isset($_GET['p'])?$persona_id=$_GET['p']:$persona_id=$form_state['storage']['persona_id'];
+function openid_ax_rename_persona_form($form_state, $user) {
+
   $form = array();
-  $persona = db_result(db_query("SELECT persona_name FROM {openid_ax_persona} WHERE persona_id='%d' AND uid='%d'",$persona_id, $user->uid));
-  $form['persona'] = array(
-    '#type' => 'textfield',
-    '#default_value' => $persona,
-    '#size' => 15,
-  );
-  $form['persona_id'] = array(
-    '#type' => 'hidden',
-    '#value' => $_GET['p'],
-  );
-  $form['submit'] = array (
-    '#type' => 'submit',
-    '#value' => 'Rename',
-  );
+  if ($persona = db_result(db_query("SELECT persona_name FROM {openid_ax_persona} WHERE persona_id = '%d' AND uid = '%d'", $_GET['p'], $user->uid))) {
+    $form['persona'] = array(
+      '#type' => 'textfield',
+      '#default_value' => $persona,
+      '#size' => 15,
+    );
+    $form['persona_id'] = array(
+      '#type' => 'hidden',
+      '#value' => $_GET['p'],
+    );
+    $form['user_id'] = array(
+      '#type' => 'hidden',
+      '#value' => $user->uid,
+    );
+    $form['submit'] = array (
+      '#type' => 'submit',
+      '#value' => t('Rename'),
+    );
+  }
   return $form;
 }
 
@@ -413,34 +414,38 @@ function openid_ax_rename_persona_form(&
  * Submit handler for openid_ax_rename_persona_form
  */
 function openid_ax_rename_persona_form_submit(&$form, &$form_state) {
-  global $user;
-  $rename = db_query("UPDATE {openid_ax_persona} SET persona_name='%s' WHERE uid='%d' AND persona_id='%d'", $form_state['values']['persona'], $user->uid, $form_state['values']['persona_id']);
-  drupal_set_message(t('Your persona has been renamed'));
-  $form_state['storage']['persona_id'] = $form_state['values']['persona_id'];
+  // Updating the users persona name in the system
+  $rename = db_query("UPDATE {openid_ax_persona} SET persona_name = '%s' WHERE uid = '%d' AND persona_id = '%d'", $form_state['values']['persona'], $form_state['values']['user_id'], $form_state['values']['persona_id']);
+  drupal_set_message(t('Persona has been renamed'));
+  $form_state['redirect'] = 'user/'. $form_state['values']['user_id'] .'/persona';
 }
 
 /**
  * Function for deleting persona
  */
-function openid_ax_persona_delete() {
-  global $user;
-  $persona_id=$_GET['p'];
-  $persona = db_result(db_query("SELECT persona_name FROM {openid_ax_persona} WHERE persona_id='%d' AND uid='%d'",$persona_id, $user->uid));
-  $content = t('Are you sure you want to delete your <a href="edit?persona='.$persona_id.'">%profile</a> profile.', array('%profile' => $persona));
+function openid_ax_persona_delete($user) {
+
+  $persona = db_result(db_query("SELECT persona_name FROM {openid_ax_persona} WHERE persona_id = '%d' AND uid = '%d'", $_GET['p'], $user->uid));
+  $link = l(t($persona), 'user/'. $user->uid .'/persona/edit', array('query' => array('persona' => $_GET['p'])));
+  $content = t('Are you sure you want to delete your !profile profile.', array('!profile' => $link));
   $content .= t(' This cannot be reversed and all your profile related details will be lost.');
-  $content .= drupal_get_form('openid_ax_persona_delete_confirm_form');
+  $content .= drupal_get_form('openid_ax_persona_delete_confirm_form', $user);
   return $content;
 }
 
 /**
  * Form for confirming if the user wants to delete the persona
  */
-function openid_ax_persona_delete_confirm_form() {
+function openid_ax_persona_delete_confirm_form($form_state, $user) {
   $form = array ();
   $form['persona_id'] = array(
     '#type' => 'hidden',
     '#value' => $_GET['p'],
   );
+  $form['user_id'] = array(
+    '#type' => 'hidden',
+    '#value' => $user->uid,
+  );
   $form['delete'] = array(
     '#type' => 'submit',
     '#value' => t('Delete'),
@@ -457,19 +462,19 @@ function openid_ax_persona_delete_confir
  * Submit handler for openid_ax_persona_delete_confirm_form
  */
 function openid_ax_persona_delete_confirm_form_submit($form, &$form_state) {
-  global $user;
-  $delValues = db_query("DELETE FROM {openid_ax_values} WHERE uid='%d'  AND persona_id='%d'", $user->uid, $form_state['values']['persona_id']);
-  $deletePersona = db_query("DELETE FROM {openid_ax_persona} WHERE uid='%d'  AND persona_id='%d'", $user->uid, $form_state['values']['persona_id']);
-  if($delValues && $deletePersona) {
-  	drupal_set_message(t('Persona deleted.'));
-  	drupal_goto('user/'.$user->uid.'/persona/manage');
+
+  $delValues = db_query("DELETE FROM {openid_ax_values} WHERE uid = '%d'  AND persona_id = '%d'", $form_state['values']['user_id'], $form_state['values']['persona_id']);
+  $deletePersona = db_query("DELETE FROM {openid_ax_persona} WHERE uid = '%d' AND persona_id = '%d'", $form_state['values']['user_id'], $form_state['values']['persona_id']);
+  if ($delValues && $deletePersona) {
+    drupal_set_message(t('Persona deleted.'));
+    drupal_goto('user/'. $form_state['values']['user_id'] .'/persona');
   }
 }
 
 /**
  * Cancel handler for openid_ax_persona_delete_confirm_form
  */
-function openid_ax_persona_delete_confirm_form_cancel() {
-  global $user;
-  drupal_goto('user/'.$user->uid.'/persona/manage');
-}
\ No newline at end of file
+function openid_ax_persona_delete_confirm_form_cancel($form, $form_state) {
+  drupal_set_message(t('Persona Deletion has been cancelled'));
+  $form_state['redirect'] = 'user/'. $form_state['values']['user_id'] .'/persona';
+}
