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.   while($persona_value = db_fetch_array($persona)) {
  27.         $option[$persona_value['persona_id']] = $persona_value['persona_name'];
  28.   }
  29.   $form['personas'] = array(
  30.     '#type' => 'select',
  31.     '#title' => 'Personas',
  32.     '#options' => array(
  33.     '0' => t('Default'),
  34.     'openid_ax_create_new' =>t('Create new')
  35.       )
  36.   );
  37.   if(is_array($option)) {
  38.         $form['personas']['#options'] = array_merge($form['personas']['#options'], $option);
  39.   }
  40.   $form['select'] = array(
  41.     '#type' => 'submit',
  42.     '#value' => 'Select Persona',
  43.     '#submit' => array('openid_ax_persona_form_submit_select_persona')
  44.     );
  45.   $ax_values = db_query("SELECT * FROM {openid_ax_values} WHERE uid=%d",$user->uid );
  46.   while($row = db_fetch_array($ax_values)) {
  47.         $persona_value[$row['ax_id']] = $row['ax_values'];
  48.   }
  49.  
  50.   while($row = db_fetch_array($identifiers)) {
  51.         $form[$row['ax_id']] = array(
  52.             '#type' => 'textfield',
  53.             '#title' => t($row['identifier']),
  54.             '#default_value' => $persona_value[$row['ax_id']],
  55.             '#size' => 25,
  56.             '#maxlength' => 100
  57.         );
  58.         if(isset($persona_value[$row['ax_id']])) {
  59.           $form['has_value'.$row['ax_id']] = array(
  60.                 '#type' => 'hidden',
  61.           );
  62.           $form['intro'] = array(
  63.             '#type' => 'markup',
  64.             '#value' => t('To delete a value, delete the value in the particular field and then submit'),
  65.             '#weight' => -1
  66.       );
  67.         }
  68.  
  69.   }
  70.   $form['submit'] = array(
  71.     '#type' => 'submit',
  72.     '#value' => t('Submit'),
  73.   );
  74.   return $form;
  75. }
  76.  
  77. function openid_ax_persona_form_submit_select_persona(&$form, $form_state) {
  78.   global $user;
  79.   if($form_state['values']['personas']=='openid_ax_create_new') {
  80.         drupal_goto('user/'.$user->uid.'/persona/create');
  81.   }
  82.   else {
  83.         $persona = $form_state['values']['personas'];
  84.   }
  85.  
  86. }
  87.  
  88.  
  89. function openid_ax_persona_form_submit(&$form, $form_state) {
  90.   global $user;
  91.   $result = db_query("SELECT ax_id FROM {openid_ax_attributes}");
  92.   //difficult to use INSERT ... ON DUPLICATE KEY UPDATE
  93.   // as there is no unique key
  94.   static $success;
  95.   while($ax_id = db_fetch_array($result)) {
  96.     $ax_value = trim($form_state['values'][$ax_id['ax_id']]);
  97.     if($ax_value != ''||(isset($form_state['values'][has_value.$ax_id['ax_id']]))){
  98.           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
  99.             $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);
  100.           }
  101.           else {
  102.                 if($ax_value != '') {
  103.           $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);
  104.                 }
  105.                 else {
  106.                   $success = db_query("DELETE FROM {openid_ax_values} WHERE ax_id=%d AND uid=%d",$ax_id['ax_id'],$user->uid);
  107.                 }
  108.           }
  109.     }
  110.   }
  111.   if($success) {
  112.         drupal_set_message(t('Your persona values have been saved'));
  113.   }
  114. }