Commit 6ecef0ee authored by Gábor Hojtsy's avatar Gábor Hojtsy

#156796 by webernet, asimmonds and pwolanin: user editing forms cleanup and...

#156796 by webernet, asimmonds and pwolanin: user editing forms cleanup and fixing deletion API remainings
parent 5658de99
......@@ -1035,7 +1035,8 @@ function user_menu() {
$items['user/%user/delete'] = array(
'title' => 'Delete',
'page callback' => 'user_edit',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_confirm_delete', 1),
'access callback' => 'user_access',
'access arguments' => array('administer users'),
'type' => MENU_CALLBACK,
......@@ -1043,8 +1044,8 @@ function user_menu() {
$items['user/%user/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_edit'),
'page callback' => 'user_edit',
'page arguments' => array(1),
'access callback' => 'user_edit_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
......@@ -1063,7 +1064,8 @@ function user_menu() {
$items['user/%user/edit/'. $category['name']] = array(
'title callback' => 'check_plain',
'title arguments' => array($category['title']),
'page arguments' => array('user_edit', 3),
'page callback' => 'user_edit',
'page arguments' => array(1, 3),
'type' => MENU_LOCAL_TASK,
'weight' => $category['weight'],
);
......@@ -1569,6 +1571,7 @@ function user_edit_form(&$form_state, $uid, $edit, $register = FALSE) {
// Account information:
$form['account'] = array('#type' => 'fieldset',
'#title' => t('Account information'),
'#weight' => -10,
);
if (user_access('change own username') || $admin || $register) {
$form['account']['name'] = array('#type' => 'textfield',
......@@ -1687,79 +1690,47 @@ function _user_edit_submit($uid, &$edit) {
}
}
function user_edit($form_state, $category = 'account') {
global $user;
/**
* Menu callback; edit a user account or one of their profile categories.
*/
function user_edit($account, $category = 'account') {
drupal_set_title(check_plain($account->name));
return drupal_get_form('user_profile_form', $account, $category);
}
$account = user_load(array('uid' => arg(1)));
if ($account === FALSE) {
drupal_set_message(t('The account does not exist or has already been deleted.'));
drupal_goto('admin/user/user');
}
$op = !empty($_POST['op']) ? $_POST['op'] : '';
$edit = $op ? $_POST : (array)$account;
/**
* Form builder; edit a user account or one of their profile categories.
*
* @ingroup forms
* @see user_profile_form_validate()
* @see user_profile_form_submit().
* @see user_edit_delete_submit().
*/
function user_profile_form($form_state, $account, $category = 'account') {
if (arg(2) == 'delete') {
if (!empty($edit['confirm'])) {
user_delete($edit, $account->uid);
drupal_goto('admin/user/user');
}
else {
return drupal_get_form('user_confirm_delete', $account->name, $account->uid);
}
}
else if ($op == t('Delete')) {
$destination = '';
if (isset($_REQUEST['destination'])) {
$destination = drupal_get_destination();
unset($_REQUEST['destination']);
}
// Note: we redirect from user/uid/edit to user/uid/delete to make the tabs disappear.
drupal_goto("user/$account->uid/delete", $destination);
}
$edit = (empty($form_state['values'])) ? (array)$account : $form_state['values'];
$form = _user_forms($edit, $account, $category);
$form['_category'] = array('#type' => 'value', '#value' => $category);
$form['_account'] = array('#type' => 'value', '#value' => $account);
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'), '#weight' => 30);
if (user_access('administer users')) {
$form['delete'] = array('#type' => 'submit', '#value' => t('Delete'), '#weight' => 31);
$form['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#weight' => 31,
'#submit' => array('user_edit_delete_submit'),
);
}
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['#validate'][] = 'user_edit_validate';
drupal_set_title(check_plain($account->name));
return $form;
}
function user_confirm_delete($name, $uid) {
$options = array('description' => t('All submissions made by this user will be attributed to the anonymous account. This action cannot be undone.'));
return confirm_form(array(),
t('Are you sure you want to delete the account %name?', array('%name' => $name)),
'user/'. $uid,
$options);
}
/**
* Delete a user.
*
* @param $edit An array of submitted form values.
* @param $uid The user ID of the user to delete.
* Validation function for the user account and profile editing form.
*/
function user_delete($edit, $uid) {
$account = user_load(array('uid' => $uid));
sess_destroy_uid($uid);
_user_mail_notify('status_deleted', $account);
db_query('DELETE FROM {users} WHERE uid = %d', $uid);
db_query('DELETE FROM {users_roles} WHERE uid = %d', $uid);
db_query('DELETE FROM {authmap} WHERE uid = %d', $uid);
$variables = array('%name' => $account->name, '%email' => '<'. $account->mail .'>');
watchdog('user', 'Deleted user: %name %email.', $variables, WATCHDOG_NOTICE);
drupal_set_message(t('%name has been deleted.', $variables));
module_invoke_all('user', 'delete', $edit, $account);
}
function user_edit_validate($form, &$form_state) {
function user_profile_form_validate($form, &$form_state) {
user_module_invoke('validate', $form_state['values'], $form_state['values']['_account'], $form_state['values']['_category']);
// Validate input to ensure that non-privileged users can't alter protected data.
if ((!user_access('administer users') && array_intersect(array_keys($form_state['values']), array('uid', 'init', 'session'))) || (!user_access('administer access control') && isset($form_state['values']['roles']))) {
......@@ -1769,7 +1740,10 @@ function user_edit_validate($form, &$form_state) {
}
}
function user_edit_submit($form, &$form_state) {
/**
* Submit function for the user account and profile editing form.
*/
function user_profile_form_submit($form, &$form_state) {
$account = $form_state['values']['_account'];
$category = $form_state['values']['_category'];
unset($form_state['values']['_account'], $form_state['values']['op'], $form_state['values']['submit'], $form_state['values']['delete'], $form_state['values']['form_token'], $form_state['values']['form_id'], $form_state['values']['_category']);
......@@ -1783,6 +1757,65 @@ function user_edit_submit($form, &$form_state) {
return;
}
/**
* Submit function for the 'Delete' button on the user edit form.
*/
function user_edit_delete_submit($form, &$form_state) {
$destination = '';
if (isset($_REQUEST['destination'])) {
$destination = drupal_get_destination();
unset($_REQUEST['destination']);
}
// Note: We redirect from user/uid/edit to user/uid/delete to make the tabs disappear.
$form_state['redirect'] = array("user/". $form_state['values']['_account']->uid ."/delete", $destination);
}
/**
* Form builder; confirm form for user deletion.
*
* @ingroup forms
* @see user_confirm_delete_submit().
*/
function user_confirm_delete(&$form_state, $account) {
$form['_account'] = array('#type' => 'value', '#value' => $account);
return confirm_form($form,
t('Are you sure you want to delete the account %name?', array('%name' => $account->name)),
'user/'. $account->uid,
t('All submissions made by this user will be attributed to the anonymous account. This action cannot be undone.'),
t('Delete'), t('Cancel'));
}
/**
* Submit function for the confirm form for user deletion.
*/
function user_confirm_delete_submit($form, &$form_state) {
user_delete($form_state['values'], $form_state['values']['_account']->uid);
if (!isset($_REQUEST['destination'])) {
$form_state['redirect'] = 'admin/user/user';
}
}
/**
* Delete a user.
*
* @param $edit An array of submitted form values.
* @param $uid The user ID of the user to delete.
*/
function user_delete($edit, $uid) {
$account = user_load(array('uid' => $uid));
sess_destroy_uid($uid);
_user_mail_notify('status_deleted', $account);
db_query('DELETE FROM {users} WHERE uid = %d', $uid);
db_query('DELETE FROM {users_roles} WHERE uid = %d', $uid);
db_query('DELETE FROM {authmap} WHERE uid = %d', $uid);
$variables = array('%name' => $account->name, '%email' => '<'. $account->mail .'>');
watchdog('user', 'Deleted user: %name %email.', $variables, WATCHDOG_NOTICE);
drupal_set_message(t('%name has been deleted.', $variables));
module_invoke_all('user', 'delete', $edit, $account);
}
function user_view($account) {
drupal_set_title(check_plain($account->name));
// Retrieve all profile fields and attach to $account->content.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment