1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Hooks and callback functions for rules.module integration.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_rules_action_info().
|
10
|
*
|
11
|
* @ingroup rules
|
12
|
*/
|
13
|
function privatemsg_rules_rules_action_info() {
|
14
|
|
15
|
// Re-use existing information and also get information about fields added
|
16
|
// to private messages.
|
17
|
$fields = entity_get_all_property_info('privatemsg_message');
|
18
|
|
19
|
// Remove properties that can't be used here.
|
20
|
unset($fields['mid']);
|
21
|
unset($fields['thread_id']);
|
22
|
unset($fields['timestamp']);
|
23
|
|
24
|
// Remove fields, they are not supported yet.
|
25
|
foreach (array_keys($fields) as $key) {
|
26
|
if (strpos($key, 'field_') === 0) {
|
27
|
unset($fields[$key]);
|
28
|
}
|
29
|
}
|
30
|
|
31
|
// Update some descriptions and other definitions.
|
32
|
$fields['subject']['description'] = t('Subject of the message. To use the tokens provided by Privatemsg, use {token}, for example: {privatemsg_message:recipient:name}.');
|
33
|
$fields['body']['description'] = t('Body of the message. To use the tokens provided by Privatemsg, use {token}, for example: {privatemsg_message:recipient:name}.');
|
34
|
$fields['body']['long'] = TRUE;
|
35
|
// Make subject and body translatable.
|
36
|
$fields['subject']['translatable'] = TRUE;
|
37
|
$fields['body']['translatable'] = TRUE;
|
38
|
|
39
|
// Reply doesn't have subject either.
|
40
|
$reply_fields = $fields;
|
41
|
unset($reply_fields['subject']);
|
42
|
|
43
|
$actions = array(
|
44
|
'privatemsg_rules_new' => array(
|
45
|
'label' => t('Send a message'),
|
46
|
'named parameter' => TRUE,
|
47
|
'parameter' => array(
|
48
|
'recipient' => array(
|
49
|
'type' => 'user',
|
50
|
'label' => t('Recipient'),
|
51
|
'description' => t('Recipient of the message.'),
|
52
|
),
|
53
|
) + $fields,
|
54
|
'new variables' => array(
|
55
|
'thread_id' => array(
|
56
|
'type' => 'integer',
|
57
|
'label' => t('ID of new thread'),
|
58
|
),
|
59
|
),
|
60
|
'group' => t('Private messages'),
|
61
|
),
|
62
|
'privatemsg_rules_reply' => array(
|
63
|
'label' => t('Reply to a message'),
|
64
|
'named parameter' => TRUE,
|
65
|
'parameter' => array(
|
66
|
'thread_id' => array(
|
67
|
'type' => 'integer',
|
68
|
'label' => t('Privatemsg thread id'),
|
69
|
'description' => t('Thread ID of the thread that should be responded to.')
|
70
|
),
|
71
|
) + $reply_fields,
|
72
|
'group' => t('Private messages'),
|
73
|
),
|
74
|
'privatemsg_rules_unread_count' => array(
|
75
|
'label' => t('Load number of unread messages'),
|
76
|
'parameter' => array(
|
77
|
'account' => array(
|
78
|
'type' => 'user',
|
79
|
'label' => t('User account'),
|
80
|
'description' => t('Specify the user for which the number of unread messages should be loaded.'),
|
81
|
),
|
82
|
),
|
83
|
'new variables' => array(
|
84
|
'unread_count' => array(
|
85
|
'type' => 'integer',
|
86
|
'label' => t('Number of unread messages'),
|
87
|
),
|
88
|
),
|
89
|
'group' => t('Private messages'),
|
90
|
),
|
91
|
);
|
92
|
if (module_exists('privatemsg_filter')) {
|
93
|
$actions['privatemsg_rules_tag_thread'] = array(
|
94
|
'label' => t('Tag a privatemsg thread'),
|
95
|
'parameter' => array(
|
96
|
'thread_id' => array(
|
97
|
'type' => 'integer',
|
98
|
'label' => t('Specify the thread ID that should be tagged.'),
|
99
|
),
|
100
|
'account' => array(
|
101
|
'type' => 'user',
|
102
|
'label' => t('Specify for which user the message should tagged.'),
|
103
|
),
|
104
|
'privatemsg_tag' => array(
|
105
|
'type' => 'text',
|
106
|
'label' => t('Name of the tag that should be added.'),
|
107
|
'description' => t('Multiple tags can be specified, separated by a comma.'),
|
108
|
),
|
109
|
),
|
110
|
'group' => t('Private messages'),
|
111
|
);
|
112
|
}
|
113
|
if (module_exists('privatemsg_roles')) {
|
114
|
$actions['privatemsg_rules_new_role'] = array(
|
115
|
'label' => t('Send a message to a role'),
|
116
|
'named parameter' => TRUE,
|
117
|
'parameter' => array(
|
118
|
'roles' => array(
|
119
|
'type' => 'list<integer>',
|
120
|
'label' => t('Roles'),
|
121
|
'options list' => 'entity_metadata_user_roles',
|
122
|
'description' => t('Select the roles whose users should receive the message.'),
|
123
|
),
|
124
|
) + $fields,
|
125
|
'new variables' => array(
|
126
|
'thread_id' => array(
|
127
|
'type' => 'integer',
|
128
|
'label' => t('ID of new thread'),
|
129
|
),
|
130
|
),
|
131
|
'group' => t('Private messages'),
|
132
|
);
|
133
|
}
|
134
|
return $actions;
|
135
|
}
|
136
|
|
137
|
|
138
|
/**
|
139
|
* Implements hook_rules_event_info().
|
140
|
*
|
141
|
* @ingroup rules
|
142
|
*/
|
143
|
function privatemsg_rules_rules_event_info() {
|
144
|
return array(
|
145
|
'privatemsg_insert' => array(
|
146
|
'label' => t('A message is sent'),
|
147
|
'group' => t('Private messages'),
|
148
|
'variables' => array(
|
149
|
'privatemsg_message' => array(
|
150
|
'type' => 'privatemsg_message',
|
151
|
'bundle' => 'privatemsg_message',
|
152
|
'label' => t('Private message'),
|
153
|
),
|
154
|
'recipient' => array(
|
155
|
'type' => 'user',
|
156
|
'label' => t('Recipient of the message.'),
|
157
|
),
|
158
|
),
|
159
|
),
|
160
|
);
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* Implements hook_rules_condition_info().
|
165
|
*
|
166
|
* @ingroup rules
|
167
|
*/
|
168
|
function privatemsg_rules_rules_condition_info() {
|
169
|
if (module_exists('privatemsg_filter')) {
|
170
|
return array(
|
171
|
'privatemsg_rules_thread_has_tag' => array(
|
172
|
'label' => t('Thread has tag'),
|
173
|
'parameter' => array(
|
174
|
'thread_id' => array(
|
175
|
'type' => 'integer',
|
176
|
'label' => t('Specify the thread ID that should be checked.'),
|
177
|
),
|
178
|
'account' => array(
|
179
|
'type' => 'user',
|
180
|
'label' => t('Specify the user that should be checked.'),
|
181
|
),
|
182
|
'privatemsg_tag' => array(
|
183
|
'type' => 'text',
|
184
|
'label' => t('Name of the tag for which should be checked.'),
|
185
|
),
|
186
|
'check_all' => array(
|
187
|
'type' => 'boolean',
|
188
|
'label' => t('Only return TRUE if all tags are found.'),
|
189
|
),
|
190
|
),
|
191
|
'group' => t('Private messages'),
|
192
|
),
|
193
|
);
|
194
|
}
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* Converts temporary token format ({token}) to real tokens.
|
199
|
*/
|
200
|
function privatemsg_rules_replace_tokens($text) {
|
201
|
return preg_replace('/{([a-z0-9_:]+)}/', '[$1]', $text);
|
202
|
}
|
203
|
|
204
|
/**
|
205
|
* Helper function for sending a new message.
|
206
|
*/
|
207
|
function privatemsg_rules_new($args, $element) {
|
208
|
// Recipient could be a wrapped entity. They do not play nice
|
209
|
// together with the Privatemsg API because that one assumes recipient
|
210
|
// objects which have additional properties like type.
|
211
|
$recipient = $args['recipient'];
|
212
|
if ($recipient instanceof EntityDrupalWrapper) {
|
213
|
$recipient = user_load($recipient->getIdentifier());
|
214
|
}
|
215
|
|
216
|
// Enforce recipient type values.
|
217
|
$recipient->recipient = $recipient->uid;
|
218
|
$recipient->type = 'user';
|
219
|
|
220
|
// Replace token placeholders.
|
221
|
$args['subject'] = privatemsg_rules_replace_tokens($args['subject']);
|
222
|
$args['body'] = privatemsg_rules_replace_tokens($args['body']);
|
223
|
rules_log('Writing new message with subject %subject to %user from %author', array('%subject' => $args['subject'], '%user' => $recipient->name, '%author' => $args['author']->name));
|
224
|
$result = privatemsg_new_thread(array($recipient), $args['subject'], $args['body'], $args);
|
225
|
if ($result['success']) {
|
226
|
rules_log('New message sucessfully sent, !link', array('!link' => l($args['subject'], 'messages/view/' . $result['message']->thread_id)));
|
227
|
}
|
228
|
else {
|
229
|
foreach ($result['messages']['error'] as $args) {
|
230
|
rules_log('Message not sent, reported error: !error', array('!error' => $args), RulesLog::ERROR);
|
231
|
}
|
232
|
}
|
233
|
foreach ($result['messages']['warning'] as $args) {
|
234
|
rules_log('Warning message reported when trying to send message: !warning', array('!warning' => $args), RulesLog::WARN);
|
235
|
}
|
236
|
|
237
|
// Return thread ID of new thread (or NULL if failed).
|
238
|
if ($result['success']) {
|
239
|
return array(
|
240
|
'thread_id' => $result['message']->thread_id,
|
241
|
);
|
242
|
}
|
243
|
else {
|
244
|
return array(
|
245
|
'thread_id' => NULL,
|
246
|
);
|
247
|
}
|
248
|
}
|
249
|
|
250
|
/**
|
251
|
* Implements ACTIONBASE_form_alter().
|
252
|
*/
|
253
|
function privatemsg_rules_new_form_alter(&$form, &$form_state) {
|
254
|
$form['parameter']['subject']['settings']['subject']['#type'] = 'textfield';
|
255
|
}
|
256
|
|
257
|
/**
|
258
|
* Helper function for sending a new message to a role.
|
259
|
*/
|
260
|
function privatemsg_rules_new_role($args, $element) {
|
261
|
$args['subject'] = privatemsg_rules_replace_tokens($args['subject']);
|
262
|
$args['body'] = privatemsg_rules_replace_tokens($args['body']);
|
263
|
|
264
|
$recipient_array = array();
|
265
|
$recipient_names = array();
|
266
|
foreach ($args['roles'] as $rid) {
|
267
|
$recipient_array[$rid] = user_role_load($rid);
|
268
|
$recipient_array[$rid]->type = 'role';
|
269
|
$recipient_array[$rid]->recipient = $recipient_array[$rid]->rid;
|
270
|
$recipient_names[] = $recipient_array[$rid]->name;
|
271
|
}
|
272
|
$recipient_names = implode(', ', $recipient_names);
|
273
|
|
274
|
rules_log('Writing new message with subject %subject to roles %roles from %author.', array('%subject' => $args['subject'], '%author' => $args['author']->name, '%roles' => $recipient_names));
|
275
|
$result = privatemsg_new_thread($recipient_array, $args['subject'], $args['body'], $args);
|
276
|
if ($result['success']) {
|
277
|
rules_log('New message sucessfully sent, !link.', array('!link' => l($args['subject'], 'messages/view/' . $result['message']->thread_id)));
|
278
|
}
|
279
|
else {
|
280
|
foreach ($result['messages']['error'] as $message) {
|
281
|
rules_log('Message not sent, reported error: !error.', array('!error' => $message), RulesLog::ERROR);
|
282
|
}
|
283
|
}
|
284
|
foreach ($result['messages']['warning'] as $message) {
|
285
|
rules_log('Warning message reported when trying to send message: !warning.', array('!warning' => $message), RulesLog::WARN);
|
286
|
}
|
287
|
|
288
|
// Return thread ID of new thread (or NULL if failed).
|
289
|
if ($result['success']) {
|
290
|
return array(
|
291
|
'thread_id' => $result['message']->thread_id,
|
292
|
);
|
293
|
}
|
294
|
else {
|
295
|
return array(
|
296
|
'thread_id' => NULL,
|
297
|
);
|
298
|
}
|
299
|
}
|
300
|
|
301
|
/**
|
302
|
* Implements ACTIONBASE_form_alter().
|
303
|
*/
|
304
|
function privatemsg_rules_new_role_form_alter(&$form, &$form_state) {
|
305
|
$form['parameter']['subject']['settings']['subject']['#type'] = 'textfield';
|
306
|
}
|
307
|
|
308
|
/**
|
309
|
* Helper function for replying to a thread.
|
310
|
*/
|
311
|
function privatemsg_rules_reply($args, $element) {
|
312
|
$args['body'] = privatemsg_rules_replace_tokens($args['body']);
|
313
|
|
314
|
rules_log('Writing reply to thread %thread_id from %author', array('%thread_id' => $args['thread_id'], '%author' => $args['author']->name));
|
315
|
$result = privatemsg_reply($args['thread_id'], $args['body'], $args);
|
316
|
if ($result['success']) {
|
317
|
rules_log('New message sucessfully sent, !link', array('!link' => l(drupal_substr($args['body'], 0, 15), 'messages/view/' . $result['message']->thread_id)));
|
318
|
}
|
319
|
else {
|
320
|
foreach ($result['messages']['error'] as $message) {
|
321
|
rules_log('Message not sent, reported error: !error', array('!error' => $message), RulesLog::ERROR);
|
322
|
}
|
323
|
}
|
324
|
foreach ($result['messages']['warning'] as $message) {
|
325
|
rules_log('Warning message reported when trying to send message: !warning', array('!warning' => $message), RulesLog::WARN);
|
326
|
}
|
327
|
}
|
328
|
|
329
|
/**
|
330
|
* Implements ACTIONBASE_form_alter().
|
331
|
*/
|
332
|
function privatemsg_rules_thread_has_tag_form_alter(&$form, &$form_state) {
|
333
|
$form['parameter']['privatemsg_tag']['settings']['privatemsg_tag']['#type'] = 'textfield';
|
334
|
$form['parameter']['privatemsg_tag']['settings']['privatemsg_tag']['#autocomplete_path'] = 'messages/filter/tag-autocomplete';
|
335
|
}
|
336
|
|
337
|
/**
|
338
|
* Check if a thread has a specific tag.
|
339
|
*
|
340
|
* @param $thread_id
|
341
|
* Which thread to check.
|
342
|
* @param $account
|
343
|
* Which user should be checked.
|
344
|
* @param $tag_settings
|
345
|
* Rules settings, includes the tag that should be checked.
|
346
|
*
|
347
|
* @return
|
348
|
* TRUE if the thread has such a tag, FALSe otherwise.
|
349
|
*/
|
350
|
function privatemsg_rules_thread_has_tag($thread_id, $account, $tag_string, $check_all = FALSE) {
|
351
|
$tags = explode(',', $tag_string);
|
352
|
$tag_ids = privatemsg_filter_create_tags($tags);
|
353
|
|
354
|
if (empty($tag_ids)) {
|
355
|
rules_log('No valid tag could be loaded or created.', array(), RulesLog::ERROR);
|
356
|
return;
|
357
|
}
|
358
|
|
359
|
$query = _privatemsg_assemble_query(array('tags', 'privatemsg_filter'), $account, array($thread_id));
|
360
|
$tag_ids_on_thread = $query->execute()->fetchCol();
|
361
|
|
362
|
if ($check_all) {
|
363
|
// If check_all is set, only return TRUE if all tags exist.
|
364
|
if (count(array_diff($tag_ids, $tag_ids_on_thread)) == 0) {
|
365
|
return TRUE;
|
366
|
}
|
367
|
}
|
368
|
else {
|
369
|
// If not, then it is enough it at least one of the submitted tags is used
|
370
|
// on the thread.
|
371
|
if (count(array_diff($tag_ids, $tag_ids_on_thread)) < count($tag_ids)) {
|
372
|
return TRUE;
|
373
|
}
|
374
|
}
|
375
|
return FALSE;
|
376
|
}
|
377
|
|
378
|
/**
|
379
|
* Implements ACTIONBASE_form_alter().
|
380
|
*/
|
381
|
function privatemsg_rules_tag_thread_form_alter(&$form, &$form_state) {
|
382
|
$form['parameter']['privatemsg_tag']['settings']['privatemsg_tag']['#type'] = 'textfield';
|
383
|
$form['parameter']['privatemsg_tag']['settings']['privatemsg_tag']['#autocomplete_path'] = 'messages/filter/tag-autocomplete';
|
384
|
}
|
385
|
|
386
|
function privatemsg_rules_tag_thread($thread_id, $account, $tag_string) {
|
387
|
$tags = explode(',', $tag_string);
|
388
|
$tag_ids = privatemsg_filter_create_tags($tags);
|
389
|
|
390
|
if (empty($tag_ids)) {
|
391
|
rules_log('No valid tag could be loaded or created.', array(), RulesLog::ERROR);
|
392
|
return;
|
393
|
}
|
394
|
|
395
|
privatemsg_filter_add_tags(array($thread_id), $tag_ids, $account);
|
396
|
}
|
397
|
|
398
|
function privatemsg_rules_unread_count($account) {
|
399
|
return array(
|
400
|
'unread_count' => privatemsg_unread_count($account),
|
401
|
);
|
402
|
}
|
403
|
|