Fix for openid_ax_persona_form

  1. this is in openid_ax.module::
  2.  
  3. $items['user/%user/persona'] = array(
  4.     'title' => 'OpenID AX Personas',
  5.     'page callback' => 'openid_ax_persona',
  6.     'access callback' => TRUE,
  7.     'type' => MENU_LOCAL_TASK,
  8.     'file' => 'openid_ax.pages.inc'
  9.     );
  10.  
  11.  
  12. Then, in .pages.inc::
  13.  
  14. function openid_ax_persona() {
  15.   return drupal_get_form('openid_ax_persona_form');
  16. }
  17.  
  18. /**
  19.  *Form for creating/editing a persona
  20.  */
  21. function openid_ax_persona_form($form_state, $persona_id = 0) {
  22.   global $user;
  23.   $form = array();
  24.   $identifiers = db_query("SELECT * FROM {openid_ax_attributes}");
  25.   $persona = db_query("SELECT * FROM {openid_ax_persona} where uid=%d", $user->uid);
  26.   $options = array(t('Default'), 'openid_ax_create_new' =>t('Create new'));
  27.   while($persona_value = db_fetch_array($persona)) {
  28.         $options[$persona_value['persona_id']] = $persona_value['persona_name'];
  29.   }
  30.   $form['personas'] = array(
  31.     '#type' => 'select',
  32.     '#title' => 'Personas',
  33.     '#options' => $options,
  34.   );
  35.   $form['select'] = array(
  36.     '#type' => 'submit',
  37.     '#value' => 'Select Persona',
  38.     '#submit' => array('openid_ax_persona_form_submit_select_persona')
  39.     );
  40.   $ax_values = db_query("SELECT * FROM {openid_ax_values} WHERE uid=%d",$user->uid );
  41.   while($row = db_fetch_array($ax_values)) {
  42.         $persona_value[$row['ax_id']] = $row['ax_values'];
  43.   }
  44.  
  45.   while($row = db_fetch_array($identifiers)) {
  46.         $form[$row['ax_id']] = array(
  47.             '#type' => 'textfield',
  48.             '#title' => t($row['identifier']),
  49.             '#default_value' => $persona_value[$row['ax_id']],
  50.             '#size' => 25,
  51.             '#maxlength' => 100
  52.         );
  53.         if(isset($persona_value[$row['ax_id']])) {
  54.           $form['has_value'.$row['ax_id']] = array(
  55.                 '#type' => 'hidden',
  56.           );
  57.           $form['intro'] = array(
  58.             '#type' => 'markup',
  59.             '#value' => t('To delete a value, delete the value in the particular field and then submit'),
  60.             '#weight' => -1
  61.       );
  62.         }
  63.  
  64.   }
  65.   $form['submit'] = array(
  66.     '#type' => 'submit',
  67.     '#value' => t('Submit'),
  68.   );
  69.   return $form;
  70. }
  71.  
  72. function openid_ax_persona_form_submit_select_persona(&$form, $form_state) {
  73.   global $user;
  74.   if($form_state['values']['personas']=='openid_ax_create_new') {
  75.         drupal_goto('user/'.$user->uid.'/persona/create');
  76.   }
  77.   else {
  78.         $persona = $form_state['values']['personas'];
  79.   }
  80.  
  81. }
  82.  
  83.  
  84. function openid_ax_persona_form_submit(&$form, $form_state) {
  85.   global $user;
  86.   $result = db_query("SELECT ax_id FROM {openid_ax_attributes}");
  87.   //difficult to use INSERT ... ON DUPLICATE KEY UPDATE
  88.   // as there is no unique key
  89.   static $success;
  90.   while($ax_id = db_fetch_array($result)) {
  91.     $ax_value = trim($form_state['values'][$ax_id['ax_id']]);
  92.     if($ax_value != ''||(isset($form_state['values'][has_value.$ax_id['ax_id']]))){
  93.           if(!db_result(db_query("SELECT * FROM {openid_ax_values} WHERE uid=%d AND ax_id=%d", $user->uid,$ax_id['ax_id']))) {//TODO add persona support
  94.             $success = db_query("INSERT INTO {openid_ax_values} (uid,ax_id,ax_values) VALUES ('%d','%d','%s')", $user->uid,$ax_id['ax_id'],$ax_value);
  95.           }
  96.           else {
  97.                 if($ax_value != '') {
  98.           $success = db_query("UPDATE {openid_ax_values} SET ax_values='%s' WHERE ax_id=%d AND uid=%d",$form_state['values'][$ax_id['ax_id']],$ax_id['ax_id'],$user->uid);
  99.                 }
  100.                 else {
  101.                   $success = db_query("DELETE FROM {openid_ax_values} WHERE ax_id=%d AND uid=%d",$ax_id['ax_id'],$user->uid);
  102.                 }
  103.           }
  104.     }
  105.   }
  106.   if($success) {
  107.         drupal_set_message(t('Your persona values have been saved'));
  108.   }
  109. }