Projet

Général

Profil

Paste
Télécharger (9,09 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_bulk_operations / plugins / operation_types / action.class.php @ b8087750

1
<?php
2

    
3
/**
4
 * @file
5
 * Defines the class for core actions.
6
 * Belongs to the "action" operation type plugin.
7
 */
8

    
9
class ViewsBulkOperationsAction extends ViewsBulkOperationsBaseOperation {
10

    
11
  /**
12
   * Contains the options provided by the user in the configuration form.
13
   *
14
   * @var array
15
   */
16
  public $formOptions = array();
17

    
18
  /**
19
   * Returns the access bitmask for the operation, used for entity access checks.
20
   */
21
  public function getAccessMask() {
22
    // Assume edit by default.
23
    if (empty($this->operationInfo['behavior'])) {
24
      $this->operationInfo['behavior'] = array('changes_property');
25
    }
26

    
27
    $mask = 0;
28
    if (in_array('views_property', $this->operationInfo['behavior'])) {
29
      $mask |= VBO_ACCESS_OP_VIEW;
30
    }
31
    if (in_array('changes_property', $this->operationInfo['behavior'])) {
32
      $mask |= VBO_ACCESS_OP_UPDATE;
33
    }
34
    if (in_array('creates_property', $this->operationInfo['behavior'])) {
35
      $mask |= VBO_ACCESS_OP_CREATE;
36
    }
37
    if (in_array('deletes_property', $this->operationInfo['behavior'])) {
38
      $mask |= VBO_ACCESS_OP_DELETE;
39
    }
40
    return $mask;
41
  }
42

    
43
  /**
44
   * Returns whether the provided account has access to execute the operation.
45
   *
46
   * @param $account
47
   */
48
  public function access($account) {
49
    // Use actions_permissions if enabled.
50
    if (module_exists('actions_permissions')) {
51
      $perm = actions_permissions_get_perm($this->operationInfo['label'], $this->operationInfo['key']);
52
      if (!user_access($perm, $account)) {
53
        return FALSE;
54
      }
55
    }
56
    // Check against additional permissions.
57
    if (!empty($this->operationInfo['permissions'])) {
58
      foreach ($this->operationInfo['permissions'] as $perm) {
59
        if (!user_access($perm, $account)) {
60
          return FALSE;
61
        }
62
      }
63
    }
64
    // Access granted.
65
    return TRUE;
66
  }
67

    
68
  /**
69
   * Returns the configuration form for the operation.
70
   * Only called if the operation is declared as configurable.
71
   *
72
   * @param $form
73
   *   The views form.
74
   * @param $form_state
75
   *   An array containing the current state of the form.
76
   * @param $context
77
   *   An array of related data provided by the caller.
78
   */
79
  public function form($form, &$form_state, array $context) {
80
    // Some modules (including this one) place their action callbacks
81
    // into separate files. At this point those files might no longer be
82
    // included due to an #ajax rebuild, so we call actions_list() to trigger
83
    // inclusion. The same thing is done by actions_do() on execute.
84
    actions_list();
85

    
86
    $context['settings'] = $this->getAdminOption('settings', array());
87
    $form_callback = $this->operationInfo['callback'] . '_form';
88
    return $form_callback($context, $form_state);
89
  }
90

    
91
  /**
92
   * Validates the configuration form.
93
   * Only called if the operation is declared as configurable.
94
   *
95
   * @param $form
96
   *   The views form.
97
   * @param $form_state
98
   *   An array containing the current state of the form.
99
   */
100
  public function formValidate($form, &$form_state) {
101
    // Some modules (including this one) place their action callbacks
102
    // into separate files. At this point those files might no longer be
103
    // included due to a page reload, so we call actions_list() to trigger
104
    // inclusion. The same thing is done by actions_do() on execute.
105
    actions_list();
106

    
107
    $validation_callback = $this->operationInfo['callback'] . '_validate';
108
    if (function_exists($validation_callback)) {
109
      $validation_callback($form, $form_state);
110
    }
111
  }
112

    
113
  /**
114
   * Handles the submitted configuration form.
115
   * This is where the operation can transform and store the submitted data.
116
   * Only called if the operation is declared as configurable.
117
   *
118
   * @param $form
119
   *   The views form.
120
   * @param $form_state
121
   *   An array containing the current state of the form.
122
   */
123
  public function formSubmit($form, &$form_state) {
124
    // Some modules (including this one) place their action callbacks
125
    // into separate files. At this point those files might no longer be
126
    // included due to a page reload, so we call actions_list() to trigger
127
    // inclusion. The same thing is done by actions_do() on execute.
128
    actions_list();
129

    
130
    $submit_callback = $this->operationInfo['callback'] . '_submit';
131
    // If the return value from the callback is an options array, store it for
132
    // later union onto the context.
133
    $options = $submit_callback($form, $form_state);
134
    if ($options && is_array($options)) {
135
      $this->formOptions = $options;
136
    }
137
  }
138

    
139
  /**
140
   * Returns the admin options form for the operation.
141
   *
142
   * The admin options form is embedded into the VBO field settings and used
143
   * to configure operation behavior. The options can later be fetched
144
   * through the getAdminOption() method.
145
   *
146
   * @param $dom_id
147
   *   The dom path to the level where the admin options form is embedded.
148
   *   Needed for #dependency.
149
   * @param $field_handler
150
   *   The Views field handler object for the VBO field.
151
   */
152
  public function adminOptionsForm($dom_id, $field_handler) {
153
    $form = parent::adminOptionsForm($dom_id, $field_handler);
154

    
155
    $settings_form_callback = $this->operationInfo['callback'] . '_views_bulk_operations_form';
156
    if (function_exists($settings_form_callback)) {
157
      $settings = $this->getAdminOption('settings', array());
158

    
159
      $form['settings'] = array(
160
        '#type' => 'fieldset',
161
        '#title' => t('Operation settings'),
162
        '#collapsible' => TRUE,
163
        '#dependency' => array(
164
          $dom_id . '-selected' => array(1),
165
        ),
166
      );
167
      $settings_dom_id = $dom_id . '-settings';
168
      $form['settings'] += $settings_form_callback($settings, $this->entityType, $settings_dom_id);
169
    }
170

    
171
    return $form;
172
  }
173

    
174
  /**
175
   * Validates the admin options form.
176
   *
177
   * @param $form
178
   *   The admin options form.
179
   * @param $form_state
180
   *   An array containing the current state of the form. Note that this array
181
   *   is constructed by the VBO views field handler, so it's not a real form
182
   *   state, it contains only the 'values' key.
183
   * @param $error_element_base
184
   *   The base to prepend to field names when using form_set_error().
185
   *   Needed because the admin settings form is embedded into a bigger form.
186
   */
187
  public function adminOptionsFormValidate($form, &$form_state, $error_element_base) {
188
    parent::adminOptionsFormValidate($form, $form_state, $error_element_base);
189

    
190
    if (!empty($form['settings'])) {
191
      $settings_validation_callback = $this->operationInfo['callback'] . '_views_bulk_operations_form_validate';
192
      if (function_exists($settings_validation_callback)) {
193
        $fake_form = $form['settings'];
194
        $fake_form_state = array('values' => &$form_state['values']['settings']);
195
        $error_element_base .= 'settings][';
196

    
197
        $settings_validation_callback($fake_form, $fake_form_state, $error_element_base);
198
      }
199
    }
200
  }
201

    
202
  /**
203
   * Handles the submitted admin options form.
204
   * Note that there is no need to handle saving the options, that is done
205
   * by the VBO views field handler, which also injects the options into the
206
   * operation object upon instantiation.
207
   *
208
   * @param $form
209
   *   The admin options form.
210
   * @param $form_state
211
   *   An array containing the current state of the form. Note that this array
212
   *   is constructed by the VBO views field handler, so it's not a real form
213
   *   state, it contains only the 'values' key.
214
   */
215
  public function adminOptionsFormSubmit($form, &$form_state) {
216
    parent::adminOptionsFormSubmit($form, $form_state);
217

    
218
    if (!empty($form['settings'])) {
219
      $settings_submit_callback = $this->operationInfo['callback'] . '_views_bulk_operations_form_submit';
220
      if (function_exists($settings_submit_callback)) {
221
        $fake_form = $form['settings'];
222
        $fake_form_state = array('values' => &$form_state['values']['settings']);
223

    
224
        $settings_submit_callback($form, $form_state);
225
      }
226
    }
227
  }
228

    
229
  /**
230
   * Returns whether the operation needs the full selected views rows to be
231
   * passed to execute() as a part of $context.
232
   */
233
  public function needsRows() {
234
    return !empty($this->operationInfo['pass rows']);
235
  }
236

    
237
  /**
238
   * Executes the selected operation on the provided data.
239
   *
240
   * @param $data
241
   *   The data to to operate on. An entity or an array of entities.
242
   * @param $context
243
   *   An array of related data (selected views rows, etc).
244
   */
245
  public function execute($data, array $context) {
246
    $context['entity_type'] = $this->entityType;
247
    $context['settings'] = $this->getAdminOption('settings', array());
248
    $context += $this->formOptions;
249
    $context += $this->operationInfo['parameters'];
250
    // Actions provided by the Drupal system module require the entity to be
251
    // present in $context, keyed by entity type.
252
    if (is_object($data)) {
253
      $context[$this->entityType] = $data;
254
    }
255

    
256
    actions_do($this->operationInfo['callback'], $data, $context);
257

    
258
    // The action might need to have its entities saved after execution.
259
    if (in_array('changes_property', $this->operationInfo['behavior'])) {
260
      $data = is_array($data) ? $data : array($data);
261
      foreach ($data as $entity) {
262
        entity_save($this->entityType, $entity);
263
      }
264
    }
265
  }
266
}