rules_component.class.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @file
  4. * Defines the class for rules components (rule, ruleset, action).
  5. * Belongs to the "rules_component" operation type plugin.
  6. */
  7. class ViewsBulkOperationsRulesComponent extends ViewsBulkOperationsBaseOperation {
  8. /**
  9. * Returns the access bitmask for the operation, used for entity access checks.
  10. *
  11. * Rules has its own permission system, so the lowest bitmask is enough.
  12. */
  13. public function getAccessMask() {
  14. return VBO_ACCESS_OP_VIEW;
  15. }
  16. /**
  17. * Returns whether the provided account has access to execute the operation.
  18. *
  19. * @param $account
  20. */
  21. public function access($account) {
  22. return rules_action('component_' . $this->operationInfo['key'])->access();
  23. }
  24. /**
  25. * Returns the configuration form for the operation.
  26. * Only called if the operation is declared as configurable.
  27. *
  28. * @param $form
  29. * The views form.
  30. * @param $form_state
  31. * An array containing the current state of the form.
  32. * @param $context
  33. * An array of related data provided by the caller.
  34. */
  35. public function form($form, &$form_state, array $context) {
  36. $entity_key = $this->operationInfo['parameters']['entity_key'];
  37. // List types need to match the original, so passing list<node> instead of
  38. // list<entity> won't work. However, passing 'node' instead of 'entity'
  39. // will work, and is needed in order to get the right tokens.
  40. $list_type = 'list<' . $this->operationInfo['type'] . '>';
  41. $entity_type = $this->aggregate() ? $list_type : $this->entityType;
  42. $info = entity_get_info($this->entityType);
  43. // The component action is wrapped in an action set using the entity, so
  44. // that the action configuration form can make use of the entity e.g. for
  45. // tokens.
  46. $set = rules_action_set(array($entity_key => array('type' => $entity_type, 'label' => $info['label'])));
  47. $action = rules_action('component_' . $this->operationInfo['key'], array($entity_key . ':select' => $entity_key));
  48. $set->action($action);
  49. // Embed the form of the component action, but default to "input" mode for
  50. // all parameters if available.
  51. foreach ($action->parameterInfo() as $name => $info) {
  52. $form_state['parameter_mode'][$name] = 'input';
  53. }
  54. $action->form($form, $form_state);
  55. // Remove the configuration form element for the "entity" param, as it
  56. // should just use the passed in entity.
  57. unset($form['parameter'][$entity_key]);
  58. // Tweak direct input forms to be more end-user friendly.
  59. foreach ($action->parameterInfo() as $name => $info) {
  60. // Remove the fieldset and move its title to the form element.
  61. if (isset($form['parameter'][$name]['settings'][$name]['#title'])) {
  62. $form['parameter'][$name]['#type'] = 'container';
  63. $form['parameter'][$name]['settings'][$name]['#title'] = $form['parameter'][$name]['#title'];
  64. }
  65. // Hide the switch button if it's there.
  66. if (isset($form['parameter'][$name]['switch_button'])) {
  67. $form['parameter'][$name]['switch_button']['#access'] = FALSE;
  68. }
  69. }
  70. return $form;
  71. }
  72. /**
  73. * Validates the configuration form.
  74. * Only called if the operation is declared as configurable.
  75. *
  76. * @param $form
  77. * The views form.
  78. * @param $form_state
  79. * An array containing the current state of the form.
  80. */
  81. public function formValidate($form, &$form_state) {
  82. rules_ui_form_rules_config_validate($form, $form_state);
  83. }
  84. /**
  85. * Stores the rules element added to the form state in form(), so that it
  86. * can be used in execute().
  87. * Only called if the operation is declared as configurable.
  88. *
  89. * @param $form
  90. * The views form.
  91. * @param $form_state
  92. * An array containing the current state of the form.
  93. */
  94. public function formSubmit($form, &$form_state) {
  95. $this->rulesElement = $form_state['rules_element']->root();
  96. }
  97. /**
  98. * Executes the selected operation on the provided data.
  99. *
  100. * @param $data
  101. * The data to to operate on. An entity or an array of entities.
  102. * @param $context
  103. * An array of related data (selected views rows, etc).
  104. */
  105. public function execute($data, array $context) {
  106. // If there was a config form, there's a rules_element.
  107. // If not, fallback to the component key.
  108. if ($this->configurable()) {
  109. $element = $this->rulesElement;
  110. }
  111. else {
  112. $element = rules_action('component_' . $this->operationInfo['parameters']['component_key']);
  113. }
  114. $wrapper_type = is_array($data) ? "list<{$this->entityType}>" : $this->entityType;
  115. $wrapper = entity_metadata_wrapper($wrapper_type, $data);
  116. $element->execute($wrapper);
  117. }
  118. }