Projet

Général

Profil

Paste
Télécharger (4,57 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_bulk_operations / plugins / operation_types / rules_component.class.php @ 9df8b457

1
<?php
2

    
3
/**
4
 * @file
5
 * Defines the class for rules components (rule, ruleset, action).
6
 * Belongs to the "rules_component" operation type plugin.
7
 */
8

    
9
class ViewsBulkOperationsRulesComponent extends ViewsBulkOperationsBaseOperation {
10

    
11
  /**
12
   * Returns the access bitmask for the operation, used for entity access checks.
13
   *
14
   * Rules has its own permission system, so the lowest bitmask is enough.
15
   */
16
  public function getAccessMask() {
17
    return VBO_ACCESS_OP_VIEW;
18
  }
19

    
20
  /**
21
   * Returns whether the provided account has access to execute the operation.
22
   *
23
   * @param $account
24
   */
25
  public function access($account) {
26
    return rules_action('component_' . $this->operationInfo['key'])->access();
27
  }
28

    
29
  /**
30
   * Returns the configuration form for the operation.
31
   * Only called if the operation is declared as configurable.
32
   *
33
   * @param $form
34
   *   The views form.
35
   * @param $form_state
36
   *   An array containing the current state of the form.
37
   * @param $context
38
   *   An array of related data provided by the caller.
39
   */
40
  public function form($form, &$form_state, array $context) {
41
    $entity_key = $this->operationInfo['parameters']['entity_key'];
42
    // List types need to match the original, so passing list<node> instead of
43
    // list<entity> won't work. However, passing 'node' instead of 'entity'
44
    // will work, and is needed in order to get the right tokens.
45
    $list_type = 'list<' . $this->operationInfo['type'] . '>';
46
    $entity_type = $this->aggregate() ? $list_type : $this->entityType;
47
    $info = entity_get_info($this->entityType);
48

    
49
    // The component action is wrapped in an action set using the entity, so
50
    // that the action configuration form can make use of the entity e.g. for
51
    // tokens.
52
    $set = rules_action_set(array($entity_key => array('type' => $entity_type, 'label' => $info['label'])));
53
    $action = rules_action('component_' . $this->operationInfo['key'], array($entity_key . ':select' => $entity_key));
54
    $set->action($action);
55

    
56
    // Embed the form of the component action, but default to "input" mode for
57
    // all parameters if available.
58
    foreach ($action->parameterInfo() as $name => $info) {
59
      $form_state['parameter_mode'][$name] = 'input';
60
    }
61
    $action->form($form, $form_state);
62

    
63
    // Remove the configuration form element for the "entity" param, as it
64
    // should just use the passed in entity.
65
    unset($form['parameter'][$entity_key]);
66

    
67
    // Tweak direct input forms to be more end-user friendly.
68
    foreach ($action->parameterInfo() as $name => $info) {
69
      // Remove the fieldset and move its title to the form element.
70
      if (isset($form['parameter'][$name]['settings'][$name]['#title'])) {
71
        $form['parameter'][$name]['#type'] = 'container';
72
        $form['parameter'][$name]['settings'][$name]['#title'] = $form['parameter'][$name]['#title'];
73
      }
74
      // Hide the switch button if it's there.
75
      if (isset($form['parameter'][$name]['switch_button'])) {
76
        $form['parameter'][$name]['switch_button']['#access'] = FALSE;
77
      }
78
    }
79

    
80
    return $form;
81
  }
82

    
83
  /**
84
   * Validates the configuration form.
85
   * Only called if the operation is declared as configurable.
86
   *
87
   * @param $form
88
   *   The views form.
89
   * @param $form_state
90
   *   An array containing the current state of the form.
91
   */
92
  public function formValidate($form, &$form_state) {
93
    rules_ui_form_rules_config_validate($form, $form_state);
94
  }
95

    
96
  /**
97
   * Stores the rules element added to the form state in form(), so that it
98
   * can be used in execute().
99
   * Only called if the operation is declared as configurable.
100
   *
101
   * @param $form
102
   *   The views form.
103
   * @param $form_state
104
   *   An array containing the current state of the form.
105
   */
106
  public function formSubmit($form, &$form_state) {
107
    $this->rulesElement = $form_state['rules_element']->root();
108
  }
109

    
110
  /**
111
   * Executes the selected operation on the provided data.
112
   *
113
   * @param $data
114
   *   The data to to operate on. An entity or an array of entities.
115
   * @param $context
116
   *   An array of related data (selected views rows, etc).
117
   */
118
  public function execute($data, array $context) {
119
    // If there was a config form, there's a rules_element.
120
    // If not, fallback to the component key.
121
    if ($this->configurable()) {
122
      $element = $this->rulesElement;
123
    }
124
    else {
125
     $element = rules_action('component_' . $this->operationInfo['parameters']['component_key']);
126
    }
127
    $wrapper_type = is_array($data) ? "list<{$this->entityType}>" : $this->entityType;
128
    $wrapper = entity_metadata_wrapper($wrapper_type, $data);
129
    $element->execute($wrapper);
130
  }
131
}