You are here

function views_bulk_operations_adjust_selection in Views Bulk Operations (VBO) 7.3

Batch API callback: loads the view page by page and enqueues all items.

Parameters

$queue_name: The name of the queue to which the items should be added.

$operation: The operation object.

$options: An array of options that affect execution (revision, entity_load_capacity, view_info). Passed along with each new queue item.

1 string reference to 'views_bulk_operations_adjust_selection'
views_bulk_operations_execute in ./views_bulk_operations.module
Entry point for executing the chosen operation upon selected rows.

File

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

Code

function views_bulk_operations_adjust_selection($queue_name, $operation, $options, &$context) {
  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = 0;
  }
  $view_info = $options['view_info'];
  if (isset($view_info['view'])) {
    $view = $view_info['view'];

    // Because of the offset, we want our view to be re-build and re-executed.
    $view->built = FALSE;
    $view->executed = FALSE;
  }
  else {
    $view = views_get_view($view_info['name']);
    $view
      ->set_exposed_input($view_info['exposed_input']);
    $view
      ->set_arguments($view_info['arguments']);
    $view
      ->set_display($view_info['display']);
  }
  $view
    ->set_offset($context['sandbox']['progress']);
  $view
    ->build();
  $view
    ->execute($view_info['display']);

  // Note the total number of rows.
  if (empty($context['sandbox']['max'])) {
    $context['sandbox']['max'] = $view->total_rows;
  }
  $vbo = _views_bulk_operations_get_field($view);

  // Call views_handler_field_entity::pre_render() to get the entities.
  $vbo
    ->pre_render($view->result);
  $rows = array();
  foreach ($view->result as $row_index => $result) {

    // Set the row index.
    $view->row_index = $row_index;
    $rows[$row_index] = array(
      'entity_id' => $vbo
        ->get_value($result, $vbo->real_field),
      'views_row' => array(),
      'position' => array(
        'current' => ++$context['sandbox']['progress'],
        'total' => $context['sandbox']['max'],
      ),
    );

    // Some operations require full selected rows.
    if ($operation
      ->needsRows()) {
      $rows[$row_index]['views_row'] = $result;
    }
  }

  // Enqueue the gathered rows.
  views_bulk_operations_enqueue_rows($queue_name, $rows, $operation, $options);
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {

    // Provide an estimation of the completion level we've reached.
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    $context['message'] = t('Prepared @current out of @total', array(
      '@current' => $context['sandbox']['progress'],
      '@total' => $context['sandbox']['max'],
    ));
  }
  else {

    // Provide a status message to the user if this is the last batch job.
    if ($operation
      ->getAdminOption('postpone_processing')) {
      $context['results']['log'][] = t('Enqueued the selected operation (%operation).', array(
        '%operation' => $operation
          ->label(),
      ));
    }
  }
}