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' => '
'. t('Your following details are being send to %site, would you like to continue?', array('%site' => $realm)) . '
' ); - 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('To delete a value, delete the value in the particular field and then submit'), - '#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', - 'Edit', - '-N/A-', - '-N/A-' - ); - while($profile = db_fetch_array($profiles)) { - $rows[$profile['persona_name']] = array ( - $profile['persona_name'], - 'Edit ', - 'Rename', - 'Delete', - ); + 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('Copy values from one persona into another:'), - ); - $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 '