Project

General

Profile

Paste
Download (12.8 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views_bulk_operations / views / views_bulk_operations_handler_field_operations.inc @ 9a28ac3f

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_entity {
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
    // Check whether this is a revision.
51
    $table_data = views_fetch_data($this->table);
52
    if (!empty($table_data['table']['revision'])) {
53
      $this->revision = TRUE;
54
    }
55
  }
56

    
57
  function option_definition() {
58
    $options = parent::option_definition();
59

    
60
    $options['vbo_settings'] = array(
61
      'contains' => array(
62
        'display_type' => array('default' => 0),
63
        'enable_select_all_pages' => array('default' => TRUE),
64
        'row_clickable' => array('default' => TRUE),
65
        'force_single' => array('default' => FALSE),
66
        'entity_load_capacity' => array('default' => 10),
67
        'skip_batching' => array('default' => 0),
68
        'save_view_object_when_batching' => array('default' => 0),
69
      ),
70
    );
71
    $options['vbo_operations'] = array(
72
      'default' => array(),
73
      'unpack_translatable' => 'unpack_operations',
74
      'export' => 'export_vbo_operations',
75
    );
76

    
77
    return $options;
78
  }
79

    
80
  function export_vbo_operations($indent, $prefix, $storage, $option, $definition, $parents) {
81
    // Anti-recursion, since we use the parent export helper.
82
    unset($definition['export']);
83

    
84
    // Find and remove all unselected/disabled operations.
85
    foreach ($storage['vbo_operations'] as $operation_id => $operation) {
86
      if (empty($operation['selected'])) {
87
        unset($storage['vbo_operations'][$operation_id]);
88
      }
89
    }
90

    
91
    return parent::export_option($indent, $prefix, $storage, $option, $definition, $parents);
92
  }
93

    
94
  function unpack_operations(&$translatable, $storage, $option, $definition, $parents, $keys) {
95
    $translatable[] = array(
96
      'value' => t('- Choose an operation -'),
97
      'keys' => array_merge($keys, array('noop')),
98
    );
99
    foreach ($storage[$option] as $key => $operation) {
100
      if (!empty($operation['override_label']) && !empty($operation['label'])) {
101
        $translatable[] = array(
102
          'value' => $operation['label'],
103
          'keys' => array_merge($keys, array($key)),
104
        );
105
      }
106
    }
107
  }
108

    
109
  function options_form(&$form, &$form_state) {
110
    parent::options_form($form, $form_state);
111

    
112
    $form['vbo_settings'] = array(
113
      '#type' => 'fieldset',
114
      '#title' => t('Bulk operations settings'),
115
      '#collapsible' => TRUE,
116
      '#collapsed' => TRUE,
117
    );
118
    $form['vbo_settings']['display_type'] = array(
119
      '#type' => 'radios',
120
      '#title' => t('Display operations as'),
121
      '#default_value' => $this->options['vbo_settings']['display_type'],
122
      '#options' => array(
123
        t('Dropdown selectbox with Submit button'),
124
        t('Each action as a separate button'),
125
      ),
126
    );
127
    $form['vbo_settings']['enable_select_all_pages'] = array(
128
      '#type' => 'checkbox',
129
      '#title' => t('Enable "Select all items on all pages"'),
130
      '#default_value' => $this->options['vbo_settings']['enable_select_all_pages'],
131
      '#description' => t('Check this box to enable the ability to select all items on all pages.'),
132
    );
133
    $form['vbo_settings']['row_clickable'] = array(
134
      '#type' => 'checkbox',
135
      '#title' => t('Make the whole row clickable'),
136
      '#default_value' => $this->options['vbo_settings']['row_clickable'],
137
      '#description' => t('Check this box to select an item when its row has been clicked. Requires JavaScript.'),
138
    );
139
    $form['vbo_settings']['force_single'] = array(
140
      '#type' => 'checkbox',
141
      '#title' => t('Force single'),
142
      '#default_value' => $this->options['vbo_settings']['force_single'],
143
      '#description' => t('Check this box to restrict selection to a single value.'),
144
    );
145
    $form['vbo_settings']['entity_load_capacity'] = array(
146
      '#type' => 'textfield',
147
      '#title' => t('Number of entities to load at once'),
148
      '#description' => t("Improve execution performance at the cost of memory usage. Set to '1' if you're having problems."),
149
      '#default_value' => $this->options['vbo_settings']['entity_load_capacity'],
150
    );
151
    $form['vbo_settings']['skip_batching'] = array(
152
      '#type' => 'checkbox',
153
      '#title' => t('Skip batching'),
154
      '#default_value' => $this->options['vbo_settings']['skip_batching'],
155
      '#description' => '<b>' . t('Warning:') . '</b> ' . t('This will cause timeouts for larger amounts of selected items.'),
156
    );
157
    $form['vbo_settings']['save_view_object_when_batching'] = array(
158
      '#type' => 'checkbox',
159
      '#title' => t('Save the whole view object when batching'),
160
      '#default_value' => $this->options['vbo_settings']['save_view_object_when_batching'],
161
      '#description' => '<b>' . t('Warning:') . '</b> ' . t('Use this option when your view contains query conditions which are not defined as arguments.'),
162
    );
163

    
164
    // Display operations and their settings.
165
    $form['vbo_operations'] = array(
166
      '#tree' => TRUE,
167
      '#type' => 'fieldset',
168
      '#title' => t('Selected bulk operations'),
169
      '#collapsible' => TRUE,
170
      '#collapsed' => FALSE,
171
    );
172

    
173
    $entity_type = $this->get_entity_type();
174
    $options = $this->options['vbo_operations'];
175
    foreach (views_bulk_operations_get_applicable_operations($entity_type, $options) as $operation_id => $operation) {
176
      $operation_options = !empty($options[$operation_id]) ? $options[$operation_id] : array();
177

    
178
      $dom_id = 'edit-options-vbo-operations-' . str_replace(array('_', ':'), array('-', ''), $operation_id);
179
      $form['vbo_operations'][$operation_id]['selected'] = array(
180
        '#type' => 'checkbox',
181
        '#title' => $operation->adminLabel(),
182
        '#default_value' => !empty($operation_options['selected']),
183
      );
184
      if (!$operation->aggregate()) {
185
        $form['vbo_operations'][$operation_id]['postpone_processing'] = array(
186
          '#type' => 'checkbox',
187
          '#title' => t('Enqueue the operation instead of executing it directly'),
188
          '#default_value' => !empty($operation_options['postpone_processing']),
189
          '#dependency' => array(
190
            $dom_id . '-selected' => array(1),
191
          ),
192
        );
193
      }
194
      $form['vbo_operations'][$operation_id]['skip_confirmation'] = array(
195
        '#type' => 'checkbox',
196
        '#title' => t('Skip confirmation step'),
197
        '#default_value' => !empty($operation_options['skip_confirmation']),
198
        '#dependency' => array(
199
          $dom_id . '-selected' => array(1),
200
        ),
201
      );
202
      $form['vbo_operations'][$operation_id]['skip_permission_check'] = array(
203
        '#type' => 'checkbox',
204
        '#title' => t('Skip permission step'),
205
        '#default_value' => !empty($operation_options['skip_permission_check']),
206
        '#dependency' => array(
207
          $dom_id . '-selected' => array(1),
208
        ),
209
      );
210

    
211
      $form['vbo_operations'][$operation_id] += $operation->adminOptionsForm($dom_id, $this);
212
    }
213
  }
214

    
215
  function options_validate(&$form, &$form_state) {
216
    parent::options_validate($form, $form_state);
217

    
218
    $entity_type = $this->get_entity_type();
219
    foreach ($form_state['values']['options']['vbo_operations'] as $operation_id => &$options) {
220
      if (empty($options['selected'])) {
221
        continue;
222
      }
223

    
224
      $operation = views_bulk_operations_get_operation($operation_id, $entity_type, $options);
225
      $fake_form = $form['vbo_operations'][$operation_id];
226
      $fake_form_state = array('values' => &$options);
227
      $error_element_base = 'vbo_operations][' . $operation_id . '][';
228
      $operation->adminOptionsFormValidate($fake_form, $fake_form_state, $error_element_base);
229
    }
230
  }
231

    
232
  function options_submit(&$form, &$form_state) {
233
    parent::options_submit($form, $form_state);
234

    
235
    $entity_type = $this->get_entity_type();
236
    foreach ($form_state['values']['options']['vbo_operations'] as $operation_id => &$options) {
237
      if (empty($options['selected'])) {
238
        continue;
239
      }
240

    
241
      $operation = views_bulk_operations_get_operation($operation_id, $entity_type, $options);
242
      $fake_form = $form['vbo_operations'][$operation_id];
243
      $fake_form_state = array('values' => &$options);
244
      $operation->adminOptionsFormSubmit($fake_form, $fake_form_state);
245
    }
246
  }
247

    
248
  /**
249
   * Returns the value of a vbo option.
250
   */
251
  function get_vbo_option($key, $default = NULL) {
252
    return isset($this->options['vbo_settings'][$key]) ? $this->options['vbo_settings'][$key] : $default;
253
  }
254

    
255
  /**
256
   * If the view is using a table style, provide a
257
   * placeholder for a "select all" checkbox.
258
   */
259
  function label() {
260
    if (!empty($this->view->style_plugin) && $this->view->style_plugin instanceof views_plugin_style_table && !$this->options['vbo_settings']['force_single']) {
261
      return '<!--views-bulk-operations-select-all-->';
262
    }
263
    else {
264
      return parent::label();
265
    }
266
  }
267

    
268
  function render($values) {
269
    return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
270
  }
271

    
272
  /**
273
   * The form which replaces the placeholder from render().
274
   */
275
  function views_form(&$form, &$form_state) {
276
    // The view is empty, abort.
277
    if (empty($this->view->result)) {
278
      return;
279
    }
280

    
281
    $form[$this->options['id']] = array(
282
      '#tree' => TRUE,
283
    );
284
    // At this point, the query has already been run, so we can access the results
285
    // in order to get the base key value (for example, nid for nodes).
286
    foreach ($this->view->result as $row_index => $row) {
287
      $this->view->row_index = $row_index;
288
      $id = $this->get_value($row, $this->real_field);
289

    
290
      if ($this->options['vbo_settings']['force_single']) {
291
        $form[$this->options['id']][$row_index] = array(
292
          '#type' => 'radio',
293
          '#parents' => array($this->options['id']),
294
          '#return_value' => $id,
295
          '#attributes' => array('class' => array('vbo-select')),
296
        );
297
      }
298
      else {
299
        $form[$this->options['id']][$row_index] = array(
300
          '#type' => 'checkbox',
301
          '#return_value' => $id,
302
          '#default_value' => FALSE,
303
          '#attributes' => array('class' => array('vbo-select')),
304
        );
305
      }
306
    }
307
  }
308

    
309
  public function get_selected_operations() {
310
    global $user;
311
    $selected = drupal_static(__FUNCTION__);
312
    if (!isset($selected)) {
313
      $entity_type = $this->get_entity_type();
314
      $selected = array();
315
      foreach ($this->options['vbo_operations'] as $operation_id => $options) {
316
        if (empty($options['selected'])) {
317
          continue;
318
        }
319
        $operation = views_bulk_operations_get_operation($operation_id, $entity_type, $options);
320
        if (!$operation) {
321
          continue;
322
        }
323
        $skip_permission_check = $operation->getAdminOption('skip_permission_check', FALSE);
324
        if (!$operation->access($user) && !$skip_permission_check) {
325
          continue;
326
        }
327
        $selected[$operation_id] = $operation;
328
      }
329
    }
330

    
331
    return $selected;
332
  }
333

    
334
  /**
335
   * Returns the options stored for the provided operation id.
336
   */
337
  public function get_operation_options($operation_id) {
338
    $options = $this->options['vbo_operations'];
339
    return isset($options[$operation_id]) ? $options[$operation_id] : array();
340
  }
341

    
342
  /**
343
   * Determine the base table of the VBO field, and then use it to determine
344
   * the entity type that VBO is operating on.
345
   */
346
  public function get_entity_type() {
347
    return $this->entity_type;
348
  }
349

    
350
}