user_cancel.action.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * @file
  4. * VBO action to cancel user accounts.
  5. */
  6. function views_bulk_operations_user_cancel_action_info() {
  7. return array('views_bulk_operations_user_cancel_action' => array(
  8. 'type' => 'user',
  9. 'label' => t('Cancel user account'),
  10. 'configurable' => TRUE,
  11. 'behavior' => array('deletes_property'),
  12. ));
  13. }
  14. function views_bulk_operations_user_cancel_action_form($context) {
  15. module_load_include('inc', 'user', 'user.pages');
  16. $form['user_cancel_method'] = array(
  17. '#type' => 'item',
  18. '#title' => t('When cancelling these accounts'),
  19. );
  20. $form['user_cancel_method'] += user_cancel_methods();
  21. // Remove method descriptions.
  22. foreach (element_children($form['user_cancel_method']) as $element) {
  23. unset($form['user_cancel_method'][$element]['#description']);
  24. }
  25. $admin_access = user_access('administer users');
  26. $default_notify = variable_get('user_mail_status_canceled_notify', FALSE);
  27. $form['user_cancel_notify'] = array(
  28. '#type' => 'checkbox',
  29. '#title' => t('Notify user when account is canceled.'),
  30. '#default_value' => ($admin_access ? FALSE : $default_notify),
  31. '#access' => $admin_access && $default_notify,
  32. '#description' => t('When enabled, the user will receive an e-mail notification after the account has been cancelled.'),
  33. );
  34. return $form;
  35. }
  36. function views_bulk_operations_user_cancel_action_submit($form, $form_state) {
  37. return array(
  38. 'user_cancel_method' => $form_state['values']['user_cancel_method'],
  39. 'user_cancel_notify' => $form_state['values']['user_cancel_notify'],
  40. );
  41. }
  42. function views_bulk_operations_user_cancel_action($account, $context) {
  43. global $user;
  44. // Prevent the user from cancelling itself.
  45. if ($account->uid == $user->uid) {
  46. return;
  47. }
  48. // Allow other modules to react on the cancellation.
  49. if ($context['user_cancel_method'] != 'user_cancel_delete') {
  50. module_invoke_all('user_cancel', array(), $account, $context['user_cancel_method']);
  51. }
  52. switch ($context['user_cancel_method']) {
  53. case 'user_cancel_block':
  54. case 'user_cancel_block_unpublish':
  55. default:
  56. // Send account blocked notification if option was checked.
  57. if (!empty($context['user_cancel_notify'])) {
  58. _user_mail_notify('status_blocked', $account);
  59. }
  60. user_save($account, array('status' => 0));
  61. watchdog('user', 'Blocked user: %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
  62. break;
  63. case 'user_cancel_reassign':
  64. case 'user_cancel_delete':
  65. // Send account canceled notification if option was checked.
  66. if (!empty($context['user_cancel_notify'])) {
  67. _user_mail_notify('status_canceled', $account);
  68. }
  69. user_delete($account->uid);
  70. watchdog('user', 'Deleted user: %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
  71. break;
  72. }
  73. }