openid_ax_persona_form_submit_select_persona

  1. <?php
  2. // $Id: openid_ax.pages.inc,v 1.8 2008/07/04 05:44:58 anshuprateek Exp $
  3.  
  4. /**
  5.  * @file
  6.  * Page callbacks for OpenID AX.
  7.  */
  8.  
  9. /**
  10.  * Primary endpoint callback - serves op_endpoint.
  11.  *
  12.  * @param array $request (Optional) request parameters.
  13.  */
  14. function openid_ax_endpoint($request = array()) {
  15.   module_load_include('inc', 'openid');
  16.   module_load_include('inc', 'openid_ax');
  17.   if (count($request) == 0) {
  18.     $request = _openid_response();
  19.   }
  20.   $ax_request = openid_ax_process($request);
  21.   return $ax_request;
  22. }
  23.  
  24. /**
  25.  * Menu callback to continue authentication process after user login. This
  26.  * callback is encountered when a user tries to login to an RP but does not yet
  27.  * have a valid local session
  28.  */
  29. function openid_ax_continue() {
  30.   if (isset($_SESSION['openid_ax']['request'])) {
  31.     $request_ax = $_SESSION['openid_ax']['request'];
  32.     unset($_SESSION['openid_ax']['request']);
  33.     return openid_ax_endpoint($request_ax);
  34.   }
  35.   else {
  36.     drupal_set_message(t('Session expired'));
  37.     drupal_goto();
  38.   }
  39. }
  40.  
  41. /**
  42.  * Page callback for processing openid_ax_send.
  43.  */
  44. function openid_ax_send() {
  45.   module_load_include('inc', 'openid');
  46.   module_load_include('inc', 'openid_ax');
  47.   $response = _openid_response();
  48.   $ax_response = openid_ax_fetch_response($response);
  49.   drupal_goto($_SESSION['openid_ax']['return_to'],$ax_response);
  50. }
  51.  
  52. /**
  53.  *Form for user interaction before sending AX values to RP
  54.  */
  55. function openid_ax_form(&$form_state,&$ax_response) {
  56.   global $user;
  57.   $form_state['ax_response'] = $ax_response;
  58.   $realm = $form_state['post']['openid_realm'];
  59.   if($realm == '') {
  60.         $realm = $_SESSION['openid_ax']['realm'];
  61.         unset($_SESSION['openid_ax']['realm']);
  62.   }
  63.   $form = array();
  64.   $form['intro'] = array(
  65.     '#type' => 'markup',
  66.     '#value' => '<p>'. t('Your following details are being send to %site, would you like to continue?', array('%site' => $realm)) . '</p>'
  67.   );
  68.   foreach($ax_response as $name=>$value) {
  69.         if(strstr($name,'value')){
  70.           $form[$name] = array(
  71.             '#type' => 'textfield',
  72.             '#title' => t(substr(strrchr($name,'value.'),6)),
  73.             '#value' => $value,
  74.             '#size' => 14,
  75.             '#maxlength' => 100
  76.           );
  77.     }
  78.     else {
  79.       $form[$name] = array(
  80.             '#type' => 'hidden',
  81.             '#title' => $name,
  82.             '#value' => $value
  83.           );
  84.     }
  85.   }
  86.  
  87.   $form['#action'] = url('openid/ax/send');
  88.  
  89.   $form['submit'] = array(
  90.     '#type' => 'submit',
  91.     '#value' => t('Yes; Send my data'),
  92.   );
  93.  
  94.   $form['cancel'] = array(
  95.     '#type' => 'submit',
  96.     '#value' => t('No! Don\'t send my data'),
  97.     '#submit' => array('openid_ax_form_submit_cancel')
  98.   );
  99.  
  100.   return $form;
  101.  
  102.  
  103. }
  104.  
  105. function openid_ax_persona() {
  106.   return drupal_get_form('openid_ax_persona_form');
  107. }
  108.  
  109. /**
  110.  *Form for creating/editing a persona
  111.  */
  112. function openid_ax_persona_form($form_state, $persona_id = 0) {
  113.   global $user;
  114.   $form = array();
  115.   $identifiers = db_query("SELECT * FROM {openid_ax_attributes}");
  116.   $persona = db_query("SELECT * FROM {openid_ax_persona} where uid=%d", $user->uid);
  117.   while($persona_value = db_fetch_array($persona)) {
  118.         $option[$persona_value['persona_id']] = $persona_value['persona_name'];
  119.   }
  120.   $form['personas'] = array(
  121.     '#type' => 'select',
  122.     '#title' => 'Personas',
  123.     '#options' => array(
  124.     '0' => t('Default'),
  125.     'openid_ax_create_new' =>t('Create new')
  126.       )
  127.   );
  128.   if(is_array($option)) {
  129.         $form['personas']['#options'] = array_merge($form['personas']['#options'], $option);
  130.   }
  131.   $form['select'] = array(
  132.     '#type' => 'submit',
  133.     '#value' => 'Select Persona',
  134.     '#submit' => array('openid_ax_persona_form_submit_select_persona')
  135.     );
  136.   $ax_values = db_query("SELECT * FROM {openid_ax_values} WHERE uid=%d",$user->uid );
  137.   while($row = db_fetch_array($ax_values)) {
  138.         $persona_value[$row['ax_id']] = $row['ax_values'];
  139.   }
  140.  
  141.   while($row = db_fetch_array($identifiers)) {
  142.         $form[$row['ax_id']] = array(
  143.             '#type' => 'textfield',
  144.             '#title' => t($row['identifier']),
  145.             '#default_value' => $persona_value[$row['ax_id']],
  146.             '#size' => 25,
  147.             '#maxlength' => 100
  148.         );
  149.         if(isset($persona_value[$row['ax_id']])) {
  150.           $form['has_value'.$row['ax_id']] = array(
  151.                 '#type' => 'hidden',
  152.           );
  153.           $form['intro'] = array(
  154.             '#type' => 'markup',
  155.             '#value' => t('To delete a value, delete the value in the particular field and then submit'),
  156.             '#weight' => -1
  157.       );
  158.         }
  159.  
  160.   }
  161.   $form['submit'] = array(
  162.     '#type' => 'submit',
  163.     '#value' => t('Submit'),
  164.   );
  165.   return $form;
  166. }
  167.  
  168. function openid_ax_persona_form_submit_select_persona(&$form, $form_state) {
  169.   global $user;
  170.   if($form_state['values']['personas']=='openid_ax_create_new') {
  171.         drupal_goto('user/'.$user->uid.'/persona/create');
  172.   }
  173.   else {
  174.         $persona = $form_state['values']['personas'];
  175.   }
  176.  
  177. }
  178.  
  179. function openid_ax_create_persona() {
  180.   return drupal_get_form('openid_ax_new_persona_form');  
  181. }
  182. function openid_ax_new_persona_form() {
  183.   $form = array();
  184.   $form['persona'] = array(
  185.         '#type' =>'textfield',
  186.         '#title' => t('New Persona name'),
  187.         '#default_value' => '',
  188.         '#required' => TRUE,
  189.         '#size' => 20
  190.         );
  191.   $form['submit'] = array(
  192.     '#type' => 'submit',
  193.     '#value' => t('Submit'),
  194.   );
  195.   return $form;
  196. }
  197.  
  198. function openid_ax_new_persona_form_submit(&$form, $form_state) {
  199.   global $user;
  200.   $persona_exists = db_result(db_query("SELECT * FROM {openid_ax_persona} WHERE uid=%d AND persona_name='%s'", $user->uid, $form_state['values']['persona']));
  201.   if($persona_exists) {
  202.         drupal_set_message(t('A persona with that name already exists. Please select another persona name.'), 'error');
  203.   }
  204.   else {
  205.         $max_persona_id = db_result(db_query("SELECT MAX(persona_id) FROM {openid_ax_persona} WHERE uid='%d'", $user->uid));
  206.         $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']);
  207.         if($create_persona) {
  208.           drupal_set_message(t('New Persona created'));
  209.           drupal_goto('user/'.$user->uid.'/persona');
  210.         }
  211.   }
  212.    
  213. }
  214. function openid_ax_persona_form_submit(&$form, $form_state) {
  215.   global $user;
  216.   $result = db_query("SELECT ax_id FROM {openid_ax_attributes}");
  217.   //difficult to use INSERT ... ON DUPLICATE KEY UPDATE
  218.   // as there is no unique key
  219.   static $success;
  220.   while($ax_id = db_fetch_array($result)) {
  221.     $ax_value = trim($form_state['values'][$ax_id['ax_id']]);
  222.     if($ax_value != ''||(isset($form_state['values'][has_value.$ax_id['ax_id']]))){
  223.           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
  224.             $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);
  225.           }
  226.           else {
  227.                 if($ax_value != '') {
  228.           $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);
  229.                 }
  230.                 else {
  231.                   $success = db_query("DELETE FROM {openid_ax_values} WHERE ax_id=%d AND uid=%d",$ax_id['ax_id'],$user->uid);
  232.                 }
  233.           }
  234.     }
  235.   }
  236.   if($success) {
  237.         drupal_set_message(t('Your persona values have been saved'));
  238.   }
  239. }