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