You are here

function views_bulk_operations_execute in Views Bulk Operations (VBO) 7.3

Same name and namespace in other branches
  1. 6.3 views_bulk_operations.module \views_bulk_operations_execute()
  2. 6 views_bulk_operations.module \views_bulk_operations_execute()

Entry point for executing the chosen operation upon selected rows.

If the selected operation is an aggregate operation (requiring all selected items to be passed at the same time), restricted to a single value, or has the skip_batching option set, the operation is executed directly. This means that there is no batching & queueing, the PHP execution time limit is ignored (if allowed), all selected entities are loaded and processed.

Otherwise, the selected entity ids are divided into groups not larger than $entity_load_capacity, and enqueued for processing. If all items on all pages should be processed, a batch job runs that collects and enqueues the items from all pages of the view, page by page.

Based on the "Enqueue the operation instead of executing it directly" VBO field setting, the newly filled queue is either processed at cron time by the VBO worker function, or right away in a new batch job.

Parameters

$vbo: The VBO field, containing a reference to the view in $vbo->view.

$operation: The operation object.

$selection: An array in the form of $row_index => $entity_id.

$select_all_pages: Whether all items on all pages should be selected.

1 call to views_bulk_operations_execute()
views_bulk_operations_form_submit in ./views_bulk_operations.module
Submit handler for all steps of the VBO multistep form.

File

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

Code

function views_bulk_operations_execute($vbo, $operation, $selection, $select_all_pages = FALSE) {
  global $user;

  // Determine if the operation needs to be executed directly.
  $aggregate = $operation
    ->aggregate();
  $skip_batching = $vbo
    ->get_vbo_option('skip_batching');
  $save_view = $vbo
    ->get_vbo_option('save_view_object_when_batching');
  $force_single = $vbo
    ->get_vbo_option('force_single');
  $execute_directly = $aggregate || $skip_batching || $force_single;

  // Try to load all rows without a batch if needed.
  if ($execute_directly && $select_all_pages) {
    views_bulk_operations_direct_adjust($selection, $vbo);
  }

  // Options that affect execution.
  $options = array(
    'revision' => $vbo->revision,
    'entity_load_capacity' => $vbo
      ->get_vbo_option('entity_load_capacity', 10),
    // The information needed to recreate the view, to avoid serializing the
    // whole object. Passed to the executed operation. Also used by
    // views_bulk_operations_adjust_selection().
    'view_info' => array(
      'name' => $vbo->view->name,
      'display' => $vbo->view->current_display,
      'arguments' => $vbo->view->args,
      'exposed_input' => $vbo->view
        ->get_exposed_input(),
    ),
  );

  // If defined, save the whole view object.
  if ($save_view) {
    $options['view_info']['view'] = $vbo->view;
  }

  // Create an array of rows in the needed format.
  $rows = array();
  $current = 1;
  foreach ($selection as $row_index => $entity_id) {
    $rows[$row_index] = array(
      'entity_id' => $entity_id,
      'views_row' => array(),
      // Some operations rely on knowing the position of the current item
      // in the execution set (because of specific things that need to be done
      // at the beginning or the end of the set).
      'position' => array(
        'current' => $current++,
        'total' => count($selection),
      ),
    );

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

    // Execute the operation directly and stop here.
    views_bulk_operations_direct_process($operation, $rows, $options);
    return;
  }

  // Determine the correct queue to use.
  if ($operation
    ->getAdminOption('postpone_processing')) {

    // Use the site queue processed on cron.
    $queue_name = 'views_bulk_operations';
  }
  else {

    // Use the active queue processed immediately by Batch API.
    $queue_name = 'views_bulk_operations_active_queue_' . db_next_id();
  }
  $batch = array(
    'operations' => array(),
    'finished' => 'views_bulk_operations_execute_finished',
    'progress_message' => '',
    'title' => t('Performing %operation on the selected items...', array(
      '%operation' => $operation
        ->label(),
    )),
  );

  // All items on all pages should be selected, add a batch job to gather
  // and enqueue them.
  if ($select_all_pages && ($vbo->view->query->pager
    ->has_more_records() || $vbo->view->query->pager
    ->get_current_page() > 0)) {
    $total_rows = $vbo->view->total_rows;
    $batch['operations'][] = array(
      'views_bulk_operations_adjust_selection',
      array(
        $queue_name,
        $operation,
        $options,
      ),
    );
  }
  else {
    $total_rows = count($rows);

    // We have all the items that we need, enqueue them right away.
    views_bulk_operations_enqueue_rows($queue_name, $rows, $operation, $options);

    // Provide a status message to the user, since this is the last step if
    // processing is postponed.
    if ($operation
      ->getAdminOption('postpone_processing')) {
      drupal_set_message(t('Enqueued the selected operation (%operation).', array(
        '%operation' => $operation
          ->label(),
      )));
    }
  }

  // Processing is not postponed, add a batch job to process the queue.
  if (!$operation
    ->getAdminOption('postpone_processing')) {
    $batch['operations'][] = array(
      'views_bulk_operations_active_queue_process',
      array(
        $queue_name,
        $operation,
        $total_rows,
      ),
    );
  }

  // If there are batch jobs to be processed, create the batch set.
  if (count($batch['operations'])) {
    batch_set($batch);
  }
}