You are here

function views_bulk_operations_form in Views Bulk Operations (VBO) 5

Same name and namespace in other branches
  1. 6.3 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()
2 string references to 'views_bulk_operations_form'
views_bulk_operations_forms in ./views_bulk_operations.module
hook_forms() implementation force each instance of function to use the same callback
views_bulk_operations_form_alter in ./views_bulk_operations.module

File

./views_bulk_operations.module, line 99
Allow bulk node operations directly within views.

Code

function views_bulk_operations_form($view, $nodes, $type, $form_values = NULL) {
  drupal_add_css(drupal_get_path('module', 'views_bulk_operations') . '/views_bulk_operations.css', 'module');
  $enabled_operations = _views_bulk_operations_get_operations('enabled', isset($view->vid) ? $view->vid : $view->name);
  if (empty($enabled_operations)) {

    // No ops found: bail out
    $error_message = t('No operations are enabled. You must enable one or more
      operations in order to use Views Bulk Operations.');
    drupal_set_message($error_message, 'error');
    drupal_goto($view->url . '/operations');
  }
  elseif (count($enabled_operations) == 1) {

    // One op found: special case
    $form['#multistep'] = FALSE;
    $form['#redirect'] = FALSE;
    $step = VIEWS_BULK_OPS_STEP_SINGLE;
  }
  else {

    // Many ops found: calculate the next step
    $form['#multistep'] = TRUE;
    $step = isset($form_values) ? (int) $form_values['step'] + 1 : VIEWS_BULK_OPS_STEP_VIEW;
    if ($step == VIEWS_BULK_OPS_STEP_CONFIG) {
      $action = $form_values['action'];
      if (!isset($enabled_operations[$action]) || !$enabled_operations[$action]['configurable']) {
        $step = VIEWS_BULK_OPS_STEP_CONFIRM;
      }
    }
  }

  // Get the operations and based on them many things are decided.
  $form['step'] = array(
    '#type' => 'hidden',
    '#value' => $step,
  );
  $form['vid'] = array(
    '#type' => 'hidden',
    '#value' => isset($view->vid) ? $view->vid : $view->name,
  );
  switch ($step) {
    case VIEWS_BULK_OPS_STEP_SINGLE:

      // Show the full thing for a single action
      $keys = array_keys($enabled_operations);
      $action = $keys[0];
      $form['action'] = array(
        '#type' => 'hidden',
        '#value' => $action,
      );
      if ($enabled_operations[$action]['configurable']) {
        $form_action = _views_bulk_operations_action_form($enabled_operations[$action]);
        $form['arguments'] = array(
          '#type' => 'fieldset',
          '#title' => t($enabled_operations[$action]['label']),
          '#collapsible' => FALSE,
        );
        $form['arguments'] = array_merge($form['arguments'], $form_action);
      }
      $form['nodes'] = array(
        '#title' => '',
        '#type' => 'views_node_selector',
        '#view' => $view,
        '#view_nodes' => $nodes,
        '#multiple' => TRUE,
        '#prefix' => '<div class="views-node-selector">',
        '#suffix' => '</div>',
      );
      $form['execute'] = array(
        '#type' => 'submit',
        '#value' => t($enabled_operations[$action]['label']),
      );
      break;
    case VIEWS_BULK_OPS_STEP_VIEW:

      // Show the view
      $options[0] = t('- Choose an operation -');
      foreach ($enabled_operations as $operation => $values) {
        $options[$operation] = t($values['label']);
      }
      $form['select'] = array(
        '#type' => 'fieldset',
        '#title' => t('Bulk operations'),
        '#prefix' => '<div id="views-bulk-operations-select">',
        '#suffix' => '</div>',
      );
      $form['select']['action'] = array(
        '#type' => 'select',
        '#title' => '',
        '#options' => $options,
        '#prefix' => '<div id="views-bulk-operations-dropdown">',
        '#suffix' => '</div>',
        '#default_value' => count($enabled_operations) == 1 ? $operation : 0,
      );
      $form['select']['execute'] = array(
        '#type' => 'submit',
        '#value' => t('Execute'),
        '#prefix' => '<div id="views-bulk-operations-submit">',
        '#suffix' => '</div>',
      );
      $form['nodes'] = array(
        '#title' => '',
        '#type' => 'views_node_selector',
        '#view' => $view,
        '#view_nodes' => $nodes,
        '#multiple' => TRUE,
        '#prefix' => '<div class="views-node-selector">',
        '#suffix' => '</div>',
      );
      $form['#redirect'] = FALSE;
      break;
    case VIEWS_BULK_OPS_STEP_CONFIG:

      // Create the action form to allow configuring parameters
      $view->use_pager = FALSE;
      $action = $form_values['action'];
      $form['action'] = array(
        '#type' => 'hidden',
        '#value' => $action,
      );
      $form['nodes'] = array(
        '#type' => 'value',
        '#value' => serialize($form_values['nodes']),
      );
      $form_action = _views_bulk_operations_action_form($enabled_operations[$action]);
      $form = array_merge($form, $form_action);

      // 'action_elements' is where we store the key values of the configurable
      // action form elements so that we can extract just those form values
      // later and serialize them in $preserved_action_form_elements for hiding
      // in the confirmation page
      $form['action_elements'] = array(
        '#type' => 'hidden',
        '#value' => serialize(array_keys($form_action)),
      );
      $form['execute'] = array(
        '#type' => 'submit',
        '#value' => t('Next'),
      );
      $form['#redirect'] = FALSE;
      drupal_set_title(t('Set parameters for \'%action\'', array(
        '%action' => $enabled_operations[$action]['label'],
      )));
      break;
    case VIEWS_BULK_OPS_STEP_CONFIRM:

      // Display the final confirmation
      $view->use_pager = FALSE;
      $action = $form_values['action'];
      $configurable = isset($enabled_operations[$action]) && $enabled_operations[$action]['configurable'];
      $form['action'] = array(
        '#type' => 'hidden',
        '#value' => $action,
      );
      if ($configurable) {
        $preserved_action_form_elements = array();

        // If this action is configurable, we came from a VIEWS_BULK_OPS_STEP_CONFIG.
        // $form_values['nodes'] is already serialized.
        $form['nodes'] = array(
          '#type' => 'value',
          '#value' => $form_values['nodes'],
        );

        // Gather the form values for the action form elements and preserve them
        // in the confirmation page form as a serialized value in a hidden
        // element to be picked up by the submit function.
        $action_elements = unserialize($form_values['action_elements']);
        foreach ($action_elements as $key) {
          if (isset($form_values[$key])) {
            $preserved_action_form_elements[$key] = $form_values[$key];
          }
        }
        $form['preserved_action_form_elements'] = array(
          '#type' => 'hidden',
          '#value' => serialize($preserved_action_form_elements),
        );
        $selected = unserialize($form_values['nodes']);

        // We'll use that in the confirm_form
      }
      else {

        // If this action is not configurable, we skipped VIEWS_BULK_OPS_STEP_CONFIG.
        // $form_values['nodes'] still needs to be serialized.
        $form['nodes'] = array(
          '#type' => 'value',
          '#value' => serialize($form_values['nodes']),
        );
        $selected = $form_values['nodes'];

        // We'll use that in the confirm_form
      }
      if ($selected['select_all']) {

        // User selected all nodes?
        $items = views_build_view('items', $view);
        $selected = array();
        foreach ($items['items'] as $node) {
          $selected[$node->nid] = $node->nid;
        }
      }
      else {
        unset($selected['select_all']);
      }
      $query = drupal_query_string_encode($_GET, array(
        'q',
      ));
      $form = confirm_form($form, t('Are you sure you want to perform \'%action\' on selected nodes?', array(
        '%action' => $enabled_operations[$action]['label'],
      )), $query ? array(
        'path' => $_GET['q'],
        'query' => $query,
      ) : array(
        'path' => $_GET['q'],
      ), theme('views_bulk_operations_confirmation', $selected));
      break;
  }

  // Use views_bulk_operations_form_submit() for form submit, regardless of form_id
  $form['#submit'] = array(
    'views_bulk_operations_form_submit' => 1,
  );
  $form['#validate'] = array(
    'views_bulk_operations_form_validate' => array(),
  );
  return $form;
}