summaryrefslogtreecommitdiff
path: root/sites/all/modules/views_bulk_operations/actions/user_cancel.action.inc
blob: 147d29208134259152a21db1c45187ad0b6c0d14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
  * @file
  * VBO action to cancel user accounts.
  */

function views_bulk_operations_user_cancel_action_info() {
  return array('views_bulk_operations_user_cancel_action' => array(
    'type' => 'user',
    'label' => t('Cancel user account'),
    'configurable' => TRUE,
    'behavior' => array('deletes_property'),
    'triggers' => array('any'),
  ));
}

function views_bulk_operations_user_cancel_action_form($context) {
  module_load_include('inc', 'user', 'user.pages');
  $form['user_cancel_method'] = array(
    '#type' => 'item',
    '#title' => t('When cancelling these accounts'),
  );
  $form['user_cancel_method'] += user_cancel_methods();
  // Remove method descriptions.
  foreach (element_children($form['user_cancel_method']) as $element) {
    unset($form['user_cancel_method'][$element]['#description']);
  }
  $admin_access = user_access('administer users');
  $default_notify = variable_get('user_mail_status_canceled_notify', FALSE);
  $form['user_cancel_notify'] = array(
    '#type' => 'checkbox',
    '#title' => t('Notify user when account is canceled.'),
    '#default_value' => ($admin_access ? FALSE : $default_notify),
    '#access' => $admin_access && $default_notify,
    '#description' => t('When enabled, the user will receive an e-mail notification after the account has been cancelled.'),
  );

  return $form;
}

function views_bulk_operations_user_cancel_action_submit($form, $form_state) {
  return array(
    'user_cancel_method' => $form_state['values']['user_cancel_method'],
    'user_cancel_notify' => $form_state['values']['user_cancel_notify'],
  );
}

function views_bulk_operations_user_cancel_action($account, $context) {
  global $user;
  // Prevent the user from cancelling itself.
  if ($account->uid == $user->uid) {
    return;
  }

  // Allow other modules to react on the cancellation.
  if ($context['user_cancel_method'] != 'user_cancel_delete') {
    module_invoke_all('user_cancel', array(), $account, $context['user_cancel_method']);
  }

  switch ($context['user_cancel_method']) {
    case 'user_cancel_block':
    case 'user_cancel_block_unpublish':
    default:
      // Send account blocked notification if option was checked.
      if (!empty($context['user_cancel_notify'])) {
        _user_mail_notify('status_blocked', $account);
      }
      user_save($account, array('status' => 0));
      watchdog('user', 'Blocked user: %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
      break;

    case 'user_cancel_reassign':
    case 'user_cancel_delete':
      // Send account canceled notification if option was checked.
      if (!empty($context['user_cancel_notify'])) {
        _user_mail_notify('status_canceled', $account);
      }
      user_delete($account->uid);
      watchdog('user', 'Deleted user: %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
      break;
  }
}