rules_component.inc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @file
  4. * CTools plugin. Provides support for rules components (rule, ruleset, action).
  5. */
  6. $plugin = array(
  7. 'title' => t('Rules component'),
  8. 'list callback' => 'views_bulk_operations_operation_rules_component_list',
  9. 'handler' => array(
  10. 'file' => 'rules_component.class.php',
  11. 'class' => 'ViewsBulkOperationsRulesComponent',
  12. ),
  13. );
  14. /**
  15. * Returns a prepared list of available rules components.
  16. *
  17. * @param $operation_id
  18. * The full, prefixed operation_id of the operation (in this case, rules
  19. * component) to return, or NULL to return an array with all operations.
  20. */
  21. function views_bulk_operations_operation_rules_component_list($operation_id = NULL) {
  22. if (!module_exists('rules')) {
  23. return array();
  24. }
  25. $entity_info = entity_get_info();
  26. $entity_types = array_keys($entity_info);
  27. $supported_types = array('entity', 'list<entity>');
  28. $list_types = array('list<entity>');
  29. foreach ($entity_types as $type) {
  30. $supported_types[] = $type;
  31. $supported_types[] = "list<$type>";
  32. $list_types[] = "list<$type>";
  33. }
  34. $components = array();
  35. if (isset($operation_id)) {
  36. $id_fragments = explode('::', $operation_id);
  37. $components[$id_fragments[1]] = rules_config_load($id_fragments[1]);
  38. // No need to go any further if the component no longer exists.
  39. if (!$components[$id_fragments[1]]) {
  40. return FALSE;
  41. }
  42. }
  43. else {
  44. $components = rules_get_components(FALSE, 'action');
  45. }
  46. $operations = array();
  47. foreach ($components as $name => $component) {
  48. $parameter_info = $component->parameterInfo();
  49. $first_parameter = reset($parameter_info);
  50. $parameter_keys = array_keys($parameter_info);
  51. $entity_key = reset($parameter_keys);
  52. // If the first param is not an entity type, skip the component.
  53. if (!in_array($first_parameter['type'], $supported_types)) {
  54. continue;
  55. }
  56. // If the first parameter is a list type (list<node>, list<entity>, etc)
  57. // then turn aggregation on, and set the correct entity type.
  58. if (in_array($first_parameter['type'], $list_types)) {
  59. $type = str_replace(array('list<', '>'), '', $first_parameter['type']);
  60. $aggregate = TRUE;
  61. }
  62. else {
  63. $type = $first_parameter['type'];
  64. $aggregate = FALSE;
  65. }
  66. // All operations must be prefixed with the operation type.
  67. $new_operation_id = 'rules_component::' . $name;
  68. $operations[$new_operation_id] = array(
  69. 'operation_type' => 'rules_component',
  70. // Keep the unprefixed key around, for internal use.
  71. 'key' => $name,
  72. 'label' => $component->label,
  73. 'parameters' => array('component_key' => $name, 'entity_key' => $entity_key),
  74. 'configurable' => count($parameter_info) > 1,
  75. 'type' => $type,
  76. 'aggregate' => $aggregate,
  77. );
  78. }
  79. if (isset($operation_id)) {
  80. return isset($operations[$operation_id]) ? $operations[$operation_id] : FALSE;
  81. }
  82. else {
  83. return $operations;
  84. }
  85. }