You are here

function user_delete_submit in User Delete 6

Same name and namespace in other branches
  1. 5 user_delete.module \user_delete_submit()

Deal with the user/content after form submission

1 string reference to 'user_delete_submit'
user_delete_form_alter in ./user_delete.module
Implementation of hook_form_alter().

File

./user_delete.module, line 134
User delete - Let users delete their own account.

Code

function user_delete_submit($form, &$form_state) {
  global $user;
  $default_op = variable_get('user_delete_default_action', 0);
  $op = $default_op ? $default_op : $form_state['values']['user_delete_action'];
  $uid = $form_state['values']['_account']->uid;
  $account = user_load(array(
    'uid' => $uid,
  ));
  $backup = variable_get('user_delete_backup', 0);
  if (!$account) {
    drupal_set_message(t('The user account %id does not exist.', array(
      '%id' => $uid,
    )), 'error');
    watchdog('user', 'Attempted to cancel non-existing user account: %id.', array(
      '%id' => $uid,
    ), WATCHDOG_ERROR);
    return;
  }
  switch ($op) {
    case 'user_delete_block':

      // block user
      db_query("UPDATE {users} SET status = 0 WHERE uid = %d", $uid);
      drupal_set_message(t('%name has been blocked.', array(
        '%name' => check_plain($account->name),
      )));
      break;
    case 'user_delete_block_unpublish':

      // block user
      db_query("UPDATE {users} SET status = 0 WHERE uid = %d", $uid);

      // unpublish content
      db_query("UPDATE {node} SET status = 0 WHERE uid = %d", $uid);
      db_query("UPDATE {comments} SET status = 1 WHERE uid = %d", $uid);
      $edit['status'] = 0;
      module_invoke_all('user', 'update', $edit, $account);
      drupal_set_message(t('%name has been blocked, all submitted content from that user has been unpublished.', array(
        '%name' => check_plain($account->name),
      )));
      break;
    case 'user_delete_reassign':

      // Set redirect
      $redirect = variable_get('user_delete_redirect', 'node');

      // delete account
      user_delete($form_values, $uid);
      drupal_set_message(t('User record deleted. All submitted content from %name has been reassigned to %anonymous.', array(
        '%name' => check_plain($account->name),
        '%anonymous' => variable_get('anonymous', t('Anonymous')),
      )));
      break;
    case 'user_delete_delete':

      // TODO: Deleting/Backing-up nodes and comments should be done with
      // http://drupal.org/project/batchapi
      // Set redirect
      $redirect = variable_get('user_delete_redirect', 'node');

      // delete comments
      $result = db_query("SELECT cid FROM {comments} WHERE uid = %d", $uid);
      while ($row = db_fetch_object($result)) {

        // backup
        if ($backup) {
          $comment = _comment_load($row->cid);
          user_delete_backup($account, $comment);
        }
        user_delete_comment_delete($row->cid);
      }

      // delete nodes
      $result = db_query("SELECT nid FROM {node} WHERE uid = %d", $uid);
      while ($row = db_fetch_object($result)) {

        // backup
        if ($backup) {
          $node = node_load($row->nid);
          user_delete_backup($account, $node);
        }
        user_delete_node_delete($row->nid);
      }

      // backup
      if ($backup) {
        user_delete_backup($account);
      }

      // delete user
      user_delete($form_values, $uid);
      drupal_set_message(t('User record deleted. All submitted content from %name has been deleted.', array(
        '%name' => check_plain($account->name),
        '!anonymous' => variable_get('anonymous', t('Anonymous')),
      )));
      break;
  }

  // After cancelling account, ensure that user is logged out.
  // Destroy the current session.
  db_query("DELETE FROM {sessions} WHERE uid = %d", $account->uid);
  if ($account->uid == $user->uid) {

    // Load the anonymous user.
    $user = drupal_anonymous_user();

    // Set redirect
    $redirect = variable_get('user_delete_redirect', 'node');
  }

  // Clear the cache for anonymous users.
  cache_clear_all();

  // Redirect
  if (!empty($redirect)) {
    drupal_goto($redirect);
  }
}