Projet

Général

Profil

Paste
Télécharger (3,42 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_bulk_operations / actions / user_cancel.action.inc @ 7547bb19

1
<?php
2
/**
3
 * @file
4
 * VBO action to cancel user accounts.
5
 */
6

    
7
function views_bulk_operations_user_cancel_action_info() {
8
  return array(
9
    'views_bulk_operations_user_cancel_action' => array(
10
      'type' => 'user',
11
      'label' => t('Cancel user account'),
12
      'configurable' => TRUE,
13
      'behavior' => array('deletes_property'),
14
      'triggers' => array('any'),
15
    ),
16
  );
17
}
18

    
19
function views_bulk_operations_user_cancel_action_form($context) {
20
  module_load_include('inc', 'user', 'user.pages');
21
  $form['user_cancel_method'] = array(
22
    '#type' => 'item',
23
    '#title' => t('When cancelling these accounts'),
24
  );
25
  $form['user_cancel_method'] += user_cancel_methods();
26
  // Remove method descriptions.
27
  foreach (element_children($form['user_cancel_method']) as $element) {
28
    unset($form['user_cancel_method'][$element]['#description']);
29
  }
30
  $admin_access = user_access('administer users');
31
  $default_notify = variable_get('user_mail_status_canceled_notify', FALSE);
32
  $form['user_cancel_notify'] = array(
33
    '#type' => 'checkbox',
34
    '#title' => t('Notify user when account is canceled.'),
35
    '#default_value' => ($admin_access ? FALSE : $default_notify),
36
    '#access' => $admin_access && $default_notify,
37
    '#description' => t('When enabled, the user will receive an e-mail notification after the account has been cancelled.'),
38
  );
39

    
40
  return $form;
41
}
42

    
43
function views_bulk_operations_user_cancel_action_submit($form, $form_state) {
44
  return array(
45
    'user_cancel_method' => $form_state['values']['user_cancel_method'],
46
    'user_cancel_notify' => $form_state['values']['user_cancel_notify'],
47
  );
48
}
49

    
50
function views_bulk_operations_user_cancel_action($account, $context) {
51
  global $user;
52
  // Prevent the user from cancelling itself.
53
  if ($account->uid == $user->uid) {
54
    return;
55
  }
56

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

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

    
74
    case 'user_cancel_reassign':
75
    case 'user_cancel_delete':
76
      // Send account canceled notification if option was checked.
77
      if (!empty($context['user_cancel_notify'])) {
78
        _user_mail_notify('status_canceled', $account);
79
      }
80
      // In cases when nodes are to be reassigned to UID 0, the user_delete must
81
      // not run until *after* the user_cancel has been invoked, otherwise the
82
      // nodes are deleted before they can be reassigned. Adding the user delete
83
      // to the batch queue ensures things happen in the correct sequence.
84
      $batch = array(
85
        'operations' => array(
86
          array('user_delete', array($account->uid)),
87
        ),
88
        'file' => drupal_get_path('module', 'node') . '/node.admin.inc',
89
      );
90
      batch_set($batch);
91
      watchdog('user', 'Deleted user: %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
92
      break;
93
  }
94
}