1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Rules integration for the Flag module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_rules_data_info().
|
10
|
* @ingroup rules
|
11
|
*/
|
12
|
function flag_rules_data_info() {
|
13
|
return array(
|
14
|
'flag' => array(
|
15
|
'label' => t('flag'),
|
16
|
'ui class' => 'FlagRulesUIClass',
|
17
|
'wrapper class' => 'FlagRulesDataWrapper',
|
18
|
'wrap' => TRUE,
|
19
|
),
|
20
|
'flagging' => array(
|
21
|
'label' => t('flagging'),
|
22
|
'parent' => 'entity',
|
23
|
'group' => t('flag'),
|
24
|
),
|
25
|
);
|
26
|
}
|
27
|
|
28
|
/**
|
29
|
* A custom wrapper class for flags to be used with Rules.
|
30
|
* @ingroup rules
|
31
|
*/
|
32
|
class FlagRulesDataWrapper extends RulesIdentifiableDataWrapper implements RulesDataWrapperSavableInterface {
|
33
|
|
34
|
protected function extractIdentifier($flag) {
|
35
|
return $flag->name;
|
36
|
}
|
37
|
|
38
|
protected function load($name) {
|
39
|
return flag_get_flag($name);
|
40
|
}
|
41
|
|
42
|
public function save() {
|
43
|
$flag = $this->value();
|
44
|
$flag->save();
|
45
|
}
|
46
|
|
47
|
public function validate($value) {
|
48
|
if (isset($value) && is_string($value)) {
|
49
|
return TRUE;
|
50
|
}
|
51
|
elseif (isset($value) && is_object($value) && $value instanceof flag_flag) {
|
52
|
return TRUE;
|
53
|
}
|
54
|
return parent::validate($value);
|
55
|
}
|
56
|
}
|
57
|
|
58
|
/**
|
59
|
* UI for inputing flags.
|
60
|
* @ingroup rules
|
61
|
*/
|
62
|
class FlagRulesUIClass extends RulesDataUI implements RulesDataDirectInputFormInterface {
|
63
|
|
64
|
public static function getDefaultMode() {
|
65
|
return 'input';
|
66
|
}
|
67
|
|
68
|
public static function inputForm($name, $info, $settings, RulesPlugin $element) {
|
69
|
$options = _flag_rules_flags_options(isset($info['flag_type']) ? $info['flag_type'] : NULL);
|
70
|
$header = array(
|
71
|
'title' => t('Flag:'),
|
72
|
'type' => t('The flag type'),
|
73
|
'global' => t('Is the flag global?'),
|
74
|
);
|
75
|
$settings += array($name => isset($info['default value']) ? $info['default value'] : '');
|
76
|
|
77
|
$form[$name] = array(
|
78
|
'#type' => 'tableselect',
|
79
|
'#header' => $header,
|
80
|
'#options' => $options,
|
81
|
'#required' => empty($info['optional']),
|
82
|
'#multiple' => FALSE,
|
83
|
'#default_value' => $settings[$name],
|
84
|
'#empty' => t('There is no suiting flag available.')
|
85
|
);
|
86
|
return $form;
|
87
|
}
|
88
|
|
89
|
public static function render($value) {
|
90
|
$flag = flag_get_flag($value);
|
91
|
|
92
|
if ($flag === FALSE) {
|
93
|
return array();
|
94
|
}
|
95
|
|
96
|
return array(
|
97
|
'content' => array('#markup' => check_plain($flag->get_title())),
|
98
|
'#attributes' => array('class' => array('rules-parameter-flag')),
|
99
|
);
|
100
|
}
|
101
|
}
|
102
|
|
103
|
function _flag_rules_flags_options($flag_type = NULL) {
|
104
|
$flags = flag_get_flags();
|
105
|
$options = array();
|
106
|
foreach ($flags as $flag) {
|
107
|
if (!isset($flag_type) || $flag->entity_type == $flag_type) {
|
108
|
$options[$flag->name] = array(
|
109
|
'title' => $flag->get_title(),
|
110
|
'type' => $flag->entity_type,
|
111
|
'global' => $flag->global ? t('Yes') : t('No'),
|
112
|
);
|
113
|
}
|
114
|
}
|
115
|
return $options;
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* Implements hook_rules_event_info().
|
120
|
*/
|
121
|
function flag_rules_event_info() {
|
122
|
$items = array();
|
123
|
|
124
|
$flags = flag_get_flags();
|
125
|
foreach ($flags as $flag) {
|
126
|
// We only support flags on entities.
|
127
|
if ($info = entity_get_info($flag->entity_type)) {
|
128
|
$variables = array(
|
129
|
'flag' => array(
|
130
|
'type' => 'flag',
|
131
|
'label' => t('flag'),
|
132
|
'flag_type' => $flag->entity_type,
|
133
|
),
|
134
|
'flagged_' . $flag->entity_type => array(
|
135
|
'type' => $flag->entity_type,
|
136
|
'label' => $info['label'],
|
137
|
),
|
138
|
'flagging_user' => array(
|
139
|
'type' => 'user',
|
140
|
'label' => t('flagging user'),
|
141
|
),
|
142
|
'flagging' => array(
|
143
|
'type' => 'flagging',
|
144
|
'label' => t('flagging'),
|
145
|
),
|
146
|
);
|
147
|
|
148
|
// For each flag we define two events.
|
149
|
$items['flag_flagged_' . $flag->name] = array(
|
150
|
'group' => t('Flag'),
|
151
|
'label' => t('A @flag-type has been flagged, under "@flag-title"', array('@flag-title' => $flag->get_title(), '@flag-type' => t($flag->entity_type))),
|
152
|
'variables' => $variables,
|
153
|
'access callback' => 'flag_rules_integration_access',
|
154
|
);
|
155
|
$items['flag_unflagged_' . $flag->name] = array(
|
156
|
'group' => t('Flag'),
|
157
|
'label' => t('A @flag-type has been unflagged, under "@flag-title"', array('@flag-title' => $flag->get_title(), '@flag-type' => t($flag->entity_type))),
|
158
|
'variables' => $variables,
|
159
|
'access callback' => 'flag_rules_integration_access',
|
160
|
);
|
161
|
}
|
162
|
}
|
163
|
return $items;
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* Implements hook_rules_action_info().
|
168
|
*/
|
169
|
function flag_rules_action_info() {
|
170
|
$param_defaults = array(
|
171
|
'flagging_user' => array(
|
172
|
'type' => 'user',
|
173
|
'label' => t('User on whose behalf to flag'),
|
174
|
'description' => t('For non-global flags, this is the user on whose behalf to flag the object. In addition, if checked below, the access permissions to the flag are checked against this user.'),
|
175
|
),
|
176
|
'permission_check' => array(
|
177
|
'type' => 'boolean',
|
178
|
'label' => t('Skip permission check'),
|
179
|
'description' => t('Whether to ignore permissions of the user on whose behalf to flag.'),
|
180
|
'restriction' => 'input',
|
181
|
),
|
182
|
);
|
183
|
$items = array(
|
184
|
'flag_trim' => array(
|
185
|
'label' => t('Trim a flag'),
|
186
|
'base' => 'flag_rules_action_trim',
|
187
|
'parameter' => array(
|
188
|
'flag' => array(
|
189
|
'type' => 'flag',
|
190
|
'label' => t('Flag'),
|
191
|
),
|
192
|
'flagging_user' => array(
|
193
|
'type' => 'user',
|
194
|
'label' => t('User whose flag to trim'),
|
195
|
'description' => t('For non-global flags, this is the user whose flag to trim. (For global flags, this argument is ignored.)'),
|
196
|
),
|
197
|
'cutoff_size' => array(
|
198
|
'type' => 'integer',
|
199
|
'label' => t('Flag queue size'),
|
200
|
'description' => t('The maximum number of objects to keep in the queue. Newly flagged objects will be kept; older ones will be removed. Tip: by typing "1" here you implement a singleton.'),
|
201
|
),
|
202
|
'trim_newest' => array(
|
203
|
'type' => 'boolean',
|
204
|
'label' => t('Trim newest flags'),
|
205
|
'description' => t('Checking this will trim the newest flags. This will prevent new flags once a limit is reached.'),
|
206
|
),
|
207
|
'permission_check' => $param_defaults['permission_check'],
|
208
|
),
|
209
|
'group' => t('Flag'),
|
210
|
'access callback' => 'flag_rules_integration_access',
|
211
|
),
|
212
|
'fetch_overall_flag_count' => array(
|
213
|
'label' => t('Fetch overall flag count'),
|
214
|
'base' => 'flag_rules_action_fetch_overall_flag_count',
|
215
|
'parameter' => array(
|
216
|
'flag' => array(
|
217
|
'type' => 'flag',
|
218
|
'label' => t('Flag'),
|
219
|
),
|
220
|
),
|
221
|
'provides' => array(
|
222
|
'overall_flag_count' => array(
|
223
|
'label' => t('Overall flag count'),
|
224
|
'type' => 'integer',
|
225
|
),
|
226
|
),
|
227
|
'group' => t('Flag'),
|
228
|
'access callback' => 'flag_rules_integration_access',
|
229
|
),
|
230
|
'fetch_entity_flag_count' => array(
|
231
|
'label' => t('Fetch entity flag count'),
|
232
|
'base' => 'flag_rules_action_fetch_entity_flag_count',
|
233
|
'parameter' => array(
|
234
|
'flag' => array(
|
235
|
'type' => 'flag',
|
236
|
'label' => t('Flag'),
|
237
|
),
|
238
|
'entity_type' => array(
|
239
|
'type' => 'text',
|
240
|
'label' => t('Entity type'),
|
241
|
'options list' => 'flag_rules_get_flag_types',
|
242
|
'restriction' => 'input',
|
243
|
),
|
244
|
),
|
245
|
'provides' => array(
|
246
|
'entity_flag_count' => array(
|
247
|
'label' => t('Entity flag count'),
|
248
|
'type' => 'integer',
|
249
|
),
|
250
|
),
|
251
|
'group' => t('Flag'),
|
252
|
'access callback' => 'flag_rules_integration_access',
|
253
|
),
|
254
|
'fetch_user_flag_count' => array(
|
255
|
'label' => t('Fetch user flag count'),
|
256
|
'base' => 'flag_rules_action_fetch_user_flag_count',
|
257
|
'parameter' => array(
|
258
|
'flag' => array(
|
259
|
'type' => 'flag',
|
260
|
'label' => t('Flag'),
|
261
|
),
|
262
|
'user' => array(
|
263
|
'type' => 'user',
|
264
|
'label' => t('User'),
|
265
|
),
|
266
|
),
|
267
|
'provides' => array(
|
268
|
'user_flag_count' => array(
|
269
|
'label' => t('User flag count'),
|
270
|
'type' => 'integer',
|
271
|
),
|
272
|
),
|
273
|
'group' => t('Flag'),
|
274
|
'access callback' => 'flag_rules_integration_access',
|
275
|
),
|
276
|
);
|
277
|
foreach (flag_get_types() as $type) {
|
278
|
$entity_info = entity_get_info($type);
|
279
|
$label = $entity_info['label'];
|
280
|
$items += array(
|
281
|
'flag_fetch_' . $type . '_by_user' => array(
|
282
|
'label' => t('Fetch @label flagged by user', array('@label' => $label)),
|
283
|
'base' => 'flag_rules_action_fetch_entity_by_user',
|
284
|
'parameter' => array(
|
285
|
'flag' => array(
|
286
|
'type' => 'flag',
|
287
|
'label' => t('Flag'),
|
288
|
'flag_type' => $type,
|
289
|
'description' => t('The flag to check for.'),
|
290
|
),
|
291
|
'flagging_user' => array(
|
292
|
'type' => 'user',
|
293
|
'label' => t('User who flagged the @label', array('@label' => $label)),
|
294
|
'description' => t('For non-global flags, this is the user who flagged the @label. (For global flags, this argument is ignored.)', array('@label' => $label)),
|
295
|
),
|
296
|
),
|
297
|
'provides' => array(
|
298
|
'content_flagged_by_user' => array(
|
299
|
'label' => t('Content flagged by user'),
|
300
|
'type' => 'list<' . $type . '>',
|
301
|
),
|
302
|
),
|
303
|
'group' => t('Flag'),
|
304
|
'access callback' => 'flag_rules_integration_access',
|
305
|
),
|
306
|
'flag_flag' . $type => array(
|
307
|
'label' => t('Flag a @label', array('@label' => $label)),
|
308
|
'base' => 'flag_rules_action_flag',
|
309
|
'parameter' => array(
|
310
|
'flag' => array(
|
311
|
'type' => 'flag',
|
312
|
'label' => t('Flag'),
|
313
|
'flag_type' => $type,
|
314
|
'description' => t('The flag to check for.'),
|
315
|
),
|
316
|
$type => array(
|
317
|
'type' => $type,
|
318
|
'label' => $label,
|
319
|
),
|
320
|
) + $param_defaults,
|
321
|
'group' => t('Flag'),
|
322
|
'access callback' => 'flag_rules_integration_access',
|
323
|
),
|
324
|
'flag_unflag' . $type => array(
|
325
|
'label' => t('Unflag a @label', array('@label' => $label)),
|
326
|
'base' => 'flag_rules_action_unflag',
|
327
|
'parameter' => array(
|
328
|
'flag' => array(
|
329
|
'type' => 'flag',
|
330
|
'label' => t('Flag'),
|
331
|
'flag_type' => $type,
|
332
|
'description' => t('The flag to check for.'),
|
333
|
),
|
334
|
$type => array(
|
335
|
'type' => $type,
|
336
|
'label' => $label,
|
337
|
),
|
338
|
) + $param_defaults,
|
339
|
'group' => t('Flag'),
|
340
|
'access callback' => 'flag_rules_integration_access',
|
341
|
),
|
342
|
);
|
343
|
$items['flag_fetch_users_' . $type] = array(
|
344
|
'label' => t('Fetch users who have flagged a @label', array('@label' => $label)),
|
345
|
'base' => 'flag_rules_action_fetch_users',
|
346
|
'parameter' => array(
|
347
|
'flag' => array(
|
348
|
'type' => 'flag',
|
349
|
'label' => t('Flag'),
|
350
|
'flag_type' => $type,
|
351
|
'description' => t('Choose the flag for which to fetch the users.'),
|
352
|
),
|
353
|
$type => array(
|
354
|
'type' => $type,
|
355
|
'label' => $label,
|
356
|
),
|
357
|
),
|
358
|
'provides' => array(
|
359
|
'users' => array(
|
360
|
'label' => t('Users who flagged'),
|
361
|
'type' => 'list<user>',
|
362
|
),
|
363
|
),
|
364
|
'group' => t('Flag'),
|
365
|
'access callback' => 'flag_rules_integration_access',
|
366
|
);
|
367
|
}
|
368
|
// For backward compatibility sake. This was the original name of the 'fetch node by user'.
|
369
|
$items['flag_fetch_entity_by_user'] = $items['flag_fetch_node_by_user'];
|
370
|
$items['flag_fetch_entity_by_user']['label'] .= ' '. t('(Legacy)');
|
371
|
return $items;
|
372
|
}
|
373
|
|
374
|
/**
|
375
|
* Base action implementation: Flag.
|
376
|
*/
|
377
|
function flag_rules_action_flag($flag, $entity, $flagging_user, $permissions_check) {
|
378
|
$flag->flag('flag', $flag->get_entity_id($entity), $flagging_user, $permissions_check);
|
379
|
}
|
380
|
|
381
|
/**
|
382
|
* Base action implementation: Unflag.
|
383
|
*/
|
384
|
function flag_rules_action_unflag($flag, $entity, $flagging_user, $permissions_check) {
|
385
|
$flag->flag('unflag', $flag->get_entity_id($entity), $flagging_user, $permissions_check);
|
386
|
}
|
387
|
|
388
|
/**
|
389
|
* Base action implementation: Trim flag.
|
390
|
*/
|
391
|
function flag_rules_action_trim($flag, $flagging_user, $cutoff_size, $trim_newest, $permissions_check) {
|
392
|
// For some reason, when this action fires in response to a flagging event,
|
393
|
// as an anonymous user, then the $flagging_user is sent through as FALSE.
|
394
|
// Not sure why. This workaround fixes the problem in this specific case.
|
395
|
if ($flagging_user === FALSE) {
|
396
|
$flagging_user = $GLOBALS['user'];
|
397
|
}
|
398
|
flag_trim_flag($flag, $flagging_user, $cutoff_size, $trim_newest, $permissions_check);
|
399
|
}
|
400
|
|
401
|
/**
|
402
|
* Base action implementation: Fetch users who flagged an entity.
|
403
|
*/
|
404
|
function flag_rules_action_fetch_users($flag, $entity) {
|
405
|
$result = db_select('flagging', 'fc')
|
406
|
->fields('fc', array('uid'))
|
407
|
->condition('entity_type', $flag->entity_type)
|
408
|
->condition('entity_id', $flag->get_entity_id($entity))
|
409
|
->condition('fid', $flag->fid)
|
410
|
->execute();
|
411
|
$uids = $result->fetchCol();
|
412
|
// Filter out anonymous users.
|
413
|
return array('users' => array_filter($uids));
|
414
|
}
|
415
|
|
416
|
/**
|
417
|
* Base action implementation: Fetch entities who were flagged a user.
|
418
|
*/
|
419
|
function flag_rules_action_fetch_entity_by_user($flag, $entity) {
|
420
|
$user = entity_metadata_wrapper('user', $entity);
|
421
|
$sid = $user->flag_sid->value();
|
422
|
$query = db_select('flagging', 'fc')
|
423
|
->fields('fc', array('entity_id'))
|
424
|
->condition('entity_type', $flag->entity_type)
|
425
|
->condition('uid', $user->uid->value())
|
426
|
->condition('fid', $flag->fid);
|
427
|
// Filter out any bad session ids and any users that aren't anonymous.
|
428
|
if (!empty($sid) && $sid != -1) {
|
429
|
$query->condition('sid', $sid);
|
430
|
}
|
431
|
$result = $query->execute();
|
432
|
$flagged = $result->fetchCol();
|
433
|
return array('content_flagged_by_user' => $flagged);
|
434
|
}
|
435
|
|
436
|
/**
|
437
|
* Base action implementation: Fetch overall count for a particular flag.
|
438
|
*/
|
439
|
function flag_rules_action_fetch_overall_flag_count($flag) {
|
440
|
$count = flag_get_flag_counts($flag->name);
|
441
|
return array('overall_flag_count' => $count);
|
442
|
}
|
443
|
|
444
|
/**
|
445
|
* Helper function which will return all the available flag types.
|
446
|
*
|
447
|
* @return
|
448
|
* An array of flag type names keyed by the type name.
|
449
|
*/
|
450
|
function flag_rules_get_flag_types() {
|
451
|
$types = array();
|
452
|
foreach (flag_get_types() as $type) {
|
453
|
$types[$type] = $type;
|
454
|
}
|
455
|
return $types;
|
456
|
}
|
457
|
|
458
|
/**
|
459
|
* Base action implementation: Fetch count of flags for a particular entity type.
|
460
|
*/
|
461
|
function flag_rules_action_fetch_entity_flag_count($flag, $entity_type) {
|
462
|
$count = flag_get_entity_flag_counts($flag, $entity_type);
|
463
|
return array('entity_flag_count' => $count);
|
464
|
}
|
465
|
|
466
|
/**
|
467
|
* Base action implementation: Fetch user's flag count.
|
468
|
*/
|
469
|
function flag_rules_action_fetch_user_flag_count($flag, $user) {
|
470
|
$count = flag_get_user_flag_counts($flag, $user);
|
471
|
return array('user_flag_count' => $count);
|
472
|
}
|
473
|
|
474
|
/**
|
475
|
* Implements hook_rules_condition_info().
|
476
|
*/
|
477
|
function flag_rules_condition_info() {
|
478
|
$items = array();
|
479
|
foreach (flag_get_types() as $type) {
|
480
|
$entity_info = entity_get_info($type);
|
481
|
$label = isset($entity_info[$type]['label']) ? $entity_info[$type]['label'] : $type;
|
482
|
$items += array(
|
483
|
'flag_threshold_' . $type => array(
|
484
|
'label' => drupal_ucfirst(t('@type has flagging count', array('@type' => $label))),
|
485
|
'base' => 'flag_rules_condition_threshold',
|
486
|
'parameter' => array(
|
487
|
'flag' => array(
|
488
|
'type' => 'flag',
|
489
|
'label' => t('Flag'),
|
490
|
'flag_type' => $type,
|
491
|
'description' => t('The flag to check for.')
|
492
|
),
|
493
|
$type => array(
|
494
|
'type' => $type,
|
495
|
'label' => $label,
|
496
|
),
|
497
|
'number' => array(
|
498
|
'type' => 'integer',
|
499
|
'label' => t('Number'),
|
500
|
'description' => t('The number against which to test the number of times the object is flagged. For example, if you type "3" here, and choose "Greater than" for the operator, then this condition will return TRUE if the object is flagged more than three times.'),
|
501
|
),
|
502
|
'operator' => array(
|
503
|
'type' => 'text',
|
504
|
'label' => t('Comparison operator'),
|
505
|
'options list' => 'flag_rules_condition_threshold_operator_options',
|
506
|
'restriction' => 'input',
|
507
|
'default value' => '=',
|
508
|
'optional' => TRUE,
|
509
|
),
|
510
|
),
|
511
|
'group' => t('Flag'),
|
512
|
'access callback' => 'flag_rules_integration_access',
|
513
|
),
|
514
|
'flag_flagged_' . $type => array(
|
515
|
'label' => drupal_ucfirst(t('@type is flagged', array('@type' => $label))),
|
516
|
'base' => 'flag_rules_condition_flagged',
|
517
|
'parameter' => array(
|
518
|
'flag' => array(
|
519
|
'type' => 'flag',
|
520
|
'label' => t('Flag'),
|
521
|
'flag_type' => $type,
|
522
|
'description' => t('The flag to check for.')
|
523
|
),
|
524
|
$type => array(
|
525
|
'type' => $type,
|
526
|
'label' => $label,
|
527
|
),
|
528
|
'flagging_user' => array(
|
529
|
'type' => 'user',
|
530
|
'label' => t('User on whose behalf to check'),
|
531
|
'description' => t('For non-global flags, this is the user on whose behalf the flag is checked.'),
|
532
|
),
|
533
|
),
|
534
|
'group' => t('Flag'),
|
535
|
'access callback' => 'flag_rules_integration_access',
|
536
|
),
|
537
|
);
|
538
|
}
|
539
|
return $items;
|
540
|
}
|
541
|
|
542
|
/**
|
543
|
* Options list callback for the operator parameter of the flagging threshold condition.
|
544
|
*/
|
545
|
function flag_rules_condition_threshold_operator_options() {
|
546
|
return array(
|
547
|
'>' => t('Greater than'),
|
548
|
'>=' => t('Greater than or equal'),
|
549
|
'=' => t('Equal to'),
|
550
|
'<=' => t('Less than or equal'),
|
551
|
'<' => t('Less than'),
|
552
|
);
|
553
|
}
|
554
|
|
555
|
/**
|
556
|
* Condition: Check flagging count.
|
557
|
*/
|
558
|
function flag_rules_condition_threshold($flag, $entity, $number, $operator = '=') {
|
559
|
$count = $flag->get_count($flag->get_entity_id($entity));
|
560
|
|
561
|
switch ($operator) {
|
562
|
case '>' : return $count > $number;
|
563
|
case '>=': return $count >= $number;
|
564
|
case '=' : return $count == $number;
|
565
|
case '<' : return $count < $number;
|
566
|
case '<=': return $count <= $number;
|
567
|
}
|
568
|
}
|
569
|
|
570
|
/**
|
571
|
* Condition: Flag is flagged.
|
572
|
*/
|
573
|
function flag_rules_condition_flagged($flag, $entity, $account) {
|
574
|
return $flag->is_flagged($flag->get_entity_id($entity), $account->uid);
|
575
|
}
|
576
|
|
577
|
/**
|
578
|
* Rules integration access callback.
|
579
|
*/
|
580
|
function flag_rules_integration_access($type, $name) {
|
581
|
return user_access('administer flags');
|
582
|
}
|