privatemsg_roles.module

  1. 7-1 privatemsg_roles/privatemsg_roles.module
  2. 7-2 privatemsg_roles/privatemsg_roles.module
  3. 6-2 privatemsg_roles/privatemsg_roles.module

Allows to send messages to all members of a role.

File

privatemsg_roles/privatemsg_roles.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Allows to send messages to all members of a role.
  5. */
  6. /**
  7. * Implements hook_perm().
  8. */
  9. function privatemsg_roles_permission() {
  10. $permissions = array(
  11. 'write privatemsg to all roles' => array(
  12. 'title' => t('Write private messages to all roles'),
  13. 'description' => t('Allows to write messages to all users which belong to a specific role.'),
  14. ),
  15. 'view roles recipients' => array(
  16. 'title' => t('View roles recipients'),
  17. 'description' => t('Ability to view roles recipients and the name of these roles.'),
  18. ),
  19. );
  20. foreach (user_roles(TRUE) as $role) {
  21. $permissions['write privatemsg to role ' . $role] = array(
  22. 'title' => t('Write private messages to role %role', array('%role' => $role)),
  23. 'description' => t('Allows to write messages to all users which belong to a specific role.'),
  24. );
  25. }
  26. return $permissions;
  27. }
  28. function privatemsg_roles_theme() {
  29. return array(
  30. 'privatemsg_roles_format' => array(
  31. 'variables' => array('recipient' => NULL, 'options' => array()),
  32. ),
  33. );
  34. }
  35. /**
  36. * Implements hook_privatemsg_recipient_type_info().
  37. */
  38. function privatemsg_roles_privatemsg_recipient_type_info() {
  39. return array(
  40. 'role' => array(
  41. 'name' => t('Role'),
  42. 'description' => t('Enter the name of a role to write a message to all users which have that role. Example: authenticated user.'),
  43. 'load' => 'privatemsg_roles_load_multiple',
  44. 'format' => 'privatemsg_roles_format',
  45. 'autocomplete' => 'privatemsg_roles_autocomplete',
  46. 'generate recipients' => 'privatemsg_roles_load_recipients',
  47. 'count' => 'privatemsg_roles_count_recipients',
  48. 'write callback' => 'privatemsg_roles_write_access',
  49. 'view access' => 'view roles recipients',
  50. ),
  51. );
  52. }
  53. /**
  54. * Write permission check for roles recipients.
  55. */
  56. function privatemsg_roles_write_access($recipient) {
  57. // Check if user has permission to write to all roles.
  58. if (user_access('write privatemsg to all roles')) {
  59. return TRUE;
  60. }
  61. if ($recipient) {
  62. if (empty($recipient->name)) {
  63. $recipient = array_shift(privatemsg_roles_load_multiple(array($recipient->recipient), $recipient->type));
  64. }
  65. // Check permission for this recipient.
  66. return user_access('write privatemsg to role ' . $recipient->name);
  67. }
  68. // Check if user has permission to write at least one role.
  69. foreach (user_roles(TRUE) as $role) {
  70. if (user_access('write privatemsg to role ' . $role)) {
  71. return TRUE;
  72. }
  73. }
  74. // No permission, return FALSE.
  75. return FALSE;
  76. }
  77. /**
  78. * Load a number of roles based on their rid.
  79. */
  80. function privatemsg_roles_load_multiple($rids, $type) {
  81. $result = db_query("SELECT name, rid AS recipient FROM {role} WHERE rid IN (:rids)", array(':rids' => $rids));
  82. $roles = array();
  83. foreach ($result as $role) {
  84. $role->type = 'role';
  85. $roles[privatemsg_recipient_key($role)] = $role;
  86. }
  87. return $roles;
  88. }
  89. /**
  90. * Format a role to be displayed as a recipient.
  91. */
  92. function theme_privatemsg_roles_format($variables) {
  93. $role = $variables['recipient'];
  94. $options = $variables['options'];
  95. if (!empty($options['plain'])) {
  96. $name = $role->name;
  97. if (!empty($options['unique'])) {
  98. $name .= ' [role]';
  99. }
  100. return $name;
  101. }
  102. return t('%role (role)', array('%role' => $role->name));
  103. }
  104. /**
  105. * Loads users with a specific role.
  106. */
  107. function privatemsg_roles_load_recipients($recipient, $limit, $offset) {
  108. $rid = isset($recipient->recipient) ? $recipient->recipient : $recipient->rid;
  109. if ($rid == DRUPAL_AUTHENTICATED_RID) {
  110. $result = db_query_range('SELECT uid FROM {users} WHERE uid > 0 ORDER BY uid ASC', $offset, $limit);
  111. }
  112. else {
  113. $result = db_query_range('SELECT uid FROM {users_roles} WHERE rid = :rid ORDER BY uid ASC', $offset, $limit, array(':rid' => $rid));
  114. }
  115. return $result->fetchCol();
  116. }
  117. /**
  118. * Return the number of users which have a given role.
  119. */
  120. function privatemsg_roles_count_recipients($recipient) {
  121. $rid = isset($recipient->recipient) ? $recipient->recipient : $recipient->rid;
  122. if ($rid == DRUPAL_AUTHENTICATED_RID) {
  123. return db_query('SELECT COUNT(uid) FROM {users}')->fetchField();
  124. }
  125. else {
  126. return db_query('SELECT COUNT(uid) FROM {users_roles} WHERE rid = :rid', array(':rid' => $rid))->fetchField();
  127. }
  128. }
  129. /**
  130. * Provides autocomplete suggestions for roles.
  131. */
  132. function privatemsg_roles_autocomplete($fragment, $names, $limit, $type) {
  133. $result = _privatemsg_assemble_query(array('autocomplete_roles', 'privatemsg_roles'), $fragment, $names)
  134. ->range(0, $limit)
  135. ->execute();
  136. $roles = array();
  137. foreach ($result as $role) {
  138. if (user_access('write privatemsg to all roles') || user_access('write privatemsg to role ' . $role->name)) {
  139. $role->type = 'role';
  140. $role->recipient = $role->rid;
  141. $roles[privatemsg_recipient_key($role)] = $role;
  142. }
  143. }
  144. return $roles;
  145. }
  146. /**
  147. * Implements hook_privatemsg_name_lookup().
  148. */
  149. function privatemsg_roles_privatemsg_name_lookup($string) {
  150. // Remove optional role specifier.
  151. $string = str_replace(t('[role]'), '', $string);
  152. $role = db_query("SELECT *, rid AS recipient FROM {role} WHERE name = :name", array(':name' => trim($string)))->fetchObject();
  153. if ($role) {
  154. $role->type = 'role';
  155. return array(privatemsg_recipient_key($role) => $role);
  156. }
  157. }
  158. /**
  159. * Query definition to search for username autocomplete suggestions.
  160. *
  161. * @param $fragments
  162. * Query fragments array.
  163. * @param $search
  164. * Which search string is currently searched for.
  165. * @param $names
  166. * Array of names not to be used as suggestions.
  167. */
  168. function privatemsg_roles_sql_autocomplete_roles($search, $names) {
  169. $query = db_select('role', 'role')
  170. ->fields('role')
  171. ->condition('role.name', $search . '%', 'LIKE')
  172. ->condition('role.rid', DRUPAL_ANONYMOUS_RID, '<>')
  173. ->orderBy('role.name', 'ASC');
  174. if (!empty($names)) {
  175. $query->condition('role.name', $names, 'NOT IN');
  176. }
  177. return $query;
  178. }

Functions

Namesort descending Description
privatemsg_roles_autocomplete Provides autocomplete suggestions for roles.
privatemsg_roles_count_recipients Return the number of users which have a given role.
privatemsg_roles_load_multiple Load a number of roles based on their rid.
privatemsg_roles_load_recipients Loads users with a specific role.
privatemsg_roles_permission Implements hook_perm().
privatemsg_roles_privatemsg_name_lookup Implements hook_privatemsg_name_lookup().
privatemsg_roles_privatemsg_recipient_type_info Implements hook_privatemsg_recipient_type_info().
privatemsg_roles_sql_autocomplete_roles Query definition to search for username autocomplete suggestions.
privatemsg_roles_theme
privatemsg_roles_write_access Write permission check for roles recipients.
theme_privatemsg_roles_format Format a role to be displayed as a recipient.