Projet

Général

Profil

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

root / drupal7 / sites / all / modules / flag / flag.rules.inc @ 1f142f4f

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
          'description' => t('During a flagging/unflagging event the count
225
            will take into account the current flagging/unflagging procedure.'),
226
          '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
          '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
          '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
          '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
          '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
379
  // 'fetch node by user'.
380
  $items['flag_fetch_entity_by_user'] = $items['flag_fetch_node_by_user'];
381
  $items['flag_fetch_entity_by_user']['label'] .= ' ' . t('(Legacy)');
382
  return $items;
383
}
384

    
385
/**
386
 * Base action implementation: Flag.
387
 */
388
function flag_rules_action_flag($flag, $entity, $flagging_user, $permissions_check) {
389
  $flag->flag('flag', $flag->get_entity_id($entity), $flagging_user, $permissions_check);
390
}
391

    
392
/**
393
 * Base action implementation: Unflag.
394
 */
395
function flag_rules_action_unflag($flag, $entity, $flagging_user, $permissions_check) {
396
  $flag->flag('unflag', $flag->get_entity_id($entity), $flagging_user, $permissions_check);
397
}
398

    
399
/**
400
 * Base action implementation: Trim flag.
401
 */
402
function flag_rules_action_trim($flag, $flagging_user, $cutoff_size, $trim_newest, $permissions_check) {
403
  // For some reason, when this action fires in response to a flagging event,
404
  // as an anonymous user, then the $flagging_user is sent through as FALSE.
405
  // Not sure why. This workaround fixes the problem in this specific case.
406
  if ($flagging_user === FALSE) {
407
    $flagging_user = $GLOBALS['user'];
408
  }
409
  flag_trim_flag($flag, $flagging_user, $cutoff_size, $trim_newest, $permissions_check);
410
}
411

    
412
/**
413
 * Base action implementation: Fetch users who flagged an entity.
414
 */
415
function flag_rules_action_fetch_users($flag, $entity) {
416
  $result = db_select('flagging', 'fc')
417
    ->fields('fc', array('uid'))
418
    ->condition('entity_type', $flag->entity_type)
419
    ->condition('entity_id', $flag->get_entity_id($entity))
420
    ->condition('fid', $flag->fid)
421
    ->execute();
422
  $uids = $result->fetchCol();
423
  // Filter out anonymous users.
424
  return array('users' => array_filter($uids));
425
}
426

    
427
/**
428
 * Base action implementation: Fetch entities flagged by a user.
429
 */
430
function flag_rules_action_fetch_entity_by_user($flag, $entity) {
431
  $query = db_select('flagging', 'fc')
432
    ->fields('fc', array('entity_id'))
433
    ->condition('entity_type', $flag->entity_type)
434
    ->condition('fid', $flag->fid);
435
  // For global flags the user parameter is ignored, so we add the
436
  // extra 'uid' condition when the flag is NOT global.
437
  if (!$flag->global) {
438
    $user = entity_metadata_wrapper('user', $entity);
439
    $sid = $user->flag_sid->value();
440
    $query = $query->condition('uid', $user->uid->value());
441
    // Filter out any bad session ids and any users that aren't anonymous.
442
    if (!empty($sid) && $sid != -1) {
443
      $query->condition('sid', $sid);
444
    }
445
  }
446
  $result = $query->execute();
447
  $flagged = $result->fetchCol();
448
  return array('content_flagged_by_user' => $flagged);
449
}
450

    
451
/**
452
 * Base action implementation: Fetch overall count for a particular flag.
453
 *
454
 * The count that is returned during a flagging or an unflagging will take into
455
 * account the current flag/unflag process.
456
 */
457
function flag_rules_action_fetch_overall_flag_count($flag) {
458
  $count = flag_get_flag_counts($flag->name);
459
  return array('overall_flag_count' => $count);
460
}
461

    
462
/**
463
 * Helper function which will return all the available flag types.
464
 *
465
 *  @return
466
 *  An array of flag type names keyed by the type name.
467
 */
468
function flag_rules_get_flag_types() {
469
  $types = array();
470
  foreach (flag_get_types() as $type) {
471
    $types[$type] = $type;
472
  }
473
  return $types;
474
}
475

    
476
/**
477
 * Base action implementation: Fetch count of flags for a particular entity
478
 * type.
479
 *
480
 * During a flagging, the current flagging will be included in the count.
481
 * During an unflagging, the current flagging being removed will not yet have
482
 * been removed from the count.
483
 */
484
function flag_rules_action_fetch_entity_flag_count($flag, $entity_type) {
485
  $count = flag_get_entity_flag_counts($flag, $entity_type);
486
  return array('entity_flag_count' => $count);
487
}
488

    
489
/**
490
 * Base action implementation: Fetch user's flag count.
491
 *
492
 * During a flagging, the current flagging will be included in the count.
493
 * During an unflagging, the current flagging will not yet have been removed
494
 * from the count.
495
 */
496
function flag_rules_action_fetch_user_flag_count($flag, $user) {
497
  $count = flag_get_user_flag_counts($flag, $user);
498
  return array('user_flag_count' => $count);
499
}
500

    
501
/**
502
 * Implements hook_rules_condition_info().
503
 */
504
function flag_rules_condition_info() {
505
  $items = array();
506
  foreach (flag_get_types() as $type) {
507
    $entity_info = entity_get_info($type);
508
    $label = isset($entity_info[$type]['label']) ? $entity_info[$type]['label'] : $type;
509
    $items += array(
510
      'flag_threshold_' . $type => array(
511
        'label' => drupal_ucfirst(t('@type has flagging count', array('@type' => $label))),
512
        'base' => 'flag_rules_condition_threshold',
513
        'parameter' => array(
514
          'flag' => array(
515
            'type' => 'flag',
516
            'label' => t('Flag'),
517
            'flag_type' => $type,
518
            'description' => t('The flag to check for.'),
519
          ),
520
          $type => array(
521
            'type' => $type,
522
            'label' => $label,
523
          ),
524
          'number' => array(
525
            'type' => 'integer',
526
            'label' => t('Number'),
527
            'description' => t('The number against which to test the number of
528
              times the object is flagged. For example, if you type "3" here,
529
              and choose "Greater than" for the operator, then this condition
530
              will return TRUE if the object is flagged more than three times.
531
              During a flagging or an unflagging event the count will take into
532
              account the current flag/unflag process.'),
533
          ),
534
          'operator' => array(
535
            'type' => 'text',
536
            'label' => t('Comparison operator'),
537
            'options list' => 'flag_rules_condition_threshold_operator_options',
538
            'restriction' => 'input',
539
            'default value' => '=',
540
            'optional' => TRUE,
541
          ),
542
        ),
543
        'group' => t('Flag'),
544
        'access callback' => 'flag_rules_integration_access',
545
      ),
546
      'flag_flagged_' . $type => array(
547
        'label' => drupal_ucfirst(t('@type is flagged', array('@type' => $label))),
548
        'base' => 'flag_rules_condition_flagged',
549
        'parameter' => array(
550
          'flag' => array(
551
            'type' => 'flag',
552
            'label' => t('Flag'),
553
            'flag_type' => $type,
554
            'description' => t('The flag to check for.'),
555
          ),
556
          $type => array(
557
            'type' => $type,
558
            'label' => $label,
559
          ),
560
          'flagging_user' => array(
561
            'type' => 'user',
562
            'label' => t('User on whose behalf to check'),
563
            'description' => t('For non-global flags, this is the user on whose behalf the flag is checked.'),
564
          ),
565
        ),
566
        'group' => t('Flag'),
567
        'access callback' => 'flag_rules_integration_access',
568
      ),
569
    );
570
  }
571
  return $items;
572
}
573

    
574
/**
575
 * Options list callback for the operator parameter of the flagging threshold
576
 * condition.
577
 */
578
function flag_rules_condition_threshold_operator_options() {
579
  return array(
580
    '>'  => t('Greater than'),
581
    '>=' => t('Greater than or equal'),
582
    '='  => t('Equal to'),
583
    '<=' => t('Less than or equal'),
584
    '<'  => t('Less than'),
585
  );
586
}
587

    
588
/**
589
 * Condition: Check flagging count.
590
 *
591
 * The count that is returned during a flagging or an unflagging will take into
592
 * acount the current flag/unflag process.
593
 */
594
function flag_rules_condition_threshold($flag, $entity, $number, $operator = '=') {
595
  $count = $flag->get_count($flag->get_entity_id($entity));
596

    
597
  switch ($operator) {
598
    case '>': return $count > $number;
599
    case '>=': return $count >= $number;
600
    case '=': return $count == $number;
601
    case '<': return $count < $number;
602
    case '<=': return $count <= $number;
603
  }
604
}
605

    
606
/**
607
 * Condition: Flag is flagged.
608
 */
609
function flag_rules_condition_flagged($flag, $entity, $account) {
610
  return $flag->is_flagged($flag->get_entity_id($entity), $account->uid);
611
}
612

    
613
/**
614
 * Rules integration access callback.
615
 */
616
function flag_rules_integration_access($type, $name) {
617
  return user_access('administer flags');
618
}