You are here

function views_bulk_operations_form in Views Bulk Operations (VBO) 6

Same name and namespace in other branches
  1. 5 views_bulk_operations.module \views_bulk_operations_form()
  2. 6.3 views_bulk_operations.module \views_bulk_operations_form()
  3. 7.3 views_bulk_operations.module \views_bulk_operations_form()

Form implementation for main VBO multistep form.

3 string references to 'views_bulk_operations_form'
views_bulk_operations_forms in ./views_bulk_operations.module
Implementation of hook_forms().
views_bulk_operations_form_alter in ./views_bulk_operations.module
Implementation of hook_form_alter().
views_bulk_operations_plugin_style::render in ./views_bulk_operations_plugin_style.inc
Implementation of views_plugin::render().

File

./views_bulk_operations.module, line 451
Allows operations to be performed on items selected in a view.

Code

function views_bulk_operations_form($form_state, $form_id, $plugin) {

  // Erase the form parameters from $_REQUEST for a clean pager.
  if (!empty($form_state['post'])) {
    $_REQUEST = array_diff($_REQUEST, $form_state['post']);
  }

  // Force browser to reload the page if Back is hit.
  if (!empty($_SERVER['HTTP_USER_AGENT']) && preg_match('/msie/i', $_SERVER['HTTP_USER_AGENT'])) {
    drupal_set_header("Cache-Control: no-cache");

    // works for IE6+
  }
  else {
    drupal_set_header("Cache-Control: no-store");

    // works for Firefox and other browsers
  }

  // Which step is this?
  if (empty($form_state['storage']['step'])) {

    // If empty view, render the empty text.
    if (empty($plugin->view->result)) {
      $form['empty'] = array(
        '#value' => $plugin->view->display_handler
          ->render_empty(),
      );
      return $form;
    }

    // If there's a session variable on this view, pre-load the old values.
    $view_id = _views_bulk_operations_view_id($plugin->view);
    $view_name = $plugin->view->name;
    if (isset($_SESSION['vbo_values'][$view_name][$view_id]) && $plugin->options['preserve_selection']) {

      // Avoid PHP warnings.
      $_SESSION['vbo_values'][$view_name][$view_id] += array(
        'selection' => array(),
        'selectall' => FALSE,
        'operation' => NULL,
      );
      $default_objects = array(
        'selection' => $_SESSION['vbo_values'][$view_name][$view_id]['selection'],
        'selectall' => $_SESSION['vbo_values'][$view_name][$view_id]['selectall'],
      );
      $default_operation = $_SESSION['vbo_values'][$view_name][$view_id]['operation'];
    }
    else {
      $default_objects = array(
        'selection' => array(),
        'selectall' => FALSE,
      );
      $default_operation = NULL;
    }
    if (count($plugin
      ->get_selected_operations()) == 1 && $plugin->options['merge_single_action']) {
      $step = VBO_STEP_SINGLE;
    }
    else {
      $step = VBO_STEP_VIEW;
    }
  }
  else {
    _views_bulk_operations_strip_view($plugin->view);
    switch ($form_state['storage']['step']) {
      case VBO_STEP_VIEW:
        $operation = $form_state['storage']['operation'];
        if ($operation['configurable']) {
          $step = VBO_STEP_CONFIG;
        }
        else {
          $step = VBO_STEP_CONFIRM;
        }
        break;
      case VBO_STEP_SINGLE:
      case VBO_STEP_CONFIG:
        $step = VBO_STEP_CONFIRM;
        break;
    }
  }
  $form['step'] = array(
    '#type' => 'value',
    '#value' => $step,
  );
  $form['#plugin'] = $plugin;
  switch ($step) {
    case VBO_STEP_VIEW:
      $form['select'] = array(
        '#type' => 'fieldset',
        '#title' => t('Bulk operations'),
        '#prefix' => '<div id="views-bulk-operations-select">',
        '#suffix' => '</div>',
      );
      $form['objects'] = array(
        '#type' => 'views_node_selector',
        '#view' => $plugin->view,
        '#sets' => $plugin->sets,
        '#default_value' => $default_objects,
        '#prefix' => '<div class="views-node-selector">',
        '#suffix' => '</div>',
      );
      if ($plugin->options['display_type'] == 0) {

        // Create dropdown and submit button.
        $form['select']['operation'] = array(
          '#type' => 'select',
          '#options' => array(
            0 => t('- Choose an operation -'),
          ) + $plugin
            ->get_selected_operations(),
          '#default_value' => $default_operation,
          '#prefix' => '<div id="views-bulk-operations-dropdown">',
          '#suffix' => '</div>',
        );
        $form['select']['submit'] = array(
          '#type' => 'submit',
          '#value' => t('Execute'),
          '#prefix' => '<div id="views-bulk-operations-submit">',
          '#suffix' => '</div>',
        );
      }
      else {

        // Create buttons for actions.
        foreach ($plugin
          ->get_selected_operations() as $md5 => $description) {
          $form['select'][$md5] = array(
            '#type' => 'submit',
            '#value' => $description,
            '#hash' => $md5,
          );
        }
      }
      break;
    case VBO_STEP_SINGLE:
      $operation_keys = array_keys($plugin
        ->get_selected_operations());
      $operation = $plugin
        ->get_operation_info($operation_keys[0]);
      $form['operation'] = array(
        '#type' => 'value',
        '#value' => $operation_keys[0],
      );
      if ($operation['configurable']) {
        $form += _views_bulk_operations_action_form($operation, $plugin->view, $plugin->result, $operation['options']['settings']);
      }
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => $operation['label'],
        '#prefix' => '<div id="views-bulk-operations-submit">',
        '#suffix' => '</div>',
      );
      $form['objects'] = array(
        '#type' => 'views_node_selector',
        '#view' => $plugin->view,
        '#sets' => $plugin->sets,
        '#default_value' => $default_objects,
        '#prefix' => '<div class="views-node-selector">',
        '#suffix' => '</div>',
      );
      break;
    case VBO_STEP_CONFIG:
      $operation = $form_state['storage']['operation'];
      $form += _views_bulk_operations_action_form($operation, $plugin->view, _views_bulk_operations_get_selection_full($plugin, $form_state), $operation['options']['settings']);
      $form['execute'] = array(
        '#type' => 'submit',
        '#value' => t('Next'),
        '#weight' => 98,
      );
      $query = drupal_query_string_encode($_GET, array(
        'q',
      ));
      $form['cancel'] = array(
        '#type' => 'markup',
        '#value' => l('Cancel', $_GET['q'], array(
          'query' => $query,
        )),
        '#weight' => 99,
      );
      drupal_set_title(t('Set parameters for %operation', array(
        '%operation' => $operation['label'],
      )));
      break;
    case VBO_STEP_CONFIRM:
      $operation = $form_state['storage']['operation'];
      $query = drupal_query_string_encode($_GET, array(
        'q',
      ));
      $form = confirm_form($form, t('Are you sure you want to perform %operation on the selected items?', array(
        '%operation' => $operation['label'],
      )), array(
        'path' => $_GET['q'],
        'query' => $query,
      ), theme('views_bulk_operations_confirmation', $form_state['storage']['selection'], $form_state['storage']['selectall'], $plugin->view));
      break;
  }

  // Use views_bulk_operations_form_submit() for form submit, regardless of form_id.
  $form['#submit'][] = 'views_bulk_operations_form_submit';
  $form['#validate'][] = 'views_bulk_operations_form_validate';
  $form['#attributes']['class'] = 'views-bulk-operations-form views-bulk-operations-form-step-' . $step;

  // A view with ajax enabled, and a space in the url, has to be decoded to work fine,
  // @see http://drupal.org/node/1325632
  $form['#action'] = urldecode(request_uri());
  return $form;
}