privatemsg_groups.module

Allows to send messages to all members of an organic group

File

privatemsg_groups/privatemsg_groups.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Allows to send messages to all members of an organic group
  5. */
  6. /**
  7. * Implements hook_permissions().
  8. */
  9. function privatemsg_groups_permission() {
  10. $permissions = array(
  11. 'write privatemsg to all organic groups' => array(
  12. 'title' => t('Write private messages to all organic groups'),
  13. 'description' => t('Allows to write messages to all users which belong to a specific organic group.'),
  14. ),
  15. 'write privatemsg to own organic groups' => array(
  16. 'title' => t('Write private messages to own organic groups'),
  17. 'description' => t('Allows to write messages to all users which belong to own organic group.'),
  18. ),
  19. 'view organic groups recipients' => array(
  20. 'title' => t('View organic groups recipients'),
  21. 'description' => t('Ability to view organic groups recipients and the name of these organic groups.'),
  22. ),
  23. );
  24. return $permissions;
  25. }
  26. /**
  27. * Implements hook_og_permission().
  28. */
  29. function privatemsg_groups_og_permission() {
  30. return array(
  31. 'write privatemsg to group' => array(
  32. 'title' => t('Write privatemsg to an organic group'),
  33. 'description' => t("Allow user to write private messages to an organic group."),
  34. 'default role' => array(OG_ADMINISTRATOR_ROLE),
  35. ),
  36. );
  37. }
  38. /**
  39. * Implements hook_theme().
  40. */
  41. function privatemsg_groups_theme() {
  42. return array(
  43. 'privatemsg_groups_format' => array(
  44. 'variables' => array('recipient' => NULL, 'options' => array()),
  45. ),
  46. );
  47. }
  48. /**
  49. * Implements hook_privatemsg_recipient_type_info().
  50. */
  51. function privatemsg_groups_privatemsg_recipient_type_info() {
  52. $info = array();
  53. foreach (og_get_all_group_bundle() as $entity_type => $bundles) {
  54. $entity_info = entity_get_info($entity_type);
  55. $name = $entity_info['label'];
  56. $info[_privatemsg_groups_get_group_recipient_type($entity_type)] = array(
  57. 'name' => t('Organic Group: ') . $entity_info['label'],
  58. 'description' => t('Enter the name of an organic group to write a message to all users in that group.'),
  59. 'load' => 'privatemsg_groups_load_multiple',
  60. 'format' => 'privatemsg_groups_format',
  61. 'autocomplete' => 'privatemsg_groups_autocomplete',
  62. 'generate recipients' => 'privatemsg_groups_load_recipients',
  63. 'count' => 'privatemsg_groups_count_recipients',
  64. 'write callback' => 'privatemsg_groups_write_access',
  65. 'view access' => 'view organic groups recipients',
  66. );
  67. }
  68. return $info;
  69. }
  70. /**
  71. * Write permission check for group recipients.
  72. */
  73. function privatemsg_groups_write_access($recipient) {
  74. // Check if user has permission to write to all groups.
  75. if (user_access('write privatemsg to all organic groups')) {
  76. return TRUE;
  77. }
  78. // Check if user has permission to write to own groups.
  79. if (!user_access('write privatemsg to own organic groups')) {
  80. return FALSE;
  81. }
  82. if ($recipient) {
  83. $group_type = _privatemsg_groups_get_group_type($recipient->type);
  84. $gid = $recipient->recipient;
  85. return og_user_access($group_type, $gid, 'write privatemsg to group');
  86. }
  87. // Give access only to users that have group subscriptions.
  88. $gids = og_get_groups_by_user();
  89. return !empty($gids);
  90. }
  91. /**
  92. * Load a number of ogs based on their group ids.
  93. */
  94. function privatemsg_groups_load_multiple($gids, $type) {
  95. $recipients = array();
  96. $group_type = _privatemsg_groups_get_group_type($type);
  97. $groups = entity_load($group_type, $gids);
  98. foreach ($groups as $gid => $group) {
  99. $recipient = clone $group;
  100. $recipient->type = $type;
  101. $recipient->recipient = $gid;
  102. $recipients[privatemsg_recipient_key($recipient)] = $recipient;
  103. }
  104. return $recipients;
  105. }
  106. /**
  107. * Format a role to be displayed as a recipient.
  108. */
  109. function theme_privatemsg_groups_format($variables) {
  110. $recipient = $variables['recipient'];
  111. $options = $variables['options'];
  112. $group_type = _privatemsg_groups_get_group_type($recipient->type);
  113. $gid = $recipient->recipient;
  114. $group = entity_load_single($group_type, $gid);
  115. $name = entity_label($group_type, $group);
  116. if (!empty($options['plain'])) {
  117. if (!empty($options['unique'])) {
  118. $name .= ' [group]';
  119. }
  120. return $name;
  121. }
  122. return t('%group (group)', array('%group' => $name));
  123. }
  124. /**
  125. * Loads users in a specific group.
  126. */
  127. function privatemsg_groups_load_recipients($recipient, $limit, $offset) {
  128. $group_type = _privatemsg_groups_get_group_type($recipient->type);
  129. $gid = $recipient->recipient;
  130. $query = new EntityFieldQuery();
  131. $query
  132. ->entityCondition('entity_type', 'og_membership')
  133. ->propertyCondition('group_type', $group_type)
  134. ->propertyCondition('gid', $gid)
  135. ->propertyCondition('entity_type', 'user')
  136. ->propertyCondition('state', OG_STATE_ACTIVE)
  137. ->range($offset, $limit);
  138. if ($result = $query->execute()) {
  139. $mids = array_keys($result['og_membership']);
  140. $memberships = og_membership_load_multiple($mids);
  141. $uids = array();
  142. foreach($memberships as $membership) {
  143. $uids[] = $membership->etid;
  144. }
  145. return $uids;
  146. }
  147. return array();
  148. }
  149. /**
  150. * Return the number of users in a group.
  151. */
  152. function privatemsg_groups_count_recipients($recipient) {
  153. $group_type = _privatemsg_groups_get_group_type($recipient->type);
  154. $gid = $recipient->recipient;
  155. $query = new EntityFieldQuery();
  156. $query
  157. ->entityCondition('entity_type', 'og_membership')
  158. ->propertyCondition('group_type', $group_type)
  159. ->propertyCondition('gid', $gid)
  160. ->propertyCondition('entity_type', 'user')
  161. ->propertyCondition('state', OG_STATE_ACTIVE);
  162. return $query->count()->execute();
  163. }
  164. /**
  165. * Provides autocomplete suggestions for group.
  166. */
  167. function privatemsg_groups_autocomplete($fragment, $names, $limit, $type) {
  168. $group_type = _privatemsg_groups_get_group_type($type);
  169. $gids = og_get_all_group($group_type);
  170. $groups = entity_load($group_type, $gids);
  171. $suggestions = array();
  172. foreach ($groups as $gid => $group) {
  173. $recipient = clone $group;
  174. $recipient->type = $type;
  175. $recipient->recipient = $gid;
  176. if (privatemsg_groups_write_access($recipient)) {
  177. $name = entity_label($group_type, $group);
  178. if (stripos($name, $fragment) !== false && !in_array($name, $names)) {
  179. $suggestions[privatemsg_recipient_key($recipient)] = $recipient;
  180. if (count($suggestions) >= $limit) {
  181. return $suggestions;
  182. }
  183. }
  184. }
  185. }
  186. return $suggestions;
  187. }
  188. /**
  189. * Implements hook_privatemsg_name_lookup().
  190. */
  191. function privatemsg_groups_privatemsg_name_lookup($string) {
  192. // Remove optional group specifier.
  193. $string = trim(str_replace(t('[group]'), '', $string));
  194. foreach (og_get_all_group_bundle() as $group_type => $bundles) {
  195. $type = _privatemsg_groups_get_group_recipient_type($group_type);
  196. $type_info = privatemsg_recipient_get_type($type);
  197. if ($type_info) {
  198. $gids = og_get_all_group($group_type);
  199. $groups = entity_load($group_type, $gids);
  200. foreach ($groups as $gid => $group) {
  201. $name = entity_label($group_type, $group);
  202. if ($string == $name) {
  203. $recipient = clone $group;
  204. $recipient->type = $type;
  205. $recipient->recipient = $gid;
  206. return array(privatemsg_recipient_key($recipient) => $recipient);
  207. }
  208. }
  209. }
  210. }
  211. }
  212. /**
  213. * Given a group entity type, return the recipient type name.
  214. */
  215. function _privatemsg_groups_get_group_recipient_type($group_type) {
  216. return 'og:' . $group_type;
  217. }
  218. /**
  219. * Given a recipient type name, check to see if it is a group recipient type.
  220. */
  221. function _privatemsg_groups_is_group_recipient_type($recipient_type) {
  222. return strpos($recipient_type, 'og:', 0) === 0;
  223. }
  224. /**
  225. * Given a group recipient type name, return the group entity type.
  226. */
  227. function _privatemsg_groups_get_group_type($group_recipient_type) {
  228. return str_replace('og:', '', $group_recipient_type);
  229. }

Functions

Namesort descending Description
privatemsg_groups_autocomplete Provides autocomplete suggestions for group.
privatemsg_groups_count_recipients Return the number of users in a group.
privatemsg_groups_load_multiple Load a number of ogs based on their group ids.
privatemsg_groups_load_recipients Loads users in a specific group.
privatemsg_groups_og_permission Implements hook_og_permission().
privatemsg_groups_permission Implements hook_permissions().
privatemsg_groups_privatemsg_name_lookup Implements hook_privatemsg_name_lookup().
privatemsg_groups_privatemsg_recipient_type_info Implements hook_privatemsg_recipient_type_info().
privatemsg_groups_theme Implements hook_theme().
privatemsg_groups_write_access Write permission check for group recipients.
theme_privatemsg_groups_format Format a role to be displayed as a recipient.
_privatemsg_groups_get_group_recipient_type Given a group entity type, return the recipient type name.
_privatemsg_groups_get_group_type Given a group recipient type name, return the group entity type.
_privatemsg_groups_is_group_recipient_type Given a recipient type name, check to see if it is a group recipient type.