source: sipes/modules_contrib/views_bulk_operations/views_bulk_operations_plugin_style.inc @ c43ea01

stableversion-3.0
Last change on this file since c43ea01 was 177a560, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agrego el directorio de modulos contribuidos de drupal

  • Propiedad mode establecida a 100755
File size: 15.9 KB
Línea 
1<?php
2
3class views_bulk_operations_plugin_style extends views_plugin_style_table {
4
5  var $all_operations = array();
6
7  /**
8   * Implementation of views_plugin::init().
9   */
10  function init(&$view, &$display, $options = NULL) {
11    parent::init($view, $display, $options);
12
13    $this->populate_operations();
14  }
15
16  /**
17   * Implementation of views_plugin::option_definition().
18   */
19  function option_definition() {
20    $options = parent::option_definition();
21
22    $options['operations'] = array('default' => array());
23    $options['execution_type'] = array('default' => VBO_EXECUTION_DIRECT);
24    $options['max_performance'] = array('default' => 0);
25    $options['display_type'] = array('default' => 0);
26    $options['display_result'] = array('default' => TRUE);
27    $options['merge_single_action'] = array('default' => TRUE);
28    $options['hide_selector'] = array('default' => FALSE);
29    $options['preserve_selection'] = array('default' => TRUE);
30
31    return $options;
32  }
33
34  /**
35   * Implementation of views_plugin::options_form().
36   */
37  function options_form(&$form, &$form_state) {
38    parent::options_form($form, $form_state);
39
40    $execution = array(
41      VBO_EXECUTION_DIRECT => t('Invoke them directly'),
42      VBO_EXECUTION_BATCH => t('Use Batch API'),
43    );
44    if (module_exists('drupal_queue')) {
45      $execution[VBO_EXECUTION_QUEUE] = t('Use <a href="@drupalqueue">Drupal Queue</a>', array('@drupalqueue' => url('http://drupal.org/project/drupal_queue')));
46    }
47    $form['execution_type'] = array(
48      '#type' => 'radios',
49      '#title' => t('To execute operations'),
50      '#default_value' => $this->options['execution_type'],
51      '#options' => $execution,
52    );
53    $form['max_performance'] = array(
54      '#type' => 'checkbox',
55      '#title' => t('Maximize Batch API performance'),
56      '#description' => t('If checked, each batch process will execute as many rows as possible within the server resource limits.'),
57      '#default_value' => $this->options['max_performance'],
58    );
59    $form['display_type'] = array(
60      '#type' => 'radios',
61      '#title' => t('Display operations as'),
62      '#default_value' => $this->options['display_type'],
63      '#options' => array(
64        t('Dropdown selectbox with Submit button'),
65        t('Each action as a separate button'),
66      ),
67    );
68    $form['hide_selector'] = array(
69      '#type' => 'checkbox',
70      '#title' => t('Hide selector dropdown'),
71      '#description' => t('Check this box to hide the selector dropdown.'),
72      '#default_value' => $this->options['hide_selector'],
73    );
74    $form['preserve_selection'] = array(
75      '#type' => 'checkbox',
76      '#title' => t('Preserve selection across pages'),
77      '#description' => t('Check this box to preserve item selection across multiple pages. Requires JavaScript.'),
78      '#default_value' => $this->options['preserve_selection'],
79    );
80    $form['display_result'] = array(
81      '#type' => 'checkbox',
82      '#title' => t('Display processing result'),
83      '#description' => t('Check this box to let Drupal display a message with the result of processing the selected objects.'),
84      '#default_value' => $this->options['display_result'],
85    );
86    $form['merge_single_action'] = array(
87      '#type' => 'checkbox',
88      '#title' => t('Merge single action\'s form with item selection view'),
89      '#description' => t('In case only one action is selected *and* this action is configurable, display its action form along with the item selection view.'),
90      '#default_value' => $this->options['merge_single_action'],
91    );
92
93    // Display operations and their settings.
94    $form['operations'] = array(
95      '#tree' => TRUE,
96      '#type' => 'fieldset',
97      '#title' => t('Selected operations'),
98      '#collapsible' => TRUE,
99      '#collapsed' => FALSE,
100    );
101    foreach ($this->get_operations_options() as $key => $label) {
102      $dom_id = 'edit-style-options-operations-' . str_replace('_', '-', $key) . '-selected';
103      $form['operations'][$key]['selected'] = array(
104        '#type' => 'checkbox',
105        '#title' => $label,
106        '#default_value' => @$this->options['operations'][$key]['selected'],
107      );
108      $form['operations'][$key]['skip_confirmation'] = array(
109        '#type' => 'checkbox',
110        '#title' => t('Skip confirmation step'),
111        '#default_value' => @$this->options['operations'][$key]['skip_confirmation'],
112        '#process' => array('views_process_dependency'),
113        '#dependency' => array(
114          $dom_id => array(1),
115        ),
116      );
117      $form['operations'][$key]['label'] = array(
118        '#type' => 'textfield',
119        '#title' => t('Override label'),
120        '#default_value' => @$this->options['operations'][$key]['label'],
121        '#process' => array('views_process_dependency'),
122        '#dependency' => array(
123          $dom_id => array(1),
124        ),
125      );
126      $form_function = $this->all_operations[$key]['callback'] . '_views_bulk_operations_form';
127      if (function_exists($form_function)) {
128        $form_settings = call_user_func($form_function, @$this->options['operations'][$key]['settings']);
129        foreach (element_children($form_settings) as $child) {
130          // The views dependency code requires special handling for checkboxes.
131          if (isset($form_settings[$child]['#type']) && $form_settings[$child]['#type'] == 'checkboxes') {
132            $child_wrapper_id = 'edit-style-options-operations-' . str_replace('_', '-', $key) . '-settings-' . str_replace('_', '-', $child) . '-wrapper';
133            $form_settings[$child] += array(
134              '#prefix' => '<div id="' . $child_wrapper_id . '"><div>',
135              '#suffix' => '</div></div>',
136              '#process' => array('expand_checkboxes', 'views_process_dependency'),
137              '#dependency' => array(
138                $dom_id => array(1),
139              ),
140            );
141          }
142          else {
143            $form_settings[$child] += array(
144              '#process' => array('views_process_dependency'),
145              '#dependency' => array(
146                $dom_id => array(1),
147              ),
148            );
149          }
150        }
151        $form['operations'][$key]['settings'] = $form_settings;
152      }
153    }
154  }
155
156  /**
157   * Implementation of views_plugin::options_validate().
158   */
159  function options_validate(&$form, &$form_state) {
160    foreach ($form_state['values']['style_options']['operations'] as $key => &$options) {
161      if (empty($options['selected'])) continue;
162      if (!isset($options['settings'])) continue;
163      $operation = $this->all_operations[$key];
164      $form_function = $operation['callback'] . '_views_bulk_operations_form_validate';
165      if (function_exists($form_function)) {
166        $options['settings']['_error_element_base'] = 'style_options][operations][' . $key . '][settings][';
167        call_user_func($form_function, $form, array('values' => $options['settings']));
168      }
169    }
170  }
171
172  /**
173   * Implementation of views_plugin::options_submit().
174   */
175  function options_submit(&$form, &$form_state) {
176    foreach ($form_state['values']['style_options']['operations'] as $key => $options) {
177      if (empty($options['selected'])) continue;
178      if (!isset($options['settings'])) continue;
179      $operation = $this->all_operations[$key];
180      $form_function = $operation['callback'] . '_views_bulk_operations_form_submit';
181      if (function_exists($form_function)) {
182        call_user_func($form_function, $form, array('values' => $options['settings']));
183      }
184    }
185
186    // Reset runtime settings for this view.
187    unset($_SESSION['vbo_values'][$this->view->name]);
188  }
189
190  /**
191   * Implementation of views_plugin::query().
192   */
193  function query() {
194    // Add base field if not present in the query.
195    if (!isset($this->view->query->fields[$this->view->base_field])) {
196      $this->view->query->add_field($this->view->base_table, $this->view->base_field);
197    }
198  }
199
200  /**
201   * Implementation of views_plugin::render().
202   */
203  function render() {
204    // Client code might flag to skip rendering if we just want to build complete results.
205    if (!empty($this->view->skip_render)) {
206      return;
207    }
208
209    // We build the groups here to pass them to the node_selector function through the form.
210    $this->sets = $this->render_grouping($this->view->result, $this->options['grouping']);
211
212    // Create a form ID for the form using the name and display of the view.
213    $parts = array(
214      'views_bulk_operations_form',
215      $this->view->name,
216      $this->view->current_display,
217    );
218    $this->form_id = implode('_', $parts);
219
220    // Populate hashed results for selection.
221    $this->result = array();
222    foreach ($this->view->result as $row) {
223      $this->result[_views_bulk_operations_hash_object($row, $this->view)] = $row;
224    }
225    if ($this->options['preserve_selection']) {
226      $view_id = _views_bulk_operations_view_id($this->view);
227      $view_name = $this->view->name;
228      if (empty($_SESSION['vbo_values'][$view_name][$view_id]['result'])) {
229        $_SESSION['vbo_values'][$view_name][$view_id]['result'] = array();
230      }
231      $_SESSION['vbo_values'][$view_name][$view_id]['result'] += $this->result;
232    }
233
234    // Copy the plugin globally because we might need it during form validation.
235    global $vbo_plugins;
236    $vbo_plugins[$this->form_id] = $this;
237
238    // Rendering the plugin as a form.
239    return drupal_get_form($this->form_id, $this);
240  }
241
242  /**
243   * API function to get list of selected operations.
244   */
245  function get_selected_operations() {
246    $selected = array();
247    foreach ($this->options['operations'] as $key => $options) {
248      if (empty($options['selected'])) continue;
249      if (empty($this->all_operations[$key])) continue;
250      if (module_exists('actions_permissions')) {
251        $perm = actions_permissions_get_perm($this->all_operations[$key]['label'], $this->all_operations[$key]['callback']);
252        if (!user_access($perm)) continue;
253      }
254      if (!empty($this->all_operations[$key]['permissions'])) foreach ($this->all_operations[$key]['permissions'] as $perm) {
255        if (!user_access($perm)) continue 2;
256      }
257      $selected[$key] = !empty($this->options['operations'][$key]['label']) ? t($this->options['operations'][$key]['label']) : $this->all_operations[$key]['label'];
258    }
259    return $selected;
260  }
261
262  /**
263   * API function to get operation information.
264   */
265  function get_operation_info($key) {
266    if (empty($this->all_operations[$key])) {
267      return NULL;
268    }
269    $operation = $this->all_operations[$key];
270    $operation['perm label'] = $operation['label'];
271    if (!empty($this->options['operations'][$key]['label'])) {
272      $operation['label'] = t($this->options['operations'][$key]['label']);
273    }
274    $operation['options'] = $this->options['operations'][$key] + array('settings' => NULL);
275    return $operation;
276  }
277
278  function get_operations_options() {
279    static $options = array();
280    if (empty($options)) {
281      $object_info = _views_bulk_operations_object_info_for_view($this->view);
282      if (!$object_info) return $options;
283      foreach ($this->all_operations as $key => $operation) {
284        if ($operation['type'] == $object_info['type'] || $operation['type'] == 'system' || in_array($object_info['hook'], (array) $operation['hooks'])) {
285          $options[$key] = $operation['label'] .' ('. $key .')';
286        }
287      }
288    }
289    return $options;
290  }
291
292  function populate_operations() {
293    module_load_include('inc', 'node', 'node.admin');
294
295    $operations = array();
296    foreach (_views_bulk_operations_get_object_info() as $object_type => $object_info) {
297      $hook_name = $object_type .'_operations';
298      foreach (module_invoke_all($hook_name) as $operation) {
299        if (empty($operation['callback'])) continue;
300        $key = $operation['callback'] . (empty($operation['callback arguments']) ? '' : '-'. md5(serialize($operation['callback arguments'])));
301        if (!isset($operation['behavior'])) { // assume operations modify nodes by default
302          $operation['behavior'] = array('changes_node_property');
303        }
304        $operations[$key] = array(
305          'key'                 => $key,
306          'label'               => $operation['label'],
307          'callback'            => $operation['callback'],
308          'callback arguments'  => isset($operation['callback arguments']) ? $operation['callback arguments'] : array(),
309          'configurable'        => isset($operation['configurable']) ? $operation['configurable'] : FALSE,
310          'form properties'     => isset($operation['form properties']) ? $operation['form properties'] : array(),
311          'source'              => 'operation',
312          'type'                => $object_type,
313          'aggregate'           => isset($operation['aggregate']) ? (int)$operation['aggregate'] : VBO_AGGREGATE_OPTIONAL,
314          'access op'           => $this->get_access_op($operation),
315          'permissions'         => isset($operation['permissions']) ? $operation['permissions'] : NULL,
316          'hooks'               => array(),
317        );
318      }
319    }
320
321    $action_operations = actions_list() + $this->get_custom_actions();
322    foreach ($action_operations as $callback => $operation) {
323      $key = isset($operation['key']) ? $operation['key'] : $callback;
324      $operations[$key] = array(
325        'key'                   => $key,
326        'label'                 => $operation['description'],
327        'callback'              => isset($operation['callback']) ? $operation['callback'] : $callback,
328        'callback arguments'    => isset($operation['parameters']) ? $operation['parameters'] : array(),
329        'configurable'          => isset($operation['configurable']) ? $operation['configurable'] : FALSE,
330        'form properties'       => isset($operation['form properties']) ? $operation['form properties'] : array(),
331        'source'                => 'action',
332        'type'                  => $operation['type'],
333        'aggregate'             => isset($operation['aggregate']) ? (int)$operation['aggregate'] : VBO_AGGREGATE_FORBIDDEN,
334        'access op'             => $this->get_access_op($operation),
335        'permissions'           => isset($operation['permissions']) ? $operation['permissions'] : NULL,
336        'hooks'                 => isset($operation['hooks']) ? array_keys((array) $operation['hooks']) : array(),
337      );
338    }
339
340    uasort($operations, create_function('$a, $b', 'return strcasecmp($a["label"], $b["label"]);'));
341    $this->all_operations = $operations;
342  }
343
344  function get_access_op($operation) {
345    $access_op = 0;
346    if (isset($operation['behavior'])) {
347      if (in_array('views_node_property', $operation['behavior'])) {
348        $access_op |= VBO_ACCESS_OP_VIEW;
349      }
350      if (in_array('changes_node_property', $operation['behavior'])) {
351        $access_op |= VBO_ACCESS_OP_UPDATE;
352      }
353      if (in_array('creates_node_property', $operation['behavior'])) {
354        $access_op |= VBO_ACCESS_OP_CREATE;
355      }
356      if (in_array('deletes_node_property', $operation['behavior'])) {
357        $access_op |= VBO_ACCESS_OP_DELETE;
358      }
359    }
360    return $access_op;
361  }
362
363  function get_custom_actions() {
364    $actions = array();
365    $static_actions = actions_list();
366    $result = db_query("SELECT * FROM {actions} WHERE parameters > ''");
367    while ($action = db_fetch_object($result)) {
368      $parameters = unserialize($action->parameters);
369      $actions[$action->aid] = array(
370        'description' => $action->description,
371        'type' => $action->type,
372        'configurable' => FALSE,
373        'parameters' => $parameters,
374        'key' => $action->callback . (empty($parameters) ? '' : '-'. md5($action->parameters)),
375      );
376      foreach (array('callback', 'behavior', 'aggregate', 'permissions', 'hooks', 'form properties') as $attribute) {
377        if (isset($static_actions[$action->callback][$attribute])) $actions[$action->aid][$attribute] = $static_actions[$action->callback][$attribute];
378      }
379      if (isset($static_actions[$action->callback]['parameters'])) {
380        $actions[$action->aid]['parameters'] = array_merge($actions[$action->aid]['parameters'], $static_actions[$action->callback]['parameters']);
381      }
382    }
383    return $actions;
384  }
385}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.