user_cancel.action.inc 2.9 KB

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