You are here

function views_bulk_operations_form in Views Bulk Operations (VBO) 6.3

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

Define multistep form for selecting and executing an operation.

2 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().

File

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

Code

function views_bulk_operations_form($form_state, $plugin) {

  // Force browser to reload the page if Back is hit.
  if (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
  }

  // If there's a session variable on this view, pre-load the old values.
  if (isset($_SESSION['vbo_values'][$_GET['q']])) {
    $default_objects = array(
      'selection' => array_filter($_SESSION['vbo_values'][$_GET['q']]['objects']['selection']),
      'select_all' => $_SESSION['vbo_values'][$_GET['q']]['objects']['select_all'],
    );
    $default_operation = $_SESSION['vbo_values'][$_GET['q']]['operation'];
  }
  else {
    $default_objects = array(
      'selection' => NULL,
      'select_all' => FALSE,
    );
    $default_operation = NULL;
  }
  if (!isset($form_state['storage']['step'])) {
    if (count($plugin
      ->get_selected_operations()) == 1 && $plugin->options['merge_single_action']) {
      $step = VIEWS_BULK_OPS_STEP_SINGLE;
    }
    else {
      $step = VIEWS_BULK_OPS_STEP_VIEW;
    }
    $form['exposed_input'] = array(
      '#type' => 'value',
      '#value' => $plugin->view
        ->get_exposed_input(),
    );
    $form['arguments'] = array(
      '#type' => 'value',
      '#value' => $plugin->view->args,
    );

    // If empty view, render the empty text.
    if (!$plugin->view->result) {
      $form['empty'] = array(
        '#value' => $plugin->view->display_handler
          ->render_empty(),
      );
      return $form;
    }
  }
  else {
    $plugin
      ->strip_view();
    switch ($form_state['storage']['step']) {
      case VIEWS_BULK_OPS_STEP_VIEW:
        $operation = $plugin
          ->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['operation']);
        if ($operation['configurable']) {
          $step = VIEWS_BULK_OPS_STEP_CONFIG;
        }
        else {
          $step = VIEWS_BULK_OPS_STEP_CONFIRM;
        }
        break;
      case VIEWS_BULK_OPS_STEP_SINGLE:
      case VIEWS_BULK_OPS_STEP_CONFIG:
        $step = VIEWS_BULK_OPS_STEP_CONFIRM;
        break;
    }
  }
  $form['step'] = array(
    '#type' => 'value',
    '#value' => $step,
  );
  $form['#plugin'] = $plugin;
  switch ($step) {
    case VIEWS_BULK_OPS_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,
        '#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 VIEWS_BULK_OPS_STEP_SINGLE:
      $ops = array_keys($plugin
        ->get_selected_operations());
      $operation = $plugin
        ->get_operation_info($ops[0]);
      $form['operation'] = array(
        '#type' => 'value',
        '#value' => $ops[0],
      );
      if ($operation['configurable']) {
        $form += _views_bulk_operations_action_form($operation, $plugin->view, $plugin->view->result, $plugin
          ->get_operation_settings($operation));
      }
      $form['objects'] = array(
        '#type' => 'views_node_selector',
        '#view' => $plugin->view,
        '#sets' => $plugin->sets,
        '#value' => $default_objects,
        '#prefix' => '<div class="views-node-selector">',
        '#suffix' => '</div>',
      );
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => $operation['label'],
        '#prefix' => '<div id="views-bulk-operations-submit">',
        '#suffix' => '</div>',
      );
      break;
    case VIEWS_BULK_OPS_STEP_CONFIG:
      $operation = $plugin
        ->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['operation']);
      $form['operation'] = array(
        '#type' => 'value',
        '#value' => $operation,
      );
      $form += _views_bulk_operations_action_form($operation, $plugin->view, array_filter($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['objects']['selection']), $plugin
        ->get_operation_settings($operation));
      $form['execute'] = array(
        '#type' => 'submit',
        '#value' => t('Next'),
        '#weight' => 98,
      );
      $query = drupal_query_string_encode($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['exposed_input']);
      $form['cancel'] = array(
        '#type' => 'markup',
        '#value' => t('<a href="@view">Cancel</a>', array(
          '@view' => url($_GET['q'], array(
            'query' => $query,
          )),
        )),
        '#weight' => 99,
      );
      drupal_set_title(t('Set parameters for %action', array(
        '%action' => $operation['label'],
      )));
      break;
    case VIEWS_BULK_OPS_STEP_CONFIRM:
      $operation = $plugin
        ->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['operation']);
      $query = drupal_query_string_encode($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['exposed_input']);
      $objects = $form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['objects']['selection'];
      $form = confirm_form($form, t('Are you sure you want to perform %action on selected rows?', array(
        '%action' => $operation['label'],
      )), array(
        'path' => $_GET['q'],
        'query' => $query,
      ), theme('views_bulk_operations_confirmation', $objects, $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['#cache'] = TRUE;
  return $form;
}