Projet

Général

Profil

Paste
Télécharger (12,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_bulk_operations / views / views_bulk_operations_handler_field_operations.inc @ 7547bb19

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
      ),
69
    );
70
    $options['vbo_operations'] = array(
71
      'default' => array(),
72
      'unpack_translatable' => 'unpack_operations',
73
      'export' => 'export_vbo_operations',
74
    );
75

    
76
    return $options;
77
  }
78

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

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

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

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

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

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

    
157
    // Display operations and their settings.
158
    $form['vbo_operations'] = array(
159
      '#tree' => TRUE,
160
      '#type' => 'fieldset',
161
      '#title' => t('Selected bulk operations'),
162
      '#collapsible' => TRUE,
163
      '#collapsed' => FALSE,
164
    );
165

    
166
    $entity_type = $this->get_entity_type();
167
    $options = $this->options['vbo_operations'];
168
    foreach (views_bulk_operations_get_applicable_operations($entity_type, $options) as $operation_id => $operation) {
169
      $operation_options = !empty($options[$operation_id]) ? $options[$operation_id] : array();
170

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

    
204
      $form['vbo_operations'][$operation_id] += $operation->adminOptionsForm($dom_id, $this);
205
    }
206
  }
207

    
208
  function options_validate(&$form, &$form_state) {
209
    parent::options_validate($form, $form_state);
210

    
211
    $entity_type = $this->get_entity_type();
212
    foreach ($form_state['values']['options']['vbo_operations'] as $operation_id => &$options) {
213
      if (empty($options['selected'])) {
214
        continue;
215
      }
216

    
217
      $operation = views_bulk_operations_get_operation($operation_id, $entity_type, $options);
218
      $fake_form = $form['vbo_operations'][$operation_id];
219
      $fake_form_state = array('values' => &$options);
220
      $error_element_base = 'vbo_operations][' . $operation_id . '][';
221
      $operation->adminOptionsFormValidate($fake_form, $fake_form_state, $error_element_base);
222
    }
223
  }
224

    
225
  function options_submit(&$form, &$form_state) {
226
    parent::options_submit($form, $form_state);
227

    
228
    $entity_type = $this->get_entity_type();
229
    foreach ($form_state['values']['options']['vbo_operations'] as $operation_id => &$options) {
230
      if (empty($options['selected'])) {
231
        continue;
232
      }
233

    
234
      $operation = views_bulk_operations_get_operation($operation_id, $entity_type, $options);
235
      $fake_form = $form['vbo_operations'][$operation_id];
236
      $fake_form_state = array('values' => &$options);
237
      $operation->adminOptionsFormSubmit($fake_form, $fake_form_state);
238
    }
239
  }
240

    
241
  /**
242
   * Returns the value of a vbo option.
243
   */
244
  function get_vbo_option($key, $default = NULL) {
245
    return isset($this->options['vbo_settings'][$key]) ? $this->options['vbo_settings'][$key] : $default;
246
  }
247

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

    
261
  function render($values) {
262
    return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
263
  }
264

    
265
  /**
266
   * The form which replaces the placeholder from render().
267
   */
268
  function views_form(&$form, &$form_state) {
269
    // The view is empty, abort.
270
    if (empty($this->view->result)) {
271
      return;
272
    }
273

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

    
283
      if ($this->options['vbo_settings']['force_single']) {
284
        $form[$this->options['id']][$row_index] = array(
285
          '#type' => 'radio',
286
          '#parents' => array($this->options['id']),
287
          '#return_value' => $id,
288
        );
289
      }
290
      else {
291
        $form[$this->options['id']][$row_index] = array(
292
          '#type' => 'checkbox',
293
          '#return_value' => $id,
294
          '#default_value' => FALSE,
295
          '#attributes' => array('class' => array('vbo-select')),
296
        );
297
      }
298
    }
299
  }
300

    
301
  public function get_selected_operations() {
302
    global $user;
303
    $selected = drupal_static(__FUNCTION__);
304
    if (!isset($selected)) {
305
      $entity_type = $this->get_entity_type();
306
      $selected = array();
307
      foreach ($this->options['vbo_operations'] as $operation_id => $options) {
308
        if (empty($options['selected'])) {
309
          continue;
310
        }
311
        $operation = views_bulk_operations_get_operation($operation_id, $entity_type, $options);
312
        if (!$operation) {
313
          continue;
314
        }
315
        $skip_permission_check = $operation->getAdminOption('skip_permission_check', FALSE);
316
        if (!$operation->access($user) && !$skip_permission_check) {
317
          continue;
318
        }
319
        $selected[$operation_id] = $operation;
320
      }
321
    }
322

    
323
    return $selected;
324
  }
325

    
326
  /**
327
   * Returns the options stored for the provided operation id.
328
   */
329
  public function get_operation_options($operation_id) {
330
    $options = $this->options['vbo_operations'];
331
    return isset($options[$operation_id]) ? $options[$operation_id] : array();
332
  }
333

    
334
  /**
335
   * Determine the base table of the VBO field, and then use it to determine
336
   * the entity type that VBO is operating on.
337
   */
338
  public function get_entity_type() {
339
    return $this->entity_type;
340
  }
341

    
342
}