Projet

Général

Profil

Paste
Télécharger (19,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / flag / flag.rules.inc @ 74f6bef0

1 85ad3d82 Assos Assos
<?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 b08d2851 Assos Assos
          'description' => t('During a flagging/unflagging event the count
225
            will take into account the current flagging/unflagging procedure.'),
226 85ad3d82 Assos Assos
          'type' => 'integer',
227
        ),
228
      ),
229
      'group' => t('Flag'),
230
      'access callback' => 'flag_rules_integration_access',
231
    ),
232
    'fetch_entity_flag_count' => array(
233
      'label' => t('Fetch entity flag count'),
234
      'base' => 'flag_rules_action_fetch_entity_flag_count',
235
      'parameter' => array(
236
        'flag' => array(
237
          'type' => 'flag',
238
          'label' => t('Flag'),
239
        ),
240
        'entity_type' => array(
241
          'type' => 'text',
242
          'label' => t('Entity type'),
243
          'options list' => 'flag_rules_get_flag_types',
244
          'restriction' => 'input',
245
        ),
246
      ),
247
      'provides' => array(
248
        'entity_flag_count' => array(
249
          'label' => t('Entity flag count'),
250 b08d2851 Assos Assos
          'description' => t('During a flagging event, the count
251
            will take into account the current flagging procedure. For
252
            an unflagging event, the count will NOT yet be decreased for the
253
            current unflagging procedure.'),
254 85ad3d82 Assos Assos
          'type' => 'integer',
255
        ),
256
      ),
257
      'group' => t('Flag'),
258
      'access callback' => 'flag_rules_integration_access',
259
    ),
260
    'fetch_user_flag_count' => array(
261
      'label' => t('Fetch user flag count'),
262
      'base' => 'flag_rules_action_fetch_user_flag_count',
263
      'parameter' => array(
264
        'flag' => array(
265
          'type' => 'flag',
266
          'label' => t('Flag'),
267
        ),
268
        'user' => array(
269
          'type' => 'user',
270
          'label' => t('User'),
271
        ),
272
      ),
273
      'provides' => array(
274
        'user_flag_count' => array(
275
          'label' => t('User flag count'),
276 b08d2851 Assos Assos
          'description' => t('During a flagging event, the count
277
            will take into account the current flagging procedure. For
278
            an unflagging event, the count will NOT yet be decreased for the
279
            current unflagging procedure.'),
280 85ad3d82 Assos Assos
          'type' => 'integer',
281
        ),
282
      ),
283
      'group' => t('Flag'),
284
      'access callback' => 'flag_rules_integration_access',
285
    ),
286
  );
287
  foreach (flag_get_types() as $type) {
288
    $entity_info = entity_get_info($type);
289
    $label = $entity_info['label'];
290
    $items += array(
291
      'flag_fetch_' . $type . '_by_user' => array(
292
        'label' => t('Fetch @label flagged by user', array('@label' => $label)),
293
        'base' => 'flag_rules_action_fetch_entity_by_user',
294
        'parameter' => array(
295
          'flag' => array(
296
            'type' => 'flag',
297
            'label' => t('Flag'),
298
            'flag_type' => $type,
299
            'description' => t('The flag to check for.'),
300
          ),
301
          'flagging_user' => array(
302
            'type' => 'user',
303
            'label' => t('User who flagged the @label', array('@label' => $label)),
304
            'description' => t('For non-global flags, this is the user who flagged the @label. (For global flags, this argument is ignored.)', array('@label' => $label)),
305
          ),
306
        ),
307
        'provides' => array(
308
          'content_flagged_by_user' => array(
309
            'label' => t('Content flagged by user'),
310
            'type' => 'list<' . $type . '>',
311
          ),
312
        ),
313
        'group' => t('Flag'),
314
        'access callback' => 'flag_rules_integration_access',
315
      ),
316
      'flag_flag' . $type => array(
317
        'label' => t('Flag a @label', array('@label' => $label)),
318
        'base' => 'flag_rules_action_flag',
319
        'parameter' => array(
320
          'flag' => array(
321
            'type' => 'flag',
322
            'label' => t('Flag'),
323
            'flag_type' => $type,
324
            'description' => t('The flag to check for.'),
325
          ),
326
          $type => array(
327
            'type' => $type,
328
            'label' => $label,
329
          ),
330
        ) + $param_defaults,
331
        'group' => t('Flag'),
332
        'access callback' => 'flag_rules_integration_access',
333
      ),
334
      'flag_unflag' . $type => array(
335
        'label' => t('Unflag a @label', array('@label' => $label)),
336
        'base' => 'flag_rules_action_unflag',
337
        'parameter' => array(
338
          'flag' => array(
339
            'type' => 'flag',
340
            'label' => t('Flag'),
341
            'flag_type' => $type,
342
            'description' => t('The flag to check for.'),
343
          ),
344
          $type => array(
345
            'type' => $type,
346
            'label' => $label,
347
          ),
348
        ) + $param_defaults,
349
        'group' => t('Flag'),
350
        'access callback' => 'flag_rules_integration_access',
351
      ),
352
    );
353
    $items['flag_fetch_users_' . $type] = array(
354
      'label' => t('Fetch users who have flagged a @label', array('@label' => $label)),
355
      'base' => 'flag_rules_action_fetch_users',
356
      'parameter' => array(
357
        'flag' => array(
358
          'type' => 'flag',
359
          'label' => t('Flag'),
360
          'flag_type' => $type,
361
          'description' => t('Choose the flag for which to fetch the users.'),
362
        ),
363
        $type => array(
364
          'type' => $type,
365
          'label' => $label,
366
        ),
367
      ),
368
      'provides' => array(
369
        'users' => array(
370
          'label' => t('Users who flagged'),
371
          'type' => 'list<user>',
372
        ),
373
      ),
374
      'group' => t('Flag'),
375
      'access callback' => 'flag_rules_integration_access',
376
    );
377
  }
378
  // For backward compatibility sake. This was the original name of the 'fetch node by user'.
379
  $items['flag_fetch_entity_by_user'] = $items['flag_fetch_node_by_user'];
380
  $items['flag_fetch_entity_by_user']['label'] .= ' '. t('(Legacy)');
381
  return $items;
382
}
383
384
/**
385
 * Base action implementation: Flag.
386
 */
387
function flag_rules_action_flag($flag, $entity, $flagging_user, $permissions_check) {
388
  $flag->flag('flag', $flag->get_entity_id($entity), $flagging_user, $permissions_check);
389
}
390
391
/**
392
 * Base action implementation: Unflag.
393
 */
394
function flag_rules_action_unflag($flag, $entity, $flagging_user, $permissions_check) {
395
  $flag->flag('unflag', $flag->get_entity_id($entity), $flagging_user, $permissions_check);
396
}
397
398
/**
399
 * Base action implementation: Trim flag.
400
 */
401
function flag_rules_action_trim($flag, $flagging_user, $cutoff_size, $trim_newest, $permissions_check) {
402
  // For some reason, when this action fires in response to a flagging event,
403
  // as an anonymous user, then the $flagging_user is sent through as FALSE.
404
  // Not sure why. This workaround fixes the problem in this specific case.
405
  if ($flagging_user === FALSE) {
406
    $flagging_user = $GLOBALS['user'];
407
  }
408
  flag_trim_flag($flag, $flagging_user, $cutoff_size, $trim_newest, $permissions_check);
409
}
410
411
/**
412
 * Base action implementation: Fetch users who flagged an entity.
413
 */
414
function flag_rules_action_fetch_users($flag, $entity) {
415
  $result = db_select('flagging', 'fc')
416
    ->fields('fc', array('uid'))
417
    ->condition('entity_type', $flag->entity_type)
418
    ->condition('entity_id', $flag->get_entity_id($entity))
419
    ->condition('fid', $flag->fid)
420
    ->execute();
421
  $uids = $result->fetchCol();
422
  // Filter out anonymous users.
423
  return array('users' => array_filter($uids));
424
}
425
426
/**
427
 * Base action implementation: Fetch entities who were flagged a user.
428
 */
429
function flag_rules_action_fetch_entity_by_user($flag, $entity) {
430
  $user = entity_metadata_wrapper('user', $entity);
431
  $sid = $user->flag_sid->value();
432
  $query = db_select('flagging', 'fc')
433
    ->fields('fc', array('entity_id'))
434
    ->condition('entity_type', $flag->entity_type)
435
    ->condition('uid', $user->uid->value())
436
    ->condition('fid', $flag->fid);
437
  // Filter out any bad session ids and any users that aren't anonymous.
438
  if (!empty($sid) && $sid != -1) {
439
    $query->condition('sid', $sid);
440
  }
441
  $result = $query->execute();
442
  $flagged = $result->fetchCol();
443
  return array('content_flagged_by_user' => $flagged);
444
}
445
446
/**
447
 * Base action implementation: Fetch overall count for a particular flag.
448 b08d2851 Assos Assos
 *
449
 * The count that is returned during a flagging or an unflagging will take into
450
 * account the current flag/unflag process.
451 85ad3d82 Assos Assos
 */
452
function flag_rules_action_fetch_overall_flag_count($flag) {
453
  $count = flag_get_flag_counts($flag->name);
454
  return array('overall_flag_count' => $count);
455
}
456
457
/**
458
 * Helper function which will return all the available flag types.
459
 *
460
 *  @return
461
 *  An array of flag type names keyed by the type name.
462
 */
463
function flag_rules_get_flag_types() {
464
  $types = array();
465
  foreach (flag_get_types() as $type) {
466
    $types[$type] = $type;
467
  }
468
  return $types;
469
}
470
471
/**
472 018e218c Assos Assos
 * Base action implementation: Fetch count of flags for a particular entity type.
473 b08d2851 Assos Assos
 *
474
 * During a flagging, the current flagging will be included in the count.
475
 * During an unflagging, the current flagging being removed will not yet have
476
 * been removed from the count.
477 85ad3d82 Assos Assos
 */
478
function flag_rules_action_fetch_entity_flag_count($flag, $entity_type) {
479
  $count = flag_get_entity_flag_counts($flag, $entity_type);
480
  return array('entity_flag_count' => $count);
481
}
482
483
/**
484
 * Base action implementation: Fetch user's flag count.
485 b08d2851 Assos Assos
 *
486
 * During a flagging, the current flagging will be included in the count.
487
 * During an unflagging, the current flagging will not yet have been removed
488
 * from the count.
489 85ad3d82 Assos Assos
 */
490
function flag_rules_action_fetch_user_flag_count($flag, $user) {
491
  $count = flag_get_user_flag_counts($flag, $user);
492
  return array('user_flag_count' => $count);
493
}
494
495
/**
496
 * Implements hook_rules_condition_info().
497
 */
498
function flag_rules_condition_info() {
499
  $items = array();
500
  foreach (flag_get_types() as $type) {
501
    $entity_info = entity_get_info($type);
502
    $label = isset($entity_info[$type]['label']) ? $entity_info[$type]['label'] : $type;
503
    $items += array(
504
      'flag_threshold_' . $type => array(
505
        'label' => drupal_ucfirst(t('@type has flagging count', array('@type' => $label))),
506
        'base' => 'flag_rules_condition_threshold',
507
        'parameter' => array(
508
          'flag' => array(
509
            'type' => 'flag',
510
            'label' => t('Flag'),
511
            'flag_type' => $type,
512
            'description' => t('The flag to check for.')
513
          ),
514
          $type => array(
515
            'type' => $type,
516
            'label' => $label,
517
          ),
518
          'number' => array(
519
            'type' => 'integer',
520
            'label' => t('Number'),
521 b08d2851 Assos Assos
            'description' => t('The number against which to test the number of
522
              times the object is flagged. For example, if you type "3" here,
523
              and choose "Greater than" for the operator, then this condition
524
              will return TRUE if the object is flagged more than three times.
525
              During a flagging or an unflagging event the count will take into
526
              account the current flag/unflag process.'),
527 85ad3d82 Assos Assos
          ),
528
          'operator' => array(
529
            'type' => 'text',
530
            'label' => t('Comparison operator'),
531
            'options list' => 'flag_rules_condition_threshold_operator_options',
532
            'restriction' => 'input',
533
            'default value' => '=',
534
            'optional' => TRUE,
535
          ),
536
        ),
537
        'group' => t('Flag'),
538
        'access callback' => 'flag_rules_integration_access',
539
      ),
540
      'flag_flagged_' . $type => array(
541
        'label' => drupal_ucfirst(t('@type is flagged', array('@type' => $label))),
542
        'base' => 'flag_rules_condition_flagged',
543
        'parameter' => array(
544
          'flag' => array(
545
            'type' => 'flag',
546
            'label' => t('Flag'),
547
            'flag_type' => $type,
548
            'description' => t('The flag to check for.')
549
          ),
550
          $type => array(
551
            'type' => $type,
552
            'label' => $label,
553
          ),
554
          'flagging_user' => array(
555
            'type' => 'user',
556
            'label' => t('User on whose behalf to check'),
557
            'description' => t('For non-global flags, this is the user on whose behalf the flag is checked.'),
558
          ),
559
        ),
560
        'group' => t('Flag'),
561
        'access callback' => 'flag_rules_integration_access',
562
      ),
563
    );
564
  }
565
  return $items;
566
}
567
568
/**
569
 * Options list callback for the operator parameter of the flagging threshold condition.
570
 */
571
function flag_rules_condition_threshold_operator_options() {
572
  return array(
573
    '>'  => t('Greater than'),
574
    '>=' => t('Greater than or equal'),
575
    '='  => t('Equal to'),
576
    '<=' => t('Less than or equal'),
577
    '<'  => t('Less than'),
578
  );
579
}
580
581
/**
582
 * Condition: Check flagging count.
583 b08d2851 Assos Assos
 *
584
 * The count that is returned during a flagging or an unflagging will take into
585
 * acount the current flag/unflag process.
586 85ad3d82 Assos Assos
 */
587
function flag_rules_condition_threshold($flag, $entity, $number, $operator = '=') {
588
  $count = $flag->get_count($flag->get_entity_id($entity));
589
590
  switch ($operator) {
591
    case '>' : return $count >  $number;
592
    case '>=': return $count >= $number;
593
    case '=' : return $count == $number;
594
    case '<' : return $count <  $number;
595
    case '<=': return $count <= $number;
596
  }
597
}
598
599
/**
600
 * Condition: Flag is flagged.
601
 */
602
function flag_rules_condition_flagged($flag, $entity, $account) {
603
  return $flag->is_flagged($flag->get_entity_id($entity), $account->uid);
604
}
605
606
/**
607
 * Rules integration access callback.
608
 */
609
function flag_rules_integration_access($type, $name) {
610
  return user_access('administer flags');
611
}