summaryrefslogtreecommitdiff
path: root/sites/all/modules/views_bulk_operations/plugins/operation_types/rules_component.inc
blob: 81f5c46d63e0809037f74e93093f7e2252374f0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php

/**
 * @file
 * CTools plugin. Provides support for rules components (rule, ruleset, action).
 */

$plugin = array(
  'title' => t('Rules component'),
  'list callback' => 'views_bulk_operations_operation_rules_component_list',
  'handler' => array(
    'file' => 'rules_component.class.php',
    'class' => 'ViewsBulkOperationsRulesComponent',
  ),
);

/**
 * Returns a prepared list of available rules components.
 *
 * @param $operation_id
 *   The full, prefixed operation_id of the operation (in this case, rules
 *   component) to return, or NULL to return an array with all operations.
 */
function views_bulk_operations_operation_rules_component_list($operation_id = NULL) {
  if (!module_exists('rules')) {
    return array();
  }

  $entity_info = entity_get_info();
  $entity_types = array_keys($entity_info);
  $supported_types = array('entity', 'list<entity>');
  $list_types = array('list<entity>');
  foreach ($entity_types as $type) {
    $supported_types[] = $type;
    $supported_types[] = "list<$type>";
    $list_types[] = "list<$type>";
  }

  $components = array();
  if (isset($operation_id)) {
    $id_fragments = explode('::', $operation_id);
    $components[$id_fragments[1]] = rules_config_load($id_fragments[1]);
    // No need to go any further if the component no longer exists.
    if (!$components[$id_fragments[1]]) {
      return FALSE;
    }
  }
  else {
    $components = rules_get_components(FALSE, 'action');
  }

  $operations = array();
  foreach ($components as $name => $component) {
    $parameter_info = $component->parameterInfo();
    $first_parameter = reset($parameter_info);
    $parameter_keys = array_keys($parameter_info);
    $entity_key = reset($parameter_keys);
    // If the first param is not an entity type, skip the component.
    if (!in_array($first_parameter['type'], $supported_types)) {
      continue;
    }

    // If the first parameter is a list type (list<node>, list<entity>, etc)
    // then turn aggregation on, and set the correct entity type.
    if (in_array($first_parameter['type'], $list_types)) {
      $type = str_replace(array('list<', '>'), '', $first_parameter['type']);
      $aggregate = TRUE;
    }
    else {
      $type = $first_parameter['type'];
      $aggregate = FALSE;
    }

    // All operations must be prefixed with the operation type.
    $new_operation_id = 'rules_component::' . $name;
    $operations[$new_operation_id] = array(
      'operation_type' => 'rules_component',
      // Keep the unprefixed key around, for internal use.
      'key' => $name,
      'label' => $component->label,
      'parameters' => array('component_key' => $name, 'entity_key' => $entity_key),
      'configurable' => count($parameter_info) > 1,
      'type' => $type,
      'aggregate' => $aggregate,
    );
  }

  if (isset($operation_id)) {
    return isset($operations[$operation_id]) ? $operations[$operation_id] : FALSE;
  }
  else {
    return $operations;
  }
}