user_roles.action.inc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. function views_bulk_operations_user_roles_action_info() {
  3. return array('views_bulk_operations_user_roles_action' => array(
  4. 'type' => 'user',
  5. 'label' => t('Modify user roles'),
  6. 'configurable' => TRUE,
  7. 'triggers' => array('any'),
  8. ));
  9. }
  10. function views_bulk_operations_user_roles_action_form($context) {
  11. $roles = user_roles(TRUE);
  12. unset($roles[DRUPAL_AUTHENTICATED_RID]); // Can't edit authenticated role.
  13. $form['add_roles'] = array(
  14. '#type' => 'select',
  15. '#multiple' => TRUE,
  16. '#title' => t('Add roles'),
  17. '#description' => t('Choose one or more roles you would like to assign to the selected users.'),
  18. '#options' => $roles,
  19. '#size' => 5
  20. );
  21. $form['remove_roles'] = array(
  22. '#type' => 'select',
  23. '#multiple' => TRUE,
  24. '#title' => t('Remove roles'),
  25. '#description' => t('Choose one or more roles you would like to remove from the selected users.'),
  26. '#options' => $roles,
  27. '#size' => 5
  28. );
  29. return $form;
  30. }
  31. function views_bulk_operations_user_roles_action_validate($form, $form_state) {
  32. if (!$form_state['values']['add_roles'] && !$form_state['values']['remove_roles']) {
  33. form_set_error('add_roles', t('You have not chosen any role to add or remove. Please select something to do.'));
  34. }
  35. }
  36. function views_bulk_operations_user_roles_action_submit($form, $form_state) {
  37. return array(
  38. 'add_roles' => array_filter($form_state['values']['add_roles']),
  39. 'remove_roles' => array_filter($form_state['values']['remove_roles']),
  40. );
  41. }
  42. function views_bulk_operations_user_roles_action($user, $context) {
  43. $wrapper = entity_metadata_wrapper('user', $user);
  44. if (!$wrapper->roles->access("update")) {
  45. // No access.
  46. return;
  47. }
  48. $roles = $wrapper->roles->value();
  49. if (is_array($context['add_roles'])) {
  50. $roles = array_merge($roles, $context['add_roles']);
  51. }
  52. if (is_array($context['remove_roles'])) {
  53. $roles = array_diff($roles, $context['remove_roles']);
  54. }
  55. $wrapper->roles->set($roles);
  56. $wrapper->save();
  57. }