workflow_notify_og.module 3.5 KB

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