workflow_notify_og.module 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @file
  4. * Notify roles by OG Groups for Workflow state transitions.
  5. */
  6. /**
  7. * Implements hook_workflow_notify().
  8. * @param $op - The operation (columns, users).
  9. * @param $args - The arguments for this call.
  10. * - may be:
  11. * 'columns' - the current list of table headings.
  12. * 'users' - The current list of users.
  13. * 'node' - The current node for getting groups focus.
  14. * 'state' - The state the node is moving to.
  15. *
  16. * @return none - Modify the list by reference.
  17. */
  18. function workflow_notify_og_workflow_notify($op, &$args) {
  19. switch ($op) {
  20. case 'columns':
  21. // Add the column heading for this module.
  22. $args['columns']['limit_by_group'] = t('Limit by group');
  23. break;
  24. case 'users':
  25. $limit = variable_get('workflow_notify_og', array());
  26. // Is this a state we care about?
  27. if (isset($limit[$args['state']]) && $limit[$args['state']]) {
  28. // Yes, so get the node's groups and make sure it has some.
  29. $groups = field_get_items('node', $args['node'], 'og_group_ref');
  30. if ($groups) {
  31. // Get the list of user accounts and check each one.
  32. $accounts = $args['users'];
  33. foreach ($accounts as $uid => $account) {
  34. $keep = FALSE;
  35. // Check each group for user's membership.
  36. foreach ($groups as $group) {
  37. if (og_is_member('node', $group['target_id'], 'user', $account)) {
  38. $keep = TRUE;
  39. break;
  40. }
  41. }
  42. // Do we find a group?
  43. if ($keep == FALSE) {
  44. // No, so remove them from the list.
  45. unset($args['users'][$uid]);
  46. }
  47. }
  48. }
  49. }
  50. break;
  51. case 'tokens':
  52. $groups = field_get_items('node', $args['node'], 'og_group_ref');
  53. $query = "SELECT g.entity_id AS gid, n.title AS name "
  54. . "FROM {field_data_group_group} g "
  55. . "INNER JOIN {node} n ON n.nid = g.entity_id "
  56. . "WHERE g.deleted = 0 ";
  57. $group_names = db_query($query)->fetchAllKeyed();
  58. if (!empty($groups)) {
  59. $list = array();
  60. foreach ($groups as $group) {
  61. $list[] = $group_names[$group['target_id']];
  62. }
  63. $args['tokens']['@groups'] = implode(', ', $list);
  64. }
  65. break;
  66. }
  67. }
  68. /**
  69. * Implements hook_form_alter().
  70. * Add a column for limiting user groups by OG group.
  71. */
  72. function workflow_notify_og_form_alter(&$form, &$form_state, $form_id) {
  73. switch($form_id) {
  74. case 'workflow_notify_settings_form':
  75. $limit = variable_get('workflow_notify_og', array());
  76. // Add a group limit flag to each state.
  77. foreach ($form['states'] as $sid => $element) {
  78. $form['states'][$sid]['limit_by_group'] = array(
  79. '#type' => 'radios',
  80. '#options' => array(t('No'), t('Yes')),
  81. '#attributes' => array('class' => array('limit-by-group')),
  82. '#default_value' => (isset($limit[$sid]) ? $limit[$sid] : 0),
  83. );
  84. }
  85. // Add our submission handler.
  86. $form['#submit'][] = 'workflow_notify_og_form_submit';
  87. }
  88. }
  89. /**
  90. * Submission handler.
  91. */
  92. function workflow_notify_og_form_submit(&$form, &$form_state) {
  93. $workflow = $form['#workflow'];
  94. $limit = variable_get('workflow_notify_og', array());
  95. // Check each state for limit flags.
  96. foreach ($form_state['values']['states'] as $sid => $values) {
  97. $limit[$sid] = $values['limit_by_group'];
  98. }
  99. // Save the new limit flags.
  100. variable_set('workflow_notify_og', $limit);
  101. $form_state['redirect'] = "admin/config/workflow/workflow/$workflow->wid";
  102. }