Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views_bulk_operations / views / views_bulk_operations_handler_field_operations.inc @ 74f6bef0

1
<?php
2

    
3
/**
4
* @file
5
* Views field handler. Contains all relevant VBO options and related logic.
6
* Implements the Views Form API.
7
*/
8

    
9
class views_bulk_operations_handler_field_operations extends views_handler_field {
10
  var $revision = FALSE;
11

    
12
  function init(&$view, &$options) {
13
    parent::init($view, $options);
14

    
15
    // Update old settings
16
    if (!empty($options['vbo']) && empty($this->options['vbo_operations'])) {
17
      $this->options['vbo_operations'] = $options['vbo']['operations'];
18
      unset($options['vbo']['operations']);
19
      $this->options['vbo_settings'] = $options['vbo'] + $this->options['vbo_settings'];
20
    }
21
    // When updating old Views it is possible for this value to stay empty.
22
    if (empty($this->options['vbo_settings']['entity_load_capacity'])) {
23
      $this->options['vbo_settings']['entity_load_capacity'] = 10;
24
    }
25

    
26
    foreach ($this->options['vbo_operations'] as $operation_id => &$operation_options) {
27
      // Prefix all un-prefixed operations.
28
      if (strpos($operation_id, '::') === FALSE) {
29
        $operations = views_bulk_operations_get_operation_info();
30
        // Basically, guess.
31
        foreach (array('action', 'rules_component') as $operation_type) {
32
          $new_operation_id = $operation_type . '::' . $operation_id;
33
          if (isset($operations[$new_operation_id])) {
34
            $this->options['vbo_operations'][$new_operation_id] = $operation_options;
35
            break;
36
          }
37
        }
38

    
39
        // Remove the old operation in any case.
40
        unset($this->options['vbo_operations'][$operation_id]);
41
      }
42

    
43
      // Rename the use_queue setting.
44
      if (isset($operation_options['use_queue']) && !isset($operation_options['postpone_processing'])) {
45
        $operation_options['postpone_processing'] = $operation_options['use_queue'];
46
        unset($operation_options['use_queue']);
47
      }
48
    }
49
  }
50

    
51
  function option_definition() {
52
    $options = parent::option_definition();
53

    
54
    $options['vbo_settings'] = array(
55
      'contains' => array(
56
        'display_type' => array('default' => 0),
57
        'enable_select_all_pages' => array('default' => TRUE),
58
        'force_single' => array('default' => FALSE),
59
        'entity_load_capacity' => array('default' => 10),
60
        'skip_batching' => array('default' => 0),
61
      ),
62
    );
63
    $options['vbo_operations'] = array(
64
      'default' => array(),
65
      'unpack_translatable' => 'unpack_operations',
66
    );
67

    
68
    return $options;
69
  }
70

    
71
  function unpack_operations(&$translatable, $storage, $option, $definition, $parents, $keys) {
72
    $translatable[] = array(
73
      'value' => t('- Choose an operation -'),
74
      'keys' => array_merge($keys, array('noop')),
75
    );
76
    foreach ($storage[$option] as $key => $operation) {
77
      if (!empty($operation['override_label']) && !empty($operation['label'])) {
78
        $translatable[] = array(
79
          'value' => $operation['label'],
80
          'keys' => array_merge($keys, array($key)),
81
        );
82
      }
83
    }
84
  }
85

    
86
  function options_form(&$form, &$form_state) {
87
    parent::options_form($form, $form_state);
88

    
89
    $form['vbo_settings'] = array(
90
      '#type' => 'fieldset',
91
      '#title' => t('Bulk operations settings'),
92
      '#collapsible' => TRUE,
93
      '#collapsed' => TRUE,
94
    );
95
    $form['vbo_settings']['display_type'] = array(
96
      '#type' => 'radios',
97
      '#title' => t('Display operations as'),
98
      '#default_value' => $this->options['vbo_settings']['display_type'],
99
      '#options' => array(
100
        t('Dropdown selectbox with Submit button'),
101
        t('Each action as a separate button'),
102
      ),
103
    );
104
    $form['vbo_settings']['enable_select_all_pages'] = array(
105
      '#type' => 'checkbox',
106
      '#title' => t('Enable "Select all items on all pages"'),
107
      '#default_value' => $this->options['vbo_settings']['enable_select_all_pages'],
108
      '#description' => t('Check this box to enable the ability to select all items on all pages.'),
109
    );
110
    $form['vbo_settings']['force_single'] = array(
111
      '#type' => 'checkbox',
112
      '#title' => t('Force single'),
113
      '#default_value' => $this->options['vbo_settings']['force_single'],
114
      '#description' => t('Check this box to restrict selection to a single value.'),
115
    );
116
    $form['vbo_settings']['entity_load_capacity'] = array(
117
      '#type' => 'textfield',
118
      '#title' => t('Number of entities to load at once'),
119
      '#description' => t("Improve execution performance at the cost of memory usage. Set to '1' if you're having problems."),
120
      '#default_value' => $this->options['vbo_settings']['entity_load_capacity'],
121
    );
122
    $form['vbo_settings']['skip_batching'] = array(
123
      '#type' => 'checkbox',
124
      '#title' => t('Skip batching'),
125
      '#default_value' => $this->options['vbo_settings']['skip_batching'],
126
      '#description' => '<b>' . t('Warning:') . '</b> ' . t('This will cause timeouts for larger amounts of selected items.'),
127
    );
128

    
129
    // Display operations and their settings.
130
    $form['vbo_operations'] = array(
131
      '#tree' => TRUE,
132
      '#type' => 'fieldset',
133
      '#title' => t('Selected bulk operations'),
134
      '#collapsible' => TRUE,
135
      '#collapsed' => FALSE,
136
    );
137

    
138
    $entity_type = $this->get_entity_type();
139
    $options = $this->options['vbo_operations'];
140
    foreach (views_bulk_operations_get_applicable_operations($entity_type, $options) as $operation_id => $operation) {
141
      $operation_options = !empty($options[$operation_id]) ? $options[$operation_id] : array();
142

    
143
      $dom_id = 'edit-options-vbo-operations-' . str_replace(array('_', ':'), array('-', ''), $operation_id);
144
      $form['vbo_operations'][$operation_id]['selected'] = array(
145
        '#type' => 'checkbox',
146
        '#title' => $operation->adminLabel(),
147
        '#default_value' => !empty($operation_options['selected']),
148
      );
149
      if (!$operation->aggregate()) {
150
        $form['vbo_operations'][$operation_id]['postpone_processing'] = array(
151
          '#type' => 'checkbox',
152
          '#title' => t('Enqueue the operation instead of executing it directly'),
153
          '#default_value' => !empty($operation_options['postpone_processing']),
154
          '#dependency' => array(
155
            $dom_id . '-selected' => array(1),
156
          ),
157
        );
158
      }
159
      $form['vbo_operations'][$operation_id]['skip_confirmation'] = array(
160
        '#type' => 'checkbox',
161
        '#title' => t('Skip confirmation step'),
162
        '#default_value' => !empty($operation_options['skip_confirmation']),
163
        '#dependency' => array(
164
          $dom_id . '-selected' => array(1),
165
        ),
166
      );
167

    
168
      $form['vbo_operations'][$operation_id] += $operation->adminOptionsForm($dom_id, $this);
169
    }
170
  }
171

    
172
  function options_validate(&$form, &$form_state) {
173
    parent::options_validate($form, $form_state);
174

    
175
    $entity_type = $this->get_entity_type();
176
    foreach ($form_state['values']['options']['vbo_operations'] as $operation_id => &$options) {
177
      if (empty($options['selected'])) {
178
        continue;
179
      }
180

    
181
      $operation = views_bulk_operations_get_operation($operation_id, $entity_type, $options);
182
      $fake_form = $form['vbo_operations'][$operation_id];
183
      $fake_form_state = array('values' => &$options);
184
      $error_element_base = 'vbo_operations][' . $operation_id . '][';
185
      $operation->adminOptionsFormValidate($fake_form, $fake_form_state, $error_element_base);
186
    }
187
  }
188

    
189
  function options_submit(&$form, &$form_state) {
190
    parent::options_submit($form, $form_state);
191

    
192
    $entity_type = $this->get_entity_type();
193
    foreach ($form_state['values']['options']['vbo_operations'] as $operation_id => &$options) {
194
      if (empty($options['selected'])) {
195
        continue;
196
      }
197

    
198
      $operation = views_bulk_operations_get_operation($operation_id, $entity_type, $options);
199
      $fake_form = $form['vbo_operations'][$operation_id];
200
      $fake_form_state = array('values' => &$options);
201
      $operation->adminOptionsFormSubmit($fake_form, $fake_form_state);
202
    }
203
  }
204

    
205
  /**
206
   * Returns the value of a vbo option.
207
   */
208
  function get_vbo_option($key, $default = NULL) {
209
    return isset($this->options['vbo_settings'][$key]) ? $this->options['vbo_settings'][$key] : $default;
210
  }
211

    
212
  /**
213
   * If the view is using a table style, provide a
214
   * placeholder for a "select all" checkbox.
215
   */
216
  function label() {
217
    if (!empty($this->view->style_plugin) && $this->view->style_plugin instanceof views_plugin_style_table && !$this->options['vbo_settings']['force_single']) {
218
      return '<!--views-bulk-operations-select-all-->';
219
    }
220
    else {
221
      return parent::label();
222
    }
223
  }
224

    
225
  function render($values) {
226
    return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
227
  }
228

    
229
  /**
230
   * The form which replaces the placeholder from render().
231
   */
232
  function views_form(&$form, &$form_state) {
233
    // The view is empty, abort.
234
    if (empty($this->view->result)) {
235
      return;
236
    }
237

    
238
    $form[$this->options['id']] = array(
239
      '#tree' => TRUE,
240
    );
241
    // At this point, the query has already been run, so we can access the results
242
    // in order to get the base key value (for example, nid for nodes).
243
    foreach ($this->view->result as $row_index => $row) {
244
      $entity_id = $this->get_value($row);
245

    
246
      if ($this->options['vbo_settings']['force_single']) {
247
        $form[$this->options['id']][$row_index] = array(
248
          '#type' => 'radio',
249
          '#parents' => array($this->options['id']),
250
          '#return_value' => $entity_id,
251
        );
252
      }
253
      else {
254
        $form[$this->options['id']][$row_index] = array(
255
          '#type' => 'checkbox',
256
          '#return_value' => $entity_id,
257
          '#default_value' => FALSE,
258
          '#attributes' => array('class' => array('vbo-select')),
259
        );
260
      }
261
    }
262
  }
263

    
264
  public function get_selected_operations() {
265
    global $user;
266
    $selected = drupal_static(__FUNCTION__);
267
    if (!isset($selected)) {
268
      $entity_type = $this->get_entity_type();
269
      $selected = array();
270
      foreach ($this->options['vbo_operations'] as $operation_id => $options) {
271
        if (empty($options['selected'])) {
272
          continue;
273
        }
274

    
275
        $operation = views_bulk_operations_get_operation($operation_id, $entity_type, $options);
276
        if (!$operation || !$operation->access($user)) {
277
          continue;
278
        }
279
        $selected[$operation_id] = $operation;
280
      }
281
    }
282

    
283
    return $selected;
284
  }
285

    
286
  /**
287
   * Returns the options stored for the provided operation id.
288
   */
289
  public function get_operation_options($operation_id) {
290
    $options = $this->options['vbo_operations'];
291
    return isset($options[$operation_id]) ? $options[$operation_id] : array();
292
  }
293

    
294
  /**
295
   * Determine the base table of the VBO field, and then use it to determine
296
   * the entity type that VBO is operating on.
297
   */
298
  public function get_entity_type() {
299
    $base_table = $this->view->base_table;
300

    
301
    // If the current field is under a relationship you can't be sure that the
302
    // base table of the view is the base table of the current field.
303
    // For example a field from a node author on a node view does have users as base table.
304
    if (!empty($this->options['relationship']) && $this->options['relationship'] != 'none') {
305
      $relationships = $this->view->display_handler->get_option('relationships');
306
      $options = $relationships[$this->options['relationship']];
307
      $data = views_fetch_data($options['table']);
308
      $base_table = $data[$options['field']]['relationship']['base'];
309
    }
310
    // The base table is now known, use it to determine the entity type.
311
    foreach (entity_get_info() as $entity_type => $info) {
312
      if (isset($info['base table']) && $info['base table'] == $base_table) {
313
        return $entity_type;
314
      }
315
      elseif (isset($info['revision table']) && $info['revision table'] == $base_table) {
316
        $this->revision = TRUE;
317
        return $entity_type;
318
      }
319
    }
320
    // This should never happen.
321
    _views_bulk_operations_report_error("Could not determine the entity type for VBO field on views base table %table", array('%table' => $base_table));
322
    return FALSE;
323
  }
324
}